From 0e1d11ad0dae33049b4a22d42a37503db2a698b0 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 5 Mar 2009 19:19:26 +0000 Subject: Make TEnv an actual lexical environment (not used yet). Proper parsing of boolean literals "true" and "false". git-svn-id: http://svn.drobilla.net/resp/tuplr@46 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- tuplr.cpp | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'tuplr.cpp') diff --git a/tuplr.cpp b/tuplr.cpp index f5db2ce..b93918b 100644 --- a/tuplr.cpp +++ b/tuplr.cpp @@ -408,6 +408,10 @@ parseExpression(PEnv& penv, const SExp& exp) return new ASTLiteral(strtol(exp.atom.c_str(), NULL, 10)); else return new ASTLiteral(strtod(exp.atom.c_str(), NULL)); + } else if (exp.atom == "true") { + return new ASTLiteral(true); + } else if (exp.atom == "false") { + return new ASTLiteral(false); } return penv.sym(exp.atom); } @@ -456,7 +460,8 @@ template struct Env : public list< map > { typedef map Frame; Env() : list(1) {} - void push_front() { list::push_front(Frame()); } + void push() { list::push_front(Frame()); } + void pop() { list::pop_front(); } const V& def(const K& k, const V& v) { typename Frame::iterator existing = this->front().find(k); if (existing != this->front().end() && existing->second != v) @@ -482,23 +487,16 @@ struct TSubst : public map { }; /// Type-Time Environment -struct TEnv { +struct TEnv : public Env { TEnv(PEnv& p) : penv(p), varID(1) {} - typedef map Types; typedef list< pair > Constraints; AType* var() { return new AType(varID++); } AType* type(const AST* ast) { - Types::iterator t = types.find(ast); - return (t != types.end()) ? t->second : (types[ast] = var()); + AType** t = ref(ast); + return t ? *t : def(ast, var()); } - AType* named(const string& name) const { - Types::const_iterator i = namedTypes.find(penv.sym(name)); - if (i == namedTypes.end()) throw Error("Unknown named type"); - return i->second; - } - void name(const string& name, const Type* type) { - ASTSymbol* sym = penv.sym(name); - namedTypes[sym] = new AType(penv.sym(name), type); + AType* named(const string& name) { + return *ref(penv.sym(name)); } void constrain(const AST* o, AType* t) { assert(!dynamic_cast(o)); @@ -508,8 +506,6 @@ struct TEnv { void apply(const TSubst& substs); static TSubst unify(const Constraints& c); PEnv& penv; - Types types; - Types namedTypes; Constraints constraints; unsigned varID; }; @@ -711,7 +707,7 @@ void TEnv::apply(const TSubst& substs) { FOREACH(TSubst::const_iterator, s, substs) - FOREACH(Types::iterator, t, types) + FOREACH(Frame::iterator, t, front()) if (*t->second == *s->first) t->second = s->second; else @@ -738,8 +734,8 @@ struct CEnv { opt.add(createCFGSimplificationPass()); // Simplify control flow } string gensym(const char* s="_") { return (format("%1%%2%") % s % symID++).str(); } - void push() { code.push_front(); vals.push_front(); } - void pop() { code.pop_front(); vals.pop_front(); } + void push() { code.push(); vals.push(); } + void pop() { code.pop(); vals.pop(); } Value* compile(AST* obj) { Value** v = vals.ref(obj); return (v) ? *v : vals.def(obj, obj->compile(*this)); @@ -1252,11 +1248,9 @@ main(int argc, char** argv) ExecutionEngine* engine = ExecutionEngine::create(module); CEnv cenv(penv, module, engine->getTargetData()); - cenv.tenv.name("Bool", Type::Int1Ty); - cenv.tenv.name("Int", Type::Int32Ty); - cenv.tenv.name("Float", Type::FloatTy); - cenv.code.def(penv.sym("true"), new ASTLiteral(true)); - cenv.code.def(penv.sym("false"), new ASTLiteral(false)); + cenv.tenv.def(penv.sym("Bool"), new AType(penv.sym("Bool"), Type::Int1Ty)); + cenv.tenv.def(penv.sym("Int"), new AType(penv.sym("Int"), Type::Int32Ty)); + cenv.tenv.def(penv.sym("Float"), new AType(penv.sym("Float"), Type::FloatTy)); // Host provided allocation primitive prototypes std::vector argsT(1, Type::Int32Ty); -- cgit v1.2.1