diff options
-rw-r--r-- | ChangeLog | 17 | ||||
-rw-r--r-- | eval.c | 4 | ||||
-rw-r--r-- | lib.c | 55 | ||||
-rw-r--r-- | lib.h | 4 | ||||
-rw-r--r-- | txr.1 | 6 |
5 files changed, 74 insertions, 12 deletions
@@ -1,5 +1,22 @@ 2011-12-03 Kaz Kylheku <kaz@kylheku.com> + Expose lazy lists in TXR Lisp. + + * eval.c (eval_init): New intrinsic functions. + + * lib.c (rplaca, rplacd, lcons_fun): New functions. + (make_lazycons): Renamed to make_lazy_cons, relocated + and turned into external function. + (lazy_stream_func, lazy_stream_cons): Follow rename + of make_lazycons. + + * lib.h (rplaca, rplacd, make_lazy_cons, lcons_fun): + Declared. + + * txr.1: Stub sections created. + +2011-12-03 Kaz Kylheku <kaz@kylheku.com> + * eval.c (uw_protect_s, return_s, return_from_s): New symbol variables. (op_unwind_protect, op_block, op_return, op_return_from): @@ -1029,8 +1029,12 @@ void eval_init(void) sethash(op_table, return_from_s, cptr((mem_t *) op_return_from)); reg_fun(cons_s, func_n2(cons)); + reg_fun(intern(lit("make-lazy-cons"), user_package), func_n1(make_lazy_cons)); + reg_fun(intern(lit("lcons-fun"), user_package), func_n1(lcons_fun)); reg_fun(car_s, func_n1(car)); reg_fun(cdr_s, func_n1(car)); + reg_fun(intern(lit("rplaca"), user_package), func_n2(rplaca)); + reg_fun(intern(lit("rplacd"), user_package), func_n2(rplacd)); reg_fun(intern(lit("first"), user_package), func_n1(car)); reg_fun(intern(lit("rest"), user_package), func_n1(cdr)); reg_fun(append_s, func_n0v(appendv)); @@ -209,6 +209,31 @@ val cdr(val cons) } } +val rplaca(val cons, val new_car) +{ + switch (type(cons)) { + case CONS: + return cons->c.car = new_car; + case LCONS: + return cons->lc.car = new_car; + default: + type_mismatch(lit("~s is not a cons"), cons, nao); + } +} + + +val rplacd(val cons, val new_car) +{ + switch (type(cons)) { + case CONS: + return cons->c.cdr = new_car; + case LCONS: + return cons->lc.cdr = new_car; + default: + type_mismatch(lit("~s is not a cons"), cons, nao); + } +} + val *car_l(val cons) { switch (type(cons)) { @@ -619,6 +644,21 @@ val cons(val car, val cdr) return obj; } +val make_lazy_cons(val func) +{ + val obj = make_obj(); + obj->lc.type = LCONS; + obj->lc.car = obj->lc.cdr = nil; + obj->lc.func = func; + return obj; +} + +val lcons_fun(val lcons) +{ + type_check(lcons, LCONS); + return lcons->lc.func; +} + val list(val first, ...) { va_list vl; @@ -2270,15 +2310,6 @@ val vec_push(val vec, val item) return fill; } -static val make_lazycons(val func) -{ - val obj = make_obj(); - obj->lc.type = LCONS; - obj->lc.car = obj->lc.cdr = nil; - obj->lc.func = func; - return obj; -} - static val lazy_stream_func(val env, val lcons) { val stream = car(env); @@ -2286,7 +2317,7 @@ static val lazy_stream_func(val env, val lcons) val ahead = get_line(stream); lcons->lc.car = next; - lcons->lc.cdr = if2(ahead, make_lazycons(lcons->lc.func)); + lcons->lc.cdr = if2(ahead, make_lazy_cons(lcons->lc.func)); lcons->lc.func = nil; if (!next || !ahead) @@ -2307,8 +2338,8 @@ val lazy_stream_cons(val stream) return nil; } - return make_lazycons(func_f1(cons(stream, cons(first, nil)), - lazy_stream_func)); + return make_lazy_cons(func_f1(cons(stream, cons(first, nil)), + lazy_stream_func)); } val lazy_str(val lst, val term, val limit) @@ -297,6 +297,8 @@ val type_check3(val obj, int, int, int); val class_check(val cobj, val class_sym); val car(val cons); val cdr(val cons); +val rplaca(val cons, val new_car); +val rplacd(val cons, val new_car); val *car_l(val cons); val *cdr_l(val cons); val first(val cons); @@ -329,6 +331,8 @@ mem_t *chk_malloc(size_t size); mem_t *chk_realloc(mem_t *, size_t size); wchar_t *chk_strdup(const wchar_t *str); val cons(val car, val cdr); +val make_lazy_cons(val func); +val lcons_fun(val lcons); val list(val first, ...); /* terminated by nao */ val consp(val obj); val nullp(val obj); @@ -4470,6 +4470,8 @@ The following are Lisp functions and variables built-in to TXR. .SS Functions cdr and rest +.SS Functions rplaca and rplacd + .SS Functions second, third, fourth, fifth and sixth .SS Function append @@ -4480,6 +4482,10 @@ The following are Lisp functions and variables built-in to TXR. .SS Function consp +.SS Function make_lazy_cons + +.SS Function lcons_fun + .SS Functions listp and proper-listp .SS Function length |