diff options
author | David Robillard <d@drobilla.net> | 2009-03-15 02:27:15 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2009-03-15 02:27:15 +0000 |
commit | 59117fc8bcd610697a45c0013fc7450de2ee512f (patch) | |
tree | d475c5d743cc2defec2db5d34c72ae8888170d5b | |
parent | b2c71ba7fd851af043cebde347647ff82eb828d6 (diff) | |
download | resp-59117fc8bcd610697a45c0013fc7450de2ee512f.tar.gz resp-59117fc8bcd610697a45c0013fc7450de2ee512f.tar.bz2 resp-59117fc8bcd610697a45c0013fc7450de2ee512f.zip |
Make TEnv strictly keyed by ASymbol.
Prettier casting.
git-svn-id: http://svn.drobilla.net/resp/tuplr@93 ad02d1e2-f140-0410-9f75-f8b11f17cedd
-rw-r--r-- | llvm.cpp | 23 | ||||
-rw-r--r-- | tuplr.hpp | 7 | ||||
-rw-r--r-- | typing.cpp | 4 |
3 files changed, 14 insertions, 20 deletions
@@ -146,17 +146,6 @@ LITERAL(int32_t, "Int", ConstantInt::get(Type::Int32Ty, val, true)) LITERAL(float, "Float", ConstantFP::get(Type::FloatTy, val)) LITERAL(bool, "Bool", ConstantInt::get(Type::Int1Ty, val, false)) -template<typename T> -T -checked_cast(AST* ast) -{ - T t = dynamic_cast<T>(ast); - if (!t) - throw Error((format("internal error: `%1%' should be a `%2%'") - % typeid(ast).name() % typeid(T).name()).str(), ast->loc); - return t; -} - static Function* compileFunction(CEnv& cenv, const std::string& name, const Type* retT, const ATuple& protT, const vector<string> argNames=vector<string>()) @@ -165,7 +154,7 @@ compileFunction(CEnv& cenv, const std::string& name, const Type* retT, const ATu vector<const Type*> cprot; for (size_t i = 0; i < protT.size(); ++i) { - AType* at = checked_cast<AType*>(protT.at(i)); + AType* at = protT.at(i)->as<AType*>(); if (!lltype(at)) throw Error("function parameter is untyped"); cprot.push_back(lltype(at)); } @@ -226,7 +215,7 @@ AClosure::lift(CEnv& cenv) const_iterator p = prot()->begin(); size_t i = 0; for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p) - cenv.def(checked_cast<ASymbol*>(*p), *p, checked_cast<AType*>(protT->at(i++)), &*a); + cenv.def((*p)->as<ASymbol*>(), *p, protT->at(i++)->as<AType*>(), &*a); // Write function body try { @@ -286,7 +275,7 @@ AClosure::liftCall(CEnv& cenv, const vector<AType*>& argsT) const_iterator p = prot()->begin(); size_t i = 0; for (Function::arg_iterator a = f->arg_begin(); a != f->arg_end(); ++a, ++p) - cenv.def(checked_cast<ASymbol*>(*p), *p, checked_cast<AType*>(protT->at(i++)), &*a); + cenv.def((*p)->as<ASymbol*>(), *p, protT->at(i++)->as<AType*>(), &*a); // Write function body try { @@ -347,7 +336,7 @@ ACall::lift(CEnv& cenv) cenv.push(); for (size_t i = 1; i < size(); ++i) - cenv.def(checked_cast<ASymbol*>(c->prot()->at(i-1)), at(i), cenv.type(at(i)), NULL); + cenv.def(c->prot()->at(i-1)->as<ASymbol*>(), at(i), cenv.type(at(i)), NULL); c->liftCall(cenv, argsT); // Lift called closure cenv.pop(); // Restore environment @@ -382,10 +371,10 @@ ACall::compile(CEnv& cenv) void ADefinition::lift(CEnv& cenv) { - if (cenv.code.lookup(checked_cast<ASymbol*>(at(1)))) + if (cenv.code.lookup(at(1)->as<ASymbol*>())) throw Error(string("`") + at(1)->str() + "' redefined", loc); // Define first for recursion - cenv.def(checked_cast<ASymbol*>(at(1)), at(2), cenv.type(at(2)), NULL); + cenv.def(at(1)->as<ASymbol*>(), at(2), cenv.type(at(2)), NULL); at(2)->lift(cenv); } @@ -153,6 +153,11 @@ struct AST { virtual void constrain(TEnv& tenv, Constraints& c) const {} virtual void lift(CEnv& cenv) {} string str() const { ostringstream ss; ss << this; return ss.str(); } + template<typename T> T as() { + T t = dynamic_cast<T>(this); + if (!t) throw Error("internal error: bad cast", loc); + return t; + } Cursor loc; private: friend class CEnv; @@ -448,7 +453,7 @@ struct Subst : public map<const AType*,AType*> { }; /// Type-Time Environment -struct TEnv : public Env<const AST*,AType*> { +struct TEnv : public Env<const ASymbol*,AType*> { TEnv(PEnv& p) : penv(p), varID(1) {} AType* fresh(const ASymbol* sym) { @@ -110,7 +110,7 @@ AClosure::constrain(TEnv& tenv, Constraints& c) const genericType = new AType(loc, tenv.penv.sym("Fn"), tsubst.apply(protT), tsubst.apply(bodyT), 0); tenv.genericTypes.insert(make_pair(this, genericType)); - tenv.def(this, genericType); + //tenv.def(this, genericType); tenv.pop(); subst = new Subst(tsubst); @@ -141,7 +141,7 @@ ADefinition::constrain(TEnv& tenv, Constraints& c) const if (!dynamic_cast<const ASymbol*>(at(1))) throw Error("`def' name is not a symbol", loc); AType* tvar = tenv.var(at(2)); - tenv.def(at(1), tvar); + tenv.def(at(1)->as<ASymbol*>(), tvar); at(2)->constrain(tenv, c); c.constrain(tenv, this, tvar); } |