From 1b61928f542f1c54ac67791f382b20b39927eac5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 2 Dec 2010 18:36:49 +0000 Subject: Remove use of ACall class hierarchy from compile phase. git-svn-id: http://svn.drobilla.net/resp/resp@281 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/c.cpp | 8 +++--- src/compile.cpp | 88 +++++++++++++++++++++++++-------------------------------- src/llvm.cpp | 8 +++--- src/resp.hpp | 4 +-- 4 files changed, 49 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/c.cpp b/src/c.cpp index 4d53436..7cd72da 100644 --- a/src/c.cpp +++ b/src/c.cpp @@ -159,8 +159,8 @@ struct CEngine : public Engine { CVal compileLiteral(CEnv& cenv, const AST* lit); CVal compileString(CEnv& cenv, const char* str); CVal compilePrimitive(CEnv& cenv, const APrimitive* prim); - CVal compileIf(CEnv& cenv, const AIf* aif); - CVal compileMatch(CEnv& cenv, const AMatch* match); + CVal compileIf(CEnv& cenv, const ACall* aif); + CVal compileMatch(CEnv& cenv, const ACall* match); CVal compileGlobal(CEnv& cenv, const AType* type, const string& sym, CVal val); CVal getGlobal(CEnv& cenv, const string& sym, CVal val); @@ -230,7 +230,7 @@ CEngine::pushFunctionArgs(CEnv& cenv, const AFn* fn, const AType* type, CFunc f) } CVal -CEngine::compileIf(CEnv& cenv, const AIf* aif) +CEngine::compileIf(CEnv& cenv, const ACall* aif) { Value* varname = new string(cenv.penv.gensymstr("if")); out += (format("%s %s;\n") % *llType(cenv.type(aif)) % *varname).str(); @@ -262,7 +262,7 @@ CEngine::compileIf(CEnv& cenv, const AIf* aif) } CVal -CEngine::compileMatch(CEnv& cenv, const AMatch* match) +CEngine::compileMatch(CEnv& cenv, const ACall* match) { return NULL; } diff --git a/src/compile.cpp b/src/compile.cpp index 8518051..9f9168a 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -66,6 +66,21 @@ compile_fn(CEnv& cenv, const AFn* fn) throw() return f; } +static CVal +compile_type(CEnv& cenv, const AType* type) throw() +{ + const ASymbol* sym = type->head()->as(); + CVal* existing = cenv.vals.ref(sym); + if (existing) { + return *existing; + } else { + CVal compiled = cenv.engine()->compileString( + cenv, (string("__T_") + type->head()->str()).c_str()); + cenv.vals.def(sym, compiled); + return compiled; + } +} + static CVal compile_call(CEnv& cenv, const ACall* call) throw() { @@ -82,7 +97,7 @@ compile_call(CEnv& cenv, const ACall* call) throw() } static CVal -compile_def(CEnv& cenv, const ADef* def) throw() +compile_def(CEnv& cenv, const ACall* def) throw() { const ASymbol* const sym = def->list_ref(1)->as(); const AST* const body = def->list_ref(2); @@ -98,7 +113,7 @@ compile_def(CEnv& cenv, const ADef* def) throw() } static CVal -compile_cons(CEnv& cenv, const ACons* cons) throw() +compile_cons(CEnv& cenv, const ACall* cons) throw() { AType* type = new AType(const_cast(cons->head()->as()), NULL, Cursor()); TList tlist(type); @@ -111,22 +126,7 @@ compile_cons(CEnv& cenv, const ACons* cons) throw() } static CVal -compile_type(CEnv& cenv, const AType* type) throw() -{ - const ASymbol* sym = type->head()->as(); - CVal* existing = cenv.vals.ref(sym); - if (existing) { - return *existing; - } else { - CVal compiled = cenv.engine()->compileString( - cenv, (string("__T_") + type->head()->str()).c_str()); - cenv.vals.def(sym, compiled); - return compiled; - } -} - -static CVal -compile_dot(CEnv& cenv, const ADot* dot) throw() +compile_dot(CEnv& cenv, const ACall* dot) throw() { ATuple::const_iterator i = dot->begin(); const AST* tup = *++i; @@ -147,10 +147,6 @@ resp_compile(CEnv& cenv, const AST* ast) throw() if (str) return cenv.engine()->compileString(cenv, str->c_str()); - const AQuote* quote = ast->to(); - if (quote) - return resp_compile(cenv, quote->list_ref(1)); - const ALexeme* lexeme = ast->to(); if (lexeme) return cenv.engine()->compileString(cenv, lexeme->c_str()); @@ -162,42 +158,36 @@ resp_compile(CEnv& cenv, const AST* ast) throw() const AFn* fn = ast->to(); if (fn) return compile_fn(cenv, fn); - - const ADef* def = ast->to(); - if (def) - return compile_def(cenv, def); - - const AIf* aif = ast->to(); - if (aif) - return cenv.engine()->compileIf(cenv, aif); - - const ACons* cons = ast->to(); - if (cons) - return compile_cons(cenv, cons); const APrimitive* prim = ast->to(); if (prim) return cenv.engine()->compilePrimitive(cenv, prim); - const AMatch* match = ast->to(); - if (match) - return cenv.engine()->compileMatch(cenv, match); - const AType* type = ast->to(); if (type) return compile_type(cenv, type); - const ADot* dot = ast->to(); - if (dot) - return compile_dot(cenv, dot); - - const ADefType* deftype = ast->to(); - if (deftype) - return NULL; - - const ACall* call = ast->to(); - if (call) - return compile_call(cenv, call); + const ACall* const call = ast->to(); + if (call) { + const ASymbol* const sym = call->head()->to(); + const std::string form = sym ? sym->cppstr : ""; + if (form == "def") + return compile_def(cenv, call); + else if (form == "if") + return cenv.engine()->compileIf(cenv, call); + else if (form == "cons" || isupper(form[0])) + return compile_cons(cenv, call); + else if (form == ".") + return compile_dot(cenv, call); + else if (form == "quote") + return resp_compile(cenv, call->list_ref(1)); + else if (form == "match") + return cenv.engine()->compileMatch(cenv, call); + else if (form == "def-type") + return NULL; + else + return compile_call(cenv, call); + } cenv.err << "Attempt to compile unknown type" << endl; assert(false); diff --git a/src/llvm.cpp b/src/llvm.cpp index 3aaf21b..29c09ee 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -197,8 +197,8 @@ struct LLVMEngine : public Engine { CVal compileLiteral(CEnv& cenv, const AST* lit); CVal compileString(CEnv& cenv, const char* str); CVal compilePrimitive(CEnv& cenv, const APrimitive* prim); - CVal compileIf(CEnv& cenv, const AIf* aif); - CVal compileMatch(CEnv& cenv, const AMatch* match); + CVal compileIf(CEnv& cenv, const ACall* aif); + CVal compileMatch(CEnv& cenv, const ACall* match); CVal compileGlobal(CEnv& cenv, const AType* type, const string& sym, CVal val); CVal getGlobal(CEnv& cenv, const string& sym, CVal val); @@ -351,7 +351,7 @@ LLVMEngine::pushFunctionArgs(CEnv& cenv, const AFn* fn, const AType* type, CFunc } CVal -LLVMEngine::compileIf(CEnv& cenv, const AIf* aif) +LLVMEngine::compileIf(CEnv& cenv, const ACall* aif) { typedef vector< pair > Branches; LLVMEngine* engine = reinterpret_cast(cenv.engine()); @@ -402,7 +402,7 @@ LLVMEngine::compileIf(CEnv& cenv, const AIf* aif) } CVal -LLVMEngine::compileMatch(CEnv& cenv, const AMatch* match) +LLVMEngine::compileMatch(CEnv& cenv, const ACall* match) { typedef vector< pair > Branches; Value* matchee = llVal(resp_compile(cenv, match->list_ref(1))); diff --git a/src/resp.hpp b/src/resp.hpp index d429168..d9c85fe 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -786,8 +786,8 @@ struct Engine { virtual CVal compileString(CEnv& cenv, const char* str) = 0; virtual CVal compileCall(CEnv& cenv, CFunc f, const AType* fT, ValVec& args) = 0; virtual CVal compilePrimitive(CEnv& cenv, const APrimitive* prim) = 0; - virtual CVal compileIf(CEnv& cenv, const AIf* aif) = 0; - virtual CVal compileMatch(CEnv& cenv, const AMatch* match) = 0; + virtual CVal compileIf(CEnv& cenv, const ACall* aif) = 0; + virtual CVal compileMatch(CEnv& cenv, const ACall* match) = 0; virtual CVal compileGlobal(CEnv& cenv, const AType* t, const string& sym, CVal val) = 0; virtual CVal getGlobal(CEnv& cenv, const string& sym, CVal val) = 0; virtual void writeModule(CEnv& cenv, std::ostream& os) = 0; -- cgit v1.2.1