diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2023-03-26 16:37:54 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2023-03-26 16:37:54 -0700 |
commit | ab59762e97c7f68f271fde7401d93c26bbbe7d6d (patch) | |
tree | ccde357039e09ec823b545aab369ae94308471f0 | |
parent | d152f874b7096d6642a00b8237a94b6fd9991c00 (diff) | |
download | txr-ab59762e97c7f68f271fde7401d93c26bbbe7d6d.tar.gz txr-ab59762e97c7f68f271fde7401d93c26bbbe7d6d.tar.bz2 txr-ab59762e97c7f68f271fde7401d93c26bbbe7d6d.zip |
New function: arithp.
* lib.h (arithp): Declared.
(plus_s): Existing symbol declared.
* arith.c (arithp): New function.
* struct.h (special_slot): New enum member plus_m.
* struct.c (special_sym): Register plus_s together as
the [plus_m] entry of the array.
* tests/016/arith.tl
* tests/016/ud-arith.tl: Tests for arithp.
* txr.1: Documented.
* stdlib/doc-syms.tl: Updated.
-rw-r--r-- | arith.c | 19 | ||||
-rw-r--r-- | lib.h | 2 | ||||
-rw-r--r-- | stdlib/doc-syms.tl | 1 | ||||
-rw-r--r-- | struct.c | 3 | ||||
-rw-r--r-- | struct.h | 1 | ||||
-rw-r--r-- | tests/016/arith.tl | 7 | ||||
-rw-r--r-- | tests/016/ud-arith.tl | 2 | ||||
-rw-r--r-- | txr.1 | 22 |
8 files changed, 55 insertions, 2 deletions
@@ -4362,6 +4362,23 @@ val numberp(val num) } } +val arithp(val obj) +{ + switch (type(obj)) { + case NUM: + case BGNUM: + case FLNUM: + case CHR: + case RNG: + return t; + default: + if (obj_struct_p(obj) && get_special_slot(obj, plus_m)) + return t; + } + + return nil; +} + val nary_op(val self, val (*bfun)(val, val), val (*ufun)(val self, val), struct args *args, val emptyval) @@ -4918,7 +4935,7 @@ void arith_init(void) reg_fun(intern(lit("floatp"), user_package), func_n1(floatp)); reg_fun(intern(lit("integerp"), user_package), func_n1(integerp)); reg_fun(intern(lit("numberp"), user_package), func_n1(numberp)); - + reg_fun(intern(lit("arithp"), user_package), func_n1(arithp)); reg_fun(signum_s, func_n1(signum)); @@ -689,6 +689,7 @@ extern val warning_s, defr_warning_s, restart_s, continue_s; extern val gensym_counter_s, length_s; extern val rplaca_s, rplacd_s, seq_iter_s; extern val lazy_streams_s; +extern val plus_s; #define gensym_counter (deref(lookup_var_l(nil, gensym_counter_s))) @@ -919,6 +920,7 @@ val bignump(val num); val floatp(val num); val integerp(val num); val numberp(val num); +val arithp(val obj); val nary_op(val self, val (*bfun)(val, val), val (*ufun)(val self, val), struct args *args, val emptyval); diff --git a/stdlib/doc-syms.tl b/stdlib/doc-syms.tl index 357b1e65..14d88c7a 100644 --- a/stdlib/doc-syms.tl +++ b/stdlib/doc-syms.tl @@ -125,6 +125,7 @@ ("apply" "N-026C3723") ("aret" "N-008216A8") ("arg" "N-02133AA5") + ("arithp" "N-03DAA473") ("array" "N-0117BE95") ("arraysize" "N-002129D6") ("as" "N-028B26DD") @@ -114,7 +114,8 @@ val iter_begin_s, iter_more_s, iter_item_s, iter_step_s, iter_reset_s; static val *special_sym[num_special_slots] = { &equal_s, &nullify_s, &from_list_s, &lambda_s, &lambda_set_s, &length_s, &car_s, &cdr_s, &rplaca_s, &rplacd_s, - &iter_begin_s, &iter_more_s, &iter_item_s, &iter_step_s, &iter_reset_s + &iter_begin_s, &iter_more_s, &iter_item_s, &iter_step_s, &iter_reset_s, + &plus_s }; static struct cobj_class *struct_type_cls; @@ -39,6 +39,7 @@ enum special_slot { equal_m, nullify_m, from_list_m, lambda_m, lambda_set_m, length_m, car_m, cdr_m, rplaca_m, rplacd_m, iter_begin_m, iter_more_m, iter_item_m, iter_step_m, iter_reset_m, + plus_m, num_special_slots }; diff --git a/tests/016/arith.tl b/tests/016/arith.tl index 86b5d9bb..d67b9908 100644 --- a/tests/016/arith.tl +++ b/tests/016/arith.tl @@ -405,3 +405,10 @@ (test (build (each-prod* ((i '(b c)) (j (cons 'a i))) (add (list i j)))) ((b a) (b b) (b c) (c a) (c b) (c c))) + +(mtest + (arithp #\a) t + (arithp 42) t + (arithp 3.14) t + (arithp (expt 2 200)) t + (arithp #R(nil nil)) t) diff --git a/tests/016/ud-arith.tl b/tests/016/ud-arith.tl index 052fcaed..5d07349a 100644 --- a/tests/016/ud-arith.tl +++ b/tests/016/ud-arith.tl @@ -138,3 +138,5 @@ (test (ash n 0) (ash 1 0)) (test (width n) (width 1)) (test (logcount n) (logcount 1)) + +(test (arithp n) t) @@ -48806,6 +48806,28 @@ a or .codn float . +.coNP Function @ arithp +.synb +.mets (arithp << object ) +.syne +.desc +The +.code arithp +function returns true if +.met object +is a character, integer, floating-point number, range or a user-defined arithmetic object. +For a range, +.code t +is returned without examining the values of the +.code from +and +.code to +fields. +A user-defined arithmetic object is identified as a struct type which +implements the +.code + +method as a static slot. + .coNP Functions @ zerop and @ nzerop .synb .mets (zerop << number ) |