-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheval.c
219 lines (181 loc) · 4.36 KB
/
eval.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
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
#include "eval.h"
#include "continue.h"
#include "fail.h"
#include "lookup.h"
#include "jit.h"
#include "gc.h"
#define PERFORM_TODOS(_val, _todos) val = _val; todos = _todos; goto todo
#define EVAL(_expr, _e, _todos) expr = _expr; e = _e; todos = _todos; goto eval
tagged* expr;
env* e;
cont* todos;
tagged *val;
void eval_star(hash_table* d)
{
eval:
switch(TAGGED_TYPE(expr)) {
case num_type:
PERFORM_TODOS(expr, todos);
case func_type: /* literal function can appear as a result of compilation */
PERFORM_TODOS(expr, todos);
case sym_type:
PERFORM_TODOS(env_lookup((symbol*)expr, e, d), todos);
case debruijn_type:
{
# define db ((debruijn_expr*)expr)
PERFORM_TODOS(env_lookup_by_pos(db->pos, e), todos);
# undef db
}
case plus_type:
case minus_type:
case times_type:
{
# define bn ((bin_op_expr*)expr)
todos = make_right_of_bin(bn->t.type,
bn->right,
e,
todos);
EVAL(bn->left, e, todos);
# undef bn
}
case app_type:
{
# define bn ((bin_op_expr*)expr)
todos = make_right_of_app(bn->right,
e,
todos);
EVAL(bn->left, e, todos);
# undef bn
}
case lambda_type:
{
# define lam ((lambda_expr *)expr)
val = make_func(expr,
e);
PERFORM_TODOS(val, todos);
# undef lam
}
case if0_type:
{
# define if0 ((if0_expr *)expr)
todos = make_finish_if0(if0->thn,
if0->els,
e,
todos);
EVAL(if0->tst, e, todos);
# undef if0
}
}
fail("unrecognized expression");
todo:
switch (TAGGED_TYPE(todos)) {
case done_type:
return;
case right_of_bin_type:
{
# define gl ((right_of_bin *)todos)
cont* new_todos;
new_todos = make_finish_bin(gl->op, val, gl->rest);
EVAL(gl->right, gl->env, new_todos);
# undef gl
}
case finish_bin_type:
{
# define gr ((finish_bin *)todos)
tagged *l = gr->left_val;
tagged *r = val;
int ln, rn, vn;
if (TAGGED_TYPE(l) != num_type)
fail("not a number");
if (TAGGED_TYPE(r) != num_type)
fail("not a number");
ln = NUM_VAL(l);
rn = NUM_VAL(r);
switch(gr->op) {
case plus_type:
vn = ln + rn;
break;
case minus_type:
vn = ln - rn;
break;
case times_type:
vn = ln * rn;
break;
}
val = make_num(vn);
PERFORM_TODOS(val, gr->rest);
# undef gr
}
case right_of_app_type:
{
# define gl ((right_of_app *)todos)
cont* new_todos;
new_todos = make_finish_app(val, gl->rest);
EVAL(gl->right, gl->env, new_todos);
# undef gl
}
case finish_app_type:
{
# define gr ((finish_app *)todos)
# define fn (gr->left_val)
if (TAGGED_TYPE(fn) == func_type) {
# define fv ((func_val *)fn)
e = make_env(fv->lam->arg_name, val, fv->e);
if (jit(fv, d)) {
# if USE_JIT
jitted_proc code = fv->lam->code;
todos = gr->rest;
/* `code` may update `todos` */
PERFORM_TODOS(code(&todos), todos);
# endif
} else {
EVAL(fv->lam->body, e, gr->rest);
}
# undef fv
} else
fail("not a function");
# undef fn
# undef gr
}
case finish_if0_type:
{
# define gi ((finish_if0 *)todos)
if (TAGGED_TYPE(val) == num_type) {
if (NUM_VAL(val) == 0) {
EVAL(gi->thn, gi->env, gi->rest);
} else {
EVAL(gi->els, gi->env, gi->rest);
}
} else
fail("not a number");
# undef gi
}
# if USE_JIT
case right_jitted_type:
case finish_jitted_type:
{
# define gj ((jitted *)todos)
/* `gj->code` updates `todos` */
PERFORM_TODOS(gj->code(), todos);
# undef gj
}
case interp_type:
{
# define gi ((interp *)todos)
EVAL(gi->expr, e, gi->rest);
# undef gi
}
# endif
}
fail("unrecognized continuation");
}
tagged* eval(tagged* _expr, env* _e, hash_table* d)
{
enable_gc();
expr = _expr;
e = _e;
todos = make_done();
eval_star(d);
disable_gc();
return val;
}