aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm.cpp10
-rw-r--r--tuplr.cpp12
-rw-r--r--tuplr.hpp44
-rw-r--r--typing.cpp6
4 files changed, 33 insertions, 39 deletions
diff --git a/llvm.cpp b/llvm.cpp
index 7cfb3b0..871fa18 100644
--- a/llvm.cpp
+++ b/llvm.cpp
@@ -240,7 +240,7 @@ AClosure::liftCall(CEnv& cenv, const vector<AType*>& argsT)
{
TEnv::GenericTypes::const_iterator gt = cenv.tenv.genericTypes.find(this);
assert(gt != cenv.tenv.genericTypes.end());
- AType* genericType = new AType(*gt->second, gt->second->loc);
+ AType* genericType = new AType(*gt->second);
// Find type and build substitution
assert(argsT.size() == prot()->size());
@@ -589,7 +589,7 @@ eval(CEnv& cenv, const string& name, istream& is)
try {
while (true) {
SExp exp = readExpression(cursor, is);
- if (exp.type == SExp::LIST && exp.list.empty())
+ if (exp.type == SExp::LIST && exp.empty())
break;
result = cenv.penv.parse(exp); // Parse input
@@ -607,7 +607,7 @@ eval(CEnv& cenv, const string& name, istream& is)
if (!ctype) throw Error("body has non-compilable type", cursor);
// Create function for top-level of program
- Function* f = compileFunction(cenv, "main", ctype, ATuple());
+ Function* f = compileFunction(cenv, "main", ctype, ATuple(cursor));
// Compile all expressions into it
Value* val = NULL;
@@ -637,7 +637,7 @@ repl(CEnv& cenv)
try {
SExp exp = readExpression(cursor, std::cin);
- if (exp.type == SExp::LIST && exp.list.empty())
+ if (exp.type == SExp::LIST && exp.empty())
break;
AST* body = cenv.penv.parse(exp); // Parse input
@@ -660,7 +660,7 @@ repl(CEnv& cenv)
if (lltype(bodyT)) {
// Create anonymous function to insert code into
- Function* f = compileFunction(cenv, cenv.gensym("_repl"), lltype(bodyT), ATuple());
+ Function* f = compileFunction(cenv, cenv.gensym("_repl"), lltype(bodyT), ATuple(cursor));
try {
Value* retVal = LLVal(cenv.compile(body));
llengine(cenv)->builder.CreateRet(retVal); // Finish function
diff --git a/tuplr.cpp b/tuplr.cpp
index 1a059a5..5124528 100644
--- a/tuplr.cpp
+++ b/tuplr.cpp
@@ -46,7 +46,7 @@ readChar(Cursor& cur, istream& in)
SExp
readExpression(Cursor& cur, istream& in)
{
-#define PUSH(s, t) { if (t != "") { s.top().list.push_back(SExp(loc, t)); t = ""; } }
+#define PUSH(s, t) { if (t != "") { s.top().push_back(SExp(loc, t)); t = ""; } }
#define YIELD(s, t) { if (s.empty()) { return SExp(loc, t); } else PUSH(s, t) }
stack<SExp> stk;
string tok;
@@ -80,7 +80,7 @@ readExpression(Cursor& cur, istream& in)
PUSH(stk, tok);
SExp l = stk.top();
stk.pop();
- stk.top().list.push_back(l);
+ stk.top().push_back(l);
}
break;
case '#':
@@ -123,13 +123,13 @@ parseLiteral(PEnv& penv, const SExp& exp, void* arg)
inline AST*
parseFn(PEnv& penv, const SExp& exp, void* arg)
{
- if (exp.list.size() < 2)
+ if (exp.size() < 2)
throw Error("Missing function parameters and body", exp.loc);
- else if (exp.list.size() < 3)
+ else if (exp.size() < 3)
throw Error("Missing function body", exp.loc);
- SExp::List::const_iterator a = exp.list.begin(); ++a;
+ SExp::const_iterator a = exp.begin(); ++a;
AClosure* ret = new AClosure(exp.loc, penv.sym("fn"), new ATuple(penv.parseTuple(*a++)));
- while (a != exp.list.end())
+ while (a != exp.end())
ret->push_back(penv.parse(*a++));
return ret;
}
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;
diff --git a/typing.cpp b/typing.cpp
index 086db79..f926dde 100644
--- a/typing.cpp
+++ b/typing.cpp
@@ -44,7 +44,7 @@ ASymbol::constrain(TEnv& tenv, Constraints& c) const
void
ATuple::constrain(TEnv& tenv, Constraints& c) const
{
- AType* t = new AType(ATuple(), loc);
+ AType* t = new AType(loc, NULL);
FOREACH(const_iterator, p, *this) {
(*p)->constrain(tenv, c);
t->push_back(tenv.var(*p));
@@ -93,7 +93,7 @@ AClosure::constrain(TEnv& tenv, Constraints& c) const
Constraints cp;
cp.push_back(Constraint(tenv.var(this), tenv.var(), loc));
- AType* protT = new AType(ATuple(), loc);
+ AType* protT = new AType(loc, NULL);
for (size_t i = 0; i < prot()->size(); ++i) {
AType* tvar = tenv.fresh(dynamic_cast<ASymbol*>(prot()->at(i)));
protT->push_back(tvar);
@@ -141,7 +141,7 @@ ACall::constrain(TEnv& tenv, Constraints& c) const
return;
}
}
- AType* argsT = new AType(ATuple(), loc);
+ AType* argsT = new AType(loc, NULL);
for (size_t i = 1; i < size(); ++i)
argsT->push_back(tenv.var(at(i)));
AType* retT = tenv.var();