-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
350 lines (350 loc) · 12.2 KB
/
main.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
"use strict";
var OpCall = /** @class */ (function () {
function OpCall(op) {
this.op = op;
this._hasNext = true;
}
OpCall.prototype.hasNext = function () {
return this._hasNext;
};
OpCall.prototype.next = function () {
this._hasNext = false;
return this.op;
};
OpCall.prototype.stackTrace = function (sb) {
// do nothing
};
return OpCall;
}());
var QuoteCall = /** @class */ (function () {
function QuoteCall(q) {
this.q = q;
this.pos = 0;
// used to generate correct stack traces after tail-call optimisation
this.calls = 1;
}
QuoteCall.prototype.hasNext = function () {
return this.pos < this.q.ops.length;
};
QuoteCall.prototype.next = function () {
return this.q.ops[this.pos++];
};
QuoteCall.prototype.stackTrace = function (sb) {
while (--this.calls >= 0) {
sb.push('\tat (');
var glue = '';
for (var i = 0; i < this.q.ops.length; ++i) {
sb.push(glue);
glue = ' ';
if (i === this.pos - 1) {
sb.push('@');
}
sb.push(this.q.ops[i].repr());
}
sb.push(')\n');
this.pos = this.q.ops.length;
}
};
return QuoteCall;
}());
var RepeatCall = /** @class */ (function () {
function RepeatCall(q, n) {
this.n = n;
this.i = 0;
this.op = new QuotedOp(q);
}
RepeatCall.prototype.hasNext = function () {
return this.i < this.n;
};
RepeatCall.prototype.next = function () {
++this.i;
return this.op;
};
RepeatCall.prototype.stackTrace = function (sb) {
sb.push('\tat iteration ' + this.i + ' of ' + this.n + '\n');
};
return RepeatCall;
}());
var Interpreter = /** @class */ (function () {
function Interpreter(out) {
if (out === void 0) { out = console.log; }
this.out = out;
this.callStack = [];
this.scopeStack = [new Scope()];
this.valueStacks = [new VStack()];
this.exported = null;
}
Interpreter.prototype.stackTrace = function () {
var sb = [];
while (this.callStack.length) {
this.callStack.pop().stackTrace(sb);
}
return sb.join('');
};
Interpreter.prototype.execQuote = function (q) {
this.callStack.clear();
this.callStack.push(new QuoteCall(q));
while (this.callStack.length) {
this.step();
}
};
Interpreter.prototype.step = function () {
var c = this.callStack.peek();
if (!c.hasNext()) {
this.callStack.pop();
return;
}
var op = c.next();
var vs = this.valueStacks.peek();
if (op instanceof NativeOp) {
switch (op.name) {
case 'true':
vs.push(BoolValue.TRUE);
break;
case 'false':
vs.push(BoolValue.FALSE);
break;
case 'import':
case 'import_as':
case 'export':
// TODO
throw new Error('Not implemented');
case '[':
this.valueStacks.push(new VStack());
break;
case '.[':
this.valueStacks.push(vs.pop('stack'));
break;
case ']':
if (this.valueStacks.length === 1) {
_ERROR.ascendFromGlobalStack();
}
this.valueStacks.pop();
break;
case '].':
if (this.valueStacks.length === 1) {
_ERROR.ascendFromGlobalStack();
}
this.valueStacks.pop();
this.valueStacks.peek().push(vs);
break;
case '{':
this.scopeStack.push(new Scope());
break;
case '.{':
this.scopeStack.push(vs.pop('scope'));
break;
case '}':
if (this.scopeStack.length === 1) {
_ERROR.ascendFromGlobalScope();
}
this.scopeStack.pop();
break;
case '}.':
if (this.scopeStack.length === 1) {
_ERROR.ascendFromGlobalScope();
}
vs.push(this.scopeStack.pop());
break;
case '!':
var q = vs.pop('quote');
if (c instanceof QuoteCall && !c.hasNext() && c.q === q) {
// tail recursion
c.pos = 0;
++c.calls;
}
else {
this.callStack.push(new QuoteCall(q));
}
break;
case 'print':
this.out(vs.popAny().toString());
break;
case 'println':
this.out(vs.popAny().toString() + '\n');
break;
case 'del':
vs.popAny();
break;
case 'push':
var v = vs.popAny();
vs.peek('stack').push(v);
break;
case 'pop':
var s = vs.peek('stack');
vs.push(s.popAny());
break;
case 'len':
s = vs.pop('stack');
vs.push(new IntValue(s.length()));
break;
case 'get':
var i = this.popInt();
s = vs.peek('stack');
vs.push(s.getValue(i));
break;
case 'and':
var b1 = this.popBool();
var b2 = this.popBool();
vs.push(b1 && b2 ? BoolValue.TRUE : BoolValue.FALSE);
break;
case 'or':
b1 = this.popBool();
b2 = this.popBool();
vs.push(b1 || b2 ? BoolValue.TRUE : BoolValue.FALSE);
break;
case 'not':
vs.push(this.popBool() ? BoolValue.FALSE : BoolValue.TRUE);
break;
case 'if':
b1 = this.popBool();
q = vs.pop('quote');
if (b1) {
this.callStack.push(new QuoteCall(q));
}
break;
case 'repeat':
var n = vs.pop('int').v;
q = vs.pop('quote');
this.callStack.push(new RepeatCall(q, n));
break;
case 'this':
vs.push(this.scopeStack.peek());
break;
case 'stack':
vs.push(vs);
break;
case '+':
vs.push(new IntValue(this.popInt() + this.popInt()));
break;
case '-':
vs.push(new IntValue((-this.popInt()) + this.popInt()));
break;
case '*':
vs.push(new IntValue(_NATIVE.imul(this.popInt(), this.popInt())));
break;
case '**':
var i2 = this.popInt();
var i1 = this.popInt();
vs.push(new IntValue(_NATIVE.ipow(i1, i2)));
break;
case '/':
i2 = this.popInt();
i1 = this.popInt();
vs.push(new IntValue(_NATIVE.idiv(i1, i2)));
break;
case '%':
i2 = this.popInt();
i1 = this.popInt();
vs.push(new IntValue(_NATIVE.imod(i1, i2)));
break;
case '&':
vs.push(new IntValue(this.popInt() & this.popInt()));
break;
case '|':
vs.push(new IntValue(this.popInt() | this.popInt()));
break;
case '~':
vs.push(new IntValue(~this.popInt()));
break;
case '<<':
i2 = this.popInt();
i1 = this.popInt();
vs.push(new IntValue(i1 << i2));
break;
case '>>':
i2 = this.popInt();
i1 = this.popInt();
vs.push(new IntValue(i1 >>> i2));
break;
case '>>>':
i2 = this.popInt();
i1 = this.popInt();
vs.push(new IntValue(i1 >>> i2));
break;
// TODO: allow comparisons of other types
case '=':
vs.push(_NATIVE.boolean(this.popInt() === this.popInt()));
break;
// operands popped in reverse order
case '<':
vs.push(_NATIVE.boolean(this.popInt() > this.popInt()));
break;
case '>':
vs.push(_NATIVE.boolean(this.popInt() < this.popInt()));
break;
case '<=':
vs.push(_NATIVE.boolean(this.popInt() >= this.popInt()));
break;
case '>=':
vs.push(_NATIVE.boolean(this.popInt() <= this.popInt()));
break;
default:
throw new Error('Illegal state: unknown operator ' + op);
}
}
else if (op instanceof DupOp) {
vs.push(vs.getValue(vs.length() - op.i));
}
else if (op instanceof QuotedOp) {
this.callStack.push(new QuoteCall(op.q));
}
else if (op instanceof PushOp) {
// TODO: check read-only status of imported stacks
vs.push(op.v);
}
else if (op instanceof AssignOp) {
// TODO: check read-only status of imported scopes
this.scopeStack.peek().doAssignment(op, vs.popAny());
}
else if (op instanceof ReadOp) {
var name_1 = op.name;
var doOp = null;
var i = this.scopeStack.length;
while (doOp === null) {
if (--i >= 0) {
doOp = this.scopeStack[i].load(name_1);
}
else {
// TODO: builtins only need to be dynamically resolved if they can be overloaded
_ERROR.nameError(name_1);
}
}
this.callStack.push(new OpCall(doOp));
}
else if (op instanceof LocalReadOp) {
var name_2 = op.name;
var doOp = this.scopeStack.peek().load(name_2) || _ERROR.nameError(name_2);
;
this.callStack.push(new OpCall(doOp));
}
else if (op instanceof CommentOp) {
// do nothing
}
else {
// typecheck to make sure all ops covered
var ignore = op;
throw new Error('Illegal state: unknown operator ' + op);
}
};
Interpreter.prototype.popBool = function () {
return this.valueStacks.peek().pop('boolean').v;
};
Interpreter.prototype.popInt = function () {
return this.valueStacks.peek().pop('int').v;
};
Interpreter.prototype.popDouble = function () {
return this.valueStacks.peek().pop('double').v;
};
Interpreter.prototype.toString = function () {
var sb = [];
for (var i = 0; i < this.valueStacks.length; ++i) {
sb.push(i, ': ', this.valueStacks[i].repr(), '\n');
}
for (var i = 0; i < this.scopeStack.length; ++i) {
sb.push(i, ': ', this.scopeStack[i].repr(), '\n');
}
return sb.join('');
};
return Interpreter;
}());