aboutsummaryrefslogtreecommitdiffstats
path: root/src/lift.cpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2010-12-03 00:35:28 +0000
committerDavid Robillard <d@drobilla.net>2010-12-03 00:35:28 +0000
commitbbad3fe368b2086ec93f082a9436b1dfbc8f0e84 (patch)
tree941799ecdfeaeb15d3235ec85c6678b07a73e8d8 /src/lift.cpp
parent35e977e8786b667155bf6b186bebb9c0938fb587 (diff)
downloadresp-bbad3fe368b2086ec93f082a9436b1dfbc8f0e84.tar.gz
resp-bbad3fe368b2086ec93f082a9436b1dfbc8f0e84.tar.bz2
resp-bbad3fe368b2086ec93f082a9436b1dfbc8f0e84.zip
Remove AFn.
git-svn-id: http://svn.drobilla.net/resp/resp@284 ad02d1e2-f140-0410-9f75-f8b11f17cedd
Diffstat (limited to 'src/lift.cpp')
-rw-r--r--src/lift.cpp43
1 files changed, 21 insertions, 22 deletions
diff --git a/src/lift.cpp b/src/lift.cpp
index 739d176..2df27c9 100644
--- a/src/lift.cpp
+++ b/src/lift.cpp
@@ -31,7 +31,7 @@ static AST*
lift_symbol(CEnv& cenv, Code& code, ASymbol* sym) throw()
{
const std::string& cppstr = sym->cppstr;
- if (!cenv.liftStack.empty() && cppstr == cenv.liftStack.top().fn->name) {
+ if (!cenv.liftStack.empty() && cppstr == cenv.name(cenv.liftStack.top().fn)) {
return cenv.penv.sym("_me"); // Reference to innermost function
} else if (!cenv.penv.handler(true, cppstr)
&& !cenv.penv.handler(false, cppstr)
@@ -50,13 +50,15 @@ lift_symbol(CEnv& cenv, Code& code, ASymbol* sym) throw()
}
static AST*
-lift_fn(CEnv& cenv, Code& code, AFn* fn) throw()
+lift_fn(CEnv& cenv, Code& code, ACall* fn) throw()
{
- AFn* impl = new AFn(fn);
- const string nameBase = cenv.penv.gensymstr(((fn->name != "") ? fn->name : "fn").c_str());
- impl->name = "_" + nameBase;
+ ACall* impl = new ACall(fn);
+ const string fnName = cenv.name(fn);
+ const string nameBase = cenv.penv.gensymstr(((fnName != "") ? fnName : "fn").c_str());
+ const string implNameStr = string("_") + nameBase;
+ cenv.setName(impl, implNameStr);
- cenv.liftStack.push(CEnv::FreeVars(fn, impl->name));
+ cenv.liftStack.push(CEnv::FreeVars(fn, implNameStr));
// Create a new stub environment frame for parameters
cenv.push();
@@ -64,7 +66,7 @@ lift_fn(CEnv& cenv, Code& code, AFn* fn) throw()
AType::const_iterator tp = type->prot()->begin();
AType* implProtT = new AType(*type->prot()->as<const AType*>());
ATuple::iterator ip = implProtT->begin();
- for (AFn::const_iterator p = fn->prot()->begin(); p != fn->prot()->end(); ++p) {
+ for (ATuple::const_iterator p = fn->prot()->begin(); p != fn->prot()->end(); ++p) {
const AType* paramType = (*tp++)->as<const AType*>();
if (paramType->kind == AType::EXPR && *paramType->head() == *cenv.tenv.Fn) {
AType* fnType = new AType(*paramType);
@@ -84,8 +86,8 @@ lift_fn(CEnv& cenv, Code& code, AFn* fn) throw()
// Lift body
const AType* implRetT = NULL;
- AFn::iterator ci = impl->iter_at(2);
- for (AFn::iterator i = fn->iter_at(2); i != fn->end(); ++i, ++ci) {
+ ATuple::iterator ci = impl->iter_at(2);
+ for (ATuple::iterator i = fn->iter_at(2); i != fn->end(); ++i, ++ci) {
*ci = resp_lift(cenv, code, *i);
implRetT = cenv.type(*ci);
}
@@ -96,7 +98,7 @@ lift_fn(CEnv& cenv, Code& code, AFn* fn) throw()
*impl->prot()->begin() = cenv.penv.sym("_me");
// Create definition for implementation fn
- ASymbol* implName = cenv.penv.sym(impl->name);
+ ASymbol* implName = cenv.penv.sym(implNameStr);
ACall* def = tup<ACall>(fn->loc, cenv.penv.sym("def"), implName, impl, NULL);
code.push_back(def);
@@ -122,8 +124,8 @@ lift_fn(CEnv& cenv, Code& code, AFn* fn) throw()
cenv.setType(cons, consT);
cenv.def(implName, impl, implT, NULL);
- if (fn->name != "")
- cenv.def(cenv.penv.sym(fn->name), fn, consT, NULL);
+ if (cenv.name(fn) != "")
+ cenv.def(cenv.penv.sym(cenv.name(fn)), fn, consT, NULL);
return cons;
}
@@ -142,12 +144,12 @@ lift_call(CEnv& cenv, Code& code, ACall* call) throw()
const AType* copyT = NULL;
ASymbol* sym = call->head()->to<ASymbol*>();
- if (sym && !cenv.liftStack.empty() && sym->cppstr == cenv.liftStack.top().fn->name) {
+ if (sym && !cenv.liftStack.empty() && sym->cppstr == cenv.name(cenv.liftStack.top().fn)) {
/* Recursive call to innermost function, call implementation directly,
* reusing the current "_me" closure parameter (no cons or .).
*/
copy.push_front(cenv.penv.sym(cenv.liftStack.top().implName));
- } else if (call->head()->to<AFn*>()) {
+ } else if (is_form(call, "fn")) {
/* Special case: ((fn ...) ...)
* Lifting (fn ...) yields: (Fn _impl ...).
* We don't want ((Fn _impl ...) (Fn _impl ...) ...),
@@ -184,9 +186,8 @@ lift_def(CEnv& cenv, Code& code, ACall* def) throw()
const ASymbol* const sym = def->list_ref(1)->as<const ASymbol*>();
AST* const body = def->list_ref(2);
cenv.def(sym, body, cenv.type(body), NULL);
- AFn* c = body->to<AFn*>();
- if (c)
- c->name = sym->str();
+ if (is_form(body, "fn"))
+ cenv.setName(body->as<const ACall*>(), sym->str());
assert(def->list_ref(1)->to<const ASymbol*>());
List<ACall, AST> copy;
@@ -198,7 +199,7 @@ lift_def(CEnv& cenv, Code& code, ACall* def) throw()
cenv.setTypeSameAs(copy, def);
if (copy.head->list_ref(1) == copy.head->list_ref(2))
- return NULL; // Definition created by AFn::lift when body was lifted
+ return NULL; // Definition created by lift_fn when body was lifted
cenv.def(copy.head->list_ref(1)->as<const ASymbol*>(),
copy.head->list_ref(2),
@@ -228,16 +229,14 @@ resp_lift(CEnv& cenv, Code& code, AST* ast) throw()
if (sym)
return lift_symbol(cenv, code, sym);
- AFn* const fn = ast->to<AFn*>();
- if (fn)
- return lift_fn(cenv, code, fn);
-
ACall* const call = ast->to<ACall*>();
if (call) {
const ASymbol* const sym = call->head()->to<const ASymbol*>();
const std::string form = sym ? sym->cppstr : "";
if (is_primitive(cenv.penv, call))
return lift_builtin_call(cenv, code, call);
+ else if (form == "fn")
+ return lift_fn(cenv, code, call);
else if (form == "def")
return lift_def(cenv, code, call);
else if (form == "if")