From 248a2c44e8f8e599d64640cff584a2ef5265b9d9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 27 Dec 2010 19:20:23 +0000 Subject: Make ATuple a normal LISPey cons cell (it always was anyway). git-svn-id: http://svn.drobilla.net/resp/resp@360 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/gc.cpp | 4 ---- src/lift.cpp | 2 +- src/repl.cpp | 4 ++-- src/resp.cpp | 2 +- src/resp.hpp | 54 ++++++++++++++++++++++-------------------------------- 5 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/gc.cpp b/src/gc.cpp index 5752457..4bae933 100644 --- a/src/gc.cpp +++ b/src/gc.cpp @@ -85,10 +85,6 @@ GC::collect(const Roots& roots) (*i)->mark(false); assert(!(*i)->marked()); } else { - const Tag tag = (*i)->tag(); - if (tag == T_TUPLE) - free(((ATuple*)*i)->_vec); - tlsf_free((tlsf_t*)_pool, ((char*)(*i) - sizeof(Object::Header))); _heap.erase(i); } diff --git a/src/lift.cpp b/src/lift.cpp index 4b57637..222cd09 100644 --- a/src/lift.cpp +++ b/src/lift.cpp @@ -125,7 +125,7 @@ lift_fn(CEnv& cenv, Code& code, const ATuple* fn) throw() for (ATuple::const_iterator p = fn->prot()->begin(); p != fn->prot()->end(); ++p) { const AST* paramType = (*tp++); if (is_form(paramType, "Fn")) { - const ATuple* fnType = new ATuple(cenv.tenv.var(), paramType, fnType->loc); + const ATuple* fnType = new ATuple(cenv.tenv.var(), paramType->as_tuple(), fnType->loc); paramType = tup((*p)->loc, cenv.tenv.Tup, fnType, NULL); } cenv.def((*p)->as_symbol(), *p, paramType, NULL); diff --git a/src/repl.cpp b/src/repl.cpp index 02a2a8e..81035bb 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -139,10 +139,10 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute) if (call && is_form(call, "def") && is_form(call->list_ref(2), "fn")) { val = resp_compile(cenv, call); } else { - assert(*i); const ATuple* tup = (*i)->to_tuple(); - if (!tup || (tup->tup_len() > 0)) + if (!tup || !tup->empty()) { exprs.push_back(*i); + } } } diff --git a/src/resp.cpp b/src/resp.cpp index 2621dd6..9834013 100644 --- a/src/resp.cpp +++ b/src/resp.cpp @@ -154,7 +154,7 @@ main(int argc, char** argv) while (is.good() && !is.eof()) { Cursor loc(*f); const AST* exp = cenv->penv.parse(loc, is); - if (!exp || (exp->to_tuple() && exp->to_tuple()->tup_len() == 1)) + if (!exp || (exp->to_tuple() && exp->to_tuple()->empty())) break; const AST* ast = penv.expand(exp); diff --git a/src/resp.hpp b/src/resp.hpp index f601ac7..56c24a6 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -214,23 +214,15 @@ private: /// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)" struct ATuple : public AST { - explicit ATuple(Cursor c) : AST(T_TUPLE, c), _len(0), _vec(0) {} - ATuple(const ATuple& exp) : AST(T_TUPLE, exp.loc), _len(exp._len) { - _vec = (const AST**)malloc(sizeof(AST*) * _len); - memcpy(_vec, exp._vec, sizeof(AST*) * _len); - } - ATuple(const AST* first, const AST* rest, Cursor c=Cursor()) : AST(T_TUPLE, c), _len(2) { - _vec = (const AST**)malloc(sizeof(AST*) * _len); - _vec[0] = first; - _vec[1] = rest; - } - ATuple(Cursor c, AST* ast, va_list args) : AST(T_TUPLE, c), _len(0), _vec(0) { - if (!ast) return; + explicit ATuple(Cursor c) : AST(T_TUPLE, c), _fst(0), _rst(0) {} + ATuple(const ATuple& exp) : AST(T_TUPLE, exp.loc), _fst(exp._fst), _rst(exp._rst) {} - _len = 2; - _vec = (const AST**)malloc(sizeof(AST*) * _len); - _vec[0] = ast; - _vec[1] = NULL; + ATuple(const AST* fst, const ATuple* rst, Cursor c=Cursor()) + : AST(T_TUPLE, c), _fst(fst), _rst(rst) {} + + ATuple(Cursor c, AST* ast, va_list args) + : AST(T_TUPLE, c), _fst(ast), _rst(0) { + if (!ast) return; ATuple* tail = this; for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*)) { @@ -240,11 +232,10 @@ struct ATuple : public AST { } } - const AST* head() const { assert(_len > 0); return _vec[0]; } - const AST* last() const { return _vec[_len - 1]; } - bool empty() const { return _len == 0; } + const AST* head() const { return _fst; } + const AST* last() const { return _rst; } + bool empty() const { return _fst == 0 && _rst ==0; } - size_t tup_len() const { return _len; } size_t list_len() const { size_t ret = 0; for (const_iterator i = begin(); i != end(); ++i, ++ret) {} @@ -265,14 +256,10 @@ struct ATuple : public AST { return NULL; } - void last(AST* ast) { _vec[_len - 1] = ast; } + void last(ATuple* ast) { _rst = ast; } struct const_iterator { - explicit const_iterator(const ATuple* n) : node(n) { - assert(!n || n->tup_len() == 0 || n->tup_len() == 2); - if (!n || n->tup_len() == 0) - node = NULL; - } + explicit const_iterator(const ATuple* n) : node(n) {} inline void increment() { if (node->last()) node = node->last()->as_tuple(); @@ -291,7 +278,7 @@ struct ATuple : public AST { return ret; } inline bool operator==(const const_iterator& i) const { - return node == i.node || (!node && i.node->tup_len() == 0); + return node == i.node; } inline bool operator!=(const const_iterator& i) const { return !operator==(i); @@ -300,7 +287,7 @@ struct ATuple : public AST { const ATuple* node; }; - const_iterator begin() const { assert(_len == 0 || _len == 2); return const_iterator(this); } + const_iterator begin() const { if (empty()) return end(); return const_iterator(this); } const_iterator end() const { return const_iterator(NULL); } const_iterator iter_at(unsigned index) const { @@ -316,9 +303,8 @@ struct ATuple : public AST { const ATuple* prot() const { return list_ref(1)->as_tuple(); } private: - friend class GC; - size_t _len; - const AST** _vec; + const AST* _fst; + const ATuple* _rst; }; inline ATuple* tup(Cursor c, AST* ast, ...) { @@ -352,7 +338,11 @@ list_contains(const ATuple* head, const AST* child) { inline bool list_equals(const ATuple* lhs, const ATuple* rhs) { - if (!rhs || rhs->tup_len() != lhs->tup_len()) return false; + if (lhs == rhs) + return true; + else if (!lhs || !rhs) + return false; + ATuple::const_iterator l = lhs->begin(); FOREACHP(ATuple::const_iterator, r, rhs) if (!(*(*l++) == *(*r))) -- cgit v1.2.1