summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-05-11 06:51:16 -0700
committerKaz Kylheku <kaz@kylheku.com>2021-05-11 06:51:16 -0700
commit00e87d26df9f2cd0580b48f92db8ea93a845fbf7 (patch)
tree71552622c8baddcc82c3b7f4ba53f9884e66a952 /lib.c
parent25b5bdc5dfe62037faef8f19c070de434b660aa6 (diff)
downloadtxr-00e87d26df9f2cd0580b48f92db8ea93a845fbf7.tar.gz
txr-00e87d26df9f2cd0580b48f92db8ea93a845fbf7.tar.bz2
txr-00e87d26df9f2cd0580b48f92db8ea93a845fbf7.zip
tree: support indexing and range extraction.
* lib.c (do_generic_funcall): Support tree object invocation with one or two arguments via sub and ref. (sub): Implement for trees via sub_tree. (ref): Implement for trees via tree_lookup. * tree.c (sub_tree): New function. (tree_init): Register sub-tree intrinsic. * tree.h (sub_tree): Declared. * tests/010/tree.tl: New tests. * txr.1: Documented: DWIM bracket syntax on trees, sub and ref support for trees, sub-tree function, * share/txr/stdlib/doc-syms.tl: Regenerated.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 721e6e48..3d79019b 100644
--- a/lib.c
+++ b/lib.c
@@ -7450,6 +7450,22 @@ INLINE val do_generic_funcall(val fun, struct args *args_in)
return vm_execute_toplevel(fun);
} else if (fun->co.cls == carray_s) {
goto carray;
+ } else if (fun->co.cls == tree_s) {
+ switch (args->fill) {
+ case 0:
+ callerror(fun, lit("missing required arguments"));
+ case 1:
+ switch (type(args->arg[0])) {
+ case RNG:
+ return sub(fun, args->arg[0]->rn.from, args->arg[0]->rn.to);
+ default:
+ return ref(fun, args->arg[0]);
+ }
+ case 2:
+ return sub(fun, args->arg[0], args->arg[1]);
+ default:
+ callerror(fun, lit("too many arguments"));
+ }
} else if (obj_struct_p(fun)) {
fun = method(fun, lambda_s);
break;
@@ -11410,6 +11426,8 @@ val sub(val seq, val from, val to)
case COBJ:
if (seq->co.cls == carray_s)
return carray_sub(seq, from, to);
+ if (seq->co.cls == tree_s)
+ return sub_tree(seq, from, to);
if (obj_struct_p(seq)) {
val lambda_meth = get_special_slot(seq, lambda_m);
if (lambda_meth)
@@ -11433,6 +11451,8 @@ val ref(val seq, val ind)
return gethash(seq, ind);
if (seq->co.cls == carray_s)
return carray_ref(seq, ind);
+ if (seq->co.cls == tree_s)
+ return tree_lookup(seq, ind);
if (obj_struct_p(seq)) {
val lambda_meth = get_special_slot(seq, lambda_m);
if (lambda_meth)