commit 2f0a767d555d7172d2ea24adeaf7cf5f4f3f19f7
parent f073b6bd8c731e4d6bcc16883c59345974659db4
Author: Ed van Bruggen <edvb@uw.edu>
Date: Tue, 17 Apr 2018 20:10:49 -0700
math: Add > >= < <= comparison tests
Diffstat:
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/tib/math.c b/tib/math.c
@@ -39,9 +39,35 @@ prim_sub(Hash env, Val args)
return mk_int(i);
}
+#define INT_TEST(V, FUNC) do { \
+ if (V->t != INTEGER) \
+ warnf(FUNC ": expected integer, recieved type [%s]", type_str(V->t)); \
+} while (0)
+
+#define PRIM_COMPARE(NAME, OP, FUNC) \
+static Val \
+prim_##NAME(Hash env, Val args) \
+{ \
+ Val v; \
+ if (!(v = eval_list(env, args))) \
+ return NULL; \
+ INT_TEST(car(v), FUNC); \
+ INT_TEST(car(cdr(v)), FUNC); \
+ return (car(v)->v.i OP car(cdr(v))->v.i) ? &t : &nil; \
+}
+
+PRIM_COMPARE(lt, <, "<")
+PRIM_COMPARE(gt, >, ">")
+PRIM_COMPARE(lte, <=, "<=")
+PRIM_COMPARE(gte, >=, ">=")
+
void
tib_math_env(Hash ht)
{
hash_add(ht, "+", mk_prim(prim_add));
hash_add(ht, "-", mk_prim(prim_sub));
+ hash_add(ht, "<", mk_prim(prim_lt));
+ hash_add(ht, ">", mk_prim(prim_gt));
+ hash_add(ht, "<=", mk_prim(prim_lte));
+ hash_add(ht, ">=", mk_prim(prim_gte));
}
diff --git a/tisp.c b/tisp.c
@@ -25,7 +25,7 @@ static int
issym(char c)
{
return BETWEEN(c, 'a', 'z') || BETWEEN(c, 'A', 'Z') ||
- BETWEEN(c, '0', '9') || strchr("+-*/=?", c);
+ BETWEEN(c, '0', '9') || strchr("+-*/=<>?", c);
}
static int