diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/c.cpp | 14 | ||||
-rw-r--r-- | src/compile.cpp | 6 | ||||
-rw-r--r-- | src/llvm.cpp | 14 | ||||
-rw-r--r-- | src/repl.cpp | 4 | ||||
-rw-r--r-- | src/tuplr.hpp | 19 |
5 files changed, 26 insertions, 31 deletions
@@ -220,7 +220,7 @@ CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) fn->impls.push_back(make_pair(thisType, f)); CVal retVal = NULL; for (size_t i = 2; i < fn->size(); ++i) - retVal = cenv.compile(fn->at(i)); + retVal = fn->at(i)->compile(cenv); cenv.engine()->finishFunction(cenv, f, cenv.type(fn->at(fn->size() - 1)), retVal); } catch (Error& e) { cenv.pop(); @@ -241,16 +241,16 @@ CEngine::compileIf(CEnv& cenv, AIf* aif) if (i > 1) out += "else {\n"; - Value* condV = llVal(cenv.compile(aif->at(i))); + Value* condV = llVal(aif->at(i)->compile(cenv)); out += (format("if (%s) {\n") % *condV).str(); - Value* thenV = llVal(cenv.compile(aif->at(i + 1))); + Value* thenV = llVal(aif->at(i + 1)->compile(cenv)); out += (format("%s = %s;\n}\n") % *varname % *thenV).str(); } // Emit final else block out += "else {\n"; - Value* elseV = llVal(cenv.compile(aif->at(aif->size() - 1))); + Value* elseV = llVal(aif->at(aif->size() - 1)->compile(cenv)); out += (format("%s = %s;\n}\n") % *varname % *elseV).str(); for (size_t i = 1; i < (aif->size() - 1) / 2; ++i) @@ -263,8 +263,8 @@ CVal CEngine::compilePrimitive(CEnv& cenv, APrimitive* prim) { CEngine* engine = reinterpret_cast<CEngine*>(cenv.engine()); - Value* a = llVal(cenv.compile(prim->at(1))); - Value* b = llVal(cenv.compile(prim->at(2))); + Value* a = llVal(prim->at(1)->compile(cenv)); + Value* b = llVal(prim->at(2)->compile(cenv)); const string n = prim->at(0)->to<ASymbol*>()->str(); string op = n; @@ -279,7 +279,7 @@ CEngine::compilePrimitive(CEnv& cenv, APrimitive* prim) string val("("); val += *a + op + *b; for (size_t i = 3; i < prim->size(); ++i) - val += op + *llVal(cenv.compile(prim->at(i))); + val += op + *llVal(prim->at(i)->compile(cenv)); val += ")"; Value* varname = new string(cenv.penv.gensymstr("x")); diff --git a/src/compile.cpp b/src/compile.cpp index 2deb713..170c198 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -34,7 +34,7 @@ COMPILE_LITERAL(bool); CVal ASymbol::compile(CEnv& cenv) { - return cenv.vals.ref(this); + return *cenv.vals.ref(this); } CVal @@ -81,7 +81,7 @@ ACall::compile(CEnv& cenv) vector<CVal> args(size() - 1); for (size_t i = 0; i < args.size(); ++i) - args[i] = cenv.compile(at(i + 1)); + args[i] = at(i + 1)->compile(cenv); return cenv.engine()->compileCall(cenv, f, args); } @@ -91,7 +91,7 @@ ADef::compile(CEnv& cenv) { // Define stub first for recursion cenv.def(sym(), at(2), cenv.type(at(2)), NULL); - CVal val = cenv.compile(at(size() - 1)); + CVal val = at(size() - 1)->compile(cenv); cenv.vals.def(sym(), val); return val; } diff --git a/src/llvm.cpp b/src/llvm.cpp index 810a7d3..34f23ca 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -317,7 +317,7 @@ LLVMEngine::compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) fn->impls.push_back(make_pair(thisType, f)); CVal retVal = NULL; for (size_t i = 2; i < fn->size(); ++i) - retVal = cenv.compile(fn->at(i)); + retVal = fn->at(i)->compile(cenv); cenv.engine()->finishFunction(cenv, f, cenv.type(fn->at(fn->size() - 1)), retVal); } catch (Error& e) { f->eraseFromParent(); // Error reading body, remove function @@ -339,7 +339,7 @@ LLVMEngine::compileIf(CEnv& cenv, AIf* aif) BasicBlock* nextBB = NULL; Branches branches; for (size_t i = 1; i < aif->size() - 1; i += 2) { - Value* condV = llVal(cenv.compile(aif->at(i))); + Value* condV = llVal(aif->at(i)->compile(cenv)); BasicBlock* thenBB = BasicBlock::Create((format("then%1%") % ((i+1)/2)).str()); nextBB = BasicBlock::Create((format("else%1%") % ((i+1)/2)).str()); @@ -349,7 +349,7 @@ LLVMEngine::compileIf(CEnv& cenv, AIf* aif) // Emit then block for this condition parent->getBasicBlockList().push_back(thenBB); engine->builder.SetInsertPoint(thenBB); - Value* thenV = llVal(cenv.compile(aif->at(i + 1))); + Value* thenV = llVal(aif->at(i + 1)->compile(cenv)); engine->builder.CreateBr(mergeBB); branches.push_back(make_pair(thenV, engine->builder.GetInsertBlock())); @@ -359,7 +359,7 @@ LLVMEngine::compileIf(CEnv& cenv, AIf* aif) // Emit final else block engine->builder.SetInsertPoint(nextBB); - Value* elseV = llVal(cenv.compile(aif->at(aif->size() - 1))); + Value* elseV = llVal(aif->at(aif->size() - 1)->compile(cenv)); engine->builder.CreateBr(mergeBB); branches.push_back(make_pair(elseV, engine->builder.GetInsertBlock())); @@ -378,8 +378,8 @@ CVal LLVMEngine::compilePrimitive(CEnv& cenv, APrimitive* prim) { LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine()); - Value* a = llVal(cenv.compile(prim->at(1))); - Value* b = llVal(cenv.compile(prim->at(2))); + Value* a = llVal(prim->at(1)->compile(cenv)); + Value* b = llVal(prim->at(2)->compile(cenv)); bool isFloat = cenv.type(prim->at(1))->str() == "Float"; const string n = prim->at(0)->to<ASymbol*>()->str(); @@ -396,7 +396,7 @@ LLVMEngine::compilePrimitive(CEnv& cenv, APrimitive* prim) if (op != 0) { Value* val = engine->builder.CreateBinOp(op, a, b); for (size_t i = 3; i < prim->size(); ++i) - val = engine->builder.CreateBinOp(op, val, llVal(cenv.compile(prim->at(i)))); + val = engine->builder.CreateBinOp(op, val, llVal(prim->at(i)->compile(cenv))); return val; } diff --git a/src/repl.cpp b/src/repl.cpp index fa08238..88885ea 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -97,7 +97,7 @@ eval(CEnv& cenv, const string& name, istream& is, bool execute) // Compile all expressions into it for (list< pair<AST*, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i) - val = cenv.compile(i->second); + val = i->second->compile(cenv); // Finish compilation cenv.engine()->finishFunction(cenv, f, resultT, val); @@ -135,7 +135,7 @@ repl(CEnv& cenv) try { // Create function for this repl loop f = cenv.engine()->startFunction(cenv, replFnName, resultT, ATuple(cursor)); - cenv.engine()->finishFunction(cenv, f, resultT, cenv.compile(result)); + cenv.engine()->finishFunction(cenv, f, resultT, result->compile(cenv)); callPrintCollect(cenv, f, result, resultT, true); } catch (Error& e) { cenv.out << e.msg << endl; diff --git a/src/tuplr.hpp b/src/tuplr.hpp index 6f79e8c..ada5412 100644 --- a/src/tuplr.hpp +++ b/src/tuplr.hpp @@ -628,15 +628,11 @@ struct CEnv { ~CEnv() { Object::pool.collect(GC::Roots()); } - typedef Env<const AST*, CVal> Vals; + typedef Env<const ASymbol*, CVal> Vals; Engine* engine() { return _engine; } void push() { tenv.push(); vals.push(); } void pop() { tenv.pop(); vals.pop(); } - CVal compile(AST* obj) { - CVal* v = vals.ref(obj); - return (v && *v) ? *v : vals.def(obj, obj->compile(*this)); - } void lock(AST* ast) { Object::pool.addRoot(ast); Object::pool.addRoot(type(ast)); } AType* type(AST* ast, const Subst& subst = Subst()) const { ASymbol* sym = ast->to<ASymbol*>(); @@ -649,13 +645,12 @@ struct CEnv { vals.def(sym, v); } - ostream& out; - ostream& err; - PEnv& penv; - TEnv& tenv; - Vals vals; - - Subst tsubst; + ostream& out; + ostream& err; + PEnv& penv; + TEnv& tenv; + Vals vals; + Subst tsubst; map<string,string> args; |