-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.c
91 lines (83 loc) · 1.37 KB
/
repl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "dat.h"
#include "fn.h"
#include <setjmp.h>
#include <stdarg.h>
#include <stdlib.h>
jmp_buf *errptr;
void
panic(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
void
error(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "ERROR => ");
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
longjmp(*errptr, 1);
exit(1);
}
/* remove all frames except Top */
static void
clearenv(Object *env)
{
env->bp->cdr = &Nil;
env->bp->car->block = &Top;
env->sp = env->bp;
env->retval = &Nil;
}
static void
repl(Object *env, FILE *f, char *pre)
{
jmp_buf err;
errptr = &err;
if(setjmp(err) == 1){
if(feof(f))
exit(1);
clearenv(env);
skipline(f);
}
while(1){
printf(pre);
Object *res = nextexpr(f);
res = eval(env, res);
printexpr(res);
}
}
static void
readlib(FILE *f, Object *env)
{
jmp_buf buf;
errptr = &buf;
if(setjmp(buf) == 1)
return;
while(1){
eval(env, nextexpr(f));
}
panic("unreachable");
errptr = 0;
}
void
lispmain(char *argv[])
{
Object *frame = newframe(gc, &Top, &Nil, &Nil, &Top);
Object *cons = newcons(gc, frame, &Nil);
Object *env = newenv(gc, cons, cons, cons);
for(; *argv; ++argv){
FILE *f = fopen(*argv, "r");
if(f == 0)
panic("can't open %s'", *argv);
readlib(f, env);
fclose(f);
}
repl(env, stdin, ">> ");
}