aboutsummaryrefslogtreecommitdiffstats
path: root/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'tuplr.hpp')
-rw-r--r--tuplr.hpp32
1 files changed, 10 insertions, 22 deletions
diff --git a/tuplr.hpp b/tuplr.hpp
index 29b8463..a931f8c 100644
--- a/tuplr.hpp
+++ b/tuplr.hpp
@@ -22,6 +22,7 @@
#include <iostream>
#include <list>
#include <map>
+#include <sstream>
#include <string>
#include <vector>
#include <boost/format.hpp>
@@ -95,19 +96,22 @@ struct CEngine; ///< Backend data (opaque)
struct TEnv; ///< Type-Time Environment
struct CEnv; ///< Compile-Time Environment
+struct AST;
+extern ostream& operator<<(ostream& out, const AST* ast);
+
/// Base class for all AST nodes
struct AST {
AST(Cursor c=Cursor()) : loc(c) {}
virtual ~AST() {}
- virtual string str() const = 0;
virtual bool operator==(const AST& o) const = 0;
virtual bool contains(const AST* child) const { return false; }
virtual void constrain(TEnv& tenv) const {}
virtual void lift(CEnv& cenv) {}
+ string str() const { ostringstream ss; ss << this; return ss.str(); }
Cursor loc;
private:
friend class CEnv;
- virtual CValue compile(CEnv& cenv) = 0;
+ virtual CValue compile(CEnv& cenv) = 0;
};
/// Literal value
@@ -118,7 +122,6 @@ struct ASTLiteral : public AST {
const ASTLiteral<VT>* r = dynamic_cast<const ASTLiteral<VT>*>(&rhs);
return (r && (val == r->val));
}
- string str() const { return (format("%1%") % val).str(); }
void constrain(TEnv& tenv) const;
CValue compile(CEnv& cenv);
const VT val;
@@ -127,12 +130,12 @@ struct ASTLiteral : public AST {
/// Symbol, e.g. "a"
struct ASTSymbol : public AST {
bool operator==(const AST& rhs) const { return this == &rhs; }
- string str() const { return cppstr; }
void lift(CEnv& cenv);
CValue compile(CEnv& cenv);
private:
friend class PEnv;
ASTSymbol(const string& s, Cursor c) : AST(c), cppstr(s) {}
+ friend ostream& operator<<(ostream&, const AST*);
const string cppstr;
};
@@ -147,12 +150,6 @@ struct ASTTuple : public AST, public vector<AST*> {
push_back(a);
va_end(args);
}
- string str() const {
- string ret = "(";
- for (size_t i = 0; i != size(); ++i)
- ret += (at(i) ? at(i)->str() : "NULL") + ((i != size() - 1) ? " " : "");
- return ret + ")";
- }
bool operator==(const AST& rhs) const {
const ASTTuple* rt = dynamic_cast<const ASTTuple*>(&rhs);
if (!rt || rt->size() != size()) return false;
@@ -189,14 +186,6 @@ struct AType : public ASTTuple {
push_back(a);
va_end(args);
}
- string str() const {
- switch (kind) {
- case VAR: return (format("?%1%") % id).str();
- case PRIM: return at(0)->str();
- case EXPR: return ASTTuple::str();
- }
- return ""; // never reached
- }
void constrain(TEnv& tenv) const {}
CValue compile(CEnv& cenv) { return NULL; }
bool var() const { return kind == VAR; }
@@ -241,10 +230,9 @@ struct Funcs : public list< pair<AType*, CFunction> > {
/// Closure (first-class function with captured lexical bindings)
struct ASTClosure : public ASTTuple {
- ASTClosure(Cursor c, ASTTuple* p, AST* b, const string& n="")
- : ASTTuple(c, 0, p, b, NULL), name(n) {}
+ ASTClosure(Cursor c, ASTSymbol* fn, ASTTuple* p, AST* b, const string& n="")
+ : ASTTuple(c, fn, p, b, NULL), name(n) {}
bool operator==(const AST& rhs) const { return this == &rhs; }
- string str() const { return (format("%1%") % this).str(); }
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
CValue compile(CEnv& cenv);
@@ -392,7 +380,7 @@ inline AST*
parseFn(PEnv& penv, const SExp& exp, void* arg)
{
SExp::List::const_iterator a = exp.list.begin(); ++a;
- return new ASTClosure(exp.loc,
+ return new ASTClosure(exp.loc, penv.sym("fn"),
new ASTTuple(pmap(penv, *a++)),
parseExpression(penv, *a++));
}