diff options
Diffstat (limited to 'src/constrain.cpp')
-rw-r--r-- | src/constrain.cpp | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/src/constrain.cpp b/src/constrain.cpp index 46da500..f9a0e11 100644 --- a/src/constrain.cpp +++ b/src/constrain.cpp @@ -50,12 +50,12 @@ AFn::constrain(TEnv& tenv, Constraints& c) const if (gt != tenv.genericTypes.end()) { genericType = gt->second; } else { - set<ASymbol*> defined; + set<const ASymbol*> defined; TEnv::Frame frame; // Add parameters to environment frame for (size_t i = 0; i < prot()->size(); ++i) { - ASymbol* sym = prot()->at(i)->to<ASymbol*>(); + const ASymbol* sym = prot()->at(i)->to<const ASymbol*>(); if (!sym) throw Error(prot()->at(i)->loc, "parameter name is not a symbol"); if (defined.find(sym) != defined.end()) @@ -67,14 +67,15 @@ AFn::constrain(TEnv& tenv, Constraints& c) const // Add internal definitions to environment frame size_t e = 2; for (; e < size(); ++e) { - AST* exp = at(e); - ADef* def = exp->to<ADef*>(); + const AST* exp = at(e); + const ADef* def = exp->to<const ADef*>(); if (def) { - ASymbol* sym = def->sym(); + const ASymbol* sym = def->sym(); if (defined.find(sym) != defined.end()) throw Error(def->loc, (format("`%1%' defined twice") % sym->str()).str()); defined.insert(def->sym()); - frame.push_back(make_pair(def->sym(), make_pair(def->at(2), (AType*)NULL))); + frame.push_back(make_pair(def->sym(), + make_pair(const_cast<AST*>(def->at(2)), (AType*)NULL))); } } @@ -84,12 +85,13 @@ AFn::constrain(TEnv& tenv, Constraints& c) const cp.push_back(Constraint(tenv.var(this), tenv.var(), loc)); AType* protT = tup<AType>(loc, NULL); - for (size_t i = 0; i < prot()->size(); ++i) { - AType* tvar = tenv.fresh(prot()->at(i)->to<ASymbol*>()); + size_t s = 0; + for (ATuple::const_iterator i = prot()->begin(); i != prot()->end(); ++i, ++s) { + AType* tvar = tenv.fresh((*i)->to<ASymbol*>()); protT->push_back(tvar); - assert(frame[i].first == prot()->at(i)); - frame[i].second.first = prot()->at(i); - frame[i].second.second = tvar; + assert(frame[s].first == (*i)); + frame[s].second.first = (*i); + frame[s].second.second = tvar; } c.push_back(Constraint(tenv.var(at(1)), protT, at(1)->loc)); @@ -118,17 +120,21 @@ ACall::constrain(TEnv& tenv, Constraints& c) const for (size_t i = 1; i < size(); ++i) at(i)->constrain(tenv, c); - AST* callee = tenv.resolve(at(0)); - AFn* closure = callee->to<AFn*>(); + const AST* callee = tenv.resolve(at(0)); + const AFn* closure = callee->to<const AFn*>(); if (closure) { if (size() - 1 != closure->prot()->size()) throw Error(loc, "incorrect number of arguments"); TEnv::GenericTypes::iterator gt = tenv.genericTypes.find(closure); if (gt != tenv.genericTypes.end()) { - for (size_t i = 1; i < size(); ++i) - c.constrain(tenv, at(i), gt->second->at(1)->as<ATuple*>()->at(i-1)->as<AType*>()); + const ATuple* prot = gt->second->at(1)->to<const ATuple*>(); + const_iterator i = begin(); + const_iterator prot_i = prot->begin(); + for (++i; i != end(); ++i, ++prot_i) + c.constrain(tenv, *i, (*prot_i)->as<AType*>()); AType* retT = tenv.var(this); - c.constrain(tenv, at(0), tup<AType>(at(0)->loc, tenv.penv.sym("Fn"), tenv.var(), retT, 0)); + c.constrain(tenv, at(0), + tup<AType>(at(0)->loc, tenv.penv.sym("Fn"), tenv.var(), retT, 0)); c.constrain(tenv, this, retT); return; } @@ -149,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(at(2), tvar)); + tenv.def(sym, make_pair(const_cast<AST*>(at(2)), tvar)); at(2)->constrain(tenv, c); c.constrain(tenv, this, tvar); } @@ -175,7 +181,7 @@ AIf::constrain(TEnv& tenv, Constraints& c) const void APrimitive::constrain(TEnv& tenv, Constraints& c) const { - const string n = at(0)->to<ASymbol*>()->str(); + const string n = at(0)->to<const ASymbol*>()->str(); enum { ARITHMETIC, BINARY, LOGICAL, COMPARISON } type; if (n == "+" || n == "-" || n == "*" || n == "/") type = ARITHMETIC; |