aboutsummaryrefslogtreecommitdiffstats
path: root/src/tuplr.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tuplr.hpp')
-rw-r--r--src/tuplr.hpp30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/tuplr.hpp b/src/tuplr.hpp
index 7494cdb..b1bb985 100644
--- a/src/tuplr.hpp
+++ b/src/tuplr.hpp
@@ -33,7 +33,8 @@
#include <vector>
#include <boost/format.hpp>
-#define FOREACH(IT, i, c) for (IT i = (c).begin(); i != (c).end(); ++i)
+#define FOREACH(IT, i, c) for (IT i = (c).begin(); i != (c).end(); ++i)
+#define FOREACHP(IT, i, c) for (IT i = (c)->begin(); i != (c)->end(); ++i)
#define THROW_IF(cond, error, ...) { if (cond) throw Error(error, __VA_ARGS__); }
using namespace std;
@@ -243,8 +244,8 @@ struct ATuple : public AST {
}
const AST* head() const { assert(_len > 0); return _vec[0]; }
AST* head() { assert(_len > 0); return _vec[0]; }
- const AST* at(size_t i) const { assert(i < _len); return _vec[i]; }
- AST*& at(size_t i) { assert(i < _len); return _vec[i]; }
+ const AST* last() const { return _vec[_len - 1]; }
+ AST* last() { return _vec[_len - 1]; }
size_t size() const { return _len; }
bool empty() const { return _len == 0; }
@@ -259,20 +260,20 @@ struct ATuple : public AST {
const ATuple* rt = rhs.to<const ATuple*>();
if (!rt || rt->size() != size()) return false;
const_iterator l = begin();
- FOREACH(const_iterator, r, *rt)
+ FOREACHP(const_iterator, r, rt)
if (!(*(*l++) == *(*r)))
return false;
return true;
}
bool contains(AST* child) const {
if (*this == *child) return true;
- FOREACH(const_iterator, p, *this)
+ FOREACHP(const_iterator, p, this)
if (**p == *child || (*p)->contains(child))
return true;
return false;
}
void constrain(TEnv& tenv, Constraints& c) const;
- void lift(CEnv& cenv) { FOREACH(iterator, t, *this) (*t)->lift(cenv); }
+ void lift(CEnv& cenv) { FOREACHP(iterator, t, this) (*t)->lift(cenv); }
CVal compile(CEnv& cenv) { throw Error(loc, "tuple compiled"); }
@@ -288,12 +289,14 @@ struct AType : public ATuple {
AType(Cursor c) : ATuple(c), kind(EXPR), id(0) {}
AType(Cursor c, AST* ast, va_list args) : ATuple(c, ast, args), kind(EXPR), id(0) {}
CVal compile(CEnv& cenv) { 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 {
switch (kind) {
case VAR: return false;
case PRIM: return head()->str() != "Nothing";
case EXPR:
- FOREACH(const_iterator, t, *this) {
+ FOREACHP(const_iterator, t, this) {
AType* kid = (*t)->to<AType*>();
if (kid && !kid->concrete())
return false;
@@ -365,8 +368,8 @@ struct AFn : public ATuple {
AST* cps(TEnv& tenv, AST* cont);
void lift(CEnv& cenv);
CVal compile(CEnv& cenv);
- const ATuple* prot() const { return at(1)->to<const ATuple*>(); }
- ATuple* prot() { return at(1)->to<ATuple*>(); }
+ const ATuple* prot() const { return (*(begin() + 1))->to<const ATuple*>(); }
+ ATuple* prot() { return (*(begin() + 1))->to<ATuple*>(); }
/// System level implementations of this (polymorphic) fn
struct Impls : public list< pair<AType*, CFunc> > {
CFunc find(AType* type) const {
@@ -395,14 +398,17 @@ struct ADef : public ACall {
ADef(const ATuple* exp) : ACall(exp) {}
ADef(Cursor c, AST* ast, va_list args) : ACall(c, ast, args) {}
const ASymbol* sym() const {
- const ASymbol* sym = at(1)->to<const ASymbol*>();
+ const AST* name = *(begin() + 1);
+ const ASymbol* sym = name->to<const ASymbol*>();
if (!sym) {
- const ATuple* tup = at(1)->to<const ATuple*>();
+ const ATuple* tup = name->to<const ATuple*>();
if (tup && !tup->empty())
return tup->head()->to<const ASymbol*>();
}
return sym;
}
+ const AST* body() const { return *(begin() + 2); }
+ AST* body() { return *(begin() + 2); }
void constrain(TEnv& tenv, Constraints& c) const;
AST* cps(TEnv& tenv, AST* cont);
void lift(CEnv& cenv);
@@ -476,7 +482,7 @@ struct PEnv : private map<const string, ASymbol*> {
}
ATuple* parseTuple(const ATuple* e) {
ATuple* ret = new ATuple(e->loc);
- FOREACH(ATuple::const_iterator, i, *e)
+ FOREACHP(ATuple::const_iterator, i, e)
ret->push_back(parse(*i));
return ret;
}