commit 71930bd11825b7c2bb594985ed81733f3c4f5308
parent 996ab46d9265a6984980dd74c7a9a6b0e5ee3b2a
Author: Ed van Bruggen <edvb@uw.edu>
Date: Wed, 7 Oct 2020 00:30:52 -0700
Abbreviate type consts and prefix with tsp
Diffstat:
main.c | | | 2 | +- |
tib/io.c | | | 12 | ++++++------ |
tib/math.c | | | 54 | +++++++++++++++++++++++++++--------------------------- |
tib/os.c | | | 2 | +- |
tib/string.c | | | 18 | +++++++++--------- |
tisp.c | | | 156 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
tisp.h | | | 40 | ++++++++++++++++++++-------------------- |
7 files changed, 142 insertions(+), 142 deletions(-)
diff --git a/main.c b/main.c
@@ -62,7 +62,7 @@ readstr:
} else { /* otherwise read as file */
v = tisp_eval_seq(st, st->global, tisp_parse_file(st, argv[i]));
}
- if (v && v->t != NONE) tisp_print(stdout, v);
+ if (v && v->t != TSP_NONE) tisp_print(stdout, v);
}
puts("");
diff --git a/tib/io.c b/tib/io.c
@@ -42,9 +42,9 @@ prim_write(Tsp st, Hash env, Val args)
mode = "a";
/* first argument can either be the symbol stdout or stderr,
* or the file as a string */
- if (car(v)->t == SYMBOL)
+ if (car(v)->t == TSP_SYM)
f = !strncmp(car(v)->v.s, "stdout", 7) ? stdout : stderr;
- else if (car(v)->t != STRING)
+ else if (car(v)->t != TSP_STR)
tsp_warnf("write: expected file name as string, received %s",
type_str(car(v)->t));
else if (!(f = fopen(car(v)->v.s, mode)))
@@ -73,7 +73,7 @@ prim_read(Tsp st, Hash env, Val args)
if (list_len(args) == 1) { /* if file name given as string, read it */
if (!(v = tisp_eval(st, env, car(args))))
return NULL;
- tsp_arg_type(v, "read", STRING);
+ tsp_arg_type(v, "read", TSP_STR);
fname = v->v.s;
}
if (!(file = tisp_read_file(fname)))
@@ -94,7 +94,7 @@ prim_parse(Tsp st, Hash env, Val args)
return NULL;
if (nilp(v))
return mk_pair(mk_sym(st, "quit"), st->nil);
- tsp_arg_type(v, "parse", STRING);
+ tsp_arg_type(v, "parse", TSP_STR);
st->file = v->v.s;
st->filec = 0;
v = tisp_read(st);
@@ -115,7 +115,7 @@ prim_save(Tsp st, Hash env, Val args)
tsp_arg_min(args, "save", 2);
if (!(v = tisp_eval_list(st, env, args)))
return NULL;
- tsp_arg_type(cadr(v), "save", STRING);
+ tsp_arg_type(cadr(v), "save", TSP_STR);
fname = cadr(v)->v.s;
if (!(f = fopen(fname, "wb")))
tsp_warnf("save: could not load file '%s'", fname);
@@ -142,7 +142,7 @@ prim_open(Tsp st, Hash env, Val args)
tsp_arg_min(args, "open", 1);
if (!(args = tisp_eval_list(st, env, args)))
return NULL;
- tsp_arg_type(car(args), "save", STRING);
+ tsp_arg_type(car(args), "save", TSP_STR);
fname = car(args)->v.s;
if (!(f = fopen(fname, "rb")))
tsp_warnf("save: could not load file '%s'", fname);
diff --git a/tib/math.c b/tib/math.c
@@ -36,7 +36,7 @@ prim_numerator(Tsp st, Hash vars, Val args)
{
Val a;
tsp_arg_num(args, "numerator", 1);
- EVAL_CHECK(a, car(args), "numerator", RATIONAL);
+ EVAL_CHECK(a, car(args), "numerator", TSP_RATIONAL);
return mk_int(num(a));
}
@@ -45,7 +45,7 @@ prim_denominator(Tsp st, Hash vars, Val args)
{
Val a;
tsp_arg_num(args, "denominator", 1);
- EVAL_CHECK(a, car(args), "denominator", RATIONAL);
+ EVAL_CHECK(a, car(args), "denominator", TSP_RATIONAL);
return mk_int(den(a));
}
@@ -75,15 +75,15 @@ create_rat(double num, double den)
* force arg is used to force number to one type:
* 0 -> no force, 1 -> force ratio/int, 2 -> force decimal */
static Val
-(*mk_num(Type a, Type b, int force))(double, double)
+(*mk_num(TspType a, TspType b, int force))(double, double)
{
if (force == 1)
return &create_rat;
if (force == 2)
return &create_dec;
- if (a & DECIMAL || b & DECIMAL)
+ if (a & TSP_DEC || b & TSP_DEC)
return &create_dec;
- if (a & RATIO || b & RATIO)
+ if (a & TSP_RATIO || b & TSP_RATIO)
return &create_rat;
return &create_int;
}
@@ -94,7 +94,7 @@ prim_##NAME(Tsp st, Hash vars, Val args) \
{ \
Val a; \
tsp_arg_num(args, #NAME, 1); \
- EVAL_CHECK(a, car(args), #NAME, NUMBER); \
+ EVAL_CHECK(a, car(args), #NAME, TSP_NUM); \
return (mk_num(a->t, a->t, FORCE))(NAME(num(a)/den(a)), 1.); \
}
@@ -114,9 +114,9 @@ prim_add(Tsp st, Hash vars, Val args)
{
Val a, b;
tsp_arg_num(args, "+", 2);
- EVAL_CHECK(a, car(args), "+", NUMBER);
- EVAL_CHECK(b, car(cdr(args)), "+", NUMBER);
- if (a->t & DECIMAL || b->t & DECIMAL)
+ EVAL_CHECK(a, car(args), "+", TSP_NUM);
+ EVAL_CHECK(b, car(cdr(args)), "+", TSP_NUM);
+ if (a->t & TSP_DEC || b->t & TSP_DEC)
return mk_dec((num(a)/den(a)) + (num(b)/den(b)));
return (mk_num(a->t, b->t, 0))
(num(a) * den(b) + den(a) * num(b),
@@ -130,14 +130,14 @@ prim_sub(Tsp st, Hash vars, Val args)
int len = list_len(args);
if (len != 2 && len != 1)
tsp_warnf("-: expected 1 or 2 arguments, recieved %d", len);
- EVAL_CHECK(a, car(args), "-", NUMBER);
+ EVAL_CHECK(a, car(args), "-", TSP_NUM);
if (len == 1) {
b = a;
a = mk_int(0);
} else {
- EVAL_CHECK(b, car(cdr(args)), "-", NUMBER);
+ EVAL_CHECK(b, car(cdr(args)), "-", TSP_NUM);
}
- if (a->t & DECIMAL || b->t & DECIMAL)
+ if (a->t & TSP_DEC || b->t & TSP_DEC)
return mk_dec((num(a)/den(a)) - (num(b)/den(b)));
return (mk_num(a->t, b->t, 0))
(num(a) * den(b) - den(a) * num(b),
@@ -149,9 +149,9 @@ prim_mul(Tsp st, Hash vars, Val args)
{
Val a, b;
tsp_arg_num(args, "*", 2);
- EVAL_CHECK(a, car(args), "*", NUMBER);
- EVAL_CHECK(b, car(cdr(args)), "*", NUMBER);
- if (a->t & DECIMAL || b->t & DECIMAL)
+ EVAL_CHECK(a, car(args), "*", TSP_NUM);
+ EVAL_CHECK(b, car(cdr(args)), "*", TSP_NUM);
+ if (a->t & TSP_DEC || b->t & TSP_DEC)
return mk_dec((num(a)/den(a)) * (num(b)/den(b)));
return (mk_num(a->t, b->t, 0))(num(a) * num(b), den(a) * den(b));
@@ -164,14 +164,14 @@ prim_div(Tsp st, Hash vars, Val args)
int len = list_len(args);
if (len != 2 && len != 1)
tsp_warnf("/: expected 1 or 2 arguments, recieved %d", len);
- EVAL_CHECK(a, car(args), "/", NUMBER);
+ EVAL_CHECK(a, car(args), "/", TSP_NUM);
if (len == 1) {
b = a;
a = mk_int(1);
} else {
- EVAL_CHECK(b, car(cdr(args)), "/", NUMBER);
+ EVAL_CHECK(b, car(cdr(args)), "/", TSP_NUM);
}
- if (a->t & DECIMAL || b->t & DECIMAL)
+ if (a->t & TSP_DEC || b->t & TSP_DEC)
return mk_dec((num(a)/den(a)) / (num(b)/den(b)));
return (mk_num(a->t, b->t, 1))(num(a) * den(b), den(a) * num(b));
}
@@ -181,8 +181,8 @@ prim_mod(Tsp st, Hash vars, Val args)
{
Val a, b;
tsp_arg_num(args, "mod", 2);
- EVAL_CHECK(a, car(args), "mod", INTEGER);
- EVAL_CHECK(b, car(cdr(args)), "mod", INTEGER);
+ EVAL_CHECK(a, car(args), "mod", TSP_INT);
+ EVAL_CHECK(b, car(cdr(args)), "mod", TSP_INT);
if (num(b) == 0)
tsp_warn("division by zero");
return mk_int((int)num(a) % abs((int)num(b)));
@@ -195,12 +195,12 @@ prim_pow(Tsp st, Hash vars, Val args)
double bnum, bden;
Val b, p;
tsp_arg_num(args, "pow", 2);
- EVAL_CHECK(b, car(args), "pow", EXPRESSION);
- EVAL_CHECK(p, car(cdr(args)), "pow", EXPRESSION);
+ EVAL_CHECK(b, car(args), "pow", TSP_EXPR);
+ EVAL_CHECK(p, car(cdr(args)), "pow", TSP_EXPR);
bnum = pow(num(b), num(p)/den(p));
bden = pow(den(b), num(p)/den(p));
if ((bnum == (int)bnum && bden == (int)bden) ||
- b->t & DECIMAL || p->t & DECIMAL)
+ b->t & TSP_DEC || p->t & TSP_DEC)
return mk_num(b->t, p->t, 0)(bnum, bden);
return mk_pair(mk_sym(st, "^"), mk_pair(b, mk_pair(p, st->nil)));
}
@@ -214,8 +214,8 @@ prim_##NAME(Tsp st, Hash vars, Val args) \
return NULL; \
if (list_len(v) != 2) \
return st->t; \
- tsp_arg_type(car(v), #OP, NUMBER); \
- tsp_arg_type(car(cdr(v)), #OP, NUMBER); \
+ tsp_arg_type(car(v), #OP, TSP_NUM); \
+ tsp_arg_type(car(cdr(v)), #OP, TSP_NUM); \
return ((num(car(v))*den(car(cdr(v)))) OP \
(num(car(cdr(v)))*den(car(v)))) ? \
st->t : st->nil; \
@@ -232,8 +232,8 @@ prim_##NAME(Tsp st, Hash vars, Val args) \
{ \
Val v; \
tsp_arg_num(args, #NAME, 1); \
- EVAL_CHECK(v, car(args), #NAME, EXPRESSION); \
- if (v->t & DECIMAL) \
+ EVAL_CHECK(v, car(args), #NAME, TSP_EXPR); \
+ if (v->t & TSP_DEC) \
return mk_dec(NAME(num(v))); \
return mk_pair(mk_sym(st, #NAME), mk_pair(v, st->nil)); \
}
diff --git a/tib/os.c b/tib/os.c
@@ -34,7 +34,7 @@ prim_cd(Tsp st, Hash env, Val args)
tsp_arg_num(args, "cd", 1);
if (!(v = tisp_eval(st, env, car(args))))
return NULL;
- if (!(v->t & (STRING|SYMBOL)))
+ if (!(v->t & (TSP_STR|TSP_SYM)))
tsp_warnf("strlen: expected string or symbol, received %s", type_str(v->t));
if (chdir(v->v.s)) {
perror("; tisp: error: cd");
diff --git a/tib/string.c b/tib/string.c
@@ -38,44 +38,44 @@ val_string(Tsp st, Val args, MkFn mk_fn)
for (; !nilp(args); args = cdr(args)) {
v = car(args);
switch (v->t) {
- case NONE:
+ case TSP_NONE:
len += 5;
ret = realloc(ret, len*sizeof(char));
strcat(ret, "Void");
break;
- case NIL:
+ case TSP_NIL:
len += 4;
ret = realloc(ret, len*sizeof(char));
strcat(ret, "Nil");
break;
- case INTEGER:
+ case TSP_INT:
snprintf(s, 21, "%d", (int)v->v.n.num);
len += strlen(s);
s[len] = '\0';
ret = realloc(ret, len*sizeof(char));
strcat(ret, s);
break;
- case DECIMAL:
+ case TSP_DEC:
snprintf(s, 17, "%.15g", v->v.n.num);
len += strlen(s);
s[len] = '\0';
ret = realloc(ret, len*sizeof(char));
strcat(ret, s);
break;
- case RATIO:
+ case TSP_RATIO:
snprintf(s, 43, "%d/%d", (int)v->v.n.num, (int)v->v.n.den);
len += strlen(s);
s[len] = '\0';
ret = realloc(ret, len*sizeof(char));
strcat(ret, s);
break;
- case STRING:
- case SYMBOL:
+ case TSP_STR:
+ case TSP_SYM:
len += strlen(v->v.s);
ret = realloc(ret, len*sizeof(char));
strcat(ret, v->v.s);
break;
- case PAIR:
+ case TSP_PAIR:
default:
tsp_warnf("could not convert type %s into string", type_str(v->t));
}
@@ -112,7 +112,7 @@ prim_strlen(Tsp st, Hash env, Val args)
tsp_arg_num(args, "strlen", 1);
if (!(v = tisp_eval(st, env, car(args))))
return NULL;
- if (!(v->t & (STRING|SYMBOL)))
+ if (!(v->t & (TSP_STR|TSP_SYM)))
tsp_warnf("strlen: expected string or symbol, received %s",
type_str(v->t));
return mk_int(strlen(v->v.s));
diff --git a/tisp.c b/tisp.c
@@ -77,26 +77,26 @@ erealloc(void *p, size_t size)
/* return named string for each type */
/* TODO loop through each type bit to print */
char *
-type_str(Type t)
+type_str(TspType t)
{
switch (t) {
- case NONE: return "Void";
- case NIL: return "Nil";
- case INTEGER: return "Int";
- case DECIMAL: return "Dec";
- case RATIO: return "Ratio";
- case STRING: return "Str";
- case SYMBOL: return "Sym";
- case PRIMITIVE: return "Prim";
- case FUNCTION: return "Func";
- case MACRO: return "Macro";
- case PAIR: return "Pair";
+ case TSP_NONE: return "Void";
+ case TSP_NIL: return "Nil";
+ case TSP_INT: return "Int";
+ case TSP_DEC: return "Dec";
+ case TSP_RATIO: return "Ratio";
+ case TSP_STR: return "Str";
+ case TSP_SYM: return "Sym";
+ case TSP_PRIM: return "Prim";
+ case TSP_FUNC: return "Func";
+ case TSP_MACRO: return "Macro";
+ case TSP_PAIR: return "Pair";
default:
- if (t == EXPRESSION)
+ if (t == TSP_EXPR)
return "Expr";
- if (t == RATIONAL)
+ if (t == TSP_RATIONAL)
return "Rational";
- if (t & NUMBER)
+ if (t & TSP_NUM)
return "Num";
return "Invalid";
}
@@ -158,7 +158,7 @@ int
list_len(Val v)
{
int len = 0;
- for (; v->t == PAIR; v = cdr(v))
+ for (; v->t == TSP_PAIR; v = cdr(v))
len++;
return nilp(v) ? len : -1;
}
@@ -167,17 +167,17 @@ list_len(Val v)
static int
vals_eq(Val a, Val b)
{
- if (a->t & NUMBER && b->t & NUMBER) { /* NUMBERs */
+ if (a->t & TSP_NUM && b->t & TSP_NUM) { /* NUMBERs */
if (num(a) != num(b) || den(a) != den(b))
return 0;
return 1;
}
if (a->t != b->t)
return 0;
- if (a->t == PAIR) /* PAIR */
+ if (a->t == TSP_PAIR) /* PAIR */
return vals_eq(car(a), car(b)) && vals_eq(cdr(a), cdr(b));
/* TODO function var names should not matter in comparison */
- if (a->t & (FUNCTION | MACRO)) /* FUNCTION, MACRO */
+ if (a->t & (TSP_FUNC | TSP_MACRO)) /* FUNCTION, MACRO */
return vals_eq(a->v.f.args, b->v.f.args) &&
vals_eq(a->v.f.body, b->v.f.body);
if (a != b) /* PRIMITIVE, STRING, SYMBOL, NIL, VOID */
@@ -289,18 +289,18 @@ hash_extend(Hash ht, Val args, Val vals)
{
Val arg, val;
for (; !nilp(args); args = cdr(args), vals = cdr(vals)) {
- if (args->t == PAIR) {
+ if (args->t == TSP_PAIR) {
arg = car(args);
val = car(vals);
} else {
arg = args;
val = vals;
}
- if (arg->t != SYMBOL)
+ if (arg->t != TSP_SYM)
tsp_warnf("expected symbol for argument of function"
" definition, recieved %s", type_str(arg->t));
hash_add(ht, arg->v.s, val);
- if (args->t != PAIR)
+ if (args->t != TSP_PAIR)
break;
}
return ht;
@@ -312,7 +312,7 @@ Val
mk_int(int i)
{
Val ret = emalloc(sizeof(struct Val));
- ret->t = INTEGER;
+ ret->t = TSP_INT;
num(ret) = i;
den(ret) = 1;
return ret;
@@ -322,7 +322,7 @@ Val
mk_dec(double d)
{
Val ret = emalloc(sizeof(struct Val));
- ret->t = DECIMAL;
+ ret->t = TSP_DEC;
num(ret) = d;
den(ret) = 1;
return ret;
@@ -341,7 +341,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 = RATIO;
+ ret->t = TSP_RATIO;
ret->v.n.num = num;
ret->v.n.den = den;
return ret;
@@ -355,7 +355,7 @@ mk_str(Tsp st, char *s)
if ((ret = hash_get(st->strs, s)))
return ret;
ret = emalloc(sizeof(struct Val));
- ret->t = STRING;
+ ret->t = TSP_STR;
ret->v.s = emalloc((strlen(s)+1) * sizeof(char));
strcpy(ret->v.s, s);
hash_add(st->strs, s, ret);
@@ -369,7 +369,7 @@ mk_sym(Tsp st, char *s)
if ((ret = hash_get(st->syms, s)))
return ret;
ret = emalloc(sizeof(struct Val));
- ret->t = SYMBOL;
+ ret->t = TSP_SYM;
ret->v.s = emalloc((strlen(s)+1) * sizeof(char));
strcpy(ret->v.s, s);
hash_add(st->syms, s, ret);
@@ -380,14 +380,14 @@ Val
mk_prim(Prim pr, char *name)
{
Val ret = emalloc(sizeof(struct Val));
- ret->t = PRIMITIVE;
+ ret->t = TSP_PRIM;
ret->v.pr.name = name;
ret->v.pr.pr = pr;
return ret;
}
Val
-mk_func(Type t, char *name, Val args, Val body, Hash env)
+mk_func(TspType t, char *name, Val args, Val body, Hash env)
{
Val ret = emalloc(sizeof(struct Val));
ret->t = t;
@@ -402,7 +402,7 @@ Val
mk_pair(Val a, Val b)
{
Val ret = emalloc(sizeof(struct Val));
- ret->t = PAIR;
+ ret->t = TSP_PAIR;
car(ret) = a;
cdr(ret) = b;
return ret;
@@ -666,7 +666,7 @@ tisp_eval_list(Tsp st, Hash env, Val v)
Val cur = mk_pair(NULL, st->none);
Val ret = cur, ev;
for (; !nilp(v); v = cdr(v), cur = cdr(cur)) {
- if (v->t != PAIR) {
+ if (v->t != TSP_PAIR) {
if (!(ev = tisp_eval(st, env, v)))
return NULL;
cdr(cur) = ev;
@@ -685,7 +685,7 @@ Val
tisp_eval_seq(Tsp st, Hash env, Val v)
{
Val ret = st->none;
- for (; v->t == PAIR; v = cdr(v))
+ for (; v->t == TSP_PAIR; v = cdr(v))
if (!(ret = tisp_eval(st, env, car(v))))
return NULL;
return nilp(v) ? ret : tisp_eval(st, env, v);
@@ -698,7 +698,7 @@ prepend_bt(Tsp st, Hash env, Val f)
return;
for (; env->next; env = env->next) ; /* bt var located at base env */
Entry e = entry_get(env, "bt");
- if (e->val->t == PAIR && car(e->val)->t == SYMBOL &&
+ if (e->val->t == TSP_PAIR && car(e->val)->t == TSP_SYM &&
!strncmp(f->v.f.name, car(e->val)->v.s, strlen(car(e->val)->v.s)))
return; /* don't record same function on recursion */
e->val = mk_pair(mk_sym(st, f->v.f.name), e->val);
@@ -711,14 +711,14 @@ eval_proc(Tsp st, Hash env, Val f, Val args)
Val ret;
Hash e;
switch (f->t) {
- case PRIMITIVE:
+ case TSP_PRIM:
return (*f->v.pr.pr)(st, env, args);
- case FUNCTION:
+ case TSP_FUNC:
/* tail call into the function body with the extended env */
if (!(args = tisp_eval_list(st, env, args)))
return NULL;
/* FALLTHROUGH */
- case MACRO:
+ case TSP_MACRO:
tsp_arg_num(args, f->v.f.name ? f->v.f.name : "anonymous",
list_len(f->v.f.args));
e = hash_new(8, f->v.f.env);
@@ -727,7 +727,7 @@ eval_proc(Tsp st, Hash env, Val f, Val args)
return NULL;
if (!(ret = tisp_eval_seq(st, e, f->v.f.body)))
return prepend_bt(st, env, f), NULL;
- if (f->t == MACRO)
+ if (f->t == TSP_MACRO)
ret = tisp_eval(st, env, ret);
return ret;
default:
@@ -741,15 +741,15 @@ tisp_eval(Tsp st, Hash env, Val v)
{
Val f;
switch (v->t) {
- case SYMBOL:
+ case TSP_SYM:
if (!(f = hash_get(env, v->v.s)))
tsp_warnf("could not find symbol %s", v->v.s);
return f;
- case PAIR:
+ case TSP_PAIR:
if (!(f = tisp_eval(st, env, car(v))))
return NULL;
return eval_proc(st, env, f, cdr(v));
- case STRING: /* TODO string interpolation */
+ case TSP_STR: /* TODO string interpolation */
default:
return v;
}
@@ -762,42 +762,42 @@ void
tisp_print(FILE *f, Val v)
{
switch (v->t) {
- case NONE:
+ case TSP_NONE:
fputs("#<void>", f);
break;
- case NIL:
+ case TSP_NIL:
fputs("Nil", f);
break;
- case INTEGER:
+ case TSP_INT:
fprintf(f, "%d", (int)num(v));
break;
- case DECIMAL:
+ case TSP_DEC:
fprintf(f, "%.15g", num(v));
if (num(v) == (int)num(v))
fprintf(f, ".0");
break;
- case RATIO:
+ case TSP_RATIO:
fprintf(f, "%d/%d", (int)num(v), (int)den(v));
break;
- case STRING:
- case SYMBOL:
+ case TSP_STR:
+ case TSP_SYM:
fputs(v->v.s, f);
break;
- case FUNCTION:
- case MACRO:
+ case TSP_FUNC:
+ case TSP_MACRO:
fprintf(f, "#<%s%s%s>", /* if proc name is not null print it */
- v->t == FUNCTION ? "function" : "macro",
+ v->t == TSP_FUNC ? "function" : "macro",
v->v.pr.name ? ":" : "",
v->v.pr.name ? v->v.pr.name : "");
break;
- case PRIMITIVE:
+ case TSP_PRIM:
fprintf(f, "#<primitive:%s>", v->v.pr.name);
break;
- case PAIR:
+ case TSP_PAIR:
putc('(', f);
tisp_print(f, car(v));
for (v = cdr(v); !nilp(v); v = cdr(v)) {
- if (v->t == PAIR) {
+ if (v->t == TSP_PAIR) {
putc(' ', f);
tisp_print(f, car(v));
} else {
@@ -823,7 +823,7 @@ prim_car(Tsp st, Hash env, Val args)
tsp_arg_num(args, "car", 1);
if (!(v = tisp_eval_list(st, env, args)))
return NULL;
- tsp_arg_type(car(v), "car", PAIR);
+ tsp_arg_type(car(v), "car", TSP_PAIR);
return caar(v);
}
@@ -835,7 +835,7 @@ prim_cdr(Tsp st, Hash env, Val args)
tsp_arg_num(args, "cdr", 1);
if (!(v = tisp_eval_list(st, env, args)))
return NULL;
- tsp_arg_type(car(v), "cdr", PAIR);
+ tsp_arg_type(car(v), "cdr", TSP_PAIR);
return cdar(v);
}
@@ -925,14 +925,14 @@ prim_get(Tsp st, Hash env, Val args)
return NULL;
if (!(prop = tisp_eval(st, env, cadr(args))))
return NULL;
- tsp_arg_type(prop, "get", SYMBOL);
+ tsp_arg_type(prop, "get", TSP_SYM);
switch (v->t) {
- case PRIMITIVE:
+ case TSP_PRIM:
if (!strncmp(prop->v.s, "name", 4))
return mk_sym(st, v->v.pr.name);
break;
- case FUNCTION:
- case MACRO:
+ case TSP_FUNC:
+ case TSP_MACRO:
if (!strncmp(prop->v.s, "name", 4)) /* TODO fix seg fault on anon proc */
return mk_sym(st, v->v.f.name);
if (!strncmp(prop->v.s, "body", 4))
@@ -940,21 +940,21 @@ prim_get(Tsp st, Hash env, Val args)
if (!strncmp(prop->v.s, "args", 4))
return v->v.f.args;
break;
- case INTEGER:
- case RATIO:
+ case TSP_INT:
+ case TSP_RATIO:
if (!strncmp(prop->v.s, "num", 3))
return mk_int(v->v.n.num);
if (!strncmp(prop->v.s, "den", 3))
return mk_int(v->v.n.den);
break;
- case PAIR:
+ case TSP_PAIR:
if (!strncmp(prop->v.s, "car", 3))
return v->v.p.car;
if (!strncmp(prop->v.s, "cdr", 3))
return v->v.p.cdr;
break;
- case STRING:
- case SYMBOL:
+ case TSP_STR:
+ case TSP_SYM:
if (!strncmp(prop->v.s, "len", 3))
return mk_int(strlen(v->v.s));
default: break;
@@ -968,7 +968,7 @@ static Val
prim_lambda(Tsp st, Hash env, Val args)
{
tsp_arg_min(args, "lambda", 2);
- return mk_func(FUNCTION, NULL, car(args), cdr(args), env);
+ return mk_func(TSP_FUNC, NULL, car(args), cdr(args), env);
}
/* creates new tisp defined macro */
@@ -976,7 +976,7 @@ static Val
prim_macro(Tsp st, Hash env, Val args)
{
tsp_arg_min(args, "macro", 2);
- return mk_func(MACRO, NULL, car(args), cdr(args), env);
+ return mk_func(TSP_MACRO, NULL, car(args), cdr(args), env);
}
/* creates new variable of given name and value
@@ -988,21 +988,21 @@ prim_def(Tsp st, Hash env, Val args)
{
Val sym, val;
tsp_arg_min(args, "def", 1);
- if (car(args)->t == PAIR) { /* create function if given argument list */
+ if (car(args)->t == TSP_PAIR) { /* create function if given argument list */
sym = caar(args); /* first element of argument list is function name */
- if (sym->t != SYMBOL)
+ if (sym->t != TSP_SYM)
tsp_warnf("def: incorrect format,"
" expected symbol for function name, received %s",
type_str(sym->t));
- val = mk_func(FUNCTION, sym->v.s, cdar(args), cdr(args), env);
- } else if (car(args)->t == SYMBOL) { /* create variable */
+ val = mk_func(TSP_FUNC, sym->v.s, cdar(args), cdr(args), env);
+ } else if (car(args)->t == TSP_SYM) { /* create variable */
sym = car(args); /* if only symbol given, make it self evaluating */
val = nilp(cdr(args)) ? sym : tisp_eval(st, env, cadr(args));
} else tsp_warn("def: incorrect format, no variable name found");
if (!val)
return NULL;
/* set procedure name if it was previously anonymous */
- if (val->t & (FUNCTION|MACRO) && !val->v.f.name)
+ if (val->t & (TSP_FUNC|TSP_MACRO) && !val->v.f.name)
val->v.f.name = sym->v.s;
hash_add(env, sym->v.s, val);
return st->none;
@@ -1016,7 +1016,7 @@ prim_set(Tsp st, Hash env, Val args)
Hash h;
Entry e = NULL;
tsp_arg_num(args, "set!", 2);
- tsp_arg_type(car(args), "set!", SYMBOL);
+ tsp_arg_type(car(args), "set!", TSP_SYM);
if (!(val = tisp_eval(st, env, cadr(args))))
return NULL;
/* find first occurrence of symbol */
@@ -1048,7 +1048,7 @@ prim_load(Tsp st, Hash env, Val args)
tsp_arg_num(args, "load", 1);
if (!(v = tisp_eval(st, env, car(args))))
return NULL;
- tsp_arg_type(v, "load", STRING);
+ tsp_arg_type(v, "load", TSP_STR);
name = emalloc(PATH_MAX * sizeof(char));
for (int i = 0; paths[i]; i++) {
@@ -1099,8 +1099,8 @@ prim_error(Tsp st, Hash env, Val args)
return NULL;
/* TODO have error auto print function name that was pre-defined */
tsp_arg_min(v, "error", 2);
- tsp_arg_type(car(v), "error", SYMBOL);
- fprintf(stderr, "; tisp: error: %s: ", car(v)->v.s);
+ tsp_arg_type(car(v), "error", TSP_SYM);
+ fprintf(stderr, "; tisp: error: %s: ", car(v)->v.s); /* TODO specify error raised by error func */
for (v = cdr(v); !nilp(v); v = cdr(v))
tisp_print(stderr, car(v));
fputc('\n', stderr);
@@ -1133,11 +1133,11 @@ tisp_env_init(size_t cap)
st->filec = 0;
st->nil = emalloc(sizeof(struct Val));
- st->nil->t = NIL;
+ st->nil->t = TSP_NIL;
st->none = emalloc(sizeof(struct Val));
- st->none->t = NONE;
+ st->none->t = TSP_NONE;
st->t = emalloc(sizeof(struct Val));
- st->t->t = SYMBOL;
+ st->t->t = TSP_SYM;
st->t->v.s = "True";
st->global = hash_new(cap, NULL);
diff --git a/tisp.h b/tisp.h
@@ -59,7 +59,7 @@
#define cadr(P) car(cdr(P))
#define cdar(P) cdr(car(P))
#define cddr(P) cdr(cdr(P))
-#define nilp(P) ((P)->t == NIL)
+#define nilp(P) ((P)->t == TSP_NIL)
#define num(P) ((P)->v.n.num)
#define den(P) ((P)->v.n.den)
@@ -80,29 +80,29 @@ typedef struct Hash {
/* possible tisp object types */
typedef enum {
- NONE = 1 << 0,
- NIL = 1 << 1,
- INTEGER = 1 << 2,
- DECIMAL = 1 << 3,
- RATIO = 1 << 4,
- STRING = 1 << 5,
- SYMBOL = 1 << 6,
- PRIMITIVE = 1 << 7,
- FUNCTION = 1 << 8,
- MACRO = 1 << 9,
- PAIR = 1 << 10,
-} Type;
-#define RATIONAL (INTEGER | RATIO)
-#define NUMBER (RATIONAL | DECIMAL)
-/* TODO rename to math ? */
-#define EXPRESSION (NUMBER | SYMBOL | PAIR)
+ TSP_NONE = 1 << 0,
+ TSP_NIL = 1 << 1,
+ TSP_INT = 1 << 2,
+ TSP_DEC = 1 << 3,
+ TSP_RATIO = 1 << 4,
+ TSP_STR = 1 << 5,
+ TSP_SYM = 1 << 6,
+ TSP_PRIM = 1 << 7,
+ TSP_FUNC = 1 << 8,
+ TSP_MACRO = 1 << 9,
+ TSP_PAIR = 1 << 10,
+} TspType;
+#define TSP_RATIONAL (TSP_INT | TSP_RATIO)
+#define TSP_NUM (TSP_RATIONAL | TSP_DEC)
+/* TODO rename to expr type to math ? */
+#define TSP_EXPR (TSP_NUM | TSP_SYM | TSP_PAIR)
/* bultin function written in C, not tisp */
typedef Val (*Prim)(Tsp, Hash, Val);
/* tisp object */
struct Val {
- Type t; /* NONE, NIL */
+ TspType t; /* NONE, NIL */
union {
char *s; /* STRING, SYMBOL */
struct { double num, den; } n; /* NUMBER */
@@ -122,7 +122,7 @@ struct Tsp {
size_t libhc;
};
-char *type_str(Type t);
+char *type_str(TspType t);
int list_len(Val v);
Val mk_int(int i);
@@ -131,7 +131,7 @@ Val mk_rat(int num, int den);
Val mk_str(Tsp st, char *s);
Val mk_sym(Tsp st, char *s);
Val mk_prim(Prim prim, char *name);
-Val mk_func(Type t, char *name, Val args, Val body, Hash env);
+Val mk_func(TspType t, char *name, Val args, Val body, Hash env);
Val mk_pair(Val a, Val b);
Val mk_list(Tsp st, int n, Val *a);