-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.c
More file actions
269 lines (248 loc) · 4.88 KB
/
eval.c
File metadata and controls
269 lines (248 loc) · 4.88 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "aux.h"
#include "types/vec.h"
#include "types/arena.h"
#include "types/sexp.h"
#include "types/ht.h"
#include "read.h"
#include "compi.h"
#include "decomp.h"
#include "comp.h"
#define STACK_MAX 4096
#define FRAME_MAX 4096
#define VM_TRACE
/*;; Glorious Lisp Virtual Machine (GLVM) ;;*/
typedef enum {
OK,
COMPILE_ERR,
RUNTIME_ERR,
} EvalErr;
typedef struct {
const char *name;
size_t idx;
} Bind;
typedef struct {
Value *bsp;
uint8_t *retip;
Chunk *retchunk;
} Frame;
typedef struct {
uint8_t *ip;
Env *env;
Frame frames[FRAME_MAX];
Value stack[STACK_MAX];
Ht(Value) dynamic;
Value *sp;
Frame *fp;
} VM;
static VM vm;
static void
enter()
{
vm.fp++;
vm.fp->bsp = vm.sp;
vm.fp->retip = vm.ip;
vm.fp->retchunk = vm.env->chunk;
}
static void
leave()
{
vm.sp = vm.fp->bsp;
vm.ip = vm.fp->retip;
vm.env->chunk = vm.fp->retchunk;
vm.fp--;
}
static void
vminit()
{
vm.sp = vm.stack;
vm.ip = nil;
vm.fp = vm.frames - 1;
vm.env = nil;
ht_ini(vm.dynamic);
shadow(&vm.env);
enter();
}
static void
vmfree()
{
ht_free(vm.dynamic);
}
inline static void push(Value v) { *vm.sp++ = v; }
inline static Value pop(void) { return *(--vm.sp); }
inline static Value peek(void) { return *(vm.sp - 1); }
inline static uint8_t fetch() { return *vm.ip++; }
inline static Value imm() { return vm.env->chunk->conspool[fetch()]; }
inline static int toplevel() { return vm.fp == vm.frames; }
#define BIN_OP(op) do { \
Value b_ = pop(); \
Value a_ = pop(); \
if (DUBP(a_) || DUBP(b_)) \
push(TO_DUB(AS_NUM(a_) op AS_NUM(b_))); \
else \
push(TO_INT(AS_INT(a_) op AS_INT(b_))); \
} while (0);
static void
printstack()
{
printf(";;; STACK BEG\n");
for (Value* slot = vm.stack; slot < vm.sp; slot++) {
printf(" %s ", valuestr(*slot));
if (slot + 1 < vm.sp) putchar('|');
}
printf("\n;;; STACK END\n");
}
/*
(define x 3)
x ;; should be delete from stack or other locals won't work
(define x
(lambda (x)
x))
*/
EvalErr
run()
{
#ifdef VM_TRACE
int i = 0;
#endif
for (;;) {
#ifdef VM_TRACE
printf(";;;; [CYCLE %04i] ;;;;;\n", ++i);
decompile_op(vm.env->chunk, vm.ip - vm.env->chunk->code);
printstack();
printf(";; EXECUTING...\n");
#endif
uint8_t opcode;
switch (opcode = fetch()) {
case OP_BIND: {
size_t slot = AS_INT(imm());
Value val = peek();
vm.fp->bsp[slot] = val;
break;
}
case OP_LOAD: {
size_t slot = AS_INT(imm());
push(vm.fp->bsp[slot]);
break;
}
case OP_PUSH: {
Value val = imm();
push(val);
break;
}
case OP_NEG:{
Value val = pop();
if (ASSERTV(NUMP, val)) break;
if INTP(val) push(TO_INT(-AS_INT(val)));
else if DUBP(val) push(TO_DUB(-AS_DUB(val)));
break;
}
case OP_POP: pop(); break;
case OP_JMP: {
int off = fetch();
vm.ip += off;
break;
} case OP_JF: {
Value a = pop();
if (IS_F(a)) {
int off = fetch();
vm.ip += off;
break;
}
fetch();
break;
} case OP_EQ: {
Value a = pop();
Value b = pop();
if (AS_INT(a) == AS_INT(b)) {
push(T);
break;
}
push(F);
break;
} case OP_ADD: BIN_OP(+); break;
case OP_SUB: BIN_OP(-); break;
case OP_MUL: BIN_OP(*); break;
case OP_DIV: BIN_OP(/); break;
case OP_CALL: {
Value fun = pop();
enter();
vm.ip = AS_FUN(fun)->body->code;
vm.env->chunk = AS_FUN(fun)->body;
vm.fp->bsp -= AS_FUN(fun)->arrity;
break;
}
case OP_RET: {
if (toplevel()) {
printf("; OK\n");
puts(valuestr(pop()));
return OK;
}
printf("; RETURNING\n");
Value res = peek();
leave();
push(res);
break;
}
default: assert(0 && "unreachable");
}
#ifdef VM_TRACE
printf("\n;; OK\n");
printstack();
#endif
}
}
EvalErr
eval(Sexp *sexp)
{
EvalErr err;
chunkfree(vm.env->chunk);
vm.env->chunk = chunknew();
compile(vm.env, sexp);
if (!vm.env->chunk) {
err = COMPILE_ERR;
return err;
}
/* there is bug because chunk->code is reallocated this points to wrong address */
/* if (!vm.ip) vm.ip = chunk->code; */
vm.ip = vm.env->chunk->code;
#ifdef VM_TRACE
decompile(vm.env->chunk, "EXECUTING");
#endif
err = run();
return err;
}
int main(int argc, char *argv[]) {
const char *input = argc > 1 ? argv[1] : NULL;
Reader *reader = ropen(input);
Sexp *sexp;
int err = 0;
vminit();
do {
do {
if (!input) printf("> ");
sexp = reades(reader);
if (readeof(reader)) goto EXIT;
if (readerr(reader)) {
fprintf(stderr, "; %ld: %s\n", readerrat(reader), readerr(reader));
sexpfree(sexp);
}
} while (readerr(reader));
#ifdef VM_TRACE
printf(";;; INPUT BEG\n");
printes(sexp);
printf("\n;;; INPUT END\n");
#endif
fflush(stdout);
switch (eval(sexp)) {
case COMPILE_ERR: err = EX_DATAERR; goto EXIT; break;
case RUNTIME_ERR: err = EX_SOFTWARE; goto EXIT; break;
case OK: break;
}
sexpfree(sexp);
} while (!readeof(reader));
EXIT:
sexpfree(sexp);
rclose(reader);
vmfree();
return err;
}