-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.html
100 lines (93 loc) · 2.49 KB
/
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
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
<!doctype HTML>
<html>
<head>
<title>fffff Compiler</title>
<script src="ops.js"></script>
<script src="values.js"></script>
<script src="parser.js"></script>
<script src="main.js"></script>
<script src="bytecode.js"></script>
<script src="jit.js"></script>
<style type="text/css">
#src { width: 100%; height: 50vh; resize: none; }
pre, textarea { white-space: pre-wrap; tab-size: 4; }
</style>
</head>
<body>
<h1 id="main_heading">fffff Compiler</h1>
<textarea id="src" spellcheck="false">
({>a,b <b,a}) >!swap
({>a,b (<a) <a,b < if (@ 1 +) <b,a - 1 - repeat}) >!range
({
@ len >n
[]. >outputs
(pop <outputs swap push del) <n repeat
del <outputs
}) >!reverse
0 10 range
(print) 10 repeat
'' println
({
>if_true,if_false,condition
<if_true <condition if
<if_false <condition not if
}) >!if_else
({
>n
(1)
(n 2 - fib n 1 - fib +)
n 1 <= if_else
}) >!fib
[0 10 range].
[]. >outputs
(
pop
fib
<outputs swap push del
) 10 repeat
<outputs println
</textarea>
<button id="compile">Compile & run</button>
<pre id="error_message"></pre>
<h2>Printed output</h2>
<pre id="printed_output"></pre>
<h2>Compiled output</h2>
<pre id="compiled_output"></pre>
<h2>Decompiled output</h2>
<pre id="decompiled_output"></pre>
<h2>JavaScript output</h2>
<pre id="js_output"></pre>
<script type="text/javascript">
var src = document.getElementById('src');
var error_message = document.getElementById('error_message');
var compiled_output = document.getElementById('compiled_output');
var decompiled_output = document.getElementById('decompiled_output');
var js_output = document.getElementById('js_output');
var printed_output = document.getElementById('printed_output');
var _OUT = function(s) {
printed_output.appendChild(document.createTextNode(s));
};
function compile() {
error_message.innerText = '';
compiled_output.innerText = '';
decompiled_output.innerText = '';
js_output.innerText = '';
printed_output.innerText = '';
try {
var q = Parser.parse(src.value);
var co = new CodeObject(q);
var j = co.toJSON();
compiled_output.innerText = JSON.stringify(j);
decompiled_output.innerText = decompile(j);
var f = new JITCompiler(j).compileAll();
js_output.innerText = f.toString();
f(_NATIVE, _OUT, _ERROR);
} catch(e) {
error_message.innerText = e.message;
throw e;
}
}
document.getElementById('compile').addEventListener('click', compile);
</script>
</body>
</html>