summaryrefslogtreecommitdiffstats
path: root/struct.h
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2020-07-07 06:46:22 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-07-07 06:46:22 -0700
commit623ce6c9829d9352d05f6dae3204c1705be95702 (patch)
tree383bc7d6127762ced11d2e80b0dfea7c3b91054d /struct.h
parent91b24fc71e53a0b356ef97708f467b9da37fbb6b (diff)
downloadtxr-623ce6c9829d9352d05f6dae3204c1705be95702.tar.gz
txr-623ce6c9829d9352d05f6dae3204c1705be95702.tar.bz2
txr-623ce6c9829d9352d05f6dae3204c1705be95702.zip
New: protocol for iteration with structs.
* lib.c (seq_iterable): Return t if argument is a structure supporting the iter-begin method. (seq_iter_get_oop, seq_iter_peek_oop, seq_iter_get_fast_oop, seq_iter_peek_fast_oop): New static functions. (seq_iter_init_with_info): Handle COBJ case. If the COBJ is a structure which suports the iter-begin method, then retrieve the iterator object by calling it, and then prepare the iterator structure for either the fast or the canonical protocol based on whether the iterator supports iter-more. (seq_iter_mark): Mark the iter member if the iterator is a struct object. (iter_begin): Rearrange tests here to check object type first before sequence kind. If the object is a structure supporting the iter-begin method, then call it and return its value. (iter_more, iter_step): Check for struct object with corresponding special methods and return. (iter_reset): Similar change like in iter_begin. We check for the iter-reset special method and try to use it, otherwise fall back on the regular iter_begin logic. * lib.h (struct seq_iter): New member next of the ul union for caching the result of a peek operation. * struct.c (iter_begin_s, iter_more_s, iter_item_s, iter_step_s, iter_reset_s): New symbol variables; (special_sym): Pointers to new symbol variables added to array. (struct_init): New symbol variables initialized. (get_special_required_slot): New function. * struct.h (iter_begin_s, iter_more_s, iter_item_s, iter_step_s, iter_reset_s): Declared. (enum special_slot): New enum members iter_begin_m, iter_more_m, iter_item_m, iter_step_m, iter_reset_m. (get_special_required_slot): Declared. * txr.1: Documented. * tests/012/oop-seq.expected: New file. * tests/012/oop-seq.tl: New file.
Diffstat (limited to 'struct.h')
-rw-r--r--struct.h3
1 files changed, 3 insertions, 0 deletions
diff --git a/struct.h b/struct.h
index 5021c993..74ed1451 100644
--- a/struct.h
+++ b/struct.h
@@ -29,12 +29,14 @@ extern val struct_type_s, meth_s, print_s, make_struct_lit_s;
extern val init_k, postinit_k;
extern val slot_s, derived_s;
extern val lambda_set_s;
+extern val iter_begin_s, iter_more_s, iter_item_s, iter_step_s, iter_reset_s;
extern struct cobj_ops struct_inst_ops;
enum special_slot {
equal_m, nullify_m, from_list_m, lambda_m, lambda_set_m,
length_m, car_m, cdr_m, rplaca_m, rplacd_m,
+ iter_begin_m, iter_more_m, iter_item_m, iter_step_m, iter_reset_m,
num_special_slots
};
@@ -88,6 +90,7 @@ val static_slot_types(val slot);
val slot_type_reg(val slot, val strct);
val static_slot_type_reg(val slot, val strct);
val get_special_slot(val obj, enum special_slot spidx);
+val get_special_required_slot(val obj, enum special_slot spidx);
val get_special_slot_by_type(val stype, enum special_slot spidx);
INLINE int obj_struct_p(val obj) { return obj->co.ops == &struct_inst_ops; }
void struct_init(void);