-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.js
284 lines (284 loc) · 9.55 KB
/
ops.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
"use strict";
var PushOp = /** @class */ (function () {
function PushOp(v) {
this.v = v;
}
PushOp.prototype.compile = function (constants) {
if (this.v.type === 'quote') {
return constants.addQuote(this.v);
}
else if (this.v.type === 'int' || this.v.type === 'double' || this.v.type === 'string') {
return constants.addPrimitive(this.v.v, this.v.type);
}
else {
throw new Error('Illegal state: compile-time constant must be int, double, string or quote');
}
};
PushOp.prototype.repr = function () {
return this.v.repr();
};
return PushOp;
}());
var QuotedOp = /** @class */ (function () {
function QuotedOp(q) {
this.q = q;
}
QuotedOp.prototype.compile = function (constants) {
throw new Error('Illegal state: QuotedOp should not occur at compile-time');
};
QuotedOp.prototype.repr = function () {
return this.q.repr() + '!';
};
return QuotedOp;
}());
var AssignOp = /** @class */ (function () {
function AssignOp(name, doNow) {
this.name = name;
this.doNow = doNow;
}
AssignOp.prototype.compile = function (constants) {
return (this.doNow ? NativeOp.STORE_QUOTE : NativeOp.STORE).opcode + constants.addName(this.name);
};
AssignOp.prototype.repr = function () {
return (this.doNow ? '>!' : '>') + this.name;
};
return AssignOp;
}());
var CommentOp = /** @class */ (function () {
function CommentOp(s) {
this.s = s;
}
CommentOp.prototype.compile = function (constants) {
return '';
};
CommentOp.prototype.repr = function () {
return '#' + this.s + '\n';
};
return CommentOp;
}());
var DupOp = /** @class */ (function () {
function DupOp(i) {
this.i = i;
}
DupOp.prototype.compile = function (constants) {
return NativeOp.DUP.opcode + this.i;
};
DupOp.prototype.repr = function () {
return new Array(this.i + 1).join('@');
};
return DupOp;
}());
var ReadOp = /** @class */ (function () {
function ReadOp(name) {
this.name = name;
}
ReadOp.prototype.compile = function (constants) {
return NativeOp.LOAD_SLOW.compile(constants) + constants.addName(this.name);
};
ReadOp.prototype.repr = function () {
return this.name;
};
return ReadOp;
}());
var LocalReadOp = /** @class */ (function () {
function LocalReadOp(name) {
this.name = name;
}
LocalReadOp.prototype.compile = function (constants) {
return NativeOp.LOAD_FAST.compile(constants) + constants.addName(this.name);
};
LocalReadOp.prototype.repr = function () {
return '<' + this.name;
};
return LocalReadOp;
}());
var NativeOp = /** @class */ (function () {
function NativeOp(name, opcode) {
this.name = name;
this.opcode = opcode;
if (name !== null) {
NativeOp.byName[name] = this;
}
if (Object.prototype.hasOwnProperty.call(NativeOp.byOpCode, opcode)) {
throw new Error('Illegal state: opcode `' + opcode + '` is already defined');
}
NativeOp.byOpCode[opcode] = this;
}
NativeOp.getByName = function (name) {
return NativeOp.byName[name] || null;
};
NativeOp.getByOpCode = function (opcode) {
return NativeOp.byOpCode[opcode] || null;
};
NativeOp.prototype.compile = function (constants) {
return this.opcode;
};
NativeOp.prototype.repr = function () {
if (this.name === null) {
throw new Error('Illegal state: opcode `' + this.opcode + '` should not exist in interactive mode');
}
return this.name;
};
NativeOp.byName = Object.create(null);
NativeOp.byOpCode = Object.create(null);
NativeOp.STACK_DESCEND = new NativeOp('[', 'd');
NativeOp.STACK_ASCEND = new NativeOp(']', 'a');
NativeOp.STACK_ENTER = new NativeOp('.[', 'e');
NativeOp.STACK_EXIT = new NativeOp('].', 'x');
NativeOp.SCOPE_DESCEND = new NativeOp('{', 'D');
NativeOp.SCOPE_ASCEND = new NativeOp('}', 'A');
NativeOp.SCOPE_ENTER = new NativeOp('.{', 'E');
NativeOp.SCOPE_EXIT = new NativeOp('}.', 'X');
NativeOp.NOW = new NativeOp('!', '!');
NativeOp.DUP = new NativeOp(null, '@');
NativeOp.STORE = new NativeOp(null, 's');
NativeOp.STORE_QUOTE = new NativeOp(null, 'q');
NativeOp.LOAD_FAST = new NativeOp(null, 'l');
NativeOp.LOAD_SLOW = new NativeOp(null, 'L');
NativeOp.CONST_QUOTE = new NativeOp(null, 'Q');
NativeOp.CONST_INT = new NativeOp(null, 'j');
NativeOp.CONST_DOUBLE = new NativeOp(null, 'J');
NativeOp.CONST_STRING = new NativeOp(null, 'k');
NativeOp.TRUE = new NativeOp('true', 't');
NativeOp.FALSE = new NativeOp('false', 'f');
// TODO: imports?
NativeOp.IMPORT = new NativeOp('import', 'i');
NativeOp.IMPORT_AS = new NativeOp('import_as', 'I');
NativeOp.EXPORT = new NativeOp('export', '$');
NativeOp.PRINT = new NativeOp('print', 'p');
NativeOp.PRINTLN = new NativeOp('println', 'P');
NativeOp.DEL = new NativeOp('del', '_');
NativeOp.PUSH = new NativeOp('push', 'b');
NativeOp.POP = new NativeOp('pop', 'B');
NativeOp.LEN = new NativeOp('len', 'n');
NativeOp.GET = new NativeOp('get', 'g');
NativeOp.AND = new NativeOp('and', 'y');
NativeOp.OR = new NativeOp('or', 'Y');
NativeOp.NOT = new NativeOp('not', 'z');
NativeOp.IF = new NativeOp('if', '?');
NativeOp.REPEAT = new NativeOp('repeat', 'r');
NativeOp.THIS = new NativeOp('this', 'T');
NativeOp.STACK = new NativeOp('stack', 'S');
NativeOp.ADD = new NativeOp('+', '+');
NativeOp.SUBTRACT = new NativeOp('-', '-');
NativeOp.MULTIPLY = new NativeOp('*', '*');
NativeOp.POW = new NativeOp('**', 'w');
NativeOp.DIVIDE = new NativeOp('/', '/');
NativeOp.MODULO = new NativeOp('%', '%');
NativeOp.BIT_AND = new NativeOp('&', '&');
NativeOp.BIT_OR = new NativeOp('|', '|');
NativeOp.BIT_NEG = new NativeOp('~', '~');
NativeOp.BIT_XOR = new NativeOp('^', '^');
NativeOp.BIT_LSHIFT = new NativeOp('<<', 'W');
NativeOp.BIT_RSHIFT = new NativeOp('>>', 'm');
NativeOp.BIT_URSHIFT = new NativeOp('>>>', 'M');
NativeOp.EQUALS = new NativeOp('=', '=');
NativeOp.LESS_THAN = new NativeOp('<', 'c');
NativeOp.GREATER_THAN = new NativeOp('>', 'h');
NativeOp.LESS_THAN_OR_EQUAL = new NativeOp('<=', 'C');
NativeOp.GREATER_THAN_OR_EQUAL = new NativeOp('>=', 'H');
return NativeOp;
}());
var _NATIVE = {
stack: function () {
return new VStack();
},
scope: function () {
return new Scope();
},
boolean: function (b) {
return b ? BoolValue.TRUE : BoolValue.FALSE;
},
int: function (i) {
return new IntValue(i);
},
double: function (d) {
return new DoubleValue(d);
},
string: function (s) {
return new StringValue(s);
},
imul: (Math.imul || function (a, b) {
// Math.imul polyfill
var aHi = (a >>> 16) & 0xffff;
var aLo = a & 0xffff;
var bHi = (b >>> 16) & 0xffff;
var bLo = b & 0xffff;
return ((aLo * bLo) + (((aHi * bLo + aLo * bHi) << 16) >>> 0) | 0);
}),
idiv: function (a, b) {
if (b === 0) {
_ERROR.arithmeticError(a + ' / ' + b);
}
return _NATIVE.trunc(a / b);
},
imod: function (a, b) {
if (b === 0) {
_ERROR.arithmeticError(a + ' % ' + b);
}
return a % b;
},
ipow: function (a, b) {
if (b < 0 || (a === 0 && b === 0)) {
_ERROR.arithmeticError(a + ' ** ' + b);
}
return Math.pow(a, b);
},
trunc: (Math.trunc || function (v) {
// Math.trunc polyfill
v = +v;
if (!isFinite(v)) {
return v;
}
return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
}),
loadSlow: function (scopes, name) {
for (var i = scopes.length - 1; i >= 0; --i) {
var v = scopes[i].load(name);
if (v) {
return v;
}
}
_ERROR.nameError(name);
},
wrapJSFunction: function (f, stacks) {
return {
type: 'function',
q: function () {
var args = stacks[stacks.length - 1].pop('stack');
var unwrappedArgs = f.wrapper.unwrap(args);
var result = f.q.call(f.wrapper.v, unwrappedArgs);
stacks[stacks.length - 1].push(f.wrapper.wrap(result));
}
};
}
};
var _ERROR = {
wrongType: function (actualType, expectedType) {
throw new Error('Type error: expected ' + expectedType + ', was ' + actualType);
},
printNotSupported: function (actualType) {
throw new Error('Type error: expected string, int, double, boolean or stack; was ' + actualType);
},
emptyStack: function () {
throw new Error('Illegal state: pop from empty stack');
},
peekEmptyStack: function () {
throw new Error('Illegal state: peek at empty stack');
},
ascendFromGlobalStack: function () {
throw new Error("Illegal state: can't ascend from global stack");
},
ascendFromGlobalScope: function () {
throw new Error("Illegal state: can't ascend from global scope");
},
nameError: function (name) {
throw new Error('Name error: ' + name);
},
arithmeticError: function (msg) {
throw new Error('Arithmetic error: ' + msg);
},
indexOutOfBounds: function (i, n) {
throw new Error('Index out of bounds: ' + i + ' from length ' + n);
}
};