forked from ucsd-cse231-w21/chocopy-wasm-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebstart.ts
111 lines (100 loc) · 3.6 KB
/
webstart.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
import {BasicREPL} from './repl';
import { Type, Value } from './ast';
import { defaultTypeEnv } from './type-check';
import { NUM, BOOL, NONE } from './utils';
function stringify(typ: Type, arg: any) : string {
switch(typ.tag) {
case "number":
return (arg as number).toString();
case "bool":
return (arg as boolean)? "True" : "False";
case "none":
return "None";
case "class":
return typ.name;
}
}
function print(typ: Type, arg : number) : any {
console.log("Logging from WASM: ", arg);
const elt = document.createElement("pre");
document.getElementById("output").appendChild(elt);
elt.innerText = stringify(typ, arg);
return arg;
}
function webStart() {
document.addEventListener("DOMContentLoaded", function() {
var importObject = {
imports: {
print_num: (arg: number) => print(NUM, arg),
print_bool: (arg: number) => print(BOOL, arg),
print_none: (arg: number) => print(NONE, arg),
abs: Math.abs,
min: Math.min,
max: Math.max,
pow: Math.pow
},
};
var repl = new BasicREPL(importObject);
function renderResult(result : Value) : void {
if(result === undefined) { console.log("skip"); return; }
if (result.tag === "none") return;
const elt = document.createElement("pre");
document.getElementById("output").appendChild(elt);
switch (result.tag) {
case "num":
elt.innerText = String(result.value);
break;
case "bool":
elt.innerHTML = (result.value) ? "True" : "False";
break;
case "object":
elt.innerHTML = `<${result.name} object at ${result.address}`
break
default: throw new Error(`Could not render value: ${result}`);
}
}
function renderError(result : any) : void {
const elt = document.createElement("pre");
document.getElementById("output").appendChild(elt);
elt.setAttribute("style", "color: red");
elt.innerText = String(result);
}
function setupRepl() {
document.getElementById("output").innerHTML = "";
const replCodeElement = document.getElementById("next-code") as HTMLTextAreaElement;
replCodeElement.addEventListener("keypress", (e) => {
if(e.shiftKey && e.key === "Enter") {
} else if (e.key === "Enter") {
e.preventDefault();
const output = document.createElement("div");
const prompt = document.createElement("span");
prompt.innerText = "»";
output.appendChild(prompt);
const elt = document.createElement("textarea");
// elt.type = "text";
elt.disabled = true;
elt.className = "repl-code";
output.appendChild(elt);
document.getElementById("output").appendChild(output);
const source = replCodeElement.value;
elt.value = source;
replCodeElement.value = "";
repl.run(source).then((r) => { renderResult(r); console.log ("run finished") })
.catch((e) => { renderError(e); console.log("run failed", e) });;
}
});
}
function resetRepl() {
document.getElementById("output").innerHTML = "";
}
document.getElementById("run").addEventListener("click", function(e) {
repl = new BasicREPL(importObject);
const source = document.getElementById("user-code") as HTMLTextAreaElement;
resetRepl();
repl.run(source.value).then((r) => { renderResult(r); console.log ("run finished") })
.catch((e) => { renderError(e); console.log("run failed", e) });;
});
setupRepl();
});
}
webStart();