-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.xhtml
154 lines (141 loc) · 4.66 KB
/
index.xhtml
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
<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
</head>
<body style="font-family:monospace;font-size:16px;">
<textarea id="input" style="width:100%;height:300px;font-size:16px;tab-size:4;"/>
<br/>
<button id="compile" style="font-size:16px;">Compile</button>
<br/>
<div id="compiled"/>
<label>Debug: <input type="checkbox" id="debug" autocomplete="off"/></label>
<button id="execute" style="font-size:16px;">Execute</button>
<br/>
<div id="debugger">
<div>
<button id="debugger-step-in">Step In</button>
<button id="debugger-step-over">Step Over</button>
<button id="debugger-continue">Continue</button>
</div>
<ol id="debugger-frames"/>
</div>
<pre id="console"/>
<div id="output"/>
<script type="module">
import { Globals, ml_call, ml_resume, ml_decode, MLNil, MLStringT, ml_debug, ml_deref } from './minilang.js';
let inputElement = document.getElementById("input");
let bytecodeElement = document.getElementById("compiled");
let consoleElement = document.getElementById("console");
let debuggerElement = document.getElementById("debugger");
let framesElement = document.getElementById("debugger-frames");
let outputElement = document.getElementById("output");
let debugElement = document.getElementById("debug");
let compileElement = document.getElementById("compile");
let executeElement = document.getElementById("execute");
inputElement.onkeydown = function(event) {
if (event.keyCode === 9) {
event.preventDefault();
inputElement.setRangeText(' ', inputElement.selectionStart, inputElement.selectionStart, 'end');
}
}
Globals.print = function(caller, args) {
if (args.length === 0) return ml_resume(caller, MLNil);
let state = {caller, index: 1, run: function(self, value) {
consoleElement.appendChild(document.createTextNode(value.toString()));
if (self.index === args.length) return ml_resume(self.caller, MLNil);
ml_call(self, MLStringT.of, [args[self.index++]]);
}};
ml_call(state, MLStringT.of, [args[0]]);
}
compileElement.onclick = function() {
let request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status === 200) {
let json = request.responseText;
bytecodeElement.textContent = json;
try {
window.main = ml_decode(JSON.parse(json));
} catch (error) {
consoleElement.textContent = error;
}
} else {
consoleElement.textContent = request.responseText;
}
}
}
bytecodeElement.textContext = "";
request.open('POST', '/compile');
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send('input=' + encodeURIComponent(input.value));
}
let breakpoints = {};
let debug = {
run: debugger_run,
breakpoints: function(source) {
return breakpoints[source] || (breakpoints[source] = []);
}
};
executeElement.onclick = function() {
if (debugElement.checked) {
debug.step_in = true;
ml_debug(debug);
} else {
ml_debug(null);
}
consoleElement.textContent = "";
ml_call(null, window.main, []);
}
let debugee = null;
function debugger_run(state, value) {
console.log("debug", state, value);
while (framesElement.firstChild) framesElement.removeChild(framesElement.firstChild);
for (let s = state; s; s = s.caller) {
if (s.source !== undefined) {
let frameElement = document.createElement("li");
frameElement.textContent = s.source + ":" + s.line;
let localsElement = document.createElement("ul");
frameElement.appendChild(localsElement);
for (let decl = s.decl; decl; decl = decl.next) {
let localElement = document.createElement("li");
let value = s.stack[decl.index] || "?";
value = ml_deref(value) || "nil";
localElement.textContent = decl.name + " = " + value.toString();
localsElement.appendChild(localElement);
}
framesElement.appendChild(frameElement);
}
}
debugee = {state, value};
}
function debugger_continue(state, value, step_in, step_over) {
while (framesElement.firstChild) framesElement.removeChild(framesElement.firstChild);
debug.step_in = step_in;
state.step_over = step_over;
ml_resume(state, value);
}
document.getElementById("debugger-step-in").onclick = function() {
if (debugee) {
let d = debugee;
debugee = null;
debugger_continue(d.state, d.value, true, false);
}
}
document.getElementById("debugger-step-over").onclick = function() {
if (debugee) {
let d = debugee;
debugee = null;
debugger_continue(d.state, d.value, false, true);
}
}
document.getElementById("debugger-continue").onclick = function() {
if (debugee) {
let d = debugee;
debugee = null;
debugger_continue(d.state, d.value, false, false);
}
}
</script>
</body>
</html>