diff options
Diffstat (limited to 'src/resp.hpp')
-rw-r--r-- | src/resp.hpp | 78 |
1 files changed, 19 insertions, 59 deletions
diff --git a/src/resp.hpp b/src/resp.hpp index 26038b6..632e1a2 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -200,12 +200,8 @@ typedef list<AST*> Code; struct AST : public Object { AST(Cursor c=Cursor()) : loc(c) {} virtual ~AST() {} - virtual bool value() const { return true; } 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 throw(Error) {} - virtual AST* lift(CEnv& cenv, Code& code) throw() { return this; } - virtual CVal compile(CEnv& env) const throw() = 0; 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); } @@ -239,7 +235,6 @@ struct ALiteral : public AST { return (r && (val == r->val)); } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - CVal compile(CEnv& env) const throw(); const T val; }; @@ -248,7 +243,6 @@ struct ALexeme : public AST, public std::string { ALexeme(Cursor c, const string& s) : AST(c), std::string(s) {} bool operator==(const AST& rhs) const { return this == &rhs; } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - CVal compile(CEnv& env) const throw(); }; /// String, e.g. ""a"" @@ -256,15 +250,12 @@ struct AString : public AST, public std::string { AString(Cursor c, const string& s) : AST(c), std::string(s) {} bool operator==(const AST& rhs) const { return this == &rhs; } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - CVal compile(CEnv& env) const throw(); }; /// 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); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); const string cppstr; private: friend class PEnv; @@ -416,7 +407,6 @@ struct ATuple : public AST { AST*& list_ref(unsigned index) { return *iter_at(index); } const AST* list_ref(unsigned index) const { return *iter_at(index); } - bool value() const { return false; } bool operator==(const AST& rhs) const { const ATuple* rt = rhs.to<const ATuple*>(); if (!rt || rt->tup_len() != tup_len()) return false; @@ -426,22 +416,29 @@ struct ATuple : public AST { return false; return true; } - bool contains(const AST* child) const { - if (*this == *child) return true; - FOREACHP(const_iterator, p, this) - if (**p == *child || (*p)->contains(child)) - return true; - return false; - } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); private: size_t _len; AST** _vec; }; +static bool +list_contains(const ATuple* head, const AST* child) { + if (*head == *child) + return true; + + FOREACHP(ATuple::const_iterator, p, head) { + if (**p == *child) + return true; + + const ATuple* tup = (*p)->to<const ATuple*>(); + if (tup && list_contains(tup, child)) + return true; + } + + return false; +} /// Type Expression, e.g. "Int", "(Fn (Int Int) Float)" struct AType : public ATuple { @@ -452,7 +449,6 @@ struct AType : public ATuple { AType(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args), kind(EXPR), id(0) {} AType(AST* first, AST* rest, Cursor c) : ATuple(first, rest, c), kind(EXPR), id(0) {} AType(const AType& copy) : ATuple(copy), kind(copy.kind), id(copy.id) {} - CVal compile(CEnv& cenv) const throw(); const ATuple* prot() const { assert(kind == EXPR); return list_ref(1)->to<const ATuple*>(); } ATuple* prot() { assert(kind == EXPR); return list_ref(1)->to<ATuple*>(); } void prot(ATuple* prot) { assert(kind == EXPR); *iter_at(1) = prot; } @@ -534,8 +530,6 @@ struct AFn : public ATuple { AFn(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args) {} bool operator==(const AST& rhs) const { return this == &rhs; } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); const ATuple* prot() const { return list_ref(1)->to<const ATuple*>(); } ATuple* prot() { return list_ref(1)->to<ATuple*>(); } void prot(ATuple* prot) { *iter_at(1) = prot; } @@ -548,8 +542,6 @@ struct ACall : public ATuple { ACall(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args) {} ACall(AST* first, AST* rest, Cursor c) : ATuple(first, rest, c) {} void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); }; /// Definition special form, e.g. "(def x 2)" @@ -557,31 +549,16 @@ struct ADef : public ACall { ADef(const ATuple* exp) : ACall(exp) {} ADef(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} ADef(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} - const ASymbol* sym() const { - const AST* name = list_ref(1); - const ASymbol* sym = name->to<const ASymbol*>(); - if (!sym) { - const ATuple* tup = name->to<const ATuple*>(); - if (tup && !tup->empty()) - return tup->head()->to<const ASymbol*>(); - } - return sym; - } const AST* body() const { return list_ref(2); } AST* body() { return list_ref(2); } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); }; struct ADefType : public ACall { ADefType(const ATuple* exp) : ACall(exp) {} ADefType(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} ADefType(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} - const ASymbol* sym() const { return list_ref(1)->as<const ASymbol*>(); } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw() { return this; } - CVal compile(CEnv& env) const throw() { return NULL; } }; struct AMatch : public ACall { @@ -589,8 +566,6 @@ struct AMatch : public ACall { AMatch(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} AMatch(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw() { return this; } - CVal compile(CEnv& env) const throw(); }; /// Conditional special form, e.g. "(if cond thenexp elseexp)" @@ -599,8 +574,6 @@ struct AIf : public ACall { AIf(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} AIf(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); }; struct ACons : public ACall { @@ -608,8 +581,6 @@ struct ACons : public ACall { ACons(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} ACons(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); }; struct ADot : public ACall { @@ -617,32 +588,19 @@ struct ADot : public ACall { ADot(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} ADot(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); }; /// Primitive (builtin arithmetic function), e.g. "(+ 2 3)" struct APrimitive : public ACall { APrimitive(const ATuple* exp) : ACall(exp) {} APrimitive(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} - bool value() const { - ATuple::const_iterator i = begin(); - for (++i; i != end(); ++i) - if (!(*i)->value()) - return false;; - return true; - } void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); }; struct AQuote : public ACall { AQuote(const ATuple* exp) : ACall(exp) {} AQuote(AST* first, AST* rest, Cursor c) : ACall(first, rest, c) {} void constrain(TEnv& tenv, Constraints& c) const throw(Error); - AST* lift(CEnv& cenv, Code& code) throw(); - CVal compile(CEnv& env) const throw(); }; @@ -737,7 +695,7 @@ struct Subst : public list<Constraint> { if (find(type) != end()) return true; FOREACHP(const_iterator, j, this) - if (*j->second == *type || j->second->contains(type)) + if (*j->second == *type || list_contains(j->second, type)) return true; return false; } @@ -954,5 +912,7 @@ 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); +AST* resp_lift(CEnv& cenv, Code& code, AST* ast) throw(); +CVal resp_compile(CEnv& cenv, const AST* ast) throw(); #endif // RESP_HPP |