diff options
Diffstat (limited to 'src/resp.hpp')
-rw-r--r-- | src/resp.hpp | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/src/resp.hpp b/src/resp.hpp index 724ed5d..7383cdd 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -133,6 +133,17 @@ typedef void* CFunc; ///< Compiled function (opaque) struct Object; +enum Tag { + T_UNKNOWN = 1<<1, + T_BOOL = 1<<2, + T_FLOAT = 1<<3, + T_INT32 = 1<<4, + T_LEXEME = 1<<5, + T_STRING = 1<<6, + T_SYMBOL = 1<<7, + T_TUPLE = 1<<8 +}; + /// Garbage collector struct GC { typedef std::list<const Object*> Roots; @@ -152,8 +163,6 @@ private: /// Garbage collected object (including AST and runtime data) struct Object { - enum Tag { OBJECT = 1<<1, AST = 1<<2 }; - struct Header { uint32_t tag; ///< Rightmost bit is mark }; @@ -198,10 +207,9 @@ typedef list<AST*> Code; /// Base class for all AST nodes struct AST : public Object { - AST(Cursor c=Cursor()) : loc(c) {} + AST(Tag t, Cursor c=Cursor()) : loc(c) { this->tag(t); } virtual ~AST() {} virtual bool operator==(const AST& o) const = 0; - virtual void constrain(TEnv& tenv, Constraints& c) const throw(Error) {} string str() const { ostringstream ss; ss << this; return ss.str(); } template<typename T> T to() { return dynamic_cast<T>(this); } template<typename T> T const to() const { return dynamic_cast<T const>(this); } @@ -229,52 +237,48 @@ static T* tup(Cursor c, AST* ast, ...) /// Literal value template<typename T> struct ALiteral : public AST { - ALiteral(T v, Cursor c) : AST(c), val(v) {} + ALiteral(Tag tag, T v, Cursor c) : AST(tag, c), val(v) {} bool operator==(const AST& rhs) const { const ALiteral<T>* r = rhs.to<const ALiteral<T>*>(); return (r && (val == r->val)); } - void constrain(TEnv& tenv, Constraints& c) const throw(Error); const T val; }; /// Lexeme (any atom in the CST, e.g. "a", "3.4", ""hello"", etc.) struct ALexeme : public AST, public std::string { - ALexeme(Cursor c, const string& s) : AST(c), std::string(s) {} + ALexeme(Cursor c, const string& s) : AST(T_LEXEME, c), std::string(s) {} bool operator==(const AST& rhs) const { return this == &rhs; } - void constrain(TEnv& tenv, Constraints& c) const throw(Error); }; /// String, e.g. ""a"" struct AString : public AST, public std::string { - AString(Cursor c, const string& s) : AST(c), std::string(s) {} + AString(Cursor c, const string& s) : AST(T_STRING, c), std::string(s) {} bool operator==(const AST& rhs) const { return this == &rhs; } - void constrain(TEnv& tenv, Constraints& c) const throw(Error); }; /// Symbol, e.g. "a" struct ASymbol : public AST { bool operator==(const AST& rhs) const { return this == &rhs; } - void constrain(TEnv& tenv, Constraints& c) const throw(Error); const string cppstr; private: friend class PEnv; - ASymbol(const string& s, Cursor c) : AST(c), cppstr(s) {} + ASymbol(const string& s, Cursor c) : AST(T_SYMBOL, c), cppstr(s) {} }; /// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)" struct ATuple : public AST { - ATuple(Cursor c) : AST(c), _len(0), _vec(0) {} - ATuple(const ATuple& exp) : AST(exp.loc), _len(exp._len) { + ATuple(Cursor c) : AST(T_TUPLE, c), _len(0), _vec(0) {} + ATuple(const ATuple& exp) : AST(T_TUPLE, exp.loc), _len(exp._len) { _vec = (AST**)malloc(sizeof(AST*) * _len); memcpy(_vec, exp._vec, sizeof(AST*) * _len); } - ATuple(AST* first, AST* rest, Cursor c=Cursor()) : AST(c), _len(2) { + ATuple(AST* first, AST* rest, Cursor c=Cursor()) : AST(T_TUPLE, c), _len(2) { _vec = (AST**)malloc(sizeof(AST*) * _len); _vec[0] = first; _vec[1] = rest; } - ATuple(Cursor c, AST* ast, va_list args) : AST(c), _len(0), _vec(0) { + ATuple(Cursor c, AST* ast, va_list args) : AST(T_TUPLE, c), _len(0), _vec(0) { if (!ast) return; _len = 2; @@ -416,7 +420,6 @@ struct ATuple : public AST { return false; return true; } - void constrain(TEnv& tenv, Constraints& c) const throw(Error); const ATuple* prot() const { return list_ref(1)->as<const ATuple*>(); } ATuple* prot() { return list_ref(1)->as<ATuple*>(); } @@ -848,8 +851,11 @@ void pprint(std::ostream& out, const AST* ast, CEnv* cenv, bool types); void initLang(PEnv& penv, TEnv& tenv); int eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute); int repl(CEnv& cenv); + +void resp_constrain(TEnv& tenv, Constraints& c, const AST* ast) throw(Error); AST* resp_lift(CEnv& cenv, Code& code, AST* ast) throw(); CVal resp_compile(CEnv& cenv, const AST* ast) throw(); + bool is_form(const AST* ast, const std::string& form); bool is_primitive(const PEnv& penv, const AST* ast); |