diff options
Diffstat (limited to 'src/resp.hpp')
-rw-r--r-- | src/resp.hpp | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/src/resp.hpp b/src/resp.hpp index 6e4dc77..3145080 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -338,18 +338,19 @@ private: /// Type Expression, e.g. "Int", "(Fn (Int Int) Float)" struct AType : public ATuple { - enum Kind { VAR, PRIM, EXPR, DOTS }; - AType(ASymbol* s) : ATuple(s->loc), kind(PRIM), id(0) { push_back(s); } + enum Kind { VAR, NAME, PRIM, EXPR, DOTS }; + AType(ASymbol* s, Kind k) : ATuple(s->loc), kind(k), id(0) { push_back(s); } AType(Cursor c, unsigned i) : ATuple(c), kind(VAR), id(i) {} AType(Cursor c, Kind k=EXPR) : ATuple(c), kind(k), id(0) {} AType(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args), kind(EXPR), id(0) {} AType(const AType& copy) : ATuple(copy), kind(copy.kind), id(copy.id) { } - CVal compile(CEnv& env) const throw() { return NULL; } + CVal compile(CEnv& cenv) const throw(); const ATuple* prot() const { assert(kind == EXPR); return (*(begin() + 1))->to<const ATuple*>(); } ATuple* prot() { assert(kind == EXPR); return (*(begin() + 1))->to<ATuple*>(); } bool concrete() const { switch (kind) { case VAR: return false; + case NAME: return false; case PRIM: return head()->str() != "Nothing"; case EXPR: FOREACHP(const_iterator, t, this) { @@ -363,14 +364,16 @@ struct AType : public ATuple { } bool operator==(const AST& rhs) const { const AType* rt = rhs.to<const AType*>(); - if (!rt || kind != rt->kind) + if (!rt || kind != rt->kind) { + assert(str() != rt->str()); return false; - else + } else switch (kind) { - case VAR: return id == rt->id; - case PRIM: return head()->str() == rt->head()->str(); - case EXPR: return ATuple::operator==(rhs); - case DOTS: return true; + case VAR: return id == rt->id; + case NAME: return head()->str() == rt->head()->str(); + case PRIM: return head()->str() == rt->head()->str(); + case EXPR: return ATuple::operator==(rhs); + case DOTS: return true; } return false; // never reached } @@ -427,6 +430,27 @@ struct ADef : public ACall { 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) {} + const ASymbol* sym() const { return (*(begin() + 1))->as<const ASymbol*>(); } + void constrain(TEnv& tenv, Constraints& c) const throw(Error); + AST* cps(TEnv& tenv, AST* cont) const; + AST* lift(CEnv& cenv, Code& code) throw() { return this; } + AST* depoly(CEnv& cenv, Code& code) throw() { return this; } + CVal compile(CEnv& env) const throw() { return NULL; } +}; + +struct AMatch : public ACall { + AMatch(const ATuple* exp) : ACall(exp) {} + AMatch(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {} + void constrain(TEnv& tenv, Constraints& c) const throw(Error); + AST* cps(TEnv& tenv, AST* cont) const; + AST* lift(CEnv& cenv, Code& code) throw() { return this; } + AST* depoly(CEnv& cenv, Code& code) throw() { return this; } + CVal compile(CEnv& env) const throw(); +}; + /// Conditional special form, e.g. "(if cond thenexp elseexp)" struct AIf : public ACall { AIf(const ATuple* exp) : ACall(exp) {} @@ -597,7 +621,7 @@ struct Constraints : public list<Constraint> { inline ostream& operator<<(ostream& out, const Constraints& c) { for (Constraints::const_iterator i = c.begin(); i != c.end(); ++i) - out << i->first << " : " << i->second << endl; + out << i->first << " <= " << i->second << endl; return out; } @@ -606,8 +630,9 @@ struct TEnv : public Env<const ASymbol*, const AType*> { TEnv(PEnv& p) : penv(p) , varID(1) - , Fn(new AType(penv.sym("Fn"))) - , Tup(new AType(penv.sym("Tup"))) + , Fn(new AType(penv.sym("Fn"), AType::PRIM)) + , Tup(new AType(penv.sym("Tup"), AType::NAME)) + , U(new AType(penv.sym("U"), AType::PRIM)) { Object::pool.addRoot(Fn); } @@ -641,6 +666,7 @@ struct TEnv : public Env<const ASymbol*, const AType*> { AType* Fn; AType* Tup; + AType* U; }; Subst unify(const Constraints& c); @@ -666,13 +692,14 @@ struct Engine { virtual void finishFunction(CEnv& cenv, CFunc f, CVal ret) = 0; virtual void eraseFunction(CEnv& cenv, CFunc f) = 0; - virtual CVal compileTup(CEnv& cenv, const AType* t, ValVec& f) = 0; + virtual CVal compileTup(CEnv& cenv, const AType* t, CVal rtti, ValVec& f) = 0; virtual CVal compileDot(CEnv& cenv, CVal tup, int32_t index) = 0; virtual CVal compileLiteral(CEnv& cenv, const AST* lit) = 0; virtual CVal compileString(CEnv& cenv, const char* str) = 0; virtual CVal compileCall(CEnv& cenv, CFunc f, const AType* fT, ValVec& args) = 0; virtual CVal compilePrimitive(CEnv& cenv, const APrimitive* prim) = 0; virtual CVal compileIf(CEnv& cenv, const AIf* aif) = 0; + virtual CVal compileMatch(CEnv& cenv, const AMatch* match) = 0; virtual CVal compileGlobal(CEnv& cenv, const AType* t, const string& sym, CVal val) = 0; virtual CVal getGlobal(CEnv& cenv, const string& sym, CVal val) = 0; virtual void writeModule(CEnv& cenv, std::ostream& os) = 0; |