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