From 18dd42f65e620326bb21ffcde92004cc9705cbf8 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 1 Nov 2015 19:18:57 -0800 Subject: 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. --- lib.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'lib.h') diff --git a/lib.h b/lib.h index b96ae19d..60ef6d8c 100644 --- a/lib.h +++ b/lib.h @@ -56,13 +56,13 @@ typedef int_ptr_t cnum; #endif typedef enum type { - NIL, NUM = TAG_NUM, CHR = TAG_CHR, LIT = TAG_LIT, CONS, + NIL = TAG_PTR, NUM = TAG_NUM, CHR = TAG_CHR, LIT = TAG_LIT, CONS, STR, SYM, PKG, FUN, VEC, LCONS, LSTR, COBJ, ENV, - BGNUM, FLNUM, MAXTYPE = FLNUM + BGNUM, FLNUM, RNG, MAXTYPE = RNG /* If extending, check TYPE_SHIFT */ } type_t; -#define TYPE_SHIFT 4 +#define TYPE_SHIFT 5 #define TYPE_PAIR(A, B) ((A) << TYPE_SHIFT | (B)) typedef enum functype @@ -251,6 +251,11 @@ struct flonum { double n; }; +struct range { + obj_common; + val from, to; +}; + union obj { struct any t; struct cons c; @@ -265,6 +270,7 @@ union obj { struct env e; struct bignum bn; struct flonum fl; + struct range rn; }; #if CONFIG_GEN_GC @@ -393,7 +399,7 @@ extern val null_s, t, cons_s, str_s, chr_s, fixnum_sl; extern val sym_s, pkg_s, fun_s, vec_s; extern val stream_s, hash_s, hash_iter_s, lcons_s, lstr_s, cobj_s, cptr_s; extern val atom_s, integer_s, number_s, sequence_s, string_s; -extern val env_s, bignum_s, float_s; +extern val env_s, bignum_s, float_s, range_s, rcons_s; extern val var_s, expr_s, regex_s, chset_s, set_s, cset_s, wild_s, oneplus_s; extern val nongreedy_s; extern val quote_s, qquote_s, unquote_s, splice_s; @@ -901,6 +907,10 @@ val update(val seq, val fun); val search(val seq, val key, val from, val to); val where(val func, val seq); val sel(val seq, val where); +val rcons(val from, val to); +val rangep(val obj); +val from(val range); +val to(val range); val env(void); val obj_print_impl(val obj, val out, val pretty); val obj_print(val obj, val stream); @@ -1000,6 +1010,11 @@ loc list_collect_append(loc pptail, val obj); CDR = cdr(c_o_n_s ## CAR ## CDR); \ } while (0) +#define range_bind(FROM, TO, RANGE) \ + obj_t *r_n_g ## FROM ## TO = RANGE; \ + obj_t *FROM = from(r_n_g ## FROM ## TO); \ + obj_t *TO = ((r_n_g ## FROM ## TO)->rn.to) + #define zero num_fast(0) #define one num_fast(1) #define two num_fast(2) -- cgit v1.2.3