aboutsummaryrefslogtreecommitdiffstats
path: root/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'tuplr.hpp')
-rw-r--r--tuplr.hpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/tuplr.hpp b/tuplr.hpp
index 506742a..5e60e75 100644
--- a/tuplr.hpp
+++ b/tuplr.hpp
@@ -453,17 +453,18 @@ struct Subst : public map<const AType*,AType*> {
};
/// Type-Time Environment
-struct TEnv : public Env<const ASymbol*,AType*> {
+struct TEnv : public Env< const ASymbol*, pair<AST*, AType*> > {
TEnv(PEnv& p) : penv(p), varID(1) {}
-
AType* fresh(const ASymbol* sym) {
assert(sym);
- return def(sym, new AType(varID++, LAddr(), sym->loc));
+ AType* ret = new AType(varID++, LAddr(), sym->loc);
+ def(sym, make_pair((AST*)NULL, ret));
+ return ret;
}
AType* var(const AST* ast=0) {
const ASymbol* sym = dynamic_cast<const ASymbol*>(ast);
if (sym)
- return deref(lookup(sym));
+ return deref(lookup(sym)).second;
Vars::iterator v = vars.find(ast);
if (v != vars.end())
@@ -476,7 +477,14 @@ struct TEnv : public Env<const ASymbol*,AType*> {
return ret;
}
AType* named(const string& name) {
- return *ref(penv.sym(name));
+ return ref(penv.sym(name))->second;
+ }
+ AST* resolve(AST* ast) {
+ ASymbol* sym = dynamic_cast<ASymbol*>(ast);
+ if (sym)
+ return ref(sym)->first;
+ else
+ return ast;
}
static Subst unify(const Constraints& c);
@@ -504,8 +512,8 @@ struct CEnv {
CEngine engine();
string gensym(const char* s="_") { return (format("%s%d") % s % symID++).str(); }
- void push() { code.push(); vals.push(); tenv.push(); }
- void pop() { code.pop(); vals.pop(); tenv.pop(); }
+ void push() { tenv.push(); vals.push(); }
+ void pop() { tenv.pop(); vals.pop(); }
void precompile(AST* obj, CValue value) { vals.def(obj, value); }
CValue compile(AST* obj);
void optimise(CFunction f);
@@ -513,18 +521,18 @@ struct CEnv {
AType* type(AST* ast, const Subst& subst = Subst()) const {
ASymbol* sym = dynamic_cast<ASymbol*>(ast);
if (sym)
- return tenv.deref(sym->addr);
+ return tenv.deref(sym->addr).second;
return dynamic_cast<AType*>(tsubst.apply(subst.apply(tenv.vars[ast])));
}
void def(ASymbol* sym, AST* c, AType* t, CValue v) {
- code.def(sym, c); tenv.def(sym, t); vals.def(sym, v);
+ tenv.def(sym, make_pair(c, t));
+ vals.def(sym, v);
}
ostream& out;
ostream& err;
PEnv& penv;
TEnv& tenv;
- Code code;
Vals vals;
unsigned symID;