From 12314c754187ae246bc38aceb827bf51d1669d73 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 25 Dec 2012 00:51:18 +0000 Subject: Use C++11 range-based for loops. git-svn-id: http://svn.drobilla.net/resp/trunk@444 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/c.cpp | 16 +++++++-------- src/constrain.cpp | 14 ++++++------- src/cps.cpp | 14 ++++++------- src/depoly.cpp | 8 ++++---- src/expand.cpp | 4 ++-- src/flatten.cpp | 4 ++-- src/gc.cpp | 8 ++++---- src/lift.cpp | 32 ++++++++++++++--------------- src/llvm.cpp | 24 +++++++++++----------- src/pprint.cpp | 4 ++-- src/repl.cpp | 44 ++++++++++++++++++++-------------------- src/resp.cpp | 13 ++++++------ src/resp.hpp | 60 +++++++++++++++++++++++++++---------------------------- src/unify.cpp | 51 +++++++++++++++++++++++----------------------- 14 files changed, 147 insertions(+), 149 deletions(-) (limited to 'src') diff --git a/src/c.cpp b/src/c.cpp index 934100c..c4cd397 100644 --- a/src/c.cpp +++ b/src/c.cpp @@ -102,8 +102,8 @@ CEngine::llType(const AST* t) return NULL; Type* ret = new Type(*llType(retT) + " (*)("); - FOREACHP(ATuple::const_iterator, i, protT) { - const Type* lt = llType(*i); + for (const auto& i : *protT) { + const Type* lt = llType(i); if (!lt) return NULL; *ret += *lt; @@ -133,8 +133,8 @@ CEngine::compileCall(CEnv& cenv, CFunc func, const ATuple* funcT, const vectorreturnType % *varname % f->name).str(); - FOREACH(vector::const_iterator, i, args) - out += *llVal(*i); + for (const auto& i : args) + out += *llVal(i); out += ");\n"; return varname; } @@ -188,10 +188,10 @@ CEngine::startFn(CEnv& cenv, const std::string& name, const ATuple* args, const const AST* retT = type->list_ref(2); vector cprot; - FOREACHP(ATuple::const_iterator, i, argsT) { - THROW_IF(!llType(*i), Cursor(), string("non-concrete parameter :: ") - + (*i)->str()) - cprot.push_back(llType(*i)); + for (const auto& i : *argsT) { + THROW_IF(!llType(i), Cursor(), string("non-concrete parameter :: ") + + i->str()) + cprot.push_back(llType(i)); } THROW_IF(!llType(retT), Cursor(), diff --git a/src/constrain.cpp b/src/constrain.cpp index baa4ca8..2a3c8fe 100644 --- a/src/constrain.cpp +++ b/src/constrain.cpp @@ -154,9 +154,9 @@ constrain_fn(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) // Add parameters to environment frame List protT; - FOREACHP(ATuple::const_iterator, i, call->prot()) { - const ASymbol* sym = (*i)->to_symbol(); - THROW_IF(!sym, (*i)->loc, "parameter name is not a symbol"); + for (const auto& i : *call->prot()) { + const ASymbol* sym = i->to_symbol(); + THROW_IF(!sym, i->loc, "parameter name is not a symbol"); THROW_IF(defs.count(sym) != 0, sym->loc, (format("duplicate parameter `%1%'") % sym->str()).str()); defs.insert(sym); @@ -312,8 +312,8 @@ resp_constrain_quoted(TEnv& tenv, Constraints& c, const AST* ast) throw(Error) c.constrain(tenv, ast, tupT); c.constrain(tenv, tup->fst(), fstT); - FOREACHP(ATuple::const_iterator, i, ast->as_tuple()) { - resp_constrain_quoted(tenv, c, *i); + for (const auto& i : *ast->as_tuple()) { + resp_constrain_quoted(tenv, c, i); } } else { @@ -335,8 +335,8 @@ constrain_call(TEnv& tenv, Constraints& c, const ATuple* call) throw(Error) { const AST* const head = call->fst(); - for (ATuple::const_iterator i = call->begin(); i != call->end(); ++i) - resp_constrain(tenv, c, *i); + for (auto i : *call) + resp_constrain(tenv, c, i); const AST* fnType = tenv.var(head); if (!AType::is_var(fnType)) { diff --git a/src/cps.cpp b/src/cps.cpp index bb375a8..38dc27a 100644 --- a/src/cps.cpp +++ b/src/cps.cpp @@ -56,8 +56,8 @@ cps_fn(CEnv& cenv, const ATuple* fn, const AST* cont) const ASymbol* k2 = cenv.penv.gensym("__k"); List copyProt; - FOREACHP(ATuple::const_iterator, i, fn->prot()) - copyProt.push_back(*i); + for (const auto& i : *fn->prot()) + copyProt.push_back(i); copyProt.push_back(k2); assert(fn->fst()); @@ -81,11 +81,11 @@ cps_call(CEnv& cenv, const ATuple* call, const AST* k) typedef std::vector ExpVec; ExpVec exprs; ExpVec args; - FOREACHP(ATuple::const_iterator, i, call) { - exprs.push_back(*i); - if (is_value(cenv, *i)) { - body.push_back(*i); - args.push_back(*i); + for (const auto& i : *call) { + exprs.push_back(i); + if (is_value(cenv, i)) { + body.push_back(i); + args.push_back(i); } else { const ASymbol* sym = cenv.penv.gensym("__a"); body.push_back(sym); diff --git a/src/depoly.cpp b/src/depoly.cpp index 0ee89c1..cb698df 100644 --- a/src/depoly.cpp +++ b/src/depoly.cpp @@ -35,8 +35,8 @@ is_concrete(const AST* type) return isupper(type->as_symbol()->str()[0]); } else { const ATuple* tup = type->as_tuple(); - for (ATuple::const_iterator i = tup->begin(); i != tup->end(); ++i) { - if (!is_concrete(*i)) { + for (auto i : *tup) { + if (!is_concrete(i)) { return false; } } @@ -93,8 +93,8 @@ raise_type(CEnv& cenv, Code& code, const ATuple* type) static const AST* depoly_args(CEnv& cenv, Code& code, const ATuple* call) throw() { - for (ATuple::const_iterator i = call->begin(); i != call->end(); ++i) { - const AST* type = cenv.type(*i); + for (auto i : *call) { + const AST* type = cenv.type(i); if (type && type->to_tuple()) { if (is_concrete(type)) { raise_type(cenv, code, type->as_tuple()); diff --git a/src/expand.cpp b/src/expand.cpp index 3c562fa..c0f2f64 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -104,8 +104,8 @@ expand_list(PEnv& penv, const ATuple* e) // No match, try to expand children List ret; - FOREACHP(ATuple::const_iterator, i, e) - ret.push_back(penv.expand(*i)); + for (const auto& i : *e) + ret.push_back(penv.expand(i)); ret.head->loc = e->loc; return ret.head; } diff --git a/src/flatten.cpp b/src/flatten.cpp index ca849fa..b190832 100644 --- a/src/flatten.cpp +++ b/src/flatten.cpp @@ -40,8 +40,8 @@ flatten_def(CEnv& cenv, Code& code, const ATuple* def) throw() const ATuple* fn = (*def->iter_at(2))->as_tuple(); const ATuple* prot = fn->frst()->as_tuple(); List pre(Cursor(), cenv.penv.sym("fn-start"), sym, 0); - for (ATuple::const_iterator i = prot->begin(); i != prot->end(); ++i) - pre.push_back(*i); + for (auto p : *prot) + pre.push_back(p); code.push_back(pre); diff --git a/src/gc.cpp b/src/gc.cpp index 59036df..d07367d 100644 --- a/src/gc.cpp +++ b/src/gc.cpp @@ -66,8 +66,8 @@ mark(const Object* obj) if (obj->tag() != T_UNKNOWN) { const ATuple* tup = ((const AST*)obj)->to_tuple(); if (tup) - FOREACHP(ATuple::const_iterator, i, tup) - mark(*i); + for (const auto& i : *tup) + mark(i); } } @@ -76,8 +76,8 @@ GC::collect(const Roots& roots) { //const size_t oldSize = _heap.size(); - for (Roots::const_iterator i = roots.begin(); i != roots.end(); ++i) - mark(*i); + for (auto r : roots) + mark(r); for (Heap::iterator i = _heap.begin(); i != _heap.end();) { Heap::iterator next = i; diff --git a/src/lift.cpp b/src/lift.cpp index 7fa834f..48fcf4e 100644 --- a/src/lift.cpp +++ b/src/lift.cpp @@ -124,14 +124,14 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() // Prepend closure parameter implProt.push_back(cenv.penv.sym("_me")); - for (ATuple::const_iterator p = fn->prot()->begin(); p != fn->prot()->end(); ++p) { + for (auto p : *fn->prot()) { const AST* paramType = (*tp++); if (is_form(paramType, "lambda")) { const ATuple* fnType = new ATuple(cenv.tenv.var(), paramType->as_tuple(), paramType->loc); - paramType = tup((*p)->loc, cenv.tenv.Tup, fnType, NULL); + paramType = tup(p->loc, cenv.tenv.Tup, fnType, NULL); } - cenv.def((*p)->as_symbol(), *p, paramType, NULL); - implProt.push_back(*p); + cenv.def(p->as_symbol(), p, paramType, NULL); + implProt.push_back(p); implProtT.push_back(paramType); } @@ -178,9 +178,9 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() const CEnv::FreeVars freeVars = cenv.liftStack.top(); cenv.liftStack.pop(); - for (CEnv::FreeVars::const_iterator i = freeVars.begin(); i != freeVars.end(); ++i) { - cons.push_back(resp_lift(cenv, code, *i)); - closureT.push_back(cenv.type(*i)); + for (auto v : freeVars) { + cons.push_back(resp_lift(cenv, code, v)); + closureT.push_back(cenv.type(v)); } // Now we know our real lifted return type @@ -207,18 +207,18 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() cenv.def(cenv.penv.sym(cenv.name(fn)), fn, closureT, NULL); // Replace return type variable with actual return type in type environment - for (TEnv::iterator i = cenv.tenv.begin(); i != cenv.tenv.end(); ++i) { - for (TEnv::Frame::iterator j = i->begin(); j != i->end(); ++j) { - if (j->second->to_tuple()) { - j->second = j->second->as_tuple()->replace(retTVar, implRetT); + for (auto& i : cenv.tenv) { + for (auto& j : i) { + if (j.second->to_tuple()) { + j.second = j.second->as_tuple()->replace(retTVar, implRetT); } } } // Replace return type variable with actual return type in code - for (Code::iterator i = code.begin(); i != code.end(); ++i) { - if (is_form(*i, "def-type")) { - *i = cenv.typedReplace((*i)->as_tuple(), retTVar, implRetT); + for (auto& i : code) { + if (is_form(i, "def-type")) { + i = cenv.typedReplace(i->as_tuple(), retTVar, implRetT); } } @@ -231,8 +231,8 @@ lift_call(CEnv& cenv, Code& code, const ATuple* call) throw() List copy; // Lift all children (callee and arguments, recursively) - for (ATuple::const_iterator i = call->begin(); i != call->end(); ++i) - copy.push_back(resp_lift(cenv, code, *i)); + for (auto i : *call) + copy.push_back(resp_lift(cenv, code, i)); copy.head->loc = call->loc; diff --git a/src/llvm.cpp b/src/llvm.cpp index 93bf066..2bd1930 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -219,8 +219,8 @@ LLVMEngine::llType(const AST* t, const char* name) return NULL; vector cprot; - FOREACHP(ATuple::const_iterator, i, protT) { - Type* lt = llType(*i); + for (const auto& i : *protT) { + Type* lt = llType(i); if (!lt) return NULL; cprot.push_back(lt); @@ -414,11 +414,11 @@ LLVMEngine::compileProt( Function::LinkageTypes linkage = Function::ExternalLinkage; vector cprot; - FOREACHP(ATuple::const_iterator, i, argsT) { - const CType iT = ((*i)->to_symbol()) - ? compileType(cenv, (*i)->str(), cenv.resolveType(*i)) - : compileType(cenv, (*i)->as_tuple()->fst()->str(), *i); - THROW_IF(!iT, Cursor(), string("non-concrete parameter :: ") + (*i)->str()); + for (const auto& i : *argsT) { + const CType iT = (i->to_symbol()) + ? compileType(cenv, i->str(), cenv.resolveType(i)) + : compileType(cenv, i->as_tuple()->fst()->str(), i); + THROW_IF(!iT, Cursor(), string("non-concrete parameter :: ") + i->str()); cprot.push_back((Type*)iT); } @@ -436,8 +436,8 @@ LLVMEngine::compileProt( // Set argument names in generated code if (args) { Function::arg_iterator a = f->arg_begin(); - for (ATuple::const_iterator i = args->begin(); i != args->end(); ++a, ++i) - a->setName((*i)->as_symbol()->sym()); + for (const auto& i : *args) + (a++)->setName(i->as_symbol()->sym()); } // Define function in the environment so any calls that get compiled before @@ -694,13 +694,13 @@ LLVMEngine::call(CEnv& cenv, CFunc f, const AST* retT) } else if (retT->str() == "String") { const std::string s(((char* (*)())fp)()); ss << "\""; - for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { - switch (*i) { + for (auto c : s) { + switch (c) { case '\"': case '\\': ss << '\\'; default: - ss << *i; + ss << c; break; } } diff --git a/src/pprint.cpp b/src/pprint.cpp index 2a8a62d..80a2519 100644 --- a/src/pprint.cpp +++ b/src/pprint.cpp @@ -205,8 +205,8 @@ print_to(ostream& out, const AST* ast, unsigned indent, CEnv* cenv, bool types) } else { // Print on multiple lines if list contains lists - for (ATuple::const_iterator ti = tup->begin(); ti != tup->end(); ++ti) { - if ((*ti)->to_tuple()) { + for (auto ti : *tup) { + if (ti->to_tuple()) { print_list(out, tup, i, indent + head_width, cenv, types, false); return out; } diff --git a/src/repl.cpp b/src/repl.cpp index afa81e9..16d766f 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -32,8 +32,8 @@ using namespace std; static inline const AST* dump(CEnv& cenv, const Code& code) { - for (Code::const_iterator i = code.begin(); i != code.end(); ++i) - pprint(cout, *i, &cenv, (cenv.args.find("-a") != cenv.args.end())); + for (auto c : code) + pprint(cout, c, &cenv, (cenv.args.find("-a") != cenv.args.end())); return 0; } @@ -91,8 +91,8 @@ compile(CEnv& cenv, const Code& parsed, Code& defs, bool& hasMain, const char* m // Convert to CPS (currently not used in the compilation process) if (cenv.args.find("-C") != cenv.args.end()) { Code cps; - for (Code::const_iterator i = parsed.begin(); i != parsed.end(); ++i) { - const AST* exp = resp_cps(cenv, *i, cenv.penv.sym("display")); + for (auto i : parsed) { + const AST* exp = resp_cps(cenv, i, cenv.penv.sym("display")); if (exp) { cps.push_back(exp); } @@ -120,8 +120,8 @@ compile(CEnv& cenv, const Code& parsed, Code& defs, bool& hasMain, const char* m for (const Pass* p = passes; p->pass; ++p) { const Code& input = stages.back(); Code output; - for (Code::const_iterator i = input.begin(); i != input.end(); ++i) { - const AST* exp = (*p->pass)(cenv, output, *i); + for (auto i : input) { + const AST* exp = (*p->pass)(cenv, output, i); if (exp) { output.push_back(exp); } @@ -136,18 +136,18 @@ compile(CEnv& cenv, const Code& parsed, Code& defs, bool& hasMain, const char* m // Flatten expressions const AST* retT = NULL; Code exprs; - for (Code::const_iterator i = stages.back().begin(); i != stages.back().end(); ++i) { - const ATuple* call = (*i)->to_tuple(); - if (call && (is_form(*i, "def-type") - || (is_form(*i, "define") && is_form(call->frrst(), "lambda")))) { + for (auto i : stages.back()) { + const ATuple* call = i->to_tuple(); + if (call && (is_form(i, "def-type") + || (is_form(i, "define") && is_form(call->frrst(), "lambda")))) { resp_flatten(cenv, defs, call); - } else if (call && is_form(*i, "prot")) { - defs.push_back(*i); + } else if (call && is_form(i, "prot")) { + defs.push_back(i); } else { - const ATuple* tup = (*i)->to_tuple(); + const ATuple* tup = i->to_tuple(); if (!tup || !tup->empty()) { - exprs.push_back(*i); - retT = cenv.type(*i); + exprs.push_back(i); + retT = cenv.type(i); } } } @@ -163,8 +163,8 @@ compile(CEnv& cenv, const Code& parsed, Code& defs, bool& hasMain, const char* m tup(Cursor(), cenv.penv.sym("fn-start"), main, NULL)); const AST* mainRet = NULL; - for (Code::const_iterator i = exprs.begin(); i != exprs.end(); ++i) - mainRet = resp_flatten(cenv, defs, *i); + for (auto e : exprs) + mainRet = resp_flatten(cenv, defs, e); defs.push_back( tup(Cursor(), cenv.penv.sym("fn-end"), main, mainRet, NULL)); @@ -210,9 +210,9 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute) } // Compile flattened code - for (Code::const_iterator i = defs.begin(); i != defs.end(); ++i) { - resp_compile(cenv, *i); - Object::pool.addRoot(*i); + for (auto d : defs) { + resp_compile(cenv, d); + Object::pool.addRoot(d); } if (cenv.args.find("-S") != cenv.args.end()) { cenv.engine()->writeModule(cenv, cenv.out); @@ -274,8 +274,8 @@ repl(CEnv& cenv) } // Compile flattened code - for (Code::const_iterator i = defs.begin(); i != defs.end(); ++i) { - resp_compile(cenv, *i); + for (auto d : defs) { + resp_compile(cenv, d); } if (cenv.args.find("-S") != cenv.args.end()) { cenv.engine()->writeModule(cenv, cenv.out); diff --git a/src/resp.cpp b/src/resp.cpp index c37fd3d..704c864 100644 --- a/src/resp.cpp +++ b/src/resp.cpp @@ -157,11 +157,11 @@ main(int argc, char** argv) fs.open(output.c_str()); ostream& os = (output == "") ? std::cout : fs; - for (Files::iterator f = files.begin(); f != files.end(); ++f) { - ifstream is(*f); + for (auto f : files) { + ifstream is(f); try { while (is.good() && !is.eof()) { - Cursor loc(*f, 1, 1); + Cursor loc(f, 1, 1); const AST* exp = cenv->penv.parse(loc, is); if (!exp || (exp->to_tuple() && exp->to_tuple()->empty())) break; @@ -193,17 +193,16 @@ main(int argc, char** argv) ret = repl(*cenv); } else { // Evalute (or just type check if -T, or just compile if -S all files - for (Files::iterator f = files.begin(); f != files.end(); ++f) { - const string filename = *f; + for (std::string filename : files) { if (!batch) output = filename + ".out"; - ifstream is(*f); + ifstream is(filename); if (is.good()) { Cursor cursor(filename, 1, 1); ret = ret | eval(*cenv, cursor, is, !batch); } else { - cerr << argv[0] << ": " << *f << ": " << strerror(errno) << endl; + cerr << argv[0] << ": " << filename << ": " << strerror(errno) << endl; ++ret; } is.close(); diff --git a/src/resp.hpp b/src/resp.hpp index fd861aa..9e1e4f7 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -38,8 +38,6 @@ #include -#define FOREACH(IT, i, c) for (IT i = (c).begin(); i != (c).end(); ++i) -#define FOREACHP(IT, i, c) for (IT i = (c)->begin(); i != (c)->end(); ++i) #define THROW_IF(cond, error, ...) { if (cond) throw Error(error, __VA_ARGS__); } using namespace std; @@ -306,11 +304,11 @@ list_contains(const ATuple* head, const AST* child) { if (*head == *child) return true; - FOREACHP(ATuple::const_iterator, p, head) { - if (**p == *child) + for (const auto& p : *head) { + if (*p == *child) return true; - const ATuple* tup = (*p)->to_tuple(); + const ATuple* tup = p->to_tuple(); if (tup && list_contains(tup, child)) return true; } @@ -327,8 +325,8 @@ list_equals(const ATuple* lhs, const ATuple* rhs) return false; ATuple::const_iterator l = lhs->begin(); - FOREACHP(ATuple::const_iterator, r, rhs) - if (!(*(*l++) == *(*r))) + for (const auto& r : *rhs) + if (!(*(*l++) == *r)) return false; return true; } @@ -383,12 +381,12 @@ inline const ATuple* ATuple::replace(const AST* from, const AST* to) const { List copy; - FOREACHP(const_iterator, i, this) { - if (*i == from) { + for (auto i : *this) { + if (i == from) { copy.push_back(to); } else { - const ATuple* tup = (*i)->to_tuple(); - copy.push_back(tup ? tup->replace(from, to) : (*i)); + const ATuple* tup = i->to_tuple(); + copy.push_back(tup ? tup->replace(from, to) : i); } } copy.head->loc = loc; @@ -456,15 +454,15 @@ struct Env : public list< vector< pair > > { return v; } V* ref(const ASymbol* key) { - for (typename Env::iterator f = this->begin(); f != this->end(); ++f) - for (typename Frame::iterator b = f->begin(); b != f->end(); ++b) - if (b->first == key->sym()) - return &b->second; + for (auto& f : *this) + for (auto& b : f) + if (b.first == key->sym()) + return &b.second; return NULL; } bool contains(const Frame& frame, const ASymbol* key) const { - for (typename Frame::const_iterator b = frame.begin(); b != frame.end(); ++b) - if (b->first == key->sym()) + for (const auto& b : frame) + if (b.first == key->sym()) return true; return false; } @@ -504,7 +502,7 @@ struct Macro { /// Parse Time Environment (really just a symbol table) struct PEnv : private map { PEnv() : symID(0) {} - ~PEnv() { FOREACHP(const_iterator, i, this) free(const_cast(i->second)); } + ~PEnv() { for (const auto& i : *this) free(const_cast(i.second)); } string gensymstr(const char* s="b") { return (format("_%s%d") % s % symID++).str(); } ASymbol* gensym(const char* s="b") { return sym(gensymstr(s)); } ASymbol* sym(const string& s, Cursor c=Cursor()) { @@ -599,17 +597,17 @@ struct Subst : public list { bool contains(const AST* type) const { if (find(type) != end()) return true; - FOREACHP(const_iterator, j, this) - if (*j->second == *type - || (AType::is_expr(j->second) && list_contains(j->second->as_tuple(), type))) + for (const auto& j : *this) + if (*j.second == *type + || (AType::is_expr(j.second) && list_contains(j.second->as_tuple(), type))) return true; return false; } }; inline ostream& operator<<(ostream& out, const Subst& s) { - for (Subst::const_iterator i = s.begin(); i != s.end(); ++i) - out << i->first << " => " << i->second << endl; + for (const auto& c : s) + out << c.first << " => " << c.second << endl; return out; } @@ -617,8 +615,8 @@ inline ostream& operator<<(ostream& out, const Subst& s) { struct Constraints : public list { Constraints() : list() {} explicit Constraints(const Subst& subst) : list() { - FOREACH(Subst::const_iterator, i, subst) - push_back(Constraint(i->first, i->second)); + for (const auto& i : subst) + push_back(Constraint(i.first, i.second)); } Constraints(const_iterator begin, const_iterator end) : list(begin, end) {} void constrain(TEnv& tenv, const AST* o, const AST* t); @@ -626,8 +624,8 @@ struct Constraints : public list { }; inline ostream& operator<<(ostream& out, const Constraints& c) { - for (Constraints::const_iterator i = c.begin(); i != c.end(); ++i) - out << i->first << " <= " << i->second << endl; + for (const auto& i : c) + out << i.first << " <= " << i.second << endl; return out; } @@ -770,12 +768,12 @@ struct CEnv { } const ATuple* typedReplace(const ATuple* in, const AST* from, const AST* to) { List copy; - FOREACHP(ATuple::const_iterator, i, in) { - if (*i == from) { + for (const auto& i : *in) { + if (i == from) { copy.push_back(to); } else { - const ATuple* tup = (*i)->to_tuple(); - copy.push_back(tup ? typedReplace(tup, from, to) : (*i)); + const ATuple* tup = i->to_tuple(); + copy.push_back(tup ? typedReplace(tup, from, to) : i); } } copy.head->loc = in->loc; diff --git a/src/unify.cpp b/src/unify.cpp index aeb024f..f8ebe1d 100644 --- a/src/unify.cpp +++ b/src/unify.cpp @@ -70,16 +70,16 @@ substitute(const AST* in, const AST* from, const AST* to) return from; List ret; - FOREACHP(ATuple::const_iterator, i, tup->as_tuple()) { - if (**i == *from) { + for (const auto& i : *tup->as_tuple()) { + if (*i == *from) { ret.push_back(to); // FIXME: should be a copy w/ (*i)->loc - } else if (*i != to) { - if (AType::is_expr(*i)) - ret.push_back(substitute(*i, from, to)); + } else if (i != to) { + if (AType::is_expr(i)) + ret.push_back(substitute(i, from, to)); else - ret.push_back(*i); + ret.push_back(i); } else { - ret.push_back(*i); + ret.push_back(i); } } return ret.head; @@ -88,9 +88,9 @@ substitute(const AST* in, const AST* from, const AST* to) void Subst::augment(const Subst& subst) { - for (Subst::const_iterator s = subst.begin(); s != subst.end(); ++s) { - if (!contains(s->first)) { - add(s->first, s->second); + for (auto s : subst) { + if (!contains(s.first)) { + add(s.first, s.second); } } } @@ -100,13 +100,13 @@ Subst Subst::compose(const Subst& delta, const Subst& gamma) { Subst r; - for (Subst::const_iterator g = gamma.begin(); g != gamma.end(); ++g) { - Subst::const_iterator d = delta.find(g->second); - r.add(g->first, ((d != delta.end()) ? d : g)->second); + for (const auto& g : gamma) { + Subst::const_iterator d = delta.find(g.second); + r.add(g.first, ((d != delta.end()) ? *d : g).second); } - for (Subst::const_iterator d = delta.begin(); d != delta.end(); ++d) { - if (gamma.find(d->first) == gamma.end()) - r.add(d->first, d->second); + for (const auto& d : delta) { + if (gamma.find(d.first) == gamma.end()) + r.add(d.first, d.second); } return r; } @@ -115,20 +115,21 @@ Subst::compose(const Subst& delta, const Subst& gamma) Constraints& Constraints::replace(const AST* s, const AST* t) { - for (Constraints::iterator c = begin(); c != end(); ++c) { - if (*c->first == *s) { - c->first = t; // FIXME: should be copy w/ c->first->loc; - } else if (AType::is_expr(c->first)) { - c->first = substitute(c->first, s, t); + for (auto& c : *this) { + if (*c.first == *s) { + c.first = t; // FIXME: should be copy w/ c.first->loc; + } else if (AType::is_expr(c.first)) { + c.first = substitute(c.first, s, t); } - if (*c->second == *s) { - c->second = t; // FIXME: should be copy w/ c->second->loc; - } else if (AType::is_expr(c->second)) { - c->second = substitute(c->second, s, t); + if (*c.second == *s) { + c.second = t; // FIXME: should be copy w/ c.second->loc; + } else if (AType::is_expr(c.second)) { + c.second = substitute(c.second, s, t); } } return *this; } + /// Unify a type constraint set (TAPL 22.4) Subst unify(const Constraints& constraints) -- cgit v1.2.1