commit 1586593b802e2c410f4233fa34c68a3c7b9a47f7
parent 9716958fb1b384b7082a34793255fe724979f549
Author: Ed van Bruggen <edvb@uw.edu>
Date: Wed, 17 Oct 2018 15:52:19 -0700
Revert some changes to remove static functions
Diffstat:
4 files changed, 50 insertions(+), 35 deletions(-)
diff --git a/main.c b/main.c
@@ -79,7 +79,7 @@ main(int argc, char *argv[])
putchar('\n');
if (!str.d) continue;
- skip_spaces(&str);
+ skip_ws(&str);
if (!*str.d) break;
}
diff --git a/tib/math.c b/tib/math.c
@@ -3,6 +3,18 @@
#include "../tisp.h"
+#define warnf(M, ...) do { \
+ fprintf(stderr, "tisp:%d: error: " M "\n", \
+ __LINE__, ##__VA_ARGS__); \
+ return NULL; \
+} while(0)
+#define warn(M) do { \
+ fprintf(stderr, "tisp:%d: error: " M "\n", \
+ __LINE__); \
+ return NULL; \
+} while(0)
+
+
#define INC(SIGN, FUNC) do { \
if (car(v)->t == INTEGER) \
i = i SIGN car(v)->v.i; \
@@ -15,7 +27,7 @@ prim_add(Env env, Val args)
{
Val v;
int i = 0;
- if (!(v = eval_list(env, args)))
+ if (!(v = tisp_eval_list(env, args)))
return NULL;
for (; !nilp(v); v = cdr(v))
INC(+, "+");
@@ -27,7 +39,7 @@ prim_sub(Env env, Val args)
{
Val v;
int i = 0;
- if (!(v = eval_list(env, args)))
+ if (!(v = tisp_eval_list(env, args)))
return NULL;
INC(+, "-");
v = cdr(v);
@@ -48,7 +60,7 @@ static Val \
prim_##NAME(Env env, Val args) \
{ \
Val v; \
- if (!(v = eval_list(env, args))) \
+ if (!(v = tisp_eval_list(env, args))) \
return NULL; \
if (list_len(v) != 2) \
return env->t; \
@@ -65,10 +77,10 @@ PRIM_COMPARE(gte, >=, ">=")
void
tib_env_math(Env env)
{
- hash_add(env->h, "+", mk_prim(prim_add));
- hash_add(env->h, "-", mk_prim(prim_sub));
- hash_add(env->h, "<", mk_prim(prim_lt));
- hash_add(env->h, ">", mk_prim(prim_gt));
- hash_add(env->h, "<=", mk_prim(prim_lte));
- hash_add(env->h, ">=", mk_prim(prim_gte));
+ tisp_env_add(env, "+", mk_prim(prim_add));
+ tisp_env_add(env, "-", mk_prim(prim_sub));
+ tisp_env_add(env, "<", mk_prim(prim_lt));
+ tisp_env_add(env, ">", mk_prim(prim_gt));
+ tisp_env_add(env, "<=", mk_prim(prim_lte));
+ tisp_env_add(env, ">=", mk_prim(prim_gte));
}
diff --git a/tisp.c b/tisp.c
@@ -64,6 +64,16 @@ type_str(Type t)
}
}
+int
+list_len(Val v)
+{
+ int len = 0;
+ Val a;
+ for (a = v; a->t == PAIR; a = cdr(a))
+ len++;
+ return a->t == NIL ? len : -1;
+}
+
static void
die(const char *fmt, ...)
{
@@ -109,8 +119,8 @@ erealloc(void *p, size_t size)
return p;
}
-static void
-skip_spaces(Str str)
+void
+skip_ws(Str str)
{
str->d += strspn(str->d, " \t\n"); /* skip white space */
for (; *str->d == ';'; str->d++) /* skip comments until newline */
@@ -121,17 +131,7 @@ static int
issym(char c)
{
return BETWEEN(c, 'a', 'z') || BETWEEN(c, 'A', 'Z') ||
- BETWEEN(c, '0', '9') || strchr("+-*/=<>?", c);
-}
-
-static int
-list_len(Val v)
-{
- int len = 0;
- Val a;
- for (a = v; a->t == PAIR; a = cdr(a))
- len++;
- return a->t == NIL ? len : -1;
+ BETWEEN(c, '0', '9') || strchr("_+-*/=<>?", c);
}
static int
@@ -395,7 +395,7 @@ read_num(Str str)
return mk_rat(num, read_int(str));
}
-Val
+static Val
read_str(Str str)
{
int len = 0;
@@ -428,23 +428,23 @@ read_list(Env env, Str str)
int n = 0;
Val *a = emalloc(sizeof(Val)), b;
str->d++;
- skip_spaces(str);
+ skip_ws(str);
while (*str->d && *str->d != ')') {
a = erealloc(a, (n+1) * sizeof(Val)); /* TODO realloc less */
a[n++] = tisp_read(env, str);
- skip_spaces(str);
+ skip_ws(str);
}
b = mk_list(env, n, a);
free(a);
str->d++;
- skip_spaces(str);
+ skip_ws(str);
return b;
}
Val
tisp_read(Env env, Str str)
{
- skip_spaces(str);
+ skip_ws(str);
if (isdigit(*str->d) || ((*str->d == '-' || *str->d == '+') && isdigit(str->d[1])))
return read_num(str);
if (*str->d == '"')
@@ -460,8 +460,8 @@ tisp_read(Env env, Str str)
return NULL;
}
-static Val
-eval_list(Env env, Val v)
+Val
+tisp_eval_list(Env env, Val v)
{
int cap = 1, size = 0;
Val *new = emalloc(sizeof(Val));
@@ -499,7 +499,7 @@ tisp_eval(Env env, Val v)
return (*f->v.pr)(env, args);
case FUNCTION:
/* tail call into the function body with the extended env */
- if (!(args = eval_list(env, args)))
+ if (!(args = tisp_eval_list(env, args)))
return NULL;
if (!(hash_extend(env->h, f->v.f.args, args)))
return NULL;
@@ -567,7 +567,7 @@ prim_car(Env env, Val args)
Val v;
if (list_len(args) != 1)
warnf("car: expected 1 argument, received [%d]", list_len(args));
- if (!(v = eval_list(env, args)))
+ if (!(v = tisp_eval_list(env, args)))
return NULL;
if (car(v)->t != PAIR)
warnf("car: expected list, received type [%s]", type_str(car(v)->t));
@@ -580,7 +580,7 @@ prim_cdr(Env env, Val args)
Val v;
if (list_len(args) != 1)
warnf("cdr: expected 1 argument, received [%d]", list_len(args));
- if (!(v = eval_list(env, args)))
+ if (!(v = tisp_eval_list(env, args)))
return NULL;
if (car(v)->t != PAIR)
warnf("cdr: expected list, received type [%s]", type_str(car(v)->t));
@@ -593,7 +593,7 @@ prim_cons(Env env, Val args)
Val v;
if (list_len(args) != 2)
warnf("cons: expected 2 arguments, received [%d]", list_len(args));
- if (!(v = eval_list(env, args)))
+ if (!(v = tisp_eval_list(env, args)))
return NULL;
return mk_pair(car(v), car(cdr(v)));
}
@@ -602,7 +602,7 @@ static Val
prim_eq(Env env, Val args)
{
Val v;
- if (!(v = eval_list(env, args)))
+ if (!(v = tisp_eval_list(env, args)))
return NULL;
if (nilp(v))
return env->t;
diff --git a/tisp.h b/tisp.h
@@ -98,6 +98,8 @@ struct Env {
};
char *type_str(Type t);
+int list_len(Val v);
+void skip_ws(Str str);
Val mk_int(int i);
Val mk_str(char *s);
@@ -110,6 +112,7 @@ Val mk_list(Env env, int n, Val *a);
Val tisp_read(Env env, Str str);
void tisp_print(Val v);
+Val tisp_eval_list(Env env, Val v);
Val tisp_eval(Env env, Val v);
void tisp_env_add(Env e, char *key, Val v);