commit aeb517b7b3d04ddbd8c3bcc6d7cfebd2b710eb00
parent 46b48386b50707c4db43b1a5560c1dc01ab46943
Author: Ed van Bruggen <edvb@uw.edu>
Date: Mon, 24 Dec 2018 00:07:40 -0800
Specify file pointer to print output to
Diffstat:
3 files changed, 18 insertions(+), 18 deletions(-)
diff --git a/main.c b/main.c
@@ -75,7 +75,7 @@ main(int argc, char *argv[])
if (!(v = tisp_eval(env, v)))
continue;
- tisp_print(v);
+ tisp_print(stdout, v);
putchar('\n');
if (!str.d) continue;
diff --git a/tisp.c b/tisp.c
@@ -592,51 +592,51 @@ tisp_eval(Env env, Val v)
/* TODO return str for error msgs? */
void
-tisp_print(Val v)
+tisp_print(FILE *f, Val v)
{
switch (v->t) {
case NONE:
break;
case NIL:
- printf("()");
+ fprintf(f, "()");
break;
case INTEGER:
- printf("%d", v->v.i);
+ fprintf(f, "%d", v->v.i);
break;
case DOUBLE:
- printf("%.1f", v->v.d);
+ fprintf(f, "%.1f", v->v.d);
break;
case RATIONAL:
- printf("%d/%d", v->v.r.num, v->v.r.den);
+ fprintf(f, "%d/%d", v->v.r.num, v->v.r.den);
break;
case STRING:
- printf("\"%s\"", v->v.s);
+ fprintf(f, "\"%s\"", v->v.s);
break;
case SYMBOL:
- printf(v->v.s);
+ fprintf(f, v->v.s);
break;
case PRIMITIVE:
- printf("#<primitive>");
+ fprintf(f, "#<primitive>");
break;
case FUNCTION:
- printf("#<function>");
+ fprintf(f, "#<function>");
break;
case PAIR:
- putchar('(');
- tisp_print(car(v));
+ putc('(', f);
+ tisp_print(f, car(v));
v = cdr(v);
while (!nilp(v)) {
if (v->t == PAIR) {
- putchar(' ');
- tisp_print(car(v));
+ putc(' ', f);
+ tisp_print(f, car(v));
v = cdr(v);
} else {
- printf(" . ");
- tisp_print(v);
+ fprintf(f, " . ");
+ tisp_print(f, v);
break;
}
}
- putchar(')');
+ putc(')', f);
break;
default:
fprintf(stderr, "tisp: could not print value type [%s]", type_str(v->t));
diff --git a/tisp.h b/tisp.h
@@ -115,9 +115,9 @@ Val mk_pair(Val a, Val b);
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_print(FILE *f, Val v);
void tisp_env_add(Env e, char *key, Val v);
Env tisp_env_init(size_t cap);