aboutsummaryrefslogtreecommitdiffstats
path: root/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'tuplr.hpp')
-rw-r--r--tuplr.hpp40
1 files changed, 19 insertions, 21 deletions
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) {