commit 5776d3ce089594b93954b02a04ba52dac38a1660
parent 92de681c0470652695614b70aad64a75ed6cbd1b
Author: Ed van Bruggen <edvb@uw.edu>
Date: Fri, 27 Apr 2018 10:14:32 -0700
Transform nil and t into Val pointers
Diffstat:
3 files changed, 25 insertions(+), 24 deletions(-)
diff --git a/tib/math.c b/tib/math.c
@@ -44,18 +44,18 @@ prim_sub(Env env, Val args)
warnf(FUNC ": expected integer, recieved type [%s]", type_str(V->t)); \
} while (0)
-#define PRIM_COMPARE(NAME, OP, FUNC) \
-static Val \
-prim_##NAME(Env env, Val args) \
-{ \
- Val v; \
- if (!(v = eval_list(env, args))) \
- return NULL; \
- if (list_len(v) != 2) \
- return &env->t; \
- INT_TEST(car(v), FUNC); \
- INT_TEST(car(cdr(v)), FUNC); \
- return (car(v)->v.i OP car(cdr(v))->v.i) ? &env->t : &env->nil; \
+#define PRIM_COMPARE(NAME, OP, FUNC) \
+static Val \
+prim_##NAME(Env env, Val args) \
+{ \
+ Val v; \
+ if (!(v = eval_list(env, args))) \
+ return NULL; \
+ if (list_len(v) != 2) \
+ return env->t; \
+ INT_TEST(car(v), FUNC); \
+ INT_TEST(car(cdr(v)), FUNC); \
+ return (car(v)->v.i OP car(cdr(v))->v.i) ? env->t : env->nil; \
}
PRIM_COMPARE(lt, <, "<")
diff --git a/tisp.c b/tisp.c
@@ -265,7 +265,7 @@ Val
mk_list(Env env, int n, Val *a)
{
int i;
- Val b = &env->nil;
+ Val b = env->nil;
for (i = n-1; i >= 0; i--)
b = mk_pair(a[i], b);
return b;
@@ -352,7 +352,7 @@ tisp_read(Env env, Str str)
return read_str(str);
if (*str->d == '\'') {
str->d++;
- return mk_pair(mk_sym("quote"), mk_pair(tisp_read(env, str), &env->nil));
+ return mk_pair(mk_sym("quote"), mk_pair(tisp_read(env, str), env->nil));
}
if (issym(*str->d))
return read_sym(str);
@@ -506,11 +506,11 @@ prim_eq(Env env, Val args)
if (!(v = eval_list(env, args)))
return NULL;
if (nilp(v))
- return &env->t;
+ return env->t;
for (; !nilp(cdr(v)); v = cdr(v))
if (!vals_eq(car(v), car(cdr(v))))
- return &env->nil;
- return &env->t;
+ return env->nil;
+ return env->t;
}
static Val
@@ -530,7 +530,7 @@ prim_cond(Env env, Val args)
return NULL;
else if (!nilp(cond))
return tisp_eval(env, car(cdr(car(v))));
- return &env->nil;
+ return env->nil;
}
static Val
@@ -557,12 +557,14 @@ Env
tisp_env_init(size_t cap)
{
Env e = emalloc(sizeof(struct Env));
- e->nil.t = NIL;
- e->t.t = SYMBOL;
- e->t.v.s = "t";
+ e->nil = emalloc(sizeof(struct Val));
+ e->nil->t = NIL;
+ e->t = emalloc(sizeof(struct Val));
+ e->t->t = SYMBOL;
+ e->t->v.s = "t";
e->h = hash_new(cap);
- hash_add(e->h, "t", &e->t);
+ hash_add(e->h, "t", e->t);
hash_add(e->h, "car", mk_prim(prim_car));
hash_add(e->h, "cdr", mk_prim(prim_cdr));
hash_add(e->h, "cons", mk_prim(prim_cons));
diff --git a/tisp.h b/tisp.h
@@ -84,8 +84,7 @@ struct Val {
};
struct Env {
- struct Val nil;
- struct Val t;
+ Val nil, t;
Hash h;
};