From 563a807be78bfe12e5bfbb9ff0d6da44242696c4 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 2 Dec 2010 06:16:29 +0000 Subject: Represent code as list structure (i.e. traditional LISP lists built from pairs), rather than tuple structure. Remove unused/crufty depoly stage. Remove cps from AST interface (but keep cps.cpp code around for later). Improved command line interface for compilation stages (options -T -L -S). git-svn-id: http://svn.drobilla.net/resp/resp@277 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/unify.cpp | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'src/unify.cpp') diff --git a/src/unify.cpp b/src/unify.cpp index a4ea035..9165d49 100644 --- a/src/unify.cpp +++ b/src/unify.cpp @@ -31,19 +31,19 @@ TEnv::buildSubst(const AType* genericT, const AType& argsT) Subst subst; // Build substitution to apply to generic type - const ATuple* genericProtT = (*(genericT->begin() + 1))->as(); + const ATuple* genericProtT = genericT->list_ref(1)->as(); ATuple::const_iterator g = genericProtT->begin(); AType::const_iterator a = argsT.begin(); for (; a != argsT.end(); ++a, ++g) { const AType* genericArgT = (*g)->to(); - AType* callArgT = (*a)->to(); + const AType* callArgT = (*a)->to(); if (callArgT->kind == AType::EXPR) { assert(genericArgT->kind == AType::EXPR); ATuple::const_iterator gi = genericArgT->begin(); ATuple::const_iterator ci = callArgT->begin(); for (; gi != genericArgT->end(); ++gi, ++ci) { const AType* gT = (*gi)->to(); - AType* aT = (*ci)->to(); + const AType* aT = (*ci)->to(); if (gT && aT) subst.add(gT, aT); } @@ -64,29 +64,27 @@ Constraints::constrain(TEnv& tenv, const AST* o, const AType* t) push_back(Constraint(tenv.var(o), t)); } -template -static const T* -substitute(const T* tup, const E* from, const E* to) +static const AType* +substitute(const AType* tup, const AType* from, const AType* to) { if (!tup) return NULL; - T* ret = new T(*tup); - typename T::iterator ri = ret->begin(); - FOREACHP(typename T::const_iterator, i, tup) { + TList ret; + FOREACHP(AType::const_iterator, i, tup) { if (**i == *from) { - T* type = new T(*to); + AType* type = new AType(*to); type->loc = (*i)->loc; - *ri++ = type; - } else if (static_cast(*i) != static_cast(to)) { - const T* subTup = dynamic_cast(*i); - if (subTup) - *ri++ = const_cast(substitute(subTup, from, to)); + ret.push_back(type); + } else if (*i != to) { + const AType* elem = (*i)->as(); + if (elem->kind == AType::EXPR) + ret.push_back(const_cast(substitute(elem, from, to))); else - *ri++ = *i; + ret.push_back(const_cast(elem)); } else { - ++ri; + ret.push_back(const_cast((*i)->as())); } } - return ret; + return ret.head; } /// Compose two substitutions (TAPL 22.1.1) @@ -114,14 +112,14 @@ Constraints::replace(const AType* s, const AType* t) AType* type = new AType(*t); type->loc = c->first->loc; c->first = type; - } else { + } else if (c->first->kind == AType::EXPR) { c->first = substitute(c->first, s, t); } if (*c->second == *s) { AType* type = new AType(*t); type->loc = c->second->loc; c->second = type; - } else { + } else if (c->second->kind == AType::EXPR) { c->second = substitute(c->second, s, t); } } @@ -150,15 +148,15 @@ unify(const Constraints& constraints) AType::const_iterator si = s->begin(); AType::const_iterator ti = t->begin(); for (; si != s->end() && ti != t->end(); ++si, ++ti) { - AType* st = (*si)->as(); - AType* tt = (*ti)->as(); + const AType* st = (*si)->as(); + const AType* tt = (*ti)->as(); if (st->kind == AType::DOTS || tt->kind == AType::DOTS) return unify(cp); else cp.push_back(Constraint(st, tt)); } - if ( (si == s->end() && (ti == t->end() || (*ti)->as()->kind == AType::DOTS)) - || (ti == t->end() && (*si)->as()->kind == AType::DOTS)) + if ( (si == s->end() && (ti == t->end() || (*ti)->as()->kind == AType::DOTS)) + || (ti == t->end() && (*si)->as()->kind == AType::DOTS)) return unify(cp); } throw Error(s->loc, -- cgit v1.2.1