-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathrepl.ts
300 lines (291 loc) · 11.4 KB
/
repl.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
import { run, Config } from "./runner";
import { GlobalEnv, libraryFuns, ListContentTag } from "./compiler";
import { tc, defaultTypeEnv, GlobalTypeEnv } from "./type-check";
import { Value, Type, Literal } from "./ast";
import { parse } from "./parser";
import { importMemoryManager, MemoryManager } from "./alloc";
import { bignumfunctions } from "./bignumfunctions";
import {
NUM,
STRING,
BOOL,
NONE,
LIST,
CLASS,
PyValue,
stringify,
PyString,
PyBigInt,
PyBool,
encodeValue,
} from "./utils";
import { InternalException } from "./error";
import { ErrorManager, importErrorManager } from "./errorManager";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface REPL {
run(source: string): Promise<any>;
}
export class BasicREPL {
currentEnv: GlobalEnv;
currentTypeEnv: GlobalTypeEnv;
functions: string;
importObject: any;
memory: any;
errorManager: ErrorManager;
memoryManager: MemoryManager;
constructor(importObject: any) {
this.importObject = importObject;
this.errorManager = new ErrorManager();
if (!importObject.js) {
const memory = new WebAssembly.Memory({ initial: 20000, maximum: 20000 });
const view = new Int32Array(memory.buffer);
view[0] = 4;
this.importObject.js = { memory: memory };
}
if (!importObject.memoryManager) {
const memory = this.importObject.js.memory;
const memoryManager = new MemoryManager(new Uint8Array(memory.buffer), {
staticStorage: 512n,
total: 20000n,
});
this.memoryManager = memoryManager;
importMemoryManager(this.importObject, memoryManager);
}
this.currentEnv = {
globals: new Map(),
classes: new Map(),
locals: new Map(),
funs: new Map(),
};
this.importObject.imports.__internal_print = (arg: any) => {
console.log("Logging from WASM: ", arg);
this.importObject.imports.print(
PyValue(NUM, arg, new Uint32Array(this.importObject.js.memory.buffer))
);
return arg;
};
this.importObject.imports.__internal_print_none = (arg: number) => {
console.log("Logging from WASM: ", arg);
this.importObject.imports.print(PyValue(NONE, arg, null));
return arg;
};
this.importObject.imports.__internal_print_num = (arg: number) => {
console.log("Logging from WASM: ", arg);
this.importObject.imports.print(
PyValue(NUM, arg, new Uint32Array(this.importObject.js.memory.buffer))
);
return arg;
};
this.importObject.imports.__internal_print_str = (arg: number) => {
console.log("Logging from WASM: ", arg);
this.importObject.imports.print(
PyValue(STRING, arg, new Uint32Array(this.importObject.js.memory.buffer))
);
return arg;
};
this.importObject.imports.__internal_print_list = (arg: number, typ: ListContentTag) => {
console.log("Logging from WASM: ", arg);
console.log("Before memview: ", window.performance.now());
const mem = new Int32Array(this.importObject.js.memory.buffer);
console.log("After memview: ", window.performance.now());
let list_length = mem[arg / 4 + 1];
//let list_bound = view[arg / 4 + 2];
var base_str = "";
var index = 0;
let p_list = [];
while (index < list_length) {
console.log("Pretty printing a list: ", index, arg);
switch (typ) {
case ListContentTag.Num:
base_str = stringify(PyValue(LIST(NUM), arg, mem));
p_list.push(stringify(PyValue(NUM, mem[arg / 4 + 3 + index], mem)));
break;
case ListContentTag.Bool:
base_str = stringify(PyValue(LIST(BOOL), arg, mem));
p_list.push(stringify(PyValue(BOOL, mem[arg / 4 + 3 + index], mem)));
break;
//Realistically can never happen
case ListContentTag.None:
base_str = stringify(PyValue(LIST(NONE), arg, mem));
p_list.push(stringify(PyValue(NONE, mem[arg / 4 + 3 + index], mem)));
break;
//We didn't actually store the name of the class anywhere
//This will display as "<list<class> object at N>"
case ListContentTag.Str:
base_str = stringify(PyValue(LIST(STRING), arg, mem));
p_list.push(stringify(PyValue(STRING, mem[arg / 4 + 3 + index], mem)));
break;
case ListContentTag.Class:
base_str = stringify(PyValue(LIST(CLASS("class")), arg, mem));
p_list.push(stringify(PyValue(CLASS("CLASS"), mem[arg / 4 + 3 + index], mem)));
break;
//Doesn't display type of inner list
//This will display as "<list<list> object at N>"
case ListContentTag.List:
base_str = stringify(PyValue(LIST(LIST(null)), arg, mem));
p_list.push(stringify(PyValue(LIST(LIST(null)), mem[arg / 4 + 3 + index], mem)));
break;
//TODO: Placeholder for Dict
case ListContentTag.Dict:
base_str = stringify(PyValue(LIST(LIST(null)), arg, mem));
p_list.push(stringify(PyValue(CLASS("Dict"), mem[arg / 4 + 3 + index], mem)));
break;
//TODO: Placeholder for Callable
case ListContentTag.Callable:
base_str = stringify(PyValue(LIST(NUM), arg, mem));
p_list.push(stringify(PyValue(CLASS("Callable"), mem[arg / 4 + 3 + index], mem)));
break;
}
index += 1;
}
this.importObject.imports.print(PyString(`${base_str} [ ${p_list.join(", ")} ]`, arg));
return arg;
};
this.importObject.imports.__internal_print_bool = (arg: number) => {
console.log("Logging from WASM: ", arg);
this.importObject.imports.print(PyValue(BOOL, arg, null));
return arg;
};
this.importObject.imports.abs = (arg: number) =>
this.uniOpInterface(arg, (val: bigint) => {
return val < 0 ? -val : val;
});
this.importObject.imports.pow = (base: number, exp: number) =>
this.binOpInterface(base, exp, (baseVal: bigint, expVal: bigint) => {
// Javascript does not allow a negative BigInt exponent.
if (expVal < 1) {
return 0n;
} else {
return baseVal ** expVal;
}
});
this.importObject.imports.max = (x: number, y: number) =>
this.binOpInterface(x, y, (xval: bigint, yval: bigint) => {
var res = xval > yval ? xval : yval;
return res;
});
this.importObject.imports.min = (x: number, y: number) =>
this.binOpInterface(x, y, (xval: bigint, yval: bigint) => {
var res = xval < yval ? xval : yval;
return res;
});
this.importObject.imports.__big_num_add = (x: number, y: number) =>
this.binOpInterface(x, y, (x: bigint, y: bigint) => {
return x + y;
});
this.importObject.imports.__big_num_sub = (x: number, y: number) =>
this.binOpInterface(x, y, (x: bigint, y: bigint) => {
return x - y;
});
this.importObject.imports.__big_num_mul = (x: number, y: number) =>
this.binOpInterface(x, y, (x: bigint, y: bigint) => {
return x * y;
});
this.importObject.imports.__big_num_div = (x: number, y: number) =>
this.binOpInterface(x, y, (x: bigint, y: bigint) => {
if (y === 0n) {
// TODO change this back to ZeroDivisionError
throw new Error("Cannot divide by zero");
// throw new ZeroDivisionError();
}
return (x - (((x % y) + y) % y)) / y;
});
this.importObject.imports.__big_num_mod = (x: number, y: number) =>
this.binOpInterface(x, y, (x: bigint, y: bigint) => {
return ((x % y) + y) % y;
});
this.importObject.imports.__big_num_eq = (x: number, y: number) =>
this.binOpInterfaceBool(x, y, (x: bigint, y: bigint) => {
return x === y;
});
this.importObject.imports.__big_num_ne = (x: number, y: number) =>
this.binOpInterfaceBool(x, y, (x: bigint, y: bigint) => {
return x !== y;
});
this.importObject.imports.__big_num_lt = (x: number, y: number) =>
this.binOpInterfaceBool(x, y, (x: bigint, y: bigint) => {
return x < y;
});
this.importObject.imports.__big_num_lte = (x: number, y: number) =>
this.binOpInterfaceBool(x, y, (x: bigint, y: bigint) => {
return x <= y;
});
this.importObject.imports.__big_num_gt = (x: number, y: number) =>
this.binOpInterfaceBool(x, y, (x: bigint, y: bigint) => {
return x > y;
});
this.importObject.imports.__big_num_gte = (x: number, y: number) =>
this.binOpInterfaceBool(x, y, (x: bigint, y: bigint) => {
return x >= y;
});
importErrorManager(this.importObject, this.errorManager);
// initialization for range() calss and its constructor.
const classFields: Map<string, [number, Literal]> = new Map();
classFields.set("cur", [0, { tag: "num", value: BigInt(0) }]);
classFields.set("stop", [1, { tag: "num", value: BigInt(0) }]);
classFields.set("step", [2, { tag: "num", value: BigInt(1) }]);
this.currentEnv.classes.set("Range", classFields);
this.currentTypeEnv = defaultTypeEnv;
this.functions = libraryFuns() + "\n\n" + bignumfunctions;
}
binOpInterface(x: number, y: number, f: (x: bigint, y: bigint) => bigint): number {
var mem = new Uint32Array(this.importObject.js.memory.buffer);
var xval = PyValue(NUM, x, mem);
var yval = PyValue(NUM, y, mem);
if (xval.tag == "num" && yval.tag == "num") {
return encodeValue(
PyBigInt(f(xval.value, yval.value)),
this.importObject.imports.gcalloc,
mem
);
}
throw new InternalException("binary operation failed at runtime");
}
binOpInterfaceBool(x: number, y: number, f: (x: bigint, y: bigint) => boolean): number {
var mem = new Uint32Array(this.importObject.js.memory.buffer);
var xval = PyValue(NUM, x, mem);
var yval = PyValue(NUM, y, mem);
if (xval.tag == "num" && yval.tag == "num") {
return encodeValue(PyBool(f(xval.value, yval.value)), this.importObject.imports.gcalloc, mem);
}
throw new InternalException("binary operation failed at runtime");
}
uniOpInterface(x: number, f: (x: bigint) => bigint): number {
var mem = new Uint32Array(this.importObject.js.memory.buffer);
var xval = PyValue(NUM, x, mem);
if (xval.tag == "num") {
return encodeValue(PyBigInt(f(xval.value)), this.importObject.imports.gcalloc, mem);
}
throw new InternalException("binary operation failed at runtime");
}
async run(source: string): Promise<Value> {
const config: Config = {
importObject: this.importObject,
env: this.currentEnv,
typeEnv: this.currentTypeEnv,
functions: this.functions,
errorManager: this.errorManager,
memoryManager: this.memoryManager,
};
const [result, newEnv, newTypeEnv, newFunctions, newErrorManager] = await run(source, config);
this.currentEnv = newEnv;
this.currentTypeEnv = newTypeEnv;
this.functions += newFunctions;
this.errorManager = newErrorManager;
this.memoryManager.forceCollect();
return result;
}
async tc(source: string): Promise<Type> {
const config: Config = {
importObject: this.importObject,
env: this.currentEnv,
typeEnv: this.currentTypeEnv,
functions: this.functions,
errorManager: this.errorManager,
memoryManager: this.memoryManager,
};
const parsed = parse(source, config);
const [result, _] = await tc(this.currentTypeEnv, parsed);
return result.a[0];
}
}