-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_generator.js
291 lines (273 loc) · 8.56 KB
/
eval_generator.js
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// [External dependencies]
// focus: visual.js
// updatePrint: visual.html
// finalizeResult: visual.html
// window.mem = {}
var locGenerator = function* () {
var cur_loc = 10000; // starting memory address
while(true)
yield cur_loc++;
}();
function new_loc() {
return locGenerator.next().value; }
function get_updated_env(x,v,env){
obj = {}
Object.assign(obj, env)
obj[x] = v;
return obj; }
function getBop(type) {
switch (type) {
case "ADD":
return function (a, b) {return a+b;};
case "SUB":
return function (a, b) {return a-b;};
case "MUL":
return function (a, b) {return a*b;};
case "DIV":
return function (a, b) {return a/b;};
} }
function unpack(vsOrNums){
function cont(c){return c.content;}
var ret = [];
for (var i = 0; i < vsOrNums.length; i++){
ret.push(cont(vsOrNums[i])); }
return ret; }
function is_empty_obj(obj){
return (Object.keys(obj).length === 0 &&
obj.constructor === Object); }
function input(msg){
return Number(prompt(msg)); }
function* evalSubExpHelper(gen, sub_results){
var next = gen.next();
var cur = null;
while (!next.done){
cur = next;
yield;
next = gen.next();}
sub_results.push(cur.value);
}
var specialSubExpCases = ["IF", "WHILE", "LETV", "LETF", "CALLV", "CALLR"];
var singleFocus = ["TRUE", "FALSE", "UNIT", "NUM", "VAR", "RECORD", "READ"];
function* evalExp(env, node){
var type = node.type;
console.log("eval node: ", type, " env", env);
var children = node.children;
var vs = unpack(node.content.vs);
var nums = unpack(node.content.nums);
if (!singleFocus.includes(type)){
focus(node, "Not available", mem, env);
yield; }
var subs = []; //results of sub expressions
if (!specialSubExpCases.includes(type)){
for (var i = 0; i < node.children.length; i++){
var gen = evalSubExpHelper(evalExp(env, children[i]), subs);
while(!gen.next().done)
yield;
} }
switch (type){
case "TRUE":
focus(node, true, mem, env);
yield true;
return;
case "FALSE":
focus(node, false, mem, env);
yield false;
return;
case "UNIT":
focus(node, null, mem, env);
yield null;
return;
case "NUM":
focus(node, nums[0], mem, env);
yield Number(nums[0]);
return; // important. finish generator
case "VAR":
focus(node, mem[env[vs[0]]], mem, env);
yield mem[env[vs[0]]];
return;
case "ADD":
case "SUB":
case "MUL":
case "DIV":
var bop = getBop(type);
console.assert(
typeof subs[0] === "number" && typeof subs[1] === "number");
var ret = bop(subs[0],subs[1]);
focus(node, ret, mem, env);
yield ret;
return;
case "EQUAL":
var ret = ( (subs[0] === subs[1]) || // n1=n2 or b1=b2
(is_empty_obj(subs[0]) && is_empty_obj(subs[0])) );
focus(node, ret, mem, env);
yield ret;
return
case "LESS":
console.assert(typeof subs[0] === "number" && typeof subs[1] === "number");
var ret = subs[0] < subs[1];
focus(node, ret, mem, env);
yield ret;
return;
case "NOT":
console.assert(typeof subs[0] === "boolean");
focus(node, !subs[0], mem, env);
yield !subs[0];
return;
case "SEQ":
focus(node, subs[1], mem, env);
yield subs[1]; // ignore subs[0]
return;
case "IF":
var gen = evalSubExpHelper(evalExp(env, children[0]), subs);
while(!gen.next().done) yield;
console.assert(typeof subs[0] === "boolean");
// When the test is true don't evalute else
// and vice versa to avoid inifinite loop
if (subs[0]) {
var gen = evalSubExpHelper(
evalExp(env, children[1]), subs);
while(!gen.next().done) yield;
focus(node, subs[1], mem, env);
yield subs[1]; }
else {
var gen = evalSubExpHelper(
evalExp(env, children[2]), subs);
while(!gen.next().done) yield;
focus(node, subs[1], mem, env); // not sub[2] because
yield subs[1]; } // true scenario not evaluated
return;
case "WHILE":
var gen = evalSubExpHelper(
evalExp(env, children[0]), subs);
while (!gen.next().done)
yield;
console.assert(typeof subs[0] === "boolean");
if (subs[0]) {
var gen = evalSubExpHelper(
evalExp(env, children[1]), subs);
while (!gen.next().done) yield;
var gen = evalSubExpHelper(
evalExp(env, node), subs);
while (!gen.next().done) yield;
focus(node, subs[2], mem, env);
yield subs[2]; } // always null(unit)
else {
focus(node, null, mem, env);
yield null;} // equivalent to unit in B langauge
return;
case "LETV":
var x = vs[0];
var gen = evalSubExpHelper(
evalExp(env, children[0]), subs);
while (!gen.next().done) yield;
var nl = new_loc();
mem[nl] = subs[0];
var gen = evalSubExpHelper(
evalExp(get_updated_env(x,nl,env), children[1]),
subs);
while (!gen.next().done) yield;
focus(node, subs[1], mem, env);
yield subs[1];
return;
case "LETF":
var fname = vs[0];
var argnames = unpack(children[0].content.vs);
var e1 = children[1]
var fbody = [argnames,e1,env];
var gen = evalSubExpHelper(
evalExp(get_updated_env(fname, fbody, env),children[2]),
subs);
while (!gen.next().done) yield;
focus(node, subs[2], mem, env);
yield subs[2];
return;
case "CALLV":
var fname = vs[0];
var args = children[0].children;
for (var i = 0; i < args.length; i++){
var gen = evalSubExpHelper(
evalExp(env, args[i]), subs);
while(!gen.next().done)
yield; }
var [argnames, f_exp, f_env] = env[fname];
for (var i = 0; i < args.length; i++){
var nl = new_loc();
f_env = get_updated_env(argnames[i], nl, f_env);
mem[nl] = subs[i]; }
var f_env = get_updated_env(fname, env[fname], f_env);
var f_returns = [];
var gen = evalSubExpHelper(
evalExp(f_env, f_exp), f_returns );
while (!gen.next().done) yield;
focus(node, f_returns[0], mem, env);
yield f_returns[0];
return;
case "CALLR":
var fname = vs[0];
var args = unpack(children[0].content.vs); // ex) ["x", "y", "z"]
var [argnames, f_exp, f_env] = env[fname];
for (var i = 0; i < argnames.length; i ++){
f_env = get_updated_env(argnames[i], env[args[i]], f_env); }
f_env = get_updated_env(fname, env[fname], f_env);
f_returns = [];
var gen = evalSubExpHelper(
evalExp(f_env, f_exp), f_returns );
while (!gen.next().done) yield;
focus(node, f_returns[0], mem, env);
yield f_returns[0];
return;
// record construction
case "RECORD":
var rec = {}
for (var i = 0; i < vs.length; i++){
var nl = new_loc();
rec[vs[i]] = nl;
mem[nl] = Number(nums[i]); }
focus(node, rec, mem, env); // need to be checked
yield rec;
return;
case "FIELD":
var rec = subs[0];
var x = vs[0];
focus(node, mem[rec[x]], mem, env);
yield mem[rec[x]];
return;
case "ASSIGN":
var x = vs[0];
var v = subs[0];
mem[env[x]] = v;
focus(node, v, mem, env);
yield v;
return;
case "ASSIGNF":
var rec = subs[0];
var x = vs[0];
var v = subs[1];
mem[rec[x]] = v;
focus(node, v, mem, env);
yield v;
return;
case "READ":
var x = vs[0];
var v = input(
"Enter the value of " + x + "\n(as an integer)");
mem[env[x]] = v;
focus(node, v, mem, env);
yield v;
return;
case "WRITE":
var n = subs[0];
updatePrint(n);
focus(node, n, mem, env);
yield n;
return;
}
}
function* eval(root){
mem = {};
var gen = evalExp({}, root);
while(!gen.next().done) yield;
finalizeResult();
yield;
}
window.eval = eval