aboutsummaryrefslogtreecommitdiffstats
path: root/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'tuplr.hpp')
-rw-r--r--tuplr.hpp35
1 files changed, 19 insertions, 16 deletions
diff --git a/tuplr.hpp b/tuplr.hpp
index 2c906d6..8910136 100644
--- a/tuplr.hpp
+++ b/tuplr.hpp
@@ -138,16 +138,15 @@ typedef void* CEngine; ///< Compiler Engine (opaque)
* Garbage Collector *
***************************************************************************/
-struct AST; ///< Abstract Syntax Tree node
-struct CEnv; ///< Compile-Time Environment
+struct Object; ///< Object (AST nodes and runtime data)
+struct CEnv; ///< Compile-Time Environment
-extern ostream& operator<<(ostream& out, const AST* ast);
struct GC {
- typedef std::list<const AST*> Roots;
- typedef std::list<AST*> Heap;
+ typedef std::list<const Object*> Roots;
+ typedef std::list<Object*> Heap;
void* alloc(size_t size);
void collect(CEnv& cenv, const Roots& roots);
- const AST* addRoot(const AST* ast) { if (ast) { cout << "ADD ROOT " << ast << endl; _roots.push_back(ast); } return ast; }
+ const Object* addRoot(const Object* obj) { if (obj) _roots.push_back(obj); return obj; }
void lock() { _roots.insert(_roots.end(), _heap.begin(), _heap.end()); }
const Roots& roots() const { return _roots; }
private:
@@ -155,6 +154,16 @@ private:
Roots _roots;
};
+struct Object {
+ Object() : used(false) {}
+ virtual ~Object() {}
+
+ mutable bool used;
+
+ static void* operator new(size_t size) { return pool.alloc(size); }
+ static void operator delete(void* ptr) {}
+ static GC pool;
+};
/***************************************************************************
* Abstract Syntax Tree *
@@ -164,13 +173,13 @@ struct Constraint; ///< Type Constraint
struct TEnv; ///< Type-Time Environment
struct Constraints;
struct Subst;
+struct AST;
extern ostream& operator<<(ostream& out, const AST* ast);
/// Base class for all AST nodes
-struct AST {
- AST(Cursor c=Cursor()) : loc(c), used(false) {}
- virtual ~AST() {}
+struct AST : public Object {
+ AST(Cursor c=Cursor()) : loc(c) {}
virtual bool operator==(const AST& o) const = 0;
virtual bool contains(const AST* child) const { return false; }
virtual void constrain(TEnv& tenv, Constraints& c) const {}
@@ -185,12 +194,6 @@ struct AST {
return t;
}
Cursor loc;
- mutable bool used;
-
- static void* operator new(size_t size) { return pool.alloc(size); }
- static void operator delete(void* ptr) {}
-
- static GC pool;
};
/// Literal value
@@ -582,7 +585,7 @@ struct CEnv {
CValue compile(AST* obj);
void optimise(CFunction f);
void write(std::ostream& os);
- void lock(AST* ast) { AST::pool.addRoot(ast); AST::pool.addRoot(type(ast)); }
+ void lock(AST* ast) { Object::pool.addRoot(ast); Object::pool.addRoot(type(ast)); }
AType* type(AST* ast, const Subst& subst = Subst()) const {
ASymbol* sym = ast->to<ASymbol*>();
if (sym)