summaryrefslogtreecommitdiffstats
path: root/struct.c
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.c
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.c')
-rw-r--r--struct.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/struct.c b/struct.c
index e12ffcec..2b76c510 100644
--- a/struct.c
+++ b/struct.c
@@ -105,9 +105,12 @@ val slot_s, derived_s;
val nullify_s, from_list_s, lambda_set_s;
+val iter_begin_s, iter_more_s, iter_item_s, iter_step_s, iter_reset_s;
+
static val *special_sym[num_special_slots] = {
&equal_s, &nullify_s, &from_list_s, &lambda_s, &lambda_set_s,
- &length_s, &car_s, &cdr_s, &rplaca_s, &rplacd_s
+ &length_s, &car_s, &cdr_s, &rplaca_s, &rplacd_s,
+ &iter_begin_s, &iter_more_s, &iter_item_s, &iter_step_s, &iter_reset_s
};
static val struct_type_hash;
@@ -141,6 +144,12 @@ void struct_init(void)
nullify_s = intern(lit("nullify"), user_package);
from_list_s = intern(lit("from-list"), user_package);
lambda_set_s = intern(lit("lambda-set"), user_package);
+ iter_begin_s = intern(lit("iter-begin"), user_package);
+ iter_more_s = intern(lit("iter-more"), user_package);
+ iter_item_s = intern(lit("iter-item"), user_package);
+ iter_step_s = intern(lit("iter-step"), user_package);
+ iter_reset_s = intern(lit("iter-reset"), user_package);
+
struct_type_hash = make_hash(nil, nil, nil);
slot_hash = make_hash(nil, nil, t);
slot_type_hash = make_hash(nil, nil, nil);
@@ -1932,6 +1941,17 @@ val get_special_slot(val obj, enum special_slot spidx)
return get_special_static_slot(si->type, spidx, slot);
}
+val get_special_required_slot(val obj, enum special_slot spidx)
+{
+ val content = get_special_slot(obj, spidx);
+ if (content == nil) {
+ val slot = *special_sym[spidx];
+ uw_throwf(error_s, lit("~s is missing required ~s slot"),
+ obj, slot, nao);
+ }
+ return content;
+}
+
val get_special_slot_by_type(val stype, enum special_slot spidx)
{
struct struct_type *st = coerce(struct struct_type *, stype->co.handle);