summaryrefslogtreecommitdiffstats
path: root/match.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-11-01 19:18:57 -0800
committerKaz Kylheku <kaz@kylheku.com>2015-11-01 19:18:57 -0800
commit18dd42f65e620326bb21ffcde92004cc9705cbf8 (patch)
tree7d343914189779a0470bc74f85ba5593bab89c9e /match.c
parentaea62af9451ce1da9db494aa07cdfb0087fa1473 (diff)
downloadtxr-18dd42f65e620326bb21ffcde92004cc9705cbf8.tar.gz
txr-18dd42f65e620326bb21ffcde92004cc9705cbf8.tar.bz2
txr-18dd42f65e620326bb21ffcde92004cc9705cbf8.zip
New range type, distinct from cons cell.
* eval.c (eval_init): Register intrinsic functions rcons, rangep from and to. (eval_init): Register rangep intrinsic. * gc.c (mark_obj): Traverse RNG objects. (finalize): Handle RNG in switch. * hash.c (equal_hash, eql_hash): Hashing for for RNG objects. * lib.c (range_s, rcons_s): New symbol variables. (code2type): Handle RNG type. (eql, equal): Equality for ranges. (less_tab_init): Table extended to cover RNG. (less): Semantics defined for ranges. (rcons, rangep, from, to): New functions. (obj_init): range_s and rcons_s variables initialized. (obj_print_impl): Produce #R notation for ranges. (generic_funcall, dwim_set): Recognize range objects for indexing * lib.h (enum type): New enum member, RNG. MAXTYPE redefined to RNG value. (TYPE_SHIFT): Increased to 5 since there are now 16 type codes. (struct range): New struct type. (union obj): New member rn, of type struct range. (range_s, rcons_s, rcons, rangep, from, to): Declared. (range_bind): New macro. * parser.l (grammar): New rule for recognizing the #R sequence as HASH_R token. * parser.y (HASH_R): New terminal symbol. (range): New nonterminal symbol. (n_expr): Derives the new range symbol. The n_expr DOTDOT n_expr rule produces rcons expression rather than const. * match.c (format_field): Recognize rcons syntax in fields which is now what ranges translate to. Also recognize range object. * tests/013/maze.tl (neigh): Fix code which destructures range as a cons. That can't be done any more. * txr.1: Document ranges.
Diffstat (limited to 'match.c')
-rw-r--r--match.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/match.c b/match.c
index 98b35cbc..383cfd80 100644
--- a/match.c
+++ b/match.c
@@ -1353,7 +1353,7 @@ val format_field(val obj, val modifier, val filter, val eval_fun)
} else if (consp(item) && car(item) == dwim_s) {
val arg_expr = second(item);
- if (consp(arg_expr) && car(arg_expr) == cons_s) {
+ if (consp(arg_expr) && car(arg_expr) == range_s) {
val from = funcall1(eval_fun, second(arg_expr));
val to = funcall1(eval_fun, third(arg_expr));
@@ -1362,6 +1362,8 @@ val format_field(val obj, val modifier, val filter, val eval_fun)
val arg = funcall1(eval_fun, arg_expr);
if (bignump(arg) || fixnump(arg)) {
obj = ref(obj, arg);
+ } else if (rangep(arg)) {
+ obj = sub(obj, from(arg), to(arg));
} else {
uw_throwf(query_error_s, lit("format_field: bad index: ~s"),
arg, nao);