aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/tuplr.hpp45
1 files changed, 29 insertions, 16 deletions
diff --git a/src/tuplr.hpp b/src/tuplr.hpp
index aaa0867..57481bb 100644
--- a/src/tuplr.hpp
+++ b/src/tuplr.hpp
@@ -23,6 +23,7 @@
#define TUPLR_HPP
#include <stdarg.h>
+#include <string.h>
#include <iostream>
#include <list>
#include <map>
@@ -278,27 +279,35 @@ private:
};
/// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)"
-struct ATuple : public AST, private vector<AST*> {
- ATuple(Cursor c) : AST(c) {}
- ATuple(Cursor c, const ATuple& t) : AST(c), vector<AST*>(t) {}
- ATuple(Cursor c, AST* ast, va_list args) : AST(c) {
+struct ATuple : public AST {
+ ATuple(Cursor c) : AST(c), _len(0), _vec(0) {}
+ ATuple(Cursor c, const ATuple& t) : AST(c), _len(t._len) {
+ _vec = (AST**)malloc(sizeof(AST*) * _len);
+ memcpy(_vec, t._vec, sizeof(AST*) * _len);
+ }
+ ATuple(Cursor c, AST* ast, va_list args) : AST(c), _len(0), _vec(0) {
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(); }
+ ~ATuple() { free(_vec); }
+ void push_back(AST* ast) {
+ AST** newvec = (AST**)realloc(_vec, sizeof(AST*) * (_len + 1));
+ newvec[_len++] = ast;
+ _vec = newvec;
+ }
+ const AST* at(size_t i) const { assert(i < _len); return _vec[i]; }
+ AST*& at(size_t i) { assert(i < _len); return _vec[i]; }
+ size_t size() const { return _len; }
+ bool empty() const { return _len == 0; }
+
+ typedef AST** iterator;
+ typedef AST* const * const_iterator;
+ const_iterator begin() const { return _vec; }
+ iterator begin() { return _vec; }
+ const_iterator end() const { return _vec + _len; }
+ iterator end() { return _vec + _len; }
bool value() const { return false; }
bool operator==(const AST& rhs) const {
const ATuple* rt = rhs.to<const ATuple*>();
@@ -320,6 +329,10 @@ struct ATuple : public AST, private vector<AST*> {
void lift(CEnv& cenv) { FOREACH(iterator, t, *this) (*t)->lift(cenv); }
CValue compile(CEnv& cenv) { throw Error(loc, "tuple compiled"); }
+
+private:
+ size_t _len;
+ AST** _vec;
};
/// Type Expression, e.g. "Int", "(Fn (Int Int) Float)"