aboutsummaryrefslogtreecommitdiffstats
path: root/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'tuplr.hpp')
-rw-r--r--tuplr.hpp44
1 files changed, 19 insertions, 25 deletions
diff --git a/tuplr.hpp b/tuplr.hpp
index 89966da..21ed121 100644
--- a/tuplr.hpp
+++ b/tuplr.hpp
@@ -55,16 +55,14 @@ struct Error {
Cursor loc;
};
-/// Expression ::= Atom | (Expression*)
+/// Expression ::= Atom | (SubExp*)
template<typename Atom>
-struct Exp {
+struct Exp : public std::vector< Exp<Atom> > {
Exp(Cursor c) : type(LIST), loc(c) {}
Exp(Cursor c, const Atom& a) : type(ATOM), loc(c), atom(a) {}
- typedef std::vector< Exp<Atom> > List;
enum { ATOM, LIST } type;
- Cursor loc;
- Atom atom;
- List list;
+ Cursor loc;
+ Atom atom;
};
/// Lexical Address
@@ -192,11 +190,9 @@ private:
/// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)"
struct ATuple : public AST, public vector<AST*> {
- ATuple(const vector<AST*>& t=vector<AST*>(), Cursor c=Cursor()) : AST(c), vector<AST*>(t) {}
- ATuple(size_t size, Cursor c) : AST(c), vector<AST*>(size) {}
+ ATuple(Cursor c, const vector<AST*>& v=vector<AST*>()) : AST(c), vector<AST*>(v) {}
ATuple(Cursor c, AST* ast, ...) : AST(c) {
- if (!ast)
- return;
+ 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*))
@@ -229,18 +225,17 @@ struct ATuple : public AST, public vector<AST*> {
/// Type Expression, e.g. "Int", "(Fn (Int Int) Float)"
struct AType : public ATuple {
- AType(unsigned i, LAddr a, Cursor c=Cursor()) : ATuple(0, c), kind(VAR), addr(a), id(i) {}
- AType(ASymbol* s) : ATuple(0, s->loc), kind(PRIM), id(0) { push_back(s); }
- AType(const ATuple& t, Cursor c) : ATuple(t, c), kind(EXPR), id(0) {}
- AType(Cursor c, AST* ast, ...) : ATuple(0, c), kind(EXPR), id(0) {
- va_list args; va_start(args, ast);
+ AType(ASymbol* s) : ATuple(s->loc), kind(PRIM), id(0) { push_back(s); }
+ 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);
push_back(ast);
for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*))
push_back(a);
va_end(args);
}
- AType(const AType& copy) : ATuple(0, copy.loc), kind(copy.kind), addr(copy.addr), id(copy.id) {
+ AType(const AType& copy) : ATuple(copy.loc), kind(copy.kind), id(copy.id) {
for (AType::const_iterator i = copy.begin(); i != copy.end(); ++i) {
AType* typ = dynamic_cast<AType*>(*i);
if (typ) {
@@ -282,7 +277,6 @@ struct AType : public ATuple {
return false; // never reached
}
enum { VAR, PRIM, EXPR } kind;
- LAddr addr;
unsigned id;
};
@@ -314,7 +308,7 @@ private:
/// Function call/application, e.g. "(func arg1 arg2)"
struct ACall : public ATuple {
- ACall(const SExp& e, const ATuple& t) : ATuple(t, e.loc) {}
+ ACall(const SExp& e, const ATuple& t) : ATuple(e.loc, t) {}
void constrain(TEnv& tenv, Constraints& c) const;
void lift(CEnv& cenv);
CValue compile(CEnv& cenv);
@@ -393,17 +387,17 @@ struct PEnv : private map<const string, ASymbol*> {
: insert(make_pair(s, new ASymbol(s, c))).first->second);
}
ATuple parseTuple(const SExp& e) {
- ATuple ret(e.list.size(), e.loc);
+ ATuple ret(e.loc, vector<AST*>(e.size()));
size_t n = 0;
- FOREACH(SExp::List::const_iterator, i, e.list)
+ FOREACH(SExp::const_iterator, i, e)
ret[n++] = parse(*i);
return ret;
}
AST* parse(const SExp& exp) {
if (exp.type == SExp::LIST) {
- if (exp.list.empty()) throw Error("call to empty list", exp.loc);
- if (exp.list.front().type == SExp::ATOM) {
- const PEnv::Handler* h = handler(true, exp.list.front().atom);
+ if (exp.empty()) throw Error("call to empty list", exp.loc);
+ if (exp.front().type == SExp::ATOM) {
+ const PEnv::Handler* h = handler(true, exp.front().atom);
if (h)
return h->func(*this, exp, h->arg);
}
@@ -470,7 +464,7 @@ struct TEnv : public Env< const ASymbol*, pair<AST*, AType*> > {
TEnv(PEnv& p) : penv(p), varID(1) {}
AType* fresh(const ASymbol* sym) {
assert(sym);
- AType* ret = new AType(varID++, LAddr(), sym->loc);
+ AType* ret = new AType(sym->loc, varID++, LAddr());
def(sym, make_pair((AST*)NULL, ret));
return ret;
}
@@ -483,7 +477,7 @@ struct TEnv : public Env< const ASymbol*, pair<AST*, AType*> > {
if (v != vars.end())
return v->second;
- AType* ret = new AType(varID++, LAddr(), ast ? ast->loc : Cursor());
+ AType* ret = new AType(ast ? ast->loc : Cursor(), varID++, LAddr());
if (ast)
vars[ast] = ret;