diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2011-12-06 15:40:47 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2011-12-06 15:40:47 -0800 |
commit | 6f051c69587d159cafbb6bcf79ba245e8ca0d6c0 (patch) | |
tree | 9f32f483a41807787a4c6b864d65404de4fd043d /lib.c | |
parent | 6cf90c4dc732318823adf7ec96e88e9e7772d4cf (diff) | |
download | txr-6f051c69587d159cafbb6bcf79ba245e8ca0d6c0.tar.gz txr-6f051c69587d159cafbb6bcf79ba245e8ca0d6c0.tar.bz2 txr-6f051c69587d159cafbb6bcf79ba245e8ca0d6c0.zip |
* eval.c (op_unwind_protect): Fixed uninitialized variable
warning.
(eval_init): New functions registered: typeof and vector functions,
as well as length_list.
* lib.c (length): Function renamed to length_list, because it is
list specific.
(length_vec, size_vec, vector_list): New functions.
(length): New function, generic over lists, vectors and strings.
* lib.h (length_list, length_vec, size_vec, vector_list): Declared.
* match.c (h_var, h_fun, robust_length, v_deffilter, v_fun): Use
length_list instead of length.
* parser.l: Introduced # token.
* parser.y (vector): New nonterminal.
(expr): vector is a kind of expr.
(chrlist): Bugfix: single-character syntax was not working;
for instance #\x to denote the charcter x.
(lit_char_helper): Use length_list instead of length.
* stream.c (string_in_get_line): Bugfix: this was using
the wrong length function: length was being applied to a string.
The genericity of length makes that correct now, but changing
to length_str anyway.
* txr.1: Blank sections created for functions. Vector syntax
documented.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 45 |
1 files changed, 44 insertions, 1 deletions
@@ -719,7 +719,7 @@ val proper_listp(val obj) return (obj == nil) ? t : nil; } -val length(val list) +val length_list(val list) { cnum len = 0; while (consp(list)) { @@ -2310,6 +2310,31 @@ val vec_push(val vec, val item) return fill; } +val length_vec(val vec) +{ + type_check(vec, VEC); + return vec->v.vec[vec_fill]; +} + +val size_vec(val vec) +{ + type_check(vec, VEC); + return vec->v.vec[vec_alloc]; +} + +val vector_list(val list) +{ + val vec = vector(num(2)); + + if (!listp(list)) + uw_throwf(error_s, lit("vector_list: list expected, not ~s"), list, nao); + + for (; consp(list); list = cdr(list)) + vec_push(vec, car(list)); + + return vec; +} + static val lazy_stream_func(val env, val lcons) { val stream = car(env); @@ -2854,6 +2879,24 @@ val set_diff(val list1, val list2, val testfun, val keyfun) return out; } +val length(val seq) +{ + if (seq == nil) + return num(0); + else switch (type(seq)) { + case CONS: + case LCONS: + return length_list(seq); + case LIT: + case STR: + return length_str(seq); + case VEC: + return length_vec(seq); + default: + type_mismatch(lit("~s is not a sequence"), cons, nao); + } +} + val env(void) { if (env_list) { |