diff options
author | David Robillard <d@drobilla.net> | 2009-06-25 20:34:39 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2009-06-25 20:34:39 +0000 |
commit | 755c9629ec34ca5536a49d88821b8b11460756ce (patch) | |
tree | abe329d31c2111376e13141e0ec90a55e2e2db13 /tuplr.hpp | |
parent | c465702dbeb1ea63a356146403eee668fb59371d (diff) | |
download | resp-755c9629ec34ca5536a49d88821b8b11460756ce.tar.gz resp-755c9629ec34ca5536a49d88821b8b11460756ce.tar.bz2 resp-755c9629ec34ca5536a49d88821b8b11460756ce.zip |
Sanify AST constructors.
git-svn-id: http://svn.drobilla.net/resp/tuplr@149 ad02d1e2-f140-0410-9f75-f8b11f17cedd
Diffstat (limited to 'tuplr.hpp')
-rw-r--r-- | tuplr.hpp | 40 |
1 files changed, 19 insertions, 21 deletions
@@ -210,6 +210,16 @@ struct AST : public Object { Cursor loc; }; +template<typename T> +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<typename VT> 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<AST*> { ATuple(Cursor c, const vector<AST*>& v=vector<AST*>()) : AST(c), vector<AST*>(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<const ATuple*>(); @@ -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<const AType*,AType*,typeLessThan> { AType* in = ast->to<AType*>(); if (!in) return ast; if (in->kind == AType::EXPR) { - AType* out = new AType(in->loc, NULL); + AType* out = tup<AType>(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<const AType*,AType*,typeLessThan> { /// 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<ASymbol*>(); if (!sym) { |