aboutsummaryrefslogtreecommitdiffstats
path: root/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'tuplr.hpp')
-rw-r--r--tuplr.hpp77
1 files changed, 42 insertions, 35 deletions
diff --git a/tuplr.hpp b/tuplr.hpp
index 1d3eb0d..37d8ad8 100644
--- a/tuplr.hpp
+++ b/tuplr.hpp
@@ -25,7 +25,12 @@
#include <string>
#include <vector>
#include <boost/format.hpp>
-#include "tuplr_llvm.hpp"
+
+// Actual types are backend specific
+typedef void* CValue;
+typedef const void* CType;
+typedef void* CFunction;
+struct CEngine;
#define FOREACH(IT, i, c) for (IT i = (c).begin(); i != (c).end(); ++i)
@@ -50,6 +55,8 @@ struct Error {
Cursor loc;
};
+struct CArg { CArg(int o=0, int a=0) : op(o), arg(a) {} int op; int arg; };
+
template<typename Atom>
struct Exp { // ::= Atom | (Exp*)
Exp(Cursor c) : type(LIST), loc(c) {}
@@ -86,7 +93,7 @@ struct AST {
virtual bool contains(AST* child) const { return false; }
virtual void constrain(TEnv& tenv) const {}
virtual void lift(CEnv& cenv) {}
- virtual CValue* compile(CEnv& cenv) = 0;
+ virtual CValue compile(CEnv& cenv) = 0;
};
/// Literal value
@@ -99,7 +106,7 @@ struct ASTLiteral : public AST {
}
string str() const { return (format("%1%") % val).str(); }
void constrain(TEnv& tenv) const;
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
const VT val;
};
@@ -108,7 +115,7 @@ struct ASTSymbol : public AST {
ASTSymbol(const string& s, Cursor c=Cursor()) : loc(c), cppstr(s) {}
bool operator==(const AST& rhs) const { return this == &rhs; }
string str() const { return cppstr; }
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
private:
Cursor loc;
const string cppstr;
@@ -151,14 +158,14 @@ struct ASTTuple : public AST, public vector<AST*> {
}
bool contains(AST* child) const;
void constrain(TEnv& tenv) const;
- CValue* compile(CEnv& cenv) { throw Error("tuple compiled"); }
+ CValue compile(CEnv& cenv) { throw Error("tuple compiled"); }
};
/// Type Expression, e.g. "Int", "(Fn (Int Int) Float)"
struct AType : public ASTTuple {
AType(const ASTTuple& t) : ASTTuple(t), kind(EXPR), ctype(0) {}
AType(unsigned i) : kind(VAR), id(i), ctype(0) {}
- AType(ASTSymbol* n, const CType* t) : kind(PRIM), ctype(t) { push_back(n); }
+ AType(ASTSymbol* n, CType t) : kind(PRIM), ctype(t) { push_back(n); }
string str() const {
switch (kind) {
case VAR: return (format("?%1%") % id).str();
@@ -168,7 +175,7 @@ struct AType : public ASTTuple {
return ""; // never reached
}
void constrain(TEnv& tenv) const {}
- CValue* compile(CEnv& cenv) { return NULL; }
+ CValue compile(CEnv& cenv) { return NULL; }
bool var() const { return kind == VAR; }
bool concrete() const {
switch (kind) {
@@ -197,23 +204,23 @@ struct AType : public ASTTuple {
}
return false; // never reached
}
- const CType* type();
+ CType type();
enum Kind { VAR, PRIM, EXPR };
Kind kind;
unsigned id;
private:
- const CType* ctype;
+ const CType ctype;
};
/// Lifted system functions (of various types) for a single Tuplr function
-struct Funcs : public list< pair<AType*, CFunction*> > {
- CFunction* find(AType* type) const {
+struct Funcs : public list< pair<AType*, CFunction> > {
+ CFunction find(AType* type) const {
for (const_iterator f = begin(); f != end(); ++f)
if (*f->first == *type)
return f->second;
return NULL;
}
- void insert(AType* type, CFunction* func) {
+ void insert(AType* type, CFunction func) {
push_back(make_pair(type, func));
}
};
@@ -226,7 +233,7 @@ struct ASTClosure : public ASTTuple {
string str() const { return (format("%1%") % this).str(); }
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
ASTTuple* prot() const { return dynamic_cast<ASTTuple*>(at(1)); }
private:
Funcs funcs;
@@ -238,7 +245,7 @@ struct ASTCall : public ASTTuple {
ASTCall(const SExp& e, const ASTTuple& t) : ASTTuple(t), exp(e) {}
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
const SExp& exp;
};
@@ -247,21 +254,21 @@ struct ASTDefinition : public ASTCall {
ASTDefinition(const SExp& e, const ASTTuple& t, CArg ca=CArg()) : ASTCall(e, t) {}
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
- CValue* compile(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, CArg ca=CArg()) : ASTCall(e, t) {}
void constrain(TEnv& tenv) const;
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
};
/// Primitive (builtin arithmetic function), e.g. "(+ 2 3)"
struct ASTPrimitive : public ASTCall {
ASTPrimitive(const SExp& e, const ASTTuple& t, CArg ca=CArg()) : ASTCall(e, t), arg(ca) {}
void constrain(TEnv& tenv) const;
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
CArg arg;
};
@@ -271,7 +278,7 @@ struct ASTConsCall : public ASTCall {
AType* functionType(CEnv& cenv);
void constrain(TEnv& tenv) const;
void lift(CEnv& cenv);
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
static Funcs funcs;
};
@@ -279,14 +286,14 @@ struct ASTConsCall : public ASTCall {
struct ASTCarCall : public ASTCall {
ASTCarCall(const SExp& e, const ASTTuple& t, CArg ca=CArg()) : ASTCall(e, t) {}
void constrain(TEnv& tenv) const;
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
};
/// Cdr special form, e.g. "(cdr p)"
struct ASTCdrCall : public ASTCall {
ASTCdrCall(const SExp& e, const ASTTuple& t, CArg ca=CArg()) : ASTCall(e, t) {}
void constrain(TEnv& tenv) const;
- CValue* compile(CEnv& cenv);
+ CValue compile(CEnv& cenv);
};
@@ -438,23 +445,23 @@ struct CEnv {
CEnv(PEnv& p, CEngine& engine);
~CEnv();
- typedef Env<const AST*, AST*> Code;
- typedef Env<const AST*, CValue*> Vals;
+ typedef Env<const AST*, AST*> Code;
+ typedef Env<const AST*, CValue> Vals;
- string gensym(const char* s="_") { return (format("%s%d") % s % symID++).str(); }
- void push() { code.push(); vals.push(); }
- void pop() { code.pop(); vals.pop(); }
- void precompile(AST* obj, CValue* value) { vals.def(obj, value); }
- CValue* compile(AST* obj);
- void optimise(CFunction& f);
+ string gensym(const char* s="_") { return (format("%s%d") % s % symID++).str(); }
+ void push() { code.push(); vals.push(); }
+ void pop() { code.pop(); vals.pop(); }
+ void precompile(AST* obj, CValue value) { vals.def(obj, value); }
+ CValue compile(AST* obj);
+ void optimise(CFunction f);
- CEngine& engine;
- PEnv& penv;
- TEnv tenv;
- Code code;
- Vals vals;
- unsigned symID;
- CFunction* alloc;
+ CEngine& engine;
+ PEnv& penv;
+ TEnv tenv;
+ Code code;
+ Vals vals;
+ unsigned symID;
+ CFunction alloc;
private:
CEnvPimpl* _pimpl;