aboutsummaryrefslogtreecommitdiffstats
path: root/src/tuplr.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2009-07-03 22:17:56 +0000
committerDavid Robillard <d@drobilla.net>2009-07-03 22:17:56 +0000
commitd3bebf39b6992814207643c238c47c3e3ceddefe (patch)
tree18959e2c752b8787fccf6174efa13108b886f992 /src/tuplr.hpp
parent22dac3110718ed92672d22f0438034c7e1b77dfd (diff)
downloadresp-d3bebf39b6992814207643c238c47c3e3ceddefe.tar.gz
resp-d3bebf39b6992814207643c238c47c3e3ceddefe.tar.bz2
resp-d3bebf39b6992814207643c238c47c3e3ceddefe.zip
Improved const correctness.
Use iterators over indices (towards non-vector ATuple). git-svn-id: http://svn.drobilla.net/resp/tuplr@176 ad02d1e2-f140-0410-9f75-f8b11f17cedd
Diffstat (limited to 'src/tuplr.hpp')
-rw-r--r--src/tuplr.hpp66
1 files changed, 45 insertions, 21 deletions
diff --git a/src/tuplr.hpp b/src/tuplr.hpp
index 03bae10..38929b5 100644
--- a/src/tuplr.hpp
+++ b/src/tuplr.hpp
@@ -151,15 +151,15 @@ struct GC {
};
typedef std::list<const Object*> Roots;
typedef std::list<Object*> Heap;
- GC(size_t pool_size);
- ~GC();
+ GC(size_t pool_size);
+ ~GC();
void* alloc(size_t size, Tag tag);
void collect(const Roots& roots);
void addRoot(const Object* obj) { assert(obj); _roots.push_back(obj); }
void lock() { _roots.insert(_roots.end(), _heap.begin(), _heap.end()); }
const Roots& roots() const { return _roots; }
private:
- void* _pool;
+ void* _pool;
Heap _heap;
Roots _roots;
};
@@ -208,13 +208,18 @@ struct AST : public Object {
virtual void lift(CEnv& cenv) {}
virtual CValue compile(CEnv& cenv) = 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 to() const { return dynamic_cast<T>(this); }
+ template<typename T> T to() { return dynamic_cast<T>(this); }
+ template<typename T> T const to() const { return dynamic_cast<T const>(this); }
template<typename T> T as() {
T t = dynamic_cast<T>(this);
if (!t) throw Error(loc, "internal error: bad cast");
return t;
}
+ template<typename T> T const as() const {
+ T const t = dynamic_cast<T const>(this);
+ if (!t) throw Error(loc, "internal error: bad cast");
+ return t;
+ }
Cursor loc;
};
@@ -254,14 +259,28 @@ private:
};
/// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)"
-struct ATuple : public AST, public vector<AST*> {
- ATuple(Cursor c, const vector<AST*>& v=vector<AST*>()) : AST(c), vector<AST*>(v) {}
+struct ATuple : public AST, private vector<AST*> {
+ ATuple(Cursor c) : AST(c) {}
+ //ATuple(Cursor c, const vector<AST*>& v=vector<AST*>()) : AST(c), vector<AST*>(v) {}
+ ATuple(Cursor c, const ATuple& t) : AST(c), vector<AST*>(t) {}
ATuple(Cursor c, AST* ast, va_list args) : AST(c) {
if (!ast) return;
push_back(ast);
for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*))
push_back(a);
}
+ void push_back(AST* ast) { vector<AST*>::push_back(ast); }
+ const AST* at(size_t i) const { return vector<AST*>::at(i); }
+ AST*& at(size_t i) { return vector<AST*>::at(i); }
+ size_t size() const { return vector<AST*>::size(); }
+ bool empty() const { return vector<AST*>::empty(); }
+
+ typedef vector<AST*>::iterator iterator;
+ typedef vector<AST*>::const_iterator const_iterator;
+ const_iterator begin() const { return vector<AST*>::begin(); }
+ iterator begin() { return vector<AST*>::begin(); }
+ const_iterator end() const { return vector<AST*>::end(); }
+ iterator end() { return vector<AST*>::end(); }
bool value() const { return false; }
bool operator==(const AST& rhs) const {
const ATuple* rt = rhs.to<const ATuple*>();
@@ -346,8 +365,8 @@ struct Subst : public list< pair<const AType*,AType*> > {
if (!in) return ast;
if (in->kind == AType::EXPR) {
AType* out = tup<AType>(in->loc, NULL);
- for (size_t i = 0; i < in->size(); ++i)
- out->push_back(apply(in->at(i)));
+ for (ATuple::iterator i = in->begin(); i != in->end(); ++i)
+ out->push_back(apply(*i));
return out;
} else {
const_iterator i = find(in);
@@ -379,7 +398,8 @@ struct AFn : public ATuple {
void lift(CEnv& cenv);
void liftCall(CEnv& cenv, const AType& argsT);
CValue compile(CEnv& cenv);
- ATuple* prot() const { return at(1)->to<ATuple*>(); }
+ const ATuple* prot() const { return at(1)->to<const ATuple*>(); }
+ ATuple* prot() { return at(1)->to<ATuple*>(); }
/// System level implementations of this (polymorphic) fn
struct Impls : public list< pair<AType*, CFunction> > {
CFunction find(AType* type) const {
@@ -408,12 +428,12 @@ struct ACall : public ATuple {
struct ADef : public ACall {
ADef(const SExp& e, const ATuple& t) : ACall(e, t) {}
ADef(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {}
- ASymbol* sym() const {
- ASymbol* sym = at(1)->to<ASymbol*>();
+ const ASymbol* sym() const {
+ const ASymbol* sym = at(1)->to<const ASymbol*>();
if (!sym) {
- ATuple* tup = at(1)->to<ATuple*>();
+ const ATuple* tup = at(1)->to<const ATuple*>();
if (tup && !tup->empty())
- return tup->at(0)->to<ASymbol*>();
+ return tup->at(0)->to<const ASymbol*>();
}
return sym;
}
@@ -436,8 +456,9 @@ struct AIf : public ACall {
struct APrimitive : public ACall {
APrimitive(const SExp& e, const ATuple& t) : ACall(e, t) {}
bool value() const {
- for (size_t i = 1; i < size(); ++i)
- if (!at(i)->value())
+ ATuple::const_iterator i = begin();
+ for (++i; i != end(); ++i)
+ if (!(*i)->value())
return false;;
return true;
}
@@ -488,10 +509,9 @@ struct PEnv : private map<const string, ASymbol*> {
}
}
ATuple parseTuple(const SExp& e) {
- ATuple ret(e.loc, vector<AST*>(e.size()));
- size_t n = 0;
+ ATuple ret(e.loc);
FOREACH(SExp::const_iterator, i, e)
- ret[n++] = parse(*i);
+ ret.push_back(parse(*i));
return ret;
}
AST* parse(const SExp& exp) {
@@ -572,7 +592,11 @@ struct TEnv : public Env< const ASymbol*, pair<AST*, AType*> > {
return ref(penv.sym(name))->second;
}
AST* resolve(AST* ast) {
- ASymbol* sym = ast->to<ASymbol*>();
+ const ASymbol* sym = ast->to<const ASymbol*>();
+ return (sym && sym->addr) ? ref(sym)->first : ast;
+ }
+ const AST* resolve(const AST* ast) {
+ const ASymbol* sym = ast->to<const ASymbol*>();
return (sym && sym->addr) ? ref(sym)->first : ast;
}
@@ -632,7 +656,7 @@ struct CEnv {
return sym->addr ? tenv.deref(sym->addr).second : NULL;
return tsubst.apply(subst.apply(tenv.vars[ast]))->to<AType*>();
}
- void def(ASymbol* sym, AST* c, AType* t, CValue v) {
+ void def(const ASymbol* sym, AST* c, AType* t, CValue v) {
tenv.def(sym, make_pair(c, t));
vals.def(sym, v);
}