tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit 6bb4b8a6345b1e78793c5e608fec31e90dcc4793
parent 99f4c4d86ec233df3f041db608722279f559c295
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat, 31 Aug 2019 21:09:05 -0700

Add int function to convert number to integer

Implement with macro that generates round functions, also replace dec
primitive implementation with same macro

Diffstat:
tibs/math.c | 39+++++++++++++++++++--------------------
1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/tibs/math.c b/tibs/math.c @@ -49,15 +49,6 @@ prim_denominator(Env env, Val args) return mk_int(den(a)); } -static Val -prim_dec(Env env, Val args) -{ - Val a; - tsp_arg_num(args, "den", 1); - EVAL_CHECK(a, car(args), "den", NUMBER); - return mk_dec(num(a)/den(a)); -} - /* wrapper functions to be returned by mk_num, all need same arguments */ static Val create_int(double num, double den) @@ -97,19 +88,26 @@ static Val return &create_int; } -#define PRIM_ROUND(NAME) \ -static Val \ -prim_##NAME(Env env, Val args) \ -{ \ - Val a; \ - tsp_arg_num(args, #NAME, 1); \ - EVAL_CHECK(a, car(args), #NAME, NUMBER); \ - return (mk_num(a->t, a->t, 0))(NAME(num(a)/den(a)), 1.); \ +#define PRIM_ROUND(NAME, FORCE) \ +static Val \ +prim_##NAME(Env env, Val args) \ +{ \ + Val a; \ + tsp_arg_num(args, #NAME, 1); \ + EVAL_CHECK(a, car(args), #NAME, NUMBER); \ + return (mk_num(a->t, a->t, FORCE))(NAME(num(a)/den(a)), 1.); \ } -PRIM_ROUND(round) -PRIM_ROUND(floor) -PRIM_ROUND(ceil) +/* define int and dec as identity functions to use them in the same macro */ +#define int(X) (X) +#define dec(X) (X) +PRIM_ROUND(int, 1) +PRIM_ROUND(dec, 2) +PRIM_ROUND(round, 0) +PRIM_ROUND(floor, 0) +PRIM_ROUND(ceil, 0) +#undef int +#undef dec static Val prim_add(Env env, Val args) @@ -260,6 +258,7 @@ tib_env_math(Env env) tsp_env_fn(numerator); tsp_env_fn(denominator); + tsp_env_fn(int); tsp_env_fn(dec); tsp_env_fn(floor); tsp_env_fn(ceil);