From 8338b52abc0e5a1c0893526bde2d347f1891773e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 22 Aug 2010 20:20:38 +0000 Subject: Simplify Engine function compilation interface. Removes duplicated code in various backends and reduces Engine code knowledge of AFn specifics (which belongs in compile.cpp). git-svn-id: http://svn.drobilla.net/resp/resp@268 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/c.cpp | 49 ++++++++++++++----------------------------------- 1 file changed, 14 insertions(+), 35 deletions(-) (limited to 'src/c.cpp') diff --git a/src/c.cpp b/src/c.cpp index 77c5fbe..6bd3a2d 100644 --- a/src/c.cpp +++ b/src/c.cpp @@ -101,10 +101,13 @@ struct CEngine : public Engine { } CFunc startFunction(CEnv& cenv, - const std::string& name, const ATuple* args, const AType* retT, const ATuple& argsT) + const std::string& name, const ATuple* args, const AType* type) { + const AType* argsT = type->prot()->as(); + const AType* retT = type->last()->as(); + vector cprot; - FOREACH(ATuple::const_iterator, i, argsT) { + FOREACHP(ATuple::const_iterator, i, argsT) { AType* at = (*i)->as(); THROW_IF(!llType(at), Cursor(), string("non-concrete parameter :: ") + at->str()) @@ -118,10 +121,10 @@ struct CEngine : public Engine { f->returnType = *llType(retT); f->name = name; f->text += f->returnType + "\n" + f->name + "("; - ATuple::const_iterator ai = argsT.begin(); + ATuple::const_iterator ai = argsT->begin(); ATuple::const_iterator ni = args->begin(); - for (; ai != argsT.end(); ++ai, ++ni) { - if (ai != argsT.begin()) + for (; ai != argsT->end(); ++ai, ++ni) { + if (ai != argsT->begin()) f->text += ", "; f->text += *llType((*ai)->as()) + " " + (*ni)->as()->cppstr; } @@ -131,6 +134,8 @@ struct CEngine : public Engine { return f; } + void pushFunctionArgs(CEnv& cenv, const AFn* fn, const AType* type, CFunc f); + void finishFunction(CEnv& cenv, CFunc f, CVal ret) { out += "return " + *(Value*)ret + ";\n}\n\n"; } @@ -149,8 +154,6 @@ struct CEngine : public Engine { return varname; } - CFunc compileFunction(CEnv& cenv, const AFn* fn, const AType* type); - CVal compileTup(CEnv& cenv, const AType* type, const vector& fields); CVal compileDot(CEnv& cenv, CVal tup, int32_t index); CVal compileLiteral(CEnv& cenv, const AST* lit); @@ -206,22 +209,12 @@ CEngine::compileString(CEnv& cenv, const char* str) return new Value(string("\"") + str + "\""); } -CFunc -CEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type) +void +CEngine::pushFunctionArgs(CEnv& cenv, const AFn* fn, const AType* type, CFunc f) { - assert(type->concrete()); - - const AType* argsT = type->prot()->as(); - const AType* retT = type->last()->as(); - Subst argsSubst = cenv.tenv.buildSubst(type, *argsT); - - // Write function declaration - const string name = (fn->name == "") ? cenv.penv.gensymstr("_fn") : fn->name; - Function* f = llFunc(cenv.engine()->startFunction(cenv, name, fn->prot(), retT, *argsT)); - cenv.push(); - Subst oldSubst = cenv.tsubst; - cenv.tsubst = Subst::compose(cenv.tsubst, argsSubst); + + const AType* argsT = type->prot()->as(); // Bind argument values in CEnv vector args; @@ -233,20 +226,6 @@ CEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type) THROW_IF(!lt, fn->loc, "untyped parameter\n"); cenv.def((*p)->as(), *p, t, new string((*p)->str())); } - - // Write function body - try { - CVal retVal = NULL; - for (AFn::const_iterator i = fn->begin() + 2; i != fn->end(); ++i) - retVal = (*i)->compile(cenv); - cenv.engine()->finishFunction(cenv, f, retVal); - } catch (Error& e) { - cenv.pop(); - throw e; - } - cenv.tsubst = oldSubst; - cenv.pop(); - return f; } CVal -- cgit v1.2.1