diff options
Diffstat (limited to 'typing.cpp')
-rw-r--r-- | typing.cpp | 34 |
1 files changed, 18 insertions, 16 deletions
@@ -17,7 +17,6 @@ #include "tuplr.hpp" - /*************************************************************************** * AST Type Contraints * ***************************************************************************/ @@ -25,7 +24,7 @@ void ASTTuple::constrain(TEnv& tenv) const { - AType* t = new AType(ASTTuple()); + AType* t = new AType(ASTTuple(), loc); FOREACH(const_iterator, p, *this) { (*p)->constrain(tenv); t->push_back(tenv.type(*p)); @@ -41,7 +40,7 @@ ASTClosure::constrain(TEnv& tenv) const AType* protT = tenv.type(at(1)); AType* bodyT = tenv.type(at(2)); tenv.constrain(this, new AType(ASTTuple( - tenv.penv.sym("Fn"), protT, bodyT, 0))); + tenv.penv.sym("Fn"), protT, bodyT, 0), loc)); } void @@ -50,11 +49,11 @@ ASTCall::constrain(TEnv& tenv) const FOREACH(const_iterator, p, *this) (*p)->constrain(tenv); AType* retT = tenv.type(this); - AType* argsT = new AType(ASTTuple()); + AType* argsT = new AType(ASTTuple(), loc); for (size_t i = 1; i < size(); ++i) argsT->push_back(tenv.type(at(i))); tenv.constrain(at(0), new AType(ASTTuple( - tenv.penv.sym("Fn"), argsT, retT, NULL))); + tenv.penv.sym("Fn"), argsT, retT, NULL), loc)); } void @@ -91,7 +90,7 @@ ASTPrimitive::constrain(TEnv& tenv) const type = ARITHMETIC; else if (n == "%") type = BINARY; - else if (n == "&" || n == "|" || n == "^") + else if (n == "and" || n == "or" || n == "xor") type = BITWISE; else if (n == "=" || n == "!=" || n == ">" || n == ">=" || n == "<" || n == "<=") type = COMPARISON; @@ -106,13 +105,13 @@ ASTPrimitive::constrain(TEnv& tenv) const if (size() < 3) throw Error((format("`%1%' requires at least 2 arguments") % n).str(), exp.loc); for (size_t i = 1; i < size(); ++i) - tenv.constrain(this, tenv.type(at(i))); + tenv.constrain(at(i), tenv.type(this)); break; case BINARY: if (size() != 3) throw Error((format("`%1%' requires exactly 2 arguments") % n).str(), exp.loc); - tenv.constrain(this, tenv.type(at(1))); - tenv.constrain(this, tenv.type(at(2))); + tenv.constrain(at(1), tenv.type(this)); + tenv.constrain(at(2), tenv.type(this)); break; case BITWISE: if (size() != 3) @@ -127,13 +126,15 @@ ASTPrimitive::constrain(TEnv& tenv) const tenv.constrain(this, tenv.named("Bool")); tenv.constrain(at(1), tenv.type(at(2))); break; + default: + throw Error((format("unknown primitive `%1%'") % n).str(), exp.loc); } } void ASTConsCall::constrain(TEnv& tenv) const { - AType* t = new AType(ASTTuple(tenv.penv.sym("Pair"), 0)); + AType* t = new AType(ASTTuple(tenv.penv.sym("Pair"), 0), loc); for (size_t i = 1; i < size(); ++i) { at(i)->constrain(tenv); t->push_back(tenv.type(at(i))); @@ -145,8 +146,8 @@ void ASTCarCall::constrain(TEnv& tenv) const { at(1)->constrain(tenv); - AType* ct = tenv.var(); - AType* tt = new AType(ASTTuple(tenv.penv.sym("Pair"), ct, tenv.var(), 0)); + AType* ct = tenv.var(loc); + AType* tt = new AType(ASTTuple(tenv.penv.sym("Pair"), ct, tenv.var(at(2)->loc), 0), loc); tenv.constrain(at(1), tt); tenv.constrain(this, ct); } @@ -155,8 +156,8 @@ void ASTCdrCall::constrain(TEnv& tenv) const { at(1)->constrain(tenv); - AType* ct = tenv.var(); - AType* tt = new AType(ASTTuple(tenv.penv.sym("Pair"), tenv.var(), ct, 0)); + AType* ct = tenv.var(loc); + AType* tt = new AType(ASTTuple(tenv.penv.sym("Pair"), tenv.var(at(1)->loc), ct, 0), loc); tenv.constrain(at(1), tt); tenv.constrain(this, ct); } @@ -227,11 +228,12 @@ TEnv::unify(const Constraints& constraints) // TAPL 22.4 AType* si = dynamic_cast<AType*>(s->at(i)); AType* ti = dynamic_cast<AType*>(t->at(i)); if (si && ti) - cp.push_back(make_pair(si, ti)); + cp.push_back(Constraint(si, ti, si->loc)); } return unify(cp); } else { - throw Error("Type unification failed"); + throw Error((format("type is `%1%' but should be `%2%'") % s->str() % t->str()).str(), + constraints.begin()->loc); } } |