aboutsummaryrefslogtreecommitdiffstats
path: root/src/c.cpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2010-08-22 20:20:38 +0000
committerDavid Robillard <d@drobilla.net>2010-08-22 20:20:38 +0000
commit8338b52abc0e5a1c0893526bde2d347f1891773e (patch)
treeffea8eedbb766ff08dd6b30636b9060232d1ebb8 /src/c.cpp
parentb4dba561084b7ce60a8b1cfdb4e3b9de87de8d35 (diff)
downloadresp-8338b52abc0e5a1c0893526bde2d347f1891773e.tar.gz
resp-8338b52abc0e5a1c0893526bde2d347f1891773e.tar.bz2
resp-8338b52abc0e5a1c0893526bde2d347f1891773e.zip
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
Diffstat (limited to 'src/c.cpp')
-rw-r--r--src/c.cpp49
1 files changed, 14 insertions, 35 deletions
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*>();
+ const AType* retT = type->last()->as<const AType*>();
+
vector<const Type*> cprot;
- FOREACH(ATuple::const_iterator, i, argsT) {
+ FOREACHP(ATuple::const_iterator, i, argsT) {
AType* at = (*i)->as<AType*>();
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<const AType*>()) + " " + (*ni)->as<const ASymbol*>()->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<CVal>& 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*>();
- const AType* retT = type->last()->as<const AType*>();
- 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<const AType*>();
// Bind argument values in CEnv
vector<Value*> 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<ASymbol*>(), *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