From 755c9629ec34ca5536a49d88821b8b11460756ce Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 25 Jun 2009 20:34:39 +0000 Subject: Sanify AST constructors. git-svn-id: http://svn.drobilla.net/resp/tuplr@149 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- cps.cpp | 26 ++++++++------------------ llvm.cpp | 2 +- tuplr.cpp | 2 +- tuplr.hpp | 40 +++++++++++++++++++--------------------- typing.cpp | 12 ++++++------ 5 files changed, 35 insertions(+), 47 deletions(-) diff --git a/cps.cpp b/cps.cpp index 60f500d..c1bddf2 100644 --- a/cps.cpp +++ b/cps.cpp @@ -26,13 +26,13 @@ AST* AST::cps(TEnv& tenv, AST* cont) { - return new ACall(loc, cont, this, 0); + return tup(loc, cont, this, 0); } AST* ATuple::cps(TEnv& tenv, AST* cont) { - ATuple* copy = new ATuple(loc, NULL); + ATuple* copy = tup(loc, NULL); FOREACH(const_iterator, p, *this) copy->push_back((*p)->cps(tenv, cont)); return copy; @@ -42,45 +42,35 @@ ATuple::cps(TEnv& tenv, AST* cont) AST* AFn::cps(TEnv& tenv, AST* cont) { - AFn* copy = new AFn(loc, tenv.penv.sym("fn"), prot()); + AFn* copy = tup(loc, tenv.penv.sym("fn"), prot(), 0); const_iterator p = begin(); ++(++p); for (; p != end(); ++p) copy->push_back((*p)->cps(tenv, cont)); return copy; - return ATuple::cps(tenv, cont); } /** (cps (f a b ...)) => (a (fn (x) (b (fn (y) ... (cont (f x y ...)) */ AST* ACall::cps(TEnv& tenv, AST* cont) { - return new ACall(loc, cont, this, 0); + return tup(loc, cont, this, 0); } /** (cps (def x y)) => (y (fn (x) (cont))) */ AST* ADef::cps(TEnv& tenv, AST* cont) { - ADef* copy = new ADef(loc, - ATuple(loc, tenv.penv.sym("def"), sym(), at(2)->cps(tenv, cont), 0)); - return copy; - /* - ASymbol* fnSym = tenv.penv.sym("deff"); - AFn* defFn = new AFn(loc, tenv.penv.sym("def"), - new ATuple(at(1)->loc, sym(), new ACall(loc, cont, this))); - ACall* defCall = new ACall(loc, at(2)->cps(tenv, defFn), 0); - */ + return tup(loc, tenv.penv.sym("def"), sym(), at(2)->cps(tenv, cont), 0); } /** (cps (if c t ... e)) => */ AST* AIf::cps(TEnv& tenv, AST* cont) { - AFn* contFn = new AFn(loc, tenv.penv.sym("if0"), - new ATuple(at(1)->loc, cont, 0)); - //ACall* contCall = new ACall(loc, cont, this, 0); - ACall* condCall = new ACall(loc, contFn, 0); + AFn* contFn = tup(loc, tenv.penv.sym("if-fn"), + new ATuple(at(1)->loc, cont, 0), 0); + ACall* condCall = tup(loc, contFn, 0); return condCall; } diff --git a/llvm.cpp b/llvm.cpp index 3688845..4fa9ca9 100644 --- a/llvm.cpp +++ b/llvm.cpp @@ -345,7 +345,7 @@ void ACall::lift(CEnv& cenv) { AFn* c = cenv.tenv.resolve(at(0))->to(); - AType argsT(loc, NULL); + AType argsT(loc, NULL); // Lift arguments for (size_t i = 1; i < size(); ++i) { diff --git a/tuplr.cpp b/tuplr.cpp index 788cd0e..5b1a357 100644 --- a/tuplr.cpp +++ b/tuplr.cpp @@ -176,7 +176,7 @@ parseFn(PEnv& penv, const SExp& exp, void* arg) else if (exp.size() < 3) throw Error(exp.loc, "Missing function body"); SExp::const_iterator a = exp.begin(); ++a; - AFn* ret = new AFn(exp.loc, penv.sym("fn"), new ATuple(penv.parseTuple(*a++))); + AFn* ret = tup(exp.loc, penv.sym("fn"), new ATuple(penv.parseTuple(*a++)), 0); while (a != exp.end()) ret->push_back(penv.parse(*a++)); return ret; diff --git a/tuplr.hpp b/tuplr.hpp index fb16fe7..b54f72a 100644 --- a/tuplr.hpp +++ b/tuplr.hpp @@ -210,6 +210,16 @@ struct AST : public Object { Cursor loc; }; +template +static T* tup(Cursor c, AST* ast, ...) +{ + va_list args; + va_start(args, ast); + T* ret = new T(c, ast, args); + va_end(args); + return ret; +} + /// Literal value template struct ALiteral : public AST { @@ -238,13 +248,11 @@ private: /// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)" struct ATuple : public AST, public vector { ATuple(Cursor c, const vector& v=vector()) : AST(c), vector(v) {} - ATuple(Cursor c, AST* ast, ...) : AST(c) { + ATuple(Cursor c, AST* ast, va_list args) : AST(c) { if (!ast) return; - va_list args; va_start(args, ast); push_back(ast); for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*)) push_back(a); - va_end(args); } bool operator==(const AST& rhs) const { const ATuple* rt = rhs.to(); @@ -275,13 +283,14 @@ struct AType : public ATuple { AType(Cursor c, unsigned i, LAddr a) : ATuple(c), kind(VAR), id(i) {} AType(Cursor c, AST* ast, ...) : ATuple(c), kind(EXPR), id(0) { if (!ast) return; - va_list args; va_start(args, ast); - if (ast) - push_back(ast); + va_list args; + va_start(args, ast); + push_back(ast); for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*)) push_back(a); va_end(args); } + AType(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args), kind(EXPR), id(0) {} CValue compile(CEnv& cenv) { return NULL; } bool var() const { return kind == VAR; } bool concrete() const { @@ -328,7 +337,7 @@ struct Subst : public map { AType* in = ast->to(); if (!in) return ast; if (in->kind == AType::EXPR) { - AType* out = new AType(in->loc, NULL); + AType* out = tup(in->loc, NULL); for (size_t i = 0; i < in->size(); ++i) out->push_back(apply(in->at(i))); return out; @@ -345,8 +354,7 @@ struct Subst : public map { /// Fn (first-class function with captured lexical bindings) struct AFn : public ATuple { - AFn(Cursor c, ASymbol* fn, ATuple* p, const string& n="") - : ATuple(c, fn, p, NULL), name(n) {} + AFn(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args) {} bool operator==(const AST& rhs) const { return this == &rhs; } void constrain(TEnv& tenv, Constraints& c) const; AST* cps(TEnv& tenv, AST* cont); @@ -371,17 +379,7 @@ struct AFn : public ATuple { /// Function call/application, e.g. "(func arg1 arg2)" struct ACall : public ATuple { ACall(const SExp& e, const ATuple& t) : ATuple(e.loc, t) {} - ACall(Cursor c, const ATuple& code) : ATuple(c, code) {} - ACall(Cursor c, AST* fn, AST* arg, ...) : ATuple(c) { - push_back(fn); - if (!arg) return; - va_list args; va_start(args, arg); - if (arg) - push_back(arg); - for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*)) - push_back(a); - va_end(args); - } + ACall(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args) {} void constrain(TEnv& tenv, Constraints& c) const; AST* cps(TEnv& tenv, AST* cont); void lift(CEnv& cenv); @@ -391,7 +389,7 @@ struct ACall : public ATuple { /// Definition special form, e.g. "(def x 2)" struct ADef : public ACall { ADef(const SExp& e, const ATuple& t) : ACall(e, t) {} - ADef(Cursor c, const ATuple& code) : ACall(c, code) {} + ADef(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} ASymbol* sym() const { ASymbol* sym = at(1)->to(); if (!sym) { diff --git a/typing.cpp b/typing.cpp index c9eeb6d..4781b2e 100644 --- a/typing.cpp +++ b/typing.cpp @@ -42,7 +42,7 @@ ASymbol::constrain(TEnv& tenv, Constraints& c) const void ATuple::constrain(TEnv& tenv, Constraints& c) const { - AType* t = new AType(loc, NULL); + AType* t = tup(loc, NULL); FOREACH(const_iterator, p, *this) { (*p)->constrain(tenv, c); t->push_back(tenv.var(*p)); @@ -91,7 +91,7 @@ AFn::constrain(TEnv& tenv, Constraints& c) const Constraints cp; cp.push_back(Constraint(tenv.var(this), tenv.var(), loc)); - AType* protT = new AType(loc, NULL); + AType* protT = tup(loc, NULL); for (size_t i = 0; i < prot()->size(); ++i) { AType* tvar = tenv.fresh(prot()->at(i)->to()); protT->push_back(tvar); @@ -106,7 +106,7 @@ AFn::constrain(TEnv& tenv, Constraints& c) const AType* bodyT = tenv.var(at(e-1)); Subst tsubst = TEnv::unify(cp); - genericType = new AType(loc, tenv.penv.sym("Fn"), + genericType = tup(loc, tenv.penv.sym("Fn"), tsubst.apply(protT), tsubst.apply(bodyT), 0); tenv.genericTypes.insert(make_pair(this, genericType)); Object::pool.addRoot(genericType); @@ -135,16 +135,16 @@ ACall::constrain(TEnv& tenv, Constraints& c) const for (size_t i = 1; i < size(); ++i) c.constrain(tenv, at(i), gt->second->at(1)->as()->at(i-1)->as()); AType* retT = tenv.var(this); - c.constrain(tenv, at(0), new AType(at(0)->loc, tenv.penv.sym("Fn"), tenv.var(), retT, 0)); + c.constrain(tenv, at(0), tup(at(0)->loc, tenv.penv.sym("Fn"), tenv.var(), retT, 0)); c.constrain(tenv, this, retT); return; } } - AType* argsT = new AType(loc, NULL); + AType* argsT = tup(loc, 0); for (size_t i = 1; i < size(); ++i) argsT->push_back(tenv.var(at(i))); AType* retT = tenv.var(); - c.constrain(tenv, at(0), new AType(at(0)->loc, tenv.penv.sym("Fn"), argsT, retT, 0)); + c.constrain(tenv, at(0), tup(at(0)->loc, tenv.penv.sym("Fn"), argsT, retT, 0)); c.constrain(tenv, this, retT); } -- cgit v1.2.1