summaryrefslogtreecommitdiffstats
path: root/lib.h
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-02-13 06:27:33 -0800
committerKaz Kylheku <kaz@kylheku.com>2019-02-13 06:27:33 -0800
commit5724a053262cc324515206180b6640ecde12addb (patch)
tree9cfa75ff02480515579afba90a1c2f7ef38d9488 /lib.h
parentdabd00c6f02b73c43cdf8c4ec760092cc5a04268 (diff)
downloadtxr-5724a053262cc324515206180b6640ecde12addb.tar.gz
txr-5724a053262cc324515206180b6640ecde12addb.tar.bz2
txr-5724a053262cc324515206180b6640ecde12addb.zip
Framework for iterating over sequences.
This has been needed for a while. While we have seq_info for classifying sequences to nicely dispatch code into various cases, those cases duplicate code. The code base could benefit from generic traversal. * lib.c (seq_iter_get_nil, seq_iter_get_list, seq_iter_get_vec, set_iter_get_hash): New static functions. (seq_iter_rewind, seq_iter_init): New functions. * lib.h (struct seq_iter, seq_iter_t): New struct type and its typedef name. (seq_iter_init, seq_iter_rewind): Declared. (seq_get): New inline function.
Diffstat (limited to 'lib.h')
-rw-r--r--lib.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib.h b/lib.h
index aab0083d..6d706b3d 100644
--- a/lib.h
+++ b/lib.h
@@ -369,6 +369,18 @@ typedef struct seq_info {
seq_kind_t kind;
} seq_info_t;
+typedef struct seq_iter {
+ seq_info_t inf;
+ union {
+ val iter;
+ cnum index;
+ } ui;
+ union {
+ cnum len;
+ } ul;
+ int (*get)(struct seq_iter *, val *pval);
+} seq_iter_t;
+
extern const seq_kind_t seq_kind_tab[MAXTYPE+1];
#define SEQ_KIND_PAIR(A, B) ((A) << 3 | (B))
@@ -517,6 +529,9 @@ val typeof(val obj);
val subtypep(val sub, val sup);
val typep(val obj, val type);
seq_info_t seq_info(val cobj);
+void seq_iter_init(val self, seq_iter_t *it, val obj);
+void seq_iter_rewind(val self, seq_iter_t *it);
+INLINE int seq_get(seq_iter_t *it, val *pval) { return it->get(it, pval); }
val throw_mismatch(val self, val obj, type_t);
INLINE val type_check(val self, val obj, type_t typecode)
{