diff options
Diffstat (limited to 'src/pprint.cpp')
-rw-r--r-- | src/pprint.cpp | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/src/pprint.cpp b/src/pprint.cpp index 2f4180b..528683f 100644 --- a/src/pprint.cpp +++ b/src/pprint.cpp @@ -60,32 +60,12 @@ print_tuple(ostream& out, const ATuple* tup, ATuple::const_iterator i, ostream& print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types) { - const ALexeme* lexeme = ast->to<const ALexeme*>(); - if (lexeme) - return out << lexeme->cppstr; - - const ALiteral<float>* flit = ast->to<const ALiteral<float>*>(); - if (flit) - return out << showpoint << flit->val; - - const ALiteral<int32_t>* ilit = ast->to<const ALiteral<int32_t>*>(); - if (ilit) - return out << ilit->val; - - const ALiteral<bool>* blit = ast->to<const ALiteral<bool>*>(); - if (blit) - return out << (blit->val ? "#t" : "#f"); - - const AString* str = ast->to<const AString*>(); - if (str) - return out << '"' << str->cppstr << '"'; - - const ASymbol* sym = ast->to<const ASymbol*>(); - if (sym) - return out << sym->cppstr; - - const AType* type = ast->to<const AType*>(); - if (type) { + switch (ast->tag()) { + case T_UNKNOWN: + return out << "?"; + case T_TYPE: + { + const AType* type = ast->as_type(); switch (type->kind) { case AType::VAR: return out << "?" << type->id; case AType::NAME: return out << type->head(); @@ -93,20 +73,20 @@ print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types) case AType::DOTS: return out << "..."; case AType::EXPR: break; // will catch Tuple case below } - } - - const ATuple* tup = ast->to<const ATuple*>(); - if (tup) { + } + case T_TUPLE: + { + const ATuple* tup = ast->as_tuple(); out << "("; ATuple::const_iterator i = tup->begin(); std::string form = ""; if (i != tup->end()) { - const ASymbol* sym = (*i)->to<const ASymbol*>(); + const ASymbol* sym = (*i)->to_symbol(); if (sym) { form = sym->cppstr; } else { - const ALexeme* lexeme = (*i)->to<const ALexeme*>(); + const ALexeme* lexeme = (*i)->to_lexeme(); if (lexeme) form = lexeme->cppstr; } @@ -127,7 +107,7 @@ print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types) } else if (form == "fn") { out << (*i++) << " "; - const ATuple* pat = (*i++)->as<const ATuple*>(); + const ATuple* pat = (*i++)->as_tuple(); // Print prototype (possibly with parameter type annotations) out << "("; @@ -161,6 +141,20 @@ print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types) } else { return print_tuple(out, tup, i, indent + 1, false, cenv, types, false); } + break; + } + case T_BOOL: + return out << showpoint << ((const ALiteral<bool>*)ast)->val; + case T_FLOAT: + return out << showpoint << ((const ALiteral<float>*)ast)->val; + case T_INT32: + return out << showpoint << ((const ALiteral<int32_t>*)ast)->val; + case T_LEXEME: + return out << ((const ALexeme*)ast)->cppstr; + case T_STRING: + return out << '"' << ((const AString*)ast)->cppstr << '"'; + case T_SYMBOL: + return out << ((const ASymbol*)ast)->cppstr; } return out << "?"; |