aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2009-06-25 20:34:39 +0000
committerDavid Robillard <d@drobilla.net>2009-06-25 20:34:39 +0000
commit755c9629ec34ca5536a49d88821b8b11460756ce (patch)
treeabe329d31c2111376e13141e0ec90a55e2e2db13
parentc465702dbeb1ea63a356146403eee668fb59371d (diff)
downloadresp-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
-rw-r--r--cps.cpp26
-rw-r--r--llvm.cpp2
-rw-r--r--tuplr.cpp2
-rw-r--r--tuplr.hpp40
-rw-r--r--typing.cpp12
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<ACall>(loc, cont, this, 0);
}
AST*
ATuple::cps(TEnv& tenv, AST* cont)
{
- ATuple* copy = new ATuple(loc, NULL);
+ ATuple* copy = tup<ATuple>(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<AFn>(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<ACall>(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<ADef>(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<AFn>(loc, tenv.penv.sym("if-fn"),
+ new ATuple(at(1)->loc, cont, 0), 0);
+ ACall* condCall = tup<ACall>(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<AFn*>();
- 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<AFn>(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<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) {
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<AType>(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<AType>(loc, NULL);
for (size_t i = 0; i < prot()->size(); ++i) {
AType* tvar = tenv.fresh(prot()->at(i)->to<ASymbol*>());
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<AType>(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<ATuple*>()->at(i-1)->as<AType*>());
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<AType>(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<AType>(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<AType>(at(0)->loc, tenv.penv.sym("Fn"), argsT, retT, 0));
c.constrain(tenv, this, retT);
}