aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/compile.cpp4
-rw-r--r--src/constrain.cpp11
-rw-r--r--src/lift.cpp2
-rw-r--r--src/parse.cpp10
-rw-r--r--src/tuplr.hpp33
5 files changed, 28 insertions, 32 deletions
diff --git a/src/compile.cpp b/src/compile.cpp
index 30a8a95..2b16a78 100644
--- a/src/compile.cpp
+++ b/src/compile.cpp
@@ -46,7 +46,7 @@ AFn::compile(CEnv& cenv)
CVal
ACall::compile(CEnv& cenv)
{
- AFn* c = cenv.tenv.resolve(at(0))->to<AFn*>();
+ AFn* c = cenv.resolve(at(0))->to<AFn*>();
if (!c) return NULL; // Primitive
@@ -54,8 +54,6 @@ ACall::compile(CEnv& cenv)
for (size_t i = 1; i < size(); ++i)
protT.push_back(cenv.type(at(i)));
- TEnv::GenericTypes::const_iterator gt = cenv.tenv.genericTypes.find(c);
- assert(gt != cenv.tenv.genericTypes.end());
AType fnT(loc);
fnT.push_back(cenv.penv.sym("Fn"));
fnT.push_back(&protT);
diff --git a/src/constrain.cpp b/src/constrain.cpp
index de07f77..10cb380 100644
--- a/src/constrain.cpp
+++ b/src/constrain.cpp
@@ -42,9 +42,9 @@ AString::constrain(TEnv& tenv, Constraints& c) const
void
ASymbol::constrain(TEnv& tenv, Constraints& c) const
{
- pair<AST*, AType*>* ref = tenv.ref(this);
+ AType** ref = tenv.ref(this);
THROW_IF(!ref, loc, (format("undefined symbol `%1%'") % cppstr).str());
- c.push_back(Constraint(tenv.var(this), ref->second, loc));
+ c.push_back(Constraint(tenv.var(this), *ref, loc));
}
void
@@ -78,7 +78,7 @@ AFn::constrain(TEnv& tenv, Constraints& c) const
(format("duplicate parameter `%1%'") % sym->str()).str());
defs.insert(sym);
AType* tvar = tenv.fresh(sym);
- frame.push_back(make_pair(sym, make_pair((AST*)(*i), tvar)));
+ frame.push_back(make_pair(sym, tvar));
protT->push_back(tvar);
}
c.push_back(Constraint(tenv.var(at(1)), protT, at(1)->loc));
@@ -93,8 +93,7 @@ AFn::constrain(TEnv& tenv, Constraints& c) const
THROW_IF(defs.count(sym) != 0, def->loc,
(format("`%1%' defined twice") % sym->str()).str());
defs.insert(def->sym());
- frame.push_back(make_pair(def->sym(),
- make_pair(const_cast<AST*>(def->at(2)), (AType*)NULL)));
+ frame.push_back(make_pair(def->sym(), (AType*)NULL));
}
}
@@ -156,7 +155,7 @@ ADef::constrain(TEnv& tenv, Constraints& c) const
THROW_IF(!sym, loc, "`def' has no symbol")
AType* tvar = tenv.var(at(2));
- tenv.def(sym, make_pair(const_cast<AST*>(at(2)), tvar));
+ tenv.def(sym, tvar);
at(2)->constrain(tenv, c);
c.constrain(tenv, at(1), tvar);
c.constrain(tenv, this, tenv.named("Nothing"));
diff --git a/src/lift.cpp b/src/lift.cpp
index 83c0491..3f0e1bf 100644
--- a/src/lift.cpp
+++ b/src/lift.cpp
@@ -47,7 +47,7 @@ AFn::lift(CEnv& cenv)
void
ACall::lift(CEnv& cenv)
{
- AFn* c = cenv.tenv.resolve(at(0))->to<AFn*>();
+ AFn* c = cenv.resolve(at(0))->to<AFn*>();
AType argsT(loc);
// Lift arguments
diff --git a/src/parse.cpp b/src/parse.cpp
index 9cf74e6..ac232de 100644
--- a/src/parse.cpp
+++ b/src/parse.cpp
@@ -102,11 +102,11 @@ void
initLang(PEnv& penv, TEnv& tenv)
{
// Types
- tenv.def(penv.sym("Nothing"), make_pair((AST*)0, new AType(penv.sym("Nothing"))));
- tenv.def(penv.sym("Bool"), make_pair((AST*)0, new AType(penv.sym("Bool"))));
- tenv.def(penv.sym("Int"), make_pair((AST*)0, new AType(penv.sym("Int"))));
- tenv.def(penv.sym("Float"), make_pair((AST*)0, new AType(penv.sym("Float"))));
- tenv.def(penv.sym("String"), make_pair((AST*)0, new AType(penv.sym("String"))));
+ tenv.def(penv.sym("Nothing"), new AType(penv.sym("Nothing")));
+ tenv.def(penv.sym("Bool"), new AType(penv.sym("Bool")));
+ tenv.def(penv.sym("Int"), new AType(penv.sym("Int")));
+ tenv.def(penv.sym("Float"), new AType(penv.sym("Float")));
+ tenv.def(penv.sym("String"), new AType(penv.sym("String")));
// Literals
static bool trueVal = true;
diff --git a/src/tuplr.hpp b/src/tuplr.hpp
index f69252a..947a62c 100644
--- a/src/tuplr.hpp
+++ b/src/tuplr.hpp
@@ -539,15 +539,15 @@ inline ostream& operator<<(ostream& out, const Constraints& c) {
}
/// Type-Time Environment
-struct TEnv : public Env< const ASymbol*, pair<AST*, AType*> > {
+struct TEnv : public Env<const ASymbol*, AType*> {
TEnv(PEnv& p) : penv(p), varID(1) {}
AType* fresh(const ASymbol* sym) {
- return def(sym, make_pair((AST*)NULL, new AType(sym->loc, varID++))).second;
+ return def(sym, new AType(sym->loc, varID++));
}
AType* var(const AST* ast=0) {
const ASymbol* sym = ast->to<const ASymbol*>();
if (sym)
- return ref(sym)->second;
+ return *ref(sym);
Vars::iterator v = vars.find(ast);
if (v != vars.end())
@@ -560,16 +560,7 @@ struct TEnv : public Env< const ASymbol*, pair<AST*, AType*> > {
return ret;
}
AType* named(const string& name) {
- return ref(penv.sym(name))->second;
- }
- AST* resolve(AST* ast) {
- const ASymbol* sym = ast->to<const ASymbol*>();
- const pair<AST*, AType*>* rec = ref(sym);
- return rec ? rec->first : ast;
- }
- const AST* resolve(const AST* ast) {
- const ASymbol* sym = ast->to<const ASymbol*>();
- return sym ? ref(sym)->first : ast;
+ return *ref(penv.sym(name));
}
static Subst buildSubst(AType* fnT, const AType& argsT);
@@ -623,19 +614,25 @@ struct CEnv {
typedef Env<const ASymbol*, CVal> Vals;
Engine* engine() { return _engine; }
- void push() { tenv.push(); vals.push(); }
- void pop() { tenv.pop(); vals.pop(); }
+ void push() { code.push(); tenv.push(); vals.push(); }
+ void pop() { code.pop(); tenv.pop(); vals.pop(); }
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*>();
if (sym)
- return tenv.ref(sym)->second;
+ return *tenv.ref(sym);
return tsubst.apply(subst.apply(tenv.vars[ast]))->to<AType*>();
}
void def(const ASymbol* sym, AST* c, AType* t, CVal v) {
- tenv.def(sym, make_pair(c, t));
+ code.def(sym, c);
+ tenv.def(sym, t);
vals.def(sym, v);
}
+ AST* resolve(AST* ast) {
+ const ASymbol* sym = ast->to<const ASymbol*>();
+ AST** rec = code.ref(sym);
+ return rec ? *rec : ast;
+ }
ostream& out;
ostream& err;
@@ -644,6 +641,8 @@ struct CEnv {
Vals vals;
Subst tsubst;
+ Env<const ASymbol*, AST*> code;
+
map<string,string> args;
private: