From c2d75892af2fdc6b9bf25365a15de5dc63bcc852 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 16 Oct 2009 01:53:16 +0000 Subject: Cons. git-svn-id: http://svn.drobilla.net/resp/tuplr@235 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/tuplr.hpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/tuplr.hpp') diff --git a/src/tuplr.hpp b/src/tuplr.hpp index dce52b2..31585d2 100644 --- a/src/tuplr.hpp +++ b/src/tuplr.hpp @@ -129,19 +129,29 @@ private: /// Garbage collected object (including AST and runtime data) struct Object { + enum Tag { OBJECT = 123, AST = 456 }; + struct Header { - uint8_t mark; + uint32_t mark; + uint32_t tag; }; - /// Always allocated with pool.alloc, so this - sizeof(Header) is a valid Header*. - inline Header* header() const { return (Header*)((char*)this - sizeof(Header)); } - + inline Tag tag() const { return (Tag)header()->tag; } + inline void tag(Tag t) { header()->tag = t; } inline bool marked() const { return header()->mark != 0; } inline void mark(bool b) const { header()->mark = 1; } - static void* operator new(size_t size) { return pool.alloc(size); } + static void* operator new(size_t size) { return pool.alloc(size); } static void operator delete(void* ptr) {} + + // Memory used with placement new MUST always be allocated with pool.alloc! + static void* operator new(size_t size, void* ptr) { return ptr; } + static GC pool; + +private: + /// Always allocated with pool.alloc, so this - sizeof(Header) is a valid Header*. + inline Header* header() const { return (Header*)((char*)this - sizeof(Header)); } }; @@ -424,6 +434,12 @@ struct AIf : public ACall { CVal compile(CEnv& cenv); }; +struct ACons : public ACall { + ACons(const ATuple* exp) : ACall(exp) {} + void constrain(TEnv& tenv, Constraints& c) const; + CVal compile(CEnv& cenv); +}; + /// Primitive (builtin arithmetic function), e.g. "(+ 2 3)" struct APrimitive : public ACall { APrimitive(const ATuple* exp) : ACall(exp) {} @@ -546,7 +562,8 @@ inline ostream& operator<<(ostream& out, const Constraints& c) { /// Type-Time Environment struct TEnv : public Env { - TEnv(PEnv& p) : penv(p), varID(1), Fn(new AType(penv.sym("Fn"))) { + TEnv(PEnv& p) : penv(p), varID(1), + Fn(new AType(penv.sym("Fn"))), Tup(new AType(penv.sym("Tup"))) { Object::pool.addRoot(Fn); } AType* fresh(const ASymbol* sym) { @@ -579,6 +596,7 @@ struct TEnv : public Env { unsigned varID; AType* Fn; + AType* Tup; }; Subst unify(const Constraints& c); @@ -600,6 +618,7 @@ struct Engine { virtual void finishFunction(CEnv& cenv, CFunc f, const AType* retT, CVal ret) = 0; virtual void eraseFunction(CEnv& cenv, CFunc f) = 0; virtual CFunc compileFunction(CEnv& cenv, AFn* fn, const AType& argsT) = 0; + virtual CVal compileTup(CEnv& cenv, const AType* t, const vector& f) = 0; virtual CVal compileLiteral(CEnv& cenv, AST* lit) = 0; virtual CVal compileCall(CEnv& cenv, CFunc f, const vector& args) = 0; virtual CVal compilePrimitive(CEnv& cenv, APrimitive* prim) = 0; -- cgit v1.2.1