diff options
Diffstat (limited to 'src/compile.cpp')
-rw-r--r-- | src/compile.cpp | 57 |
1 files changed, 27 insertions, 30 deletions
diff --git a/src/compile.cpp b/src/compile.cpp index ebd6083..142fa0a 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -69,7 +69,7 @@ compile_fn(CEnv& cenv, const ATuple* fn) throw() static CVal compile_type(CEnv& cenv, const AType* type) throw() { - const ASymbol* sym = type->head()->as<const ASymbol*>(); + const ASymbol* sym = type->head()->as_symbol(); CVal* existing = cenv.vals.ref(sym); if (existing) { return *existing; @@ -99,7 +99,7 @@ compile_call(CEnv& cenv, const ATuple* call) throw() static CVal compile_def(CEnv& cenv, const ATuple* def) throw() { - const ASymbol* const sym = def->list_ref(1)->as<const ASymbol*>(); + const ASymbol* const sym = def->list_ref(1)->as_symbol(); const AST* const body = def->list_ref(2); cenv.def(sym, body, cenv.type(body), NULL); // define stub first for recursion CVal val = resp_compile(cenv, body); @@ -115,7 +115,7 @@ compile_def(CEnv& cenv, const ATuple* def) throw() static CVal compile_cons(CEnv& cenv, const ATuple* cons) throw() { - AType* type = new AType(const_cast<ASymbol*>(cons->head()->as<const ASymbol*>()), NULL, Cursor()); + AType* type = new AType(const_cast<ASymbol*>(cons->head()->as_symbol()), NULL, Cursor()); TList tlist(type); vector<CVal> fields; for (ATuple::const_iterator i = cons->iter_at(1); i != cons->end(); ++i) { @@ -128,9 +128,10 @@ compile_cons(CEnv& cenv, const ATuple* cons) throw() static CVal compile_dot(CEnv& cenv, const ATuple* dot) throw() { - ATuple::const_iterator i = dot->begin(); - const AST* tup = *++i; - const ALiteral<int32_t>* index = (*++i)->as<const ALiteral<int32_t>*>(); + ATuple::const_iterator i = dot->begin(); + const AST* tup = *++i; + const ALiteral<int32_t>* index = (ALiteral<int32_t>*)(*++i); + assert(index->tag() == T_INT32); CVal tupVal = resp_compile(cenv, tup); return cenv.engine()->compileDot(cenv, tupVal, index->val); } @@ -138,33 +139,28 @@ compile_dot(CEnv& cenv, const ATuple* dot) throw() CVal resp_compile(CEnv& cenv, const AST* ast) throw() { - if (ast->to<const ALiteral<int32_t>*>() - || ast->to<const ALiteral<float>*>() - || ast->to<const ALiteral<bool>*>()) + switch (ast->tag()) { + case T_UNKNOWN: + return NULL; + case T_TYPE: + return compile_type(cenv, ast->as_type()); + case T_BOOL: + case T_FLOAT: + case T_INT32: return cenv.engine()->compileLiteral(cenv, ast); - - const AString* str = ast->to<const AString*>(); - if (str) - return cenv.engine()->compileString(cenv, str->cppstr.c_str()); - - const ALexeme* lexeme = ast->to<const ALexeme*>(); - if (lexeme) - return cenv.engine()->compileString(cenv, lexeme->cppstr.c_str()); - - const ASymbol* sym = ast->to<const ASymbol*>(); - if (sym) - return compile_symbol(cenv, sym); - - const AType* type = ast->to<const AType*>(); - if (type) - return compile_type(cenv, type); - - const ATuple* const call = ast->to<const ATuple*>(); - if (call) { - const ASymbol* const sym = call->head()->to<const ASymbol*>(); + case T_LEXEME: + return cenv.engine()->compileString(cenv, ((ALexeme*)ast)->cppstr.c_str()); + case T_STRING: + return cenv.engine()->compileString(cenv, ((AString*)ast)->cppstr.c_str()); + case T_SYMBOL: + return compile_symbol(cenv, ast->as_symbol()); + case T_TUPLE: + { + const ATuple* const call = ast->as_tuple(); + const ASymbol* const sym = call->head()->to_symbol(); const std::string form = sym ? sym->cppstr : ""; if (is_primitive(cenv.penv, call)) - return cenv.engine()->compilePrimitive(cenv, ast->as<const ATuple*>()); + return cenv.engine()->compilePrimitive(cenv, ast->as_tuple()); else if (form == "fn") return compile_fn(cenv, call); else if (form == "def") @@ -184,6 +180,7 @@ resp_compile(CEnv& cenv, const AST* ast) throw() else return compile_call(cenv, call); } + } cenv.err << "Attempt to compile unknown type: " << ast << endl; assert(false); |