-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.html
170 lines (163 loc) · 6.57 KB
/
repl.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>stacked</title>
<link href="style.css" rel="STYLESHEET" type="text/css">
<script src="./src/decimal.js"></script>
<script src="./src/stdlib.js"></script>
<script src="./src/utf8.js"></script>
<script src="./src/color.js"></script>
<script src="./src/icon.js"></script>
<script src="./src/table.js"></script>
<script src="./src/element.js"></script>
<script src="./src/turtle.js"></script>
<script src="./src/funcs.js"></script>
<script src="./src/complex.js"></script>
<script src="./src/automata.js"></script>
<script src="./src/stacked.js"></script>
</head>
<body>
<header>
<h1>stacked repl <a href="./">(interpreter)</a> <a href="https://github.com/ConorOBrien-Foxx/stacked">(github)</a></h1>
</header>
<div id="repl"></div>
<script>
const highlight = (prog) => {
try {
return tokenize(prog, { keepWhiteSpace: true, ignoreError: true }).map(e => {
let s = e.raw;
//if(e.type === "string" || e.type === "charString"){
// s = repr(e.value);
//}
return "<span class=\"" + e.type + "\">" + s + "</span>";
}).join("");
}
catch(e) {
return prog;
}
};
let lastError = "";
error = (e) => {
throw new Error(e);
}
let replInfo = {
stack: [],
vars: clone(vars),
ops: clone(ops),
history: [],
index: 0,
display: repr,
};
// todo: minimal repr
ops.set("setdisp", new StackedFunc([
[[STP(FUNC_LIKE)], function(f){
replInfo.display = (e) => f.overWith(this, e);
}],
], 1));
ops.set("cls", function(){
repl.innerHTML = "";
replInfo.history = [];
replInfo.index = 0;
});
const replStep = () => {
let holder = document.createElement("div");
holder.className = "current";
let ptr = document.createElement("span");
ptr.appendChild(document.createTextNode("> "));
ptr.className = "pointer";
holder.appendChild(ptr);
let input = document.createElement("input");
input.rows = "1";
input.className = "replStage";
holder.appendChild(input);
repl.appendChild(holder);
input.addEventListener("keydown", (e) => {
if(e.key === "ArrowUp"){
if(replInfo.index > 0)
setTimeout(() => input.value = replInfo.history[--replInfo.index], 0);
} else if(e.key === "ArrowDown"){
if(replInfo.index + 1 < replInfo.history.length)
setTimeout(() => input.value = replInfo.history[++replInfo.index], 0);
} else if(e.key === "Enter"){
let syntaxed = document.createElement("span");
syntaxed.innerHTML = highlight(input.value);
let result = document.createElement("div");
result.classList.add("result");
let makeError = (e) => {
holder.classList.add("error");
let message = e.message ? "error: " + e.message : e.toString();
errEl = document.createElement("div");
errEl.appendChild(document.createTextNode(message));
}
let inst;
let errEl;
try {
inst = new Stacked(input.value);
} catch(e){
makeError(e);
let repstack = repr(replInfo.stack);
result.innerHTML = highlight(repstack);
holder.removeChild(input);
holder.appendChild(syntaxed);
if(errEl)
holder.appendChild(errEl);
holder.appendChild(result);
holder.classList.remove("current");
holder.classList.add("finished");
replStep();
return;
}
let outted = [];
inst.output = (e) => {
let spn = document.createElement("span");
spn.classList.add("output");
spn.appendChild(document.createTextNode(pp(e)));
outted.push(spn);
};
inst.stack = clone(replInfo.stack);
inst.vars = clone(replInfo.vars);
inst.ops = clone(replInfo.ops);
replInfo.history.push(input.value);
replInfo.index = replInfo.history.length;
try {
inst.run();
} catch(e){
inst.stack = replInfo.stack;
inst.vars = replInfo.vars;
inst.ops = replInfo.ops;
makeError(e);
}
replInfo.stack = inst.stack;
replInfo.vars = inst.vars;
replInfo.ops = inst.ops;
let repstack = replInfo.display(replInfo.stack);
result.innerHTML = highlight(repstack);
holder.removeChild(input);
holder.appendChild(syntaxed);
if(outted.length)
holder.appendChild(document.createElement("br"));
outted.forEach(el => holder.appendChild(el));
if(errEl)
holder.appendChild(errEl);
holder.appendChild(result);
holder.classList.remove("current");
holder.classList.add("finished");
replStep();
} else if(e.key === "Escape"){
setTimeout(() => input.value = "", 0)
}
});
input.focus();
};
replStep();
let encountered = new Map([]);
const before = () => {
let c = code.value;
let res = encountered.has(c) ? encountered.get(c) : Decimal(0);
vars.set("execCounter", res);
encountered.set(c, res.add(1));
}
</script>
</body>
</html>