summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2013-06-11 16:12:49 -0700
committerKaz Kylheku <kaz@kylheku.com>2013-06-11 16:12:49 -0700
commita6b0130ceaeadce6845d698fb68712dc2786e918 (patch)
treedc7bf64d77c90a277ed9d322ae66f4b70cfb73c0 /lib.c
parent573ac250cbf85b32f50545269c994292440d0b5d (diff)
downloadtxr-a6b0130ceaeadce6845d698fb68712dc2786e918.tar.gz
txr-a6b0130ceaeadce6845d698fb68712dc2786e918.tar.bz2
txr-a6b0130ceaeadce6845d698fb68712dc2786e918.zip
* eval.c (eval_init): lazy-str's third argument is optional.
Added lazy-stringp. Changing names of length-str-{gt,ge,lt,le} to be consistent with the >, >=, < and <= functions. * lib.c (lazy_stream_func): Greatly simplified implementation. The lazy list now continues by means of recursing via an optimized version of lazy_stream_cons called lazy_stream_cont. The environment structure is simplified to just hold the next item, rather than a pointless list. The pointless setting of lcons->lc.func to nil is also removed; this is always done by the caller. (lazy_stream_cont): New static function, similar to lazy_stream_cons, but optimized by not consing up a new function and new environment cell. (lazy_stream_cons): The environment for the update function is simplified to just a single cons. * txr.1: Documented lazy string functions and lazy-stream-cons.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/lib.c b/lib.c
index d5f5dd32..97844d32 100644
--- a/lib.c
+++ b/lib.c
@@ -3674,23 +3674,28 @@ val cat_vec(val list)
return vec;
}
-static val lazy_stream_func(val env, val lcons)
+static val lazy_stream_cont(val stream, val func, val env)
{
- val stream = car(env);
- val next = cdr(env) ? pop(cdr_l(env)) : get_line(stream);
- val ahead = get_line(stream);
-
- set(lcons->lc.car, next);
- set(lcons->lc.cdr, if2(ahead, make_lazy_cons(lcons->lc.func)));
- lcons->lc.func = nil;
+ val next = get_line(stream);
- if (!next || !ahead)
+ if (!next) {
close_stream(stream, t);
+ return nil;
+ }
+
+ rplacd(env, next);
+ return make_lazy_cons(func);
+}
+
+static val lazy_stream_func(val env, val lcons)
+{
+ val stream = car(env);
+ val prefetched_line = cdr(env);
- if (ahead)
- mpush(ahead, *cdr_l(env));
+ set(lcons->lc.car, prefetched_line);
+ set(lcons->lc.cdr, lazy_stream_cont(stream, lcons->lc.func, env));
- return next;
+ return prefetched_line;
}
val lazy_stream_cons(val stream)
@@ -3702,7 +3707,7 @@ val lazy_stream_cons(val stream)
return nil;
}
- return make_lazy_cons(func_f1(cons(stream, cons(first, nil)),
+ return make_lazy_cons(func_f1(cons(stream, first),
lazy_stream_func));
}
@@ -3872,7 +3877,7 @@ val lazy_str_get_trailing_list(val lstr, val index)
{
uses_or2;
val split_suffix = split_str(sub_str(lstr->ls.prefix, index, nil),
- or2(car(lstr->ls.opts), string(L"\n")));
+ or2(car(lstr->ls.opts), string(L"\n")));
return nappend2(split_suffix, lstr->ls.list);
}