tisp

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

commit 94d9e23c3a1bdfa52511c5f66280ad39ce06acaf
parent 1af37476220c8841a4d06b869cc1efb5ee23ac2b
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Wed,  9 Jan 2019 23:51:34 -0800

Make types flags to support composite number type

Diffstat:
tisp.c | 15+++++++++------
tisp.h | 33+++++++++++++++++----------------
2 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/tisp.c b/tisp.c @@ -44,13 +44,16 @@ type_str(Type t) case NIL: return "nil"; case INTEGER: return "integer"; case DOUBLE: return "double"; - case RATIONAL: return "rational"; + case RATIO: return "ratio"; case STRING: return "string"; case SYMBOL: return "symbol"; case PRIMITIVE: return "primitive"; case FUNCTION: return "function"; case PAIR: return "pair"; - default: return "invalid"; + default: + if (t & NUMBER) + return "number"; + return "invalid"; } } @@ -153,7 +156,7 @@ vals_eq(Val a, Val b) if (a->v.d != b->v.d) return 0; break; - case RATIONAL: + case RATIO: if (a->v.r.num != b->v.r.num || a->v.r.den != b->v.r.den) return 0; break; @@ -331,7 +334,7 @@ mk_rat(int num, int den) if (den == 1) /* simplify into integer if denominator is 1 */ return mk_int(num); Val ret = emalloc(sizeof(struct Val)); - ret->t = RATIONAL; + ret->t = RATIO; ret->v.r = (Ratio){ num, den }; return ret; } @@ -567,7 +570,7 @@ tisp_eval(Env env, Val v) case NIL: case INTEGER: case DOUBLE: - case RATIONAL: + case RATIO: case STRING: return v; case SYMBOL: @@ -613,7 +616,7 @@ tisp_print(FILE *f, Val v) case DOUBLE: fprintf(f, "%.1f", v->v.d); break; - case RATIONAL: + case RATIO: fprintf(f, "%d/%d", v->v.r.num, v->v.r.den); break; case STRING: diff --git a/tisp.h b/tisp.h @@ -19,23 +19,23 @@ * 3. This notice may not be removed or altered from any source distribution. */ -#define tsp_warnf(M, ...) do { \ +#define tsp_warnf(M, ...) do { \ fprintf(stderr, "tisp:%d: error: " M "\n", \ __LINE__, ##__VA_ARGS__); \ return NULL; \ } while(0) -#define tsp_warn(M) do { \ +#define tsp_warn(M) do { \ fprintf(stderr, "tisp:%d: error: " M "\n", \ __LINE__); \ return NULL; \ } while(0) -#define tsp_arg_num(ARGS, NAME, NARGS) do { \ +#define tsp_arg_num(ARGS, NAME, NARGS) do { \ if (list_len(ARGS) != NARGS) \ - tsp_warnf(NAME ": expected %d argument%s, received %d", \ + tsp_warnf(NAME ": expected %d argument%s, received %d", \ NARGS, NARGS > 1 ? "s" : "", list_len(ARGS)); \ } while(0) -#define tsp_arg_type(ARG, NAME, TYPE) do { \ - if (ARG->t != TYPE) \ +#define tsp_arg_type(ARG, NAME, TYPE) do { \ + if (!(ARG->t & TYPE)) \ tsp_warnf(NAME ": expected %s, received %s", type_str(TYPE), type_str(ARG->t)); \ } while(0) @@ -88,17 +88,18 @@ typedef struct { } Pair; typedef enum { - NONE, - NIL, - INTEGER, - RATIONAL, - DOUBLE, - STRING, - SYMBOL, - PRIMITIVE, - FUNCTION, - PAIR, + NONE = 1 << 0, + NIL = 1 << 1, + INTEGER = 1 << 2, + RATIO = 1 << 3, + DOUBLE = 1 << 4, + STRING = 1 << 5, + SYMBOL = 1 << 6, + PRIMITIVE = 1 << 7, + FUNCTION = 1 << 8, + PAIR = 1 << 9, } Type; +static Type const NUMBER = INTEGER | DOUBLE | RATIO; struct Val { Type t;