aboutsummaryrefslogtreecommitdiffstats
path: root/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'tuplr.hpp')
-rw-r--r--tuplr.hpp92
1 files changed, 46 insertions, 46 deletions
diff --git a/tuplr.hpp b/tuplr.hpp
index 9a636b7..f095689 100644
--- a/tuplr.hpp
+++ b/tuplr.hpp
@@ -116,10 +116,10 @@ private:
/// Literal value
template<typename VT>
-struct ASTLiteral : public AST {
- ASTLiteral(VT v, Cursor c) : AST(c), val(v) {}
+struct ALiteral : public AST {
+ ALiteral(VT v, Cursor c) : AST(c), val(v) {}
bool operator==(const AST& rhs) const {
- const ASTLiteral<VT>* r = dynamic_cast<const ASTLiteral<VT>*>(&rhs);
+ const ALiteral<VT>* r = dynamic_cast<const ALiteral<VT>*>(&rhs);
return (r && (val == r->val));
}
void constrain(TEnv& tenv) const;
@@ -128,22 +128,22 @@ struct ASTLiteral : public AST {
};
/// Symbol, e.g. "a"
-struct ASTSymbol : public AST {
+struct ASymbol : public AST {
bool operator==(const AST& rhs) const { return this == &rhs; }
void lift(CEnv& cenv);
CValue compile(CEnv& cenv);
private:
friend class PEnv;
- ASTSymbol(const string& s, Cursor c) : AST(c), cppstr(s) {}
+ ASymbol(const string& s, Cursor c) : AST(c), cppstr(s) {}
friend ostream& operator<<(ostream&, const AST*);
const string cppstr;
};
/// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)"
-struct ASTTuple : public AST, public vector<AST*> {
- ASTTuple(const vector<AST*>& t=vector<AST*>(), Cursor c=Cursor()) : AST(c), vector<AST*>(t) {}
- ASTTuple(size_t size, Cursor c) : AST(c), vector<AST*>(size) {}
- ASTTuple(Cursor c, AST* ast, ...) : AST(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, AST* ast, ...) : AST(c) {
va_list args; va_start(args, ast);
push_back(ast);
for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*))
@@ -151,7 +151,7 @@ struct ASTTuple : public AST, public vector<AST*> {
va_end(args);
}
bool operator==(const AST& rhs) const {
- const ASTTuple* rt = dynamic_cast<const ASTTuple*>(&rhs);
+ const ATuple* rt = dynamic_cast<const ATuple*>(&rhs);
if (!rt || rt->size() != size()) return false;
const_iterator l = begin();
FOREACH(const_iterator, r, *rt)
@@ -175,11 +175,11 @@ struct ASTTuple : public AST, public vector<AST*> {
};
/// Type Expression, e.g. "Int", "(Fn (Int Int) Float)"
-struct AType : public ASTTuple {
- AType(unsigned i, Cursor c=Cursor()) : ASTTuple(0, c), kind(VAR), id(i) {}
- AType(ASTSymbol* s) : ASTTuple(0, s->loc), kind(PRIM), id(0) { push_back(s); }
- AType(const ASTTuple& t, Cursor c) : ASTTuple(t, c), kind(EXPR), id(0) {}
- AType(Cursor c, AST* ast, ...) : ASTTuple(0, c), kind(EXPR), id(0) {
+struct AType : public ATuple {
+ AType(unsigned i, Cursor c=Cursor()) : ATuple(0, c), kind(VAR), 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);
push_back(ast);
for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*))
@@ -210,7 +210,7 @@ struct AType : public ASTTuple {
switch (kind) {
case VAR: return id == rt->id;
case PRIM: return at(0)->str() == rt->at(0)->str();
- case EXPR: return ASTTuple::operator==(rhs);
+ case EXPR: return ATuple::operator==(rhs);
}
return false; // never reached
}
@@ -229,52 +229,52 @@ struct Funcs : public list< pair<AType*, CFunction> > {
};
/// Closure (first-class function with captured lexical bindings)
-struct ASTClosure : public ASTTuple {
- ASTClosure(Cursor c, ASTSymbol* fn, ASTTuple* p, AST* b, const string& n="")
- : ASTTuple(c, fn, p, b, NULL), name(n) {}
+struct AClosure : public ATuple {
+ AClosure(Cursor c, ASymbol* fn, ATuple* p, AST* b, const string& n="")
+ : ATuple(c, fn, p, b, NULL), name(n) {}
bool operator==(const AST& rhs) const { return this == &rhs; }
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
CValue compile(CEnv& cenv);
- ASTTuple* prot() const { return dynamic_cast<ASTTuple*>(at(1)); }
+ ATuple* prot() const { return dynamic_cast<ATuple*>(at(1)); }
private:
Funcs funcs;
string name;
};
/// Function call/application, e.g. "(func arg1 arg2)"
-struct ASTCall : public ASTTuple {
- ASTCall(const SExp& e, const ASTTuple& t) : ASTTuple(t, e.loc) {}
+struct ACall : public ATuple {
+ ACall(const SExp& e, const ATuple& t) : ATuple(t, e.loc) {}
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
CValue compile(CEnv& cenv);
};
/// Definition special form, e.g. "(def x 2)"
-struct ASTDefinition : public ASTCall {
- ASTDefinition(const SExp& e, const ASTTuple& t) : ASTCall(e, t) {}
+struct ADefinition : public ACall {
+ ADefinition(const SExp& e, const ATuple& t) : ACall(e, t) {}
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
CValue compile(CEnv& cenv);
};
/// Conditional special form, e.g. "(if cond thenexp elseexp)"
-struct ASTIf : public ASTCall {
- ASTIf(const SExp& e, const ASTTuple& t) : ASTCall(e, t) {}
+struct AIf : public ACall {
+ AIf(const SExp& e, const ATuple& t) : ACall(e, t) {}
void constrain(TEnv& tenv) const;
CValue compile(CEnv& cenv);
};
/// Primitive (builtin arithmetic function), e.g. "(+ 2 3)"
-struct ASTPrimitive : public ASTCall {
- ASTPrimitive(const SExp& e, const ASTTuple& t) : ASTCall(e, t) {}
+struct APrimitive : public ACall {
+ APrimitive(const SExp& e, const ATuple& t) : ACall(e, t) {}
void constrain(TEnv& tenv) const;
CValue compile(CEnv& cenv);
};
/// Cons special form, e.g. "(cons 1 2)"
-struct ASTConsCall : public ASTCall {
- ASTConsCall(const SExp& e, const ASTTuple& t) : ASTCall(e, t) {}
+struct AConsCall : public ACall {
+ AConsCall(const SExp& e, const ATuple& t) : ACall(e, t) {}
AType* functionType(CEnv& cenv);
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
@@ -283,15 +283,15 @@ struct ASTConsCall : public ASTCall {
};
/// Car special form, e.g. "(car p)"
-struct ASTCarCall : public ASTCall {
- ASTCarCall(const SExp& e, const ASTTuple& t) : ASTCall(e, t) {}
+struct ACarCall : public ACall {
+ ACarCall(const SExp& e, const ATuple& t) : ACall(e, t) {}
void constrain(TEnv& tenv) const;
CValue compile(CEnv& cenv);
};
/// Cdr special form, e.g. "(cdr p)"
-struct ASTCdrCall : public ASTCall {
- ASTCdrCall(const SExp& e, const ASTTuple& t) : ASTCall(e, t) {}
+struct ACdrCall : public ACall {
+ ACdrCall(const SExp& e, const ATuple& t) : ACall(e, t) {}
void constrain(TEnv& tenv) const;
CValue compile(CEnv& cenv);
};
@@ -302,7 +302,7 @@ struct ASTCdrCall : public ASTCall {
***************************************************************************/
/// Parse Time Environment (symbol table)
-struct PEnv : private map<const string, ASTSymbol*> {
+struct PEnv : private map<const string, ASymbol*> {
typedef AST* (*PF)(PEnv&, const SExp&, void*); // Parse Function
struct Handler { Handler(PF f, void* a=0) : func(f), arg(a) {} PF func; void* arg; };
map<const string, Handler> aHandlers; ///< Atom parse functions
@@ -315,22 +315,22 @@ struct PEnv : private map<const string, ASTSymbol*> {
map<string, Handler>::const_iterator i = handlers.find(s);
return (i != handlers.end()) ? &i->second : NULL;
}
- ASTSymbol* sym(const string& s, Cursor c=Cursor()) {
+ ASymbol* sym(const string& s, Cursor c=Cursor()) {
const const_iterator i = find(s);
return ((i != end())
? i->second
- : insert(make_pair(s, new ASTSymbol(s, c))).first->second);
+ : insert(make_pair(s, new ASymbol(s, c))).first->second);
}
};
/// The fundamental parser method
static AST* parseExpression(PEnv& penv, const SExp& exp);
-static ASTTuple
+static ATuple
pmap(PEnv& penv, const SExp& e)
{
assert(e.type == SExp::LIST);
- ASTTuple ret(e.list.size(), e.loc);
+ ATuple ret(e.list.size(), e.loc);
size_t n = 0;
FOREACH(SExp::List::const_iterator, i, e.list)
ret[n++] = parseExpression(penv, *i);
@@ -347,12 +347,12 @@ parseExpression(PEnv& penv, const SExp& exp)
if (handler) // Dispatch to list parse function
return handler->func(penv, exp, handler->arg);
}
- return new ASTCall(exp, pmap(penv, exp)); // Parse as regular call
+ return new ACall(exp, pmap(penv, exp)); // Parse as regular call
} else if (isdigit(exp.atom[0])) {
if (exp.atom.find('.') == string::npos)
- return new ASTLiteral<int32_t>(strtol(exp.atom.c_str(), NULL, 10), exp.loc);
+ return new ALiteral<int32_t>(strtol(exp.atom.c_str(), NULL, 10), exp.loc);
else
- return new ASTLiteral<float>(strtod(exp.atom.c_str(), NULL), exp.loc);
+ return new ALiteral<float>(strtod(exp.atom.c_str(), NULL), exp.loc);
} else {
const PEnv::Handler* handler = penv.handler(false, exp.atom);
if (handler) // Dispatch to atom parse function
@@ -372,15 +372,15 @@ template<typename T>
inline AST*
parseLiteral(PEnv& penv, const SExp& exp, void* arg)
{
- return new ASTLiteral<T>(*reinterpret_cast<T*>(arg), exp.loc);
+ return new ALiteral<T>(*reinterpret_cast<T*>(arg), exp.loc);
}
inline AST*
parseFn(PEnv& penv, const SExp& exp, void* arg)
{
SExp::List::const_iterator a = exp.list.begin(); ++a;
- return new ASTClosure(exp.loc, penv.sym("fn"),
- new ASTTuple(pmap(penv, *a++)),
+ return new AClosure(exp.loc, penv.sym("fn"),
+ new ATuple(pmap(penv, *a++)),
parseExpression(penv, *a++));
}
@@ -460,7 +460,7 @@ struct CEnv {
CEnv(PEnv& p, TEnv& t, CEngine& e, ostream& os=std::cout, ostream& es=std::cerr);
~CEnv();
- typedef Env<const ASTSymbol*, AST*> Code;
+ typedef Env<const ASymbol*, AST*> Code;
typedef Env<const AST*, CValue> Vals;
string gensym(const char* s="_") { return (format("%s%d") % s % symID++).str(); }