aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/c.cpp26
-rw-r--r--src/compile.cpp26
-rw-r--r--src/depoly.cpp4
-rw-r--r--src/llvm.cpp32
-rw-r--r--src/resp.hpp60
5 files changed, 74 insertions, 74 deletions
diff --git a/src/c.cpp b/src/c.cpp
index 7f4a563..66ab971 100644
--- a/src/c.cpp
+++ b/src/c.cpp
@@ -150,14 +150,14 @@ struct CEngine : public Engine {
return varname;
}
- CFunc compileFunction(CEnv& cenv, AFn* fn, const AType* type);
+ CFunc compileFunction(CEnv& cenv, const AFn* fn, const AType* type);
CVal compileTup(CEnv& cenv, const AType* type, const vector<CVal>& fields);
CVal compileDot(CEnv& cenv, CVal tup, int32_t index);
- CVal compileLiteral(CEnv& cenv, AST* lit);
+ CVal compileLiteral(CEnv& cenv, const AST* lit);
CVal compileString(CEnv& cenv, const char* str);
- CVal compilePrimitive(CEnv& cenv, APrimitive* prim);
- CVal compileIf(CEnv& cenv, AIf* aif);
+ CVal compilePrimitive(CEnv& cenv, const APrimitive* prim);
+ CVal compileIf(CEnv& cenv, const AIf* aif);
CVal compileGlobal(CEnv& cenv, const AType* type, const string& sym, CVal val);
CVal getGlobal(CEnv& cenv, const string& sym, CVal val);
@@ -196,7 +196,7 @@ CEngine::compileDot(CEnv& cenv, CVal tup, int32_t index)
}
CVal
-CEngine::compileLiteral(CEnv& cenv, AST* lit)
+CEngine::compileLiteral(CEnv& cenv, const AST* lit)
{
return new Value(lit->str());
}
@@ -208,7 +208,7 @@ CEngine::compileString(CEnv& cenv, const char* str)
}
CFunc
-CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType* type)
+CEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type)
{
assert(type->concrete());
@@ -242,7 +242,7 @@ CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType* type)
// Write function body
try {
CVal retVal = NULL;
- for (AFn::iterator i = fn->begin() + 2; i != fn->end(); ++i)
+ for (AFn::const_iterator i = fn->begin() + 2; i != fn->end(); ++i)
retVal = (*i)->compile(cenv);
cenv.engine()->finishFunction(cenv, f, retVal);
} catch (Error& e) {
@@ -255,13 +255,13 @@ CEngine::compileFunction(CEnv& cenv, AFn* fn, const AType* type)
}
CVal
-CEngine::compileIf(CEnv& cenv, AIf* aif)
+CEngine::compileIf(CEnv& cenv, const AIf* aif)
{
Value* varname = new string(cenv.penv.gensymstr("if"));
out += (format("%s %s;\n") % *llType(cenv.type(aif)) % *varname).str();
size_t idx = 1;
- for (AIf::iterator i = aif->begin() + 1; ; ++i, idx += 2) {
- AIf::iterator next = i;
+ for (AIf::const_iterator i = aif->begin() + 1; ; ++i, idx += 2) {
+ AIf::const_iterator next = i;
if (++next == aif->end())
break;
@@ -287,14 +287,14 @@ CEngine::compileIf(CEnv& cenv, AIf* aif)
}
CVal
-CEngine::compilePrimitive(CEnv& cenv, APrimitive* prim)
+CEngine::compilePrimitive(CEnv& cenv, const APrimitive* prim)
{
- APrimitive::iterator i = prim->begin();
+ APrimitive::const_iterator i = prim->begin();
++i;
Value* a = llVal((*i++)->compile(cenv));
Value* b = llVal((*i++)->compile(cenv));
- const string n = prim->head()->to<ASymbol*>()->str();
+ const string n = prim->head()->to<const ASymbol*>()->str();
string op = n;
// Convert operator to C operator if they don't match
diff --git a/src/compile.cpp b/src/compile.cpp
index 31fa889..7a875f3 100644
--- a/src/compile.cpp
+++ b/src/compile.cpp
@@ -24,7 +24,7 @@
using namespace std;
#define COMPILE_LITERAL(CT) \
-template<> CVal ALiteral<CT>::compile(CEnv& cenv) throw() { \
+template<> CVal ALiteral<CT>::compile(CEnv& cenv) const throw() { \
return cenv.engine()->compileLiteral(cenv, this); \
}
COMPILE_LITERAL(int32_t);
@@ -32,25 +32,25 @@ COMPILE_LITERAL(float);
COMPILE_LITERAL(bool);
CVal
-AString::compile(CEnv& cenv) throw()
+AString::compile(CEnv& cenv) const throw()
{
return cenv.engine()->compileString(cenv, c_str());
}
CVal
-AQuote::compile(CEnv& cenv) throw()
+AQuote::compile(CEnv& cenv) const throw()
{
return (*(begin() + 1))->compile(cenv);
}
CVal
-ALexeme::compile(CEnv& cenv) throw()
+ALexeme::compile(CEnv& cenv) const throw()
{
return cenv.engine()->compileString(cenv, c_str());
}
CVal
-ASymbol::compile(CEnv& cenv) throw()
+ASymbol::compile(CEnv& cenv) const throw()
{
if (cenv.vals.topLevel(this) && cenv.type(this)->head()->str() != "Fn") {
return cenv.engine()->getGlobal(cenv, cppstr, *cenv.vals.ref(this));
@@ -60,7 +60,7 @@ ASymbol::compile(CEnv& cenv) throw()
}
CVal
-AFn::compile(CEnv& cenv) throw()
+AFn::compile(CEnv& cenv) const throw()
{
const AType* type = cenv.type(this);
CFunc f = cenv.findImpl(this, type);
@@ -73,7 +73,7 @@ AFn::compile(CEnv& cenv) throw()
}
CVal
-ACall::compile(CEnv& cenv) throw()
+ACall::compile(CEnv& cenv) const throw()
{
CFunc f = (*begin())->compile(cenv);
@@ -88,7 +88,7 @@ ACall::compile(CEnv& cenv) throw()
}
CVal
-ADef::compile(CEnv& cenv) throw()
+ADef::compile(CEnv& cenv) const throw()
{
cenv.def(sym(), body(), cenv.type(body()), NULL); // define stub first for recursion
CVal val = body()->compile(cenv);
@@ -102,13 +102,13 @@ ADef::compile(CEnv& cenv) throw()
}
CVal
-AIf::compile(CEnv& cenv) throw()
+AIf::compile(CEnv& cenv) const throw()
{
return cenv.engine()->compileIf(cenv, this);
}
CVal
-ACons::compile(CEnv& cenv) throw()
+ACons::compile(CEnv& cenv) const throw()
{
const AType* type = cenv.type(this);
vector<CVal> fields;
@@ -118,7 +118,7 @@ ACons::compile(CEnv& cenv) throw()
}
CVal
-ATuple::compile(CEnv& cenv) throw()
+ATuple::compile(CEnv& cenv) const throw()
{
const AType* type = cenv.type(this);
vector<CVal> fields;
@@ -128,7 +128,7 @@ ATuple::compile(CEnv& cenv) throw()
}
CVal
-ADot::compile(CEnv& cenv) throw()
+ADot::compile(CEnv& cenv) const throw()
{
const_iterator i = begin();
AST* tup = *++i;
@@ -138,7 +138,7 @@ ADot::compile(CEnv& cenv) throw()
}
CVal
-APrimitive::compile(CEnv& cenv) throw()
+APrimitive::compile(CEnv& cenv) const throw()
{
return cenv.engine()->compilePrimitive(cenv, this);
}
diff --git a/src/depoly.cpp b/src/depoly.cpp
index 9b457c2..cd9c5e8 100644
--- a/src/depoly.cpp
+++ b/src/depoly.cpp
@@ -45,8 +45,8 @@ template<typename T>
AST*
depoly_call(CEnv& cenv, T* call, Code& code) throw()
{
- AST* head = cenv.resolve(call->head());
- AFn* callee = head->to<AFn*>();
+ const AST* head = cenv.resolve(call->head());
+ const AFn* callee = head->to<const AFn*>();
if (!callee || cenv.type(callee)->concrete())
return call;
diff --git a/src/llvm.cpp b/src/llvm.cpp
index 6aedd93..7039a97 100644
--- a/src/llvm.cpp
+++ b/src/llvm.cpp
@@ -186,14 +186,14 @@ struct LLVMEngine : public Engine {
return builder.CreateCall(llFunc(f), llArgs.begin(), llArgs.end());
}
- CFunc compileFunction(CEnv& cenv, AFn* fn, const AType* type);
+ CFunc compileFunction(CEnv& cenv, const AFn* fn, const AType* type);
CVal compileTup(CEnv& cenv, const AType* type, const vector<CVal>& fields);
CVal compileDot(CEnv& cenv, CVal tup, int32_t index);
- CVal compileLiteral(CEnv& cenv, AST* lit);
+ CVal compileLiteral(CEnv& cenv, const AST* lit);
CVal compileString(CEnv& cenv, const char* str);
- CVal compilePrimitive(CEnv& cenv, APrimitive* prim);
- CVal compileIf(CEnv& cenv, AIf* aif);
+ CVal compilePrimitive(CEnv& cenv, const APrimitive* prim);
+ CVal compileIf(CEnv& cenv, const AIf* aif);
CVal compileGlobal(CEnv& cenv, const AType* type, const string& sym, CVal val);
CVal getGlobal(CEnv& cenv, const string& sym, CVal val);
@@ -299,17 +299,17 @@ LLVMEngine::compileDot(CEnv& cenv, CVal tup, int32_t index)
}
CVal
-LLVMEngine::compileLiteral(CEnv& cenv, AST* lit)
+LLVMEngine::compileLiteral(CEnv& cenv, const AST* lit)
{
- ALiteral<int32_t>* ilit = dynamic_cast<ALiteral<int32_t>*>(lit);
+ const ALiteral<int32_t>* ilit = dynamic_cast<const ALiteral<int32_t>*>(lit);
if (ilit)
return ConstantInt::get(Type::getInt32Ty(context), ilit->val, true);
- ALiteral<float>* flit = dynamic_cast<ALiteral<float>*>(lit);
+ const ALiteral<float>* flit = dynamic_cast<const ALiteral<float>*>(lit);
if (flit)
return ConstantFP::get(Type::getFloatTy(context), flit->val);
- ALiteral<bool>* blit = dynamic_cast<ALiteral<bool>*>(lit);
+ const ALiteral<bool>* blit = dynamic_cast<const ALiteral<bool>*>(lit);
if (blit)
return ConstantFP::get(Type::getFloatTy(context), blit->val);
@@ -323,7 +323,7 @@ LLVMEngine::compileString(CEnv& cenv, const char* str)
}
CFunc
-LLVMEngine::compileFunction(CEnv& cenv, AFn* fn, const AType* type)
+LLVMEngine::compileFunction(CEnv& cenv, const AFn* fn, const AType* type)
{
assert(type->concrete());
@@ -359,7 +359,7 @@ LLVMEngine::compileFunction(CEnv& cenv, AFn* fn, const AType* type)
try {
cenv.currentFn = f;
CVal retVal = NULL;
- for (AFn::iterator i = fn->begin() + 2; i != fn->end(); ++i)
+ for (AFn::const_iterator i = fn->begin() + 2; i != fn->end(); ++i)
retVal = (*i)->compile(cenv);
cenv.engine()->finishFunction(cenv, f, retVal);
} catch (Error& e) {
@@ -374,7 +374,7 @@ LLVMEngine::compileFunction(CEnv& cenv, AFn* fn, const AType* type)
}
CVal
-LLVMEngine::compileIf(CEnv& cenv, AIf* aif)
+LLVMEngine::compileIf(CEnv& cenv, const AIf* aif)
{
typedef vector< pair<Value*, BasicBlock*> > Branches;
LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
@@ -383,8 +383,8 @@ LLVMEngine::compileIf(CEnv& cenv, AIf* aif)
BasicBlock* nextBB = NULL;
Branches branches;
size_t idx = 1;
- for (AIf::iterator i = aif->begin() + 1; ; ++i, idx += 2) {
- AIf::iterator next = i;
+ for (AIf::const_iterator i = aif->begin() + 1; ; ++i, idx += 2) {
+ AIf::const_iterator next = i;
if (++next == aif->end())
break;
@@ -426,15 +426,15 @@ LLVMEngine::compileIf(CEnv& cenv, AIf* aif)
}
CVal
-LLVMEngine::compilePrimitive(CEnv& cenv, APrimitive* prim)
+LLVMEngine::compilePrimitive(CEnv& cenv, const APrimitive* prim)
{
- APrimitive::iterator i = prim->begin();
+ APrimitive::const_iterator i = prim->begin();
LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
bool isFloat = cenv.type(*++i)->str() == "Float";
Value* a = llVal((*i++)->compile(cenv));
Value* b = llVal((*i++)->compile(cenv));
- const string n = prim->head()->to<ASymbol*>()->str();
+ const string n = prim->head()->to<const ASymbol*>()->str();
// Binary arithmetic operations
Instruction::BinaryOps op = (Instruction::BinaryOps)0;
diff --git a/src/resp.hpp b/src/resp.hpp
index e551c0c..1432b0f 100644
--- a/src/resp.hpp
+++ b/src/resp.hpp
@@ -203,7 +203,7 @@ struct AST : public Object {
virtual AST* cps(TEnv& tenv, AST* cont) const;
virtual AST* lift(CEnv& cenv, Code& code) throw() { return this; }
virtual AST* depoly(CEnv& cenv, Code& code) throw() { return this; }
- virtual CVal compile(CEnv& cenv) throw() = 0;
+ 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); }
@@ -237,7 +237,7 @@ struct ALiteral : public AST {
return (r && (val == r->val));
}
void constrain(TEnv& tenv, Constraints& c) const throw(Error);
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
const T val;
};
@@ -246,7 +246,7 @@ 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& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
/// String, e.g. ""a""
@@ -254,7 +254,7 @@ 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& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
/// Symbol, e.g. "a"
@@ -262,7 +262,7 @@ 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& cenv) throw();
+ CVal compile(CEnv& env) const throw();
const string cppstr;
private:
friend class PEnv;
@@ -329,7 +329,7 @@ struct ATuple : public AST {
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
private:
size_t _len;
@@ -344,7 +344,7 @@ struct AType : public ATuple {
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& cenv) throw() { return NULL; }
+ CVal compile(CEnv& env) const throw() { return NULL; }
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 {
@@ -387,7 +387,7 @@ struct AFn : public ATuple {
AST* cps(TEnv& tenv, AST* cont) const;
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
const ATuple* prot() const { return (*(begin() + 1))->to<const ATuple*>(); }
ATuple* prot() { return (*(begin() + 1))->to<ATuple*>(); }
string name;
@@ -401,7 +401,7 @@ struct ACall : public ATuple {
AST* cps(TEnv& tenv, AST* cont) const;
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
/// Definition special form, e.g. "(def x 2)"
@@ -424,7 +424,7 @@ struct ADef : public ACall {
AST* cps(TEnv& tenv, AST* cont) const;
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
/// Conditional special form, e.g. "(if cond thenexp elseexp)"
@@ -435,7 +435,7 @@ struct AIf : public ACall {
AST* cps(TEnv& tenv, AST* cont) const;
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
struct ACons : public ACall {
@@ -444,7 +444,7 @@ struct ACons : public ACall {
void constrain(TEnv& tenv, Constraints& c) const throw(Error);
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
struct ADot : public ACall {
@@ -453,7 +453,7 @@ struct ADot : public ACall {
void constrain(TEnv& tenv, Constraints& c) const throw(Error);
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
/// Primitive (builtin arithmetic function), e.g. "(+ 2 3)"
@@ -470,14 +470,14 @@ struct APrimitive : public ACall {
AST* cps(TEnv& tenv, AST* cont) const;
AST* lift(CEnv& cenv, Code& code) throw();
AST* depoly(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
struct AQuote : public ACall {
AQuote(const ATuple* exp) : ACall(exp) {}
void constrain(TEnv& tenv, Constraints& c) const throw(Error);
AST* lift(CEnv& cenv, Code& code) throw();
- CVal compile(CEnv& cenv) throw();
+ CVal compile(CEnv& env) const throw();
};
@@ -665,14 +665,14 @@ struct Engine {
virtual void finishFunction(CEnv& cenv, CFunc f, CVal ret) = 0;
virtual void eraseFunction(CEnv& cenv, CFunc f) = 0;
- virtual CFunc compileFunction(CEnv& cenv, AFn* fn, const AType* type) = 0;
+ virtual CFunc compileFunction(CEnv& cenv, const AFn* fn, const AType* type) = 0;
virtual CVal compileTup(CEnv& cenv, const AType* t, ValVec& f) = 0;
virtual CVal compileDot(CEnv& cenv, CVal tup, int32_t index) = 0;
- virtual CVal compileLiteral(CEnv& cenv, AST* lit) = 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, APrimitive* prim) = 0;
- virtual CVal compileIf(CEnv& cenv, AIf* aif) = 0;
+ virtual CVal compilePrimitive(CEnv& cenv, const APrimitive* prim) = 0;
+ virtual CVal compileIf(CEnv& cenv, const AIf* aif) = 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;
@@ -696,13 +696,13 @@ struct CEnv {
Engine* engine() { return _engine; }
void push() { code.push(); tenv.push(); vals.push(); }
void pop() { code.pop(); tenv.pop(); vals.pop(); }
- void lock(AST* ast) {
+ void lock(const AST* ast) {
Object::pool.addRoot(ast);
if (type(ast))
Object::pool.addRoot(type(ast));
}
- const AType* type(AST* ast, const Subst& subst = Subst()) const {
- ASymbol* sym = ast->to<ASymbol*>();
+ const AType* type(const AST* ast, const Subst& subst = Subst()) const {
+ const ASymbol* sym = ast->to<const ASymbol*>();
if (sym) {
const AType** rec = tenv.ref(sym);
return rec ? *rec : NULL;
@@ -713,14 +713,14 @@ struct CEnv {
}
return NULL;
}
- void def(const ASymbol* sym, AST* c, const AType* t, CVal v) {
+ void def(const ASymbol* sym, const AST* c, const AType* t, CVal v) {
code.def(sym, c);
tenv.def(sym, t);
vals.def(sym, v);
}
- AST* resolve(AST* ast) {
+ const AST* resolve(const AST* ast) {
const ASymbol* sym = ast->to<const ASymbol*>();
- AST** rec = code.ref(sym);
+ const AST** rec = code.ref(sym);
return rec ? *rec : ast;
}
void setType(AST* ast, const AType* type) {
@@ -739,17 +739,17 @@ struct CEnv {
Vals vals;
Subst tsubst;
- Env<const ASymbol*, AST*> code;
+ Env<const ASymbol*, const AST*> code;
- typedef map<AFn*, CFunc> Impls;
+ typedef map<const AFn*, CFunc> Impls;
Impls impls;
- CFunc findImpl(AFn* fn, const AType* type) {
- Impls::iterator i = impls.find(fn);
+ CFunc findImpl(const AFn* fn, const AType* type) {
+ Impls::const_iterator i = impls.find(fn);
return (i != impls.end()) ? i->second : NULL;
}
- void addImpl(AFn* fn, CFunc impl) {
+ void addImpl(const AFn* fn, CFunc impl) {
impls.insert(make_pair(fn, impl));
}