/* Tuplr Serialisation * Copyright (C) 2008-2009 David Robillard * * Tuplr is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * Tuplr is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General * Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Tuplr. If not, see . */ #include "tuplr.hpp" ostream& operator<<(ostream& out, const AST* ast) { const ALiteral* flit = ast->to*>(); if (flit) return out << showpoint << flit->val; const ALiteral* ilit = ast->to*>(); if (ilit) return out << ilit->val; const ALiteral* blit = ast->to*>(); if (blit) return out << (blit->val ? "#t" : "#f"); const ASymbol* sym = ast->to(); if (sym) return out << sym->cppstr; const AType* type = ast->to(); if (type) { switch (type->kind) { case AType::VAR: return out << "?" << type->id; case AType::PRIM: return out << type->at(0); case AType::EXPR: break; // will catch Tuple case below } } const ATuple* tup = ast->to(); if (tup) { out << "("; for (size_t i = 0; i != tup->size(); ++i) out << tup->at(i) << ((i != tup->size() - 1) ? " " : ""); return out << ")"; } return out << "?"; } void pprint_internal(ostream& out, const AST* ast, unsigned indent) { out << string().insert(0, indent, ' '); const ATuple* tup = ast->to(); if (tup) { const string head = tup->at(0)->str(); out << "(" << head; if (tup->size() > 1) out << " " << tup->at(1); for (size_t i = 2; i != tup->size(); ++i) { out << endl; pprint_internal(out, tup->at(i), indent + head.length() + 2); } out << ")"; } else { out << ast; } } void pprint(ostream& out, const AST* ast) { pprint_internal(out, ast, 0); out << endl; }