-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.js
367 lines (359 loc) · 10.1 KB
/
interp.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
'use strict';
/* jshint unused:false*/
/* eslint no-unused-vars: 0, block-scoped-var: 0 */
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
var arr2 = Array(arr.length);
for (var i = 0; i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
/* jshint unused: false */
/* jshint evil: true */
var rbInterp;
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
var tokenTypes = require('./types');
var builtins = require('./builtins');
}
(function () {
function RuntimeError(message) {
this.name = 'RuntimeError';
this.message = message || '';
this.stack = new Error().stack;
}
RuntimeError.prototype = Object.create(Error.prototype);
RuntimeError.prototype.constructor = RuntimeError;
var env;
function cast(type, value) {
if (type === 'string') {
return value.toString();
} else if (type === 'int') {
return parseInt(value, 10);
} else if (type === 'float') {
return parseFloat(value);
}
return value;
}
function truthy(cond) {
if (typeof cond !== 'boolean') throw new RuntimeError('Condition must be of type `bool`');
return cond;
}
function arithHelper(x, y) {
var a = x.ti();
var b = y.ti();
if (a === 'dict' || b === 'dict') throw new RuntimeError('cannot add dicts');
if (a === 'list' || b === 'list') throw new RuntimeError('cannot add lists');
var ret;
['string', 'float', 'int'].forEach(function (type) {
if (ret) return;
if (a === type || b === type) ret = type;
});
return ret || 'unknown';
}
var idRegex = /[a-zA-Z_][a-zA-Z0-9_]*/;
rbInterp = {
Program: function (x, y) {
env = {}; // clear it out
var lines = y.rb(); return lines[lines.length - 1];
},
SourceElement: function (x) {
return x.rb();
},
ExpressionStatement: function (x, y) {
return x.rb();
},
Expression: function (x) {
return x.rb();
},
EqualityExpression_equal: function (x, y, z) {
return x.rb() === z.rb();
},
EqualityExpression_notEqual: function (x, y, z) {
return x.rb() !== z.rb();
},
EqualityExpression_eq: function (x, y, z) {
return x.rb() === z.rb();
},
EqualityExpression_notEq: function (x, y, z) {
return x.rb() !== z.rb();
},
RelationalExpression_lt: function (a, b, c) {
return a.rb() < c.rb();
},
RelationalExpression_gt: function (a, b, c) {
return a.rb() > c.rb();
},
RelationalExpression_le: function (a, b, c) {
return a.rb() <= c.rb();
},
RelationalExpression_ge: function (a, b, c) {
return a.rb() >= c.rb();
},
AdditiveExpression_add: function (x, _, y) {
return x.rb() + y.rb();
},
AdditiveExpression_sub: function (x, _, y) {
return x.rb() - y.rb();
},
MultiplicativeExpression_mul: function (x, _, y) {
return x.rb() * y.rb();
},
MultiplicativeExpression_div: function (x, _, y) {
var retType = arithHelper(x, y);
var ret = x.rb() / y.rb();
return retType === 'int' ? Math.floor(ret) : ret;
},
MultiplicativeExpression_mod: function (x, _, y) {
return x.rb() % y.rb();
},
UnaryExpression_unaryMinus: function (x, y) {
return -1 * y.rb();
},
UnaryExpression_unaryPlus: function (x, y) {
return y.rb();
},
UnaryExpression_lnot: function (x, y) {
return !y.rb();
},
decimalLiteral_integerOnly: function (x, y) {
return parseInt(this.sourceString, 10);
},
decimalLiteral_bothParts: function (x, y, z, w) {
return parseFloat(this.sourceString);
},
Block: function (a, b, c) {
b.rb(); return null;
},
FunctionDeclaration: function (a, id, c, params, e, f, myBody, h) {
var argList = params.sourceString.split(/,\s*/);
var idName = id.sourceString;
if (env[idName] !== undefined) throw new RuntimeError('redefining ' + idName + ' is not allowed');
env[idName] = {
args: argList,
body: myBody,
};
return null;
},
FunctionBody: function (a, b) {
try {
b.rb();
} catch (e) {
if (!e.hasOwnProperty('val')) throw e;
return e.val;
}
},
IfStatement: function (a, b, cond, d, expr, f, elseCase) {
if (truthy(cond.rb())) {
expr.rb();
} else {
elseCase.rb();
}
return null;
},
VariableStatement: function (x, y, z) {
y.rb(); // this is an Array
return null;
},
VariableDeclaration: function (x, y) {
var val = y.rb();
val = val[val.length - 1];
env[x.sourceString] = val; // override
return null; // doesn't matter
},
Initialiser: function (x, y) {
var type = y.rb();
return type;
},
EmptyListOf: function () {
return [];
},
NonemptyListOf: function (first, _sep, others) {
return [first.rb()].concat(others.rb());
},
TryStatement: function (x) {
return x.rb();
},
TryStatement_tryCatch: function (x, y, z) {
y.rb(); z.rb();
return null;
},
TryStatement_tryFinally: function (x, y, z) {
y.rb(); z.rb();
return null;
},
TryStatement_tryCatchFinally: function (x, y, z, w) {
y.rb(); z.rb(); w.rb();
return null;
},
Catch: function (a, b, c, d, e) {
c.rb(); e.rb();
return null;
},
Finally: function (a, b) {
b.rb();
return null;
},
ArrayLiteral: function (x, y, z) {
return y.rb();
},
stringLiteral: function (x, y, z) {
return y.sourceString;
},
booleanLiteral: function (x) {
return JSON.parse(this.sourceString);
},
ReturnStatement: function (x, y, z, w) {
throw { val: z.rb()[0] };
},
PostfixExpression_postIncrement: function (a, b, c) {
return env[a.sourceString]++;
},
PostfixExpression_postDecrement: function (a, b, c) {
return env[a.sourceString]--;
},
UnaryExpression_preIncrement: function (a, b) {
return ++env[b.sourceString];
},
UnaryExpression_preDecrement: function (a, b) {
return --env[b.sourceString];
},
AssignmentExpression_assignment: function (x, y, z) {
var val = z.rb();
env[x.sourceString] = val; // override
return cast(x.ti(), val);
},
identifier: function (x) {
var ret = env[x.sourceString];
if (ret === undefined) throw new RuntimeError(x.sourceString + ' is an undefined identifier');
var type = this.ti();
return cast(type, ret);
},
IterationStatement: function (a) {
return a.rb();
},
IterationStatement_doWhile: function (a, b, c, d, e, f, g) {
do {
b.rb();
} while (truthy(e.rb()));
return null;
},
_terminal: function () {
return null;
},
IterationStatement_whileDo: function (a, b, c, d, e) {
while (truthy(c.rb())) {
e.rb();
}
return null;
},
IterationStatement_for3: function (a, b, c, d, e, f, g, h, i) {
// TODO(nate): fix
for (c.rb(); truthy(e.rb()[0]); g.rb()) {
i.rb();
}
return null;
},
IterationStatement_for3var: function (a, b, _var, c, d, e, f, g, h, i) {
// TODO(nate): fix
for (c.rb(); truthy(e.rb()[0]); g.rb()) {
i.rb();
}
return null;
},
IterationStatement_forIn: function (a, b, lhs, c, expr, d, stmt) {
// TODO(nate): fix
lhs.rb();
var type = expr.rb();
tokenTypes.setVal(lhs.sourceString, type === 'dict' ? 'string' : 'int');
stmt.rb();
return null;
},
IterationStatement_forInVar: function (a, b, _var, lhs, c, expr, d, stmt) {
// TODO(nate): fix
lhs.rb();
var type = expr.rb();
tokenTypes.setVal(lhs.sourceString, type === 'dict' ? 'string' : 'int');
stmt.rb();
return null;
},
CallExpression_memberExpExp: function (x, y) {
// TODO(nate): fix this line to use .rb()
var funDecl = env[x.sourceString];
var ret;
var funInv = { args: y.rb() };
if (funDecl) {
var oldEnv = env;
env = Object.create(oldEnv);
for (var k = 0; k < funInv.args.length; k++) {
env[funDecl.args[k]] = funInv.args[k];
}
ret = funDecl.body.rb(); // execute it with the new environment
env = oldEnv;
} else if (builtins[x.sourceString] !== undefined) {
funDecl = builtins[x.sourceString];
ret = funDecl.apply(undefined, _toConsumableArray(funInv.args));
} else {
throw new RuntimeError('undefined function');
}
var type = x.ti();
if (type === 'string') {
return ret.toString();
} else if (type === 'int') {
return parseInt(ret, 10);
} else if (type === 'float') {
return parseFloat(ret);
}
return ret;
},
Arguments: function (a, b, c) {
return b.rb();
},
MemberExpression_propRefExp: function (a, b, c) {
var that = a.rb();
if (that[c.sourceString] !== undefined) {
return that[c.sourceString];
} else {
throw new RuntimeError('builtin properties not yet supported');
}
},
MemberExpression_arrayRefExp: function (a, b, c, d) {
var that = a.rb();
var idx = c.rb();
if (that[idx] !== undefined) {
return that[idx];
} else {
throw new RuntimeError('builtin properties not yet supported');
}
},
PrimaryExpression_parenExpr: function (x, y, z) {
return y.rb();
},
ObjectLiteral_noTrailingComma: function (a, b, c) {
var ret = {};
b.rb().forEach(function (x) {
ret[x.key] = x.val;
});
return ret;
},
PropertyAssignment_simple: function (a, b, c) {
return {
key: a.sourceString,
val: c.rb(),
};
},
ObjectLiteral_trailingComma: function (a, b, c, d) {
var ret = {};
b.rb().forEach(function (x) {
ret[x.key] = x.val;
});
return ret;
},
};
}());
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports.rbInterp = rbInterp;
}