commit b6f853c5d4282f67a106d0e5ba90963f5525cad0
parent a9a8fc00f773543bcd569ba40f843d728b84d637
Author: Ed van Bruggen <edvb@uw.edu>
Date: Sat, 14 Apr 2018 15:54:02 -0700
math: Add INC macro to increment value if integer
Diffstat:
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/tib/math.c b/tib/math.c
@@ -4,6 +4,13 @@
#include "../tisp.h"
#include "math.h"
+#define INC(SIGN, FUNC) do { \
+ if (car(v)->t == INTEGER) \
+ i = i SIGN car(v)->v.i; \
+ else \
+ warnf(FUNC ": expected integer, recieved type [%s]", type_str(car(v)->t)); \
+} while (0)
+
static Val
prim_add(Hash env, Val args)
{
@@ -12,10 +19,7 @@ prim_add(Hash env, Val args)
if (!(v = eval_list(env, args)))
return NULL;
for (; !nilp(v); v = cdr(v))
- if (car(v)->t == INTEGER)
- i += car(v)->v.i;
- else
- warnf("+: expected integer, recieved type [%d]", car(v)->t);
+ INC(+, "+");
return mk_int(i);
}
diff --git a/tisp.c b/tisp.c
@@ -78,7 +78,7 @@ frac_reduce(int *num, int *den)
*den = *den / b;
}
-static char *
+char *
type_str(Type t)
{
switch (t) {
diff --git a/tisp.h b/tisp.h
@@ -86,6 +86,7 @@ struct Val nil;
struct Val t;
void skip_spaces(Str str);
+char *type_str(Type t);
Val hash_add(Hash ht, char *key, Val val);