-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathweb_compiler.html
53 lines (52 loc) · 1.78 KB
/
web_compiler.html
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
<!doctype html>
<html>
<head>
<title>WASM compiler test</title>
<script type="importmap">
{
"imports": {
"web_compiler": "./web_compiler/pkg/web_compiler.js",
"alan_std": "./alanStdBundle.js"
}
}
</script>
<script type="module">
import { default as init, compile } from "web_compiler";
import * as alan_std from "alan_std";
let t1 = performance.now();
document.addEventListener("DOMContentLoaded", async (evt) => {
let t2 = performance.now();
console.log(`DOM loaded in ${t2 - t1}ms`);
await init();
let t3 = performance.now();
console.log(`WASM initialized in ${t3 - t2}ms`);
document.getElementById("run").addEventListener("click", () => {
let t4 = performance.now();
let ln = document.getElementById("source").value;
// Add a redirect a print to write to the 'out' div
let preface = `
fn print{T}(v: T) = {"((v) => document.getElementById('out').innerHTML = v.toString())" :: T}(v);
fn print(v: string) = {"((v) => document.getElementById('out').innerHTML = v.toString())" :: string}(v);
`;
let js = compile(preface + ln);
let t5 = performance.now();
console.log(`Alan compilation took ${t5 - t4}ms`);
// Remove any `import` from the generated Javascript before eval'ing
eval(js.replaceAll(/^import.*/g, ""));
});
});
</script>
</head>
<body>
<form>
<div>
<textarea id="source" rows="24" cols="80">export fn main = 'Hello, World!'.print;</textarea>
</div>
<div>
<button id="run" type="button">Run</button>
</div>
</form>
<p>Standard Out:</p>
<div id="out"></div>
</body>
</html>