diff options
author | David Robillard <d@drobilla.net> | 2009-06-20 18:16:00 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2009-06-20 18:16:00 +0000 |
commit | ced439fb6918e8848882d0e3a65f11b72da92425 (patch) | |
tree | 02864e53389ac2339266cc043b0b80ae0d511555 | |
parent | f8e16165e6666fceaac66c777851a0f99ba8f5fc (diff) | |
download | resp-ced439fb6918e8848882d0e3a65f11b72da92425.tar.gz resp-ced439fb6918e8848882d0e3a65f11b72da92425.tar.bz2 resp-ced439fb6918e8848882d0e3a65f11b72da92425.zip |
Move eval and repl to tuplr.cpp (non-backend dependent code).
git-svn-id: http://svn.drobilla.net/resp/tuplr@133 ad02d1e2-f140-0410-9f75-f8b11f17cedd
-rw-r--r-- | llvm.cpp | 119 | ||||
-rw-r--r-- | tuplr.cpp | 121 | ||||
-rw-r--r-- | tuplr.hpp | 8 |
3 files changed, 129 insertions, 119 deletions
@@ -139,7 +139,7 @@ LITERAL(bool, "Bool", ConstantInt::get(Type::Int1Ty, val, false)) CFunction startFunction(CEnv& cenv, const std::string& name, const AType* retT, const ATuple& argsT, - const vector<string> argNames=vector<string>()) + const vector<string> argNames) { Function::LinkageTypes linkage = Function::ExternalLinkage; @@ -522,123 +522,6 @@ call(CEnv& cenv, CFunction f, AType* retT) return ss.str(); } -int -eval(CEnv& cenv, const string& name, istream& is) -{ - AST* result = NULL; - AType* resultType = NULL; - list< pair<SExp, AST*> > exprs; - Cursor cursor(name); - try { - while (true) { - SExp exp = readExpression(cursor, is); - if (exp.type == SExp::LIST && exp.empty()) - break; - - result = cenv.penv.parse(exp); // Parse input - Constraints c; - result->constrain(cenv.tenv, c); // Constrain types - cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints - resultType = cenv.type(result); - result->lift(cenv); // Lift functions - exprs.push_back(make_pair(exp, result)); - - // Add definitions as GC roots - if (result->to<ADefinition*>()) - cenv.lock(result); - - // Add types in type substition as GC roots - for (Subst::iterator i = cenv.tsubst.begin(); i != cenv.tsubst.end(); ++i) { - Object::pool.addRoot(i->first); - Object::pool.addRoot(i->second); - } - } - - THROW_IF(!llType(resultType), cursor, "body has non-compilable type") - - // Create function for top-level of program - CFunction f = startFunction(cenv, "main", resultType, ATuple(cursor)); - - // Compile all expressions into it - CValue val = NULL; - for (list< pair<SExp, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i) - val = cenv.compile(i->second); - - finishFunction(cenv, f, val); - - cenv.out << call(cenv, f, resultType) << " : " << resultType << endl; - - Object::pool.collect(Object::pool.roots()); - - if (cenv.args.find("-d") != cenv.args.end()) - cenv.write(cenv.out); - - } catch (Error& e) { - cenv.err << e.what() << endl; - return 1; - } - return 0; -} - -int -repl(CEnv& cenv) -{ - while (1) { - cenv.out << "() "; - cenv.out.flush(); - Cursor cursor("(stdin)"); - - try { - SExp exp = readExpression(cursor, std::cin); - if (exp.type == SExp::LIST && exp.empty()) - break; - - AST* body = cenv.penv.parse(exp); // Parse input - Constraints c; - body->constrain(cenv.tenv, c); // Constrain types - - Subst oldSubst = cenv.tsubst; - cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints - - AType* bodyT = cenv.type(body); - THROW_IF(!bodyT, cursor, "call to untyped body") - - body->lift(cenv); - - CFunction f = NULL; - try { - // Create anonymous function to insert code into - f = startFunction(cenv, cenv.gensym("_repl"), bodyT, ATuple(cursor)); - CValue retVal = cenv.compile(body); - finishFunction(cenv, f, retVal); - cenv.out << call(cenv, f, bodyT); - } catch (Error& e) { - ADefinition* def = body->to<ADefinition*>(); - if (def) - cenv.out << def->sym(); - else - cenv.out << "?"; - eraseFunction(cenv, f); - } - cenv.out << " : " << cenv.type(body) << endl; - - // Add definitions as GC roots - if (body->to<ADefinition*>()) - cenv.lock(body); - - Object::pool.collect(Object::pool.roots()); - - cenv.tsubst = oldSubst; - if (cenv.args.find("-d") != cenv.args.end()) - cenv.write(cenv.out); - - } catch (Error& e) { - cenv.err << e.what() << endl; - } - } - return 0; -} - CEnv* newCenv(PEnv& penv, TEnv& tenv) { @@ -226,9 +226,128 @@ initLang(PEnv& penv, TEnv& tenv) } +/*************************************************************************** + * EVAL/REPL * + ***************************************************************************/ + +int +eval(CEnv& cenv, const string& name, istream& is) +{ + AST* result = NULL; + AType* resultType = NULL; + list< pair<SExp, AST*> > exprs; + Cursor cursor(name); + try { + while (true) { + SExp exp = readExpression(cursor, is); + if (exp.type == SExp::LIST && exp.empty()) + break; + + result = cenv.penv.parse(exp); // Parse input + Constraints c; + result->constrain(cenv.tenv, c); // Constrain types + cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints + resultType = cenv.type(result); + result->lift(cenv); // Lift functions + exprs.push_back(make_pair(exp, result)); + + // Add definitions as GC roots + if (result->to<ADefinition*>()) + cenv.lock(result); + + // Add types in type substition as GC roots + for (Subst::iterator i = cenv.tsubst.begin(); i != cenv.tsubst.end(); ++i) { + Object::pool.addRoot(i->first); + Object::pool.addRoot(i->second); + } + } + + // Create function for top-level of program + CFunction f = startFunction(cenv, "main", resultType, ATuple(cursor)); + + // Compile all expressions into it + CValue val = NULL; + for (list< pair<SExp, AST*> >::const_iterator i = exprs.begin(); i != exprs.end(); ++i) + val = cenv.compile(i->second); + + finishFunction(cenv, f, val); + + cenv.out << call(cenv, f, resultType) << " : " << resultType << endl; + + Object::pool.collect(Object::pool.roots()); + + if (cenv.args.find("-d") != cenv.args.end()) + cenv.write(cenv.out); + + } catch (Error& e) { + cenv.err << e.what() << endl; + return 1; + } + return 0; +} + +int +repl(CEnv& cenv) +{ + while (1) { + cenv.out << "() "; + cenv.out.flush(); + Cursor cursor("(stdin)"); + + try { + SExp exp = readExpression(cursor, std::cin); + if (exp.type == SExp::LIST && exp.empty()) + break; + + AST* body = cenv.penv.parse(exp); // Parse input + Constraints c; + body->constrain(cenv.tenv, c); // Constrain types + + Subst oldSubst = cenv.tsubst; + cenv.tsubst = Subst::compose(cenv.tsubst, TEnv::unify(c)); // Solve type constraints + + AType* bodyT = cenv.type(body); + THROW_IF(!bodyT, cursor, "call to untyped body") + + body->lift(cenv); + + CFunction f = NULL; + try { + // Create anonymous function to insert code into + f = startFunction(cenv, cenv.gensym("_repl"), bodyT, ATuple(cursor)); + CValue retVal = cenv.compile(body); + finishFunction(cenv, f, retVal); + cenv.out << call(cenv, f, bodyT); + } catch (Error& e) { + ADefinition* def = body->to<ADefinition*>(); + if (def) + cenv.out << def->sym(); + else + cenv.out << "?"; + eraseFunction(cenv, f); + } + cenv.out << " : " << cenv.type(body) << endl; + + // Add definitions as GC roots + if (body->to<ADefinition*>()) + cenv.lock(body); + + Object::pool.collect(Object::pool.roots()); + + cenv.tsubst = oldSubst; + if (cenv.args.find("-d") != cenv.args.end()) + cenv.write(cenv.out); + + } catch (Error& e) { + cenv.err << e.what() << endl; + } + } + return 0; +} + /*************************************************************************** - * EVAL/REPL/MAIN * + * MAIN * ***************************************************************************/ int @@ -556,6 +556,14 @@ struct TEnv : public Env< const ASymbol*, pair<AST*, AType*> > { * Code Generation * ***************************************************************************/ +CFunction startFunction(CEnv& cenv, const std::string& name, + const AType* retT, const ATuple& argsT, + const vector<string> argNames=vector<string>()); + +void finishFunction(CEnv& cenv, CFunction f, CValue ret); +void eraseFunction(CEnv& cenv, CFunction f); +const string call(CEnv& cenv, CFunction f, AType* retT); + /// Compile-Time Environment struct CEnv { CEnv(PEnv& p, TEnv& t, CEngine e, ostream& os=std::cout, ostream& es=std::cerr); |