aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm.cpp23
-rw-r--r--tuplr.hpp7
-rw-r--r--typing.cpp4
3 files changed, 14 insertions, 20 deletions
diff --git a/llvm.cpp b/llvm.cpp
index f9e045a..240cb2f 100644
--- a/llvm.cpp
+++ b/llvm.cpp
@@ -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);
}
diff --git a/tuplr.hpp b/tuplr.hpp
index 130307e..506742a 100644
--- a/tuplr.hpp
+++ b/tuplr.hpp
@@ -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) {
diff --git a/typing.cpp b/typing.cpp
index d6f7acc..105140e 100644
--- a/typing.cpp
+++ b/typing.cpp
@@ -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);
}