From 894aec019d3ce82f861a5777236ac079c2f2388d Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 25 Dec 2011 00:42:19 -0800 Subject: * eval.c (eval_init): New function interned. * lib.c:x (lazy_flatten_scan, lazy_flatten_func): New static functions. (lazy_flatten): New function. * lib.h (lazy_flatten): Declared. * match.c (v_next): Use lazy_flatten instead of flatten for processing a :list source. This means that @(next :list ...) can be used to process infinite lazy lists. * txr.1: Documented lazy-flatten. --- lib.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 7240869b..a1663217 100644 --- a/lib.c +++ b/lib.c @@ -488,6 +488,62 @@ val flatten(val list) return mappend(func_n1(flatten), list); } +/* + * Return the embedded list whose car is the non-nil atom in the given nested + * list, updating the escape stack in the process. If no such atom is found in + * the list, try to retreate up the stack to find it in the surrounding + * structure, finally returning nil if nothing is found. + */ +static val lazy_flatten_scan(val list, val *escape) +{ + for (;;) { + if (list) { + val a = car(list); + if (nullp(a)) { + list = cdr(list); + } else if (atom(a)) { + return list; + } else do { + push(cdr(list), escape); + list = a; + a = car(list); + } while (consp(a)); + return list; + } else if (*escape) { + list = pop(escape); + } else { + return nil; + } + } +} + +static val lazy_flatten_func(val env, val lcons) +{ + cons_bind (list, escape, env); + val atom = car(list); + val next = lazy_flatten_scan(cdr(list), &escape); + + rplaca(lcons, atom); + rplaca(env, next); + rplacd(env, escape); + + if (next) + rplacd(lcons, make_lazy_cons(lcons_fun(lcons))); + + return nil; +} + +val lazy_flatten(val list) +{ + val escape = nil; + val next = lazy_flatten_scan(list, &escape); + + if (!next) + return nil; + + return make_lazy_cons(func_f1(cons(next, escape), lazy_flatten_func)); +} + cnum c_num(val num); val eql(val left, val right) -- cgit v1.2.3