summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2013-12-01 23:46:41 -0800
committerKaz Kylheku <kaz@kylheku.com>2013-12-01 23:46:41 -0800
commit980a1c1d78d838bbc13e90053ba90e02671878ae (patch)
treed059f1a9189b91205ebfb5118b4a9916383a7d76 /lib.c
parent360dc82e12c0031401b81f18e6b9108801277d69 (diff)
downloadtxr-980a1c1d78d838bbc13e90053ba90e02671878ae.tar.gz
txr-980a1c1d78d838bbc13e90053ba90e02671878ae.tar.bz2
txr-980a1c1d78d838bbc13e90053ba90e02671878ae.zip
Steps toward fixing an issue: lazy list readahead.
The problem is that accurate lazy lists are not suitable for real time use, where we want the TXR program to respond immediately to matching some datum. I'm implementing a simple, naive variant of lazy stream lists which simply populates the lazy cons by reading from the stream when the car or cdr fields are accessed. This type of stream can never be nil (empty list) even if the file is empty; in that case it will be (nil) and in general, it will have a spurious nil item at the end instead of ending in a string. (An adjustment was made in match.c to detect this; more will be needed.) I'm adding attributes to streams so streams can now have a "real-time" attribute. When a lazy string list is constructed over a real-time stream, the simple implementation is used. File streams are automatically real-time if (on Unix) they are tied to tty streams. Tail streams are also real-time. More work is needed to achieve the goal of this change, but this is a big step in the right direction. * configure: Detect isatty function. * lib.c (simple_lazy_stream_func): New static function. (lazy_stream_cons): Use simple implementation for real-time streams. * match.c (match_files): Do not call match_line_completely with a data line that is nil (as a result of simple lazy list over a real-time stream). A nil item in a lazy list of strings is treated as eof. * stream.c (real_time_k): New symbol variable. (struct strm_ops): New members: get_prop, set_prop. (struct stdio_handle): New member: is_real_time. (stdio_get_prop, stdio_set_prop): New static function. (stdio_ops, tail_ops, pipe_ops): stdio_get_prop and stdio_set_prop funtions wired in. (make_stdio_stream_common): Attribute streams as real-time if they are tty devices. (make_tail_stream): Tail streams are real-time attributed. (stream_set_prop, real_time_stream_p): New functions. (stream_init): Initialize real_time_k. * stream.h (real_time_k): Declared. (real_time_stream_p, stream_set_prop): Likewise.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/lib.c b/lib.c
index 6a59ecd3..48349c2e 100644
--- a/lib.c
+++ b/lib.c
@@ -3836,6 +3836,16 @@ val cat_vec(val list)
return vec;
}
+static val simple_lazy_stream_func(val stream, val lcons)
+{
+ if (set(lcons->lc.car, get_line(stream)) != nil)
+ set(lcons->lc.cdr, make_lazy_cons(lcons->lc.func));
+ else
+ lcons->lc.cdr = nil;
+
+ return nil;
+}
+
static val lazy_stream_cont(val stream, val func, val env)
{
val next = get_line(stream);
@@ -3862,15 +3872,19 @@ static val lazy_stream_func(val env, val lcons)
val lazy_stream_cons(val stream)
{
- val first = get_line(stream);
+ if (real_time_stream_p(stream)) {
+ return make_lazy_cons(func_f1(stream, simple_lazy_stream_func));
+ } else {
+ val first = get_line(stream);
- if (!first) {
- close_stream(stream, t);
- return nil;
- }
+ if (!first) {
+ close_stream(stream, t);
+ return nil;
+ }
- return make_lazy_cons(func_f1(cons(stream, first),
- lazy_stream_func));
+ return make_lazy_cons(func_f1(cons(stream, first),
+ lazy_stream_func));
+ }
}
val lazy_str(val lst, val term, val limit)