From cd8cf4f8fd827e428c53f2e6d7fcce5cd9727e7f Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 1 Jun 2016 07:05:35 -0700 Subject: Methods for turning objects into sequences. Struct objects can now define methods car, cdr and nullify. With these, they can participate in operations on sequences. * eval.h (car_s, cdr_s): Declared. * lib.c (nullify_s): New symbol variable. (car, cdr): Implement for struct objects via, respectively, their car and cdr methods. (tolist): Handle objects by mapping through identity. (nullify): Implement for objects optionally: if an object is a struct with a nullify method, use it, otherwise go through default case of just returning the object. (empty): Implement for objects that have nullify method. (obj_init): Initialize nullify_s. * struct.c (maybe_slot): New function. * struct.h (maybe_slot): Declared. * txr.1: Documented car, cdr and nullify method convention. --- struct.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'struct.c') diff --git a/struct.c b/struct.c index 21a3a18a..2075b42f 100644 --- a/struct.c +++ b/struct.c @@ -759,6 +759,20 @@ val slot(val strct, val sym) no_such_slot(self, si->type->self, sym); } +val maybe_slot(val strct, val sym) +{ + const val self = lit("slot"); + struct struct_inst *si = struct_handle(strct, self); + + if (symbolp(sym)) { + loc ptr = lookup_slot_load(strct, si, sym); + if (!nullocp(ptr)) + return deref(ptr); + } + + return nil; +} + val slotset(val strct, val sym, val newval) { const val self = lit("slotset"); -- cgit v1.2.3