-
Notifications
You must be signed in to change notification settings - Fork 11
/
repl.html
213 lines (201 loc) · 6.83 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scrapscript Web REPL</title>
<link rel="shortcut icon" type="image/x-icon" href="data:image/vnd.microsoft.icon;base64,AAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAD/AAAA////AAAA/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAREBEQAAAAAQAQEAEAAAABABAQAQAAAAAREBEQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACIiIiIiIiIiIiIiIiIiIiIiERIiESEiIiIiESEiISIiIhEiISIhEiIiIREiESEhIiIiIiIiIiIiIiIiIiIiIiIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA">
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Rubik:ital,wght@0,500;0,700&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Nunito+Sans:ital,wght@0,400;0,700;0,900;1,400&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="/style.css" />
</head>
<body>
<main>
<div>
<p>See <a href="https://scrapscript.org/">scrapscript.org</a> for a slightly
out of date language reference.</p>
<p>This REPL is completely client-side and works by running
<a href="https://github.com/tekknolagi/scrapscript">scrapscript.py</a> in the
browser using <a href="https://pyodide.org/">Pyodide</a>.</p>
</div>
<div>
<!-- TODO(max): Add button to save to/load from disk. -->
<button id="clear-local-storage">Clear</button>
</div>
<div id="output" style="height: 400px; overflow: auto;">
Output:
</div>
<div>
<input id="input" type="text" disabled placeholder="Loading pyodide..." /><input id="submit-input" type="submit" value="Submit" />
</div>
<script type="module">
"use strict";
function updateHistory(inp, out) {
const wrap = document.createElement("div");
const pre_inp = document.createElement("pre");
pre_inp.setAttribute("class", "language-text");
const code_inp = document.createElement("code");
code_inp.setAttribute("class", "language-text");
code_inp.append(`>>> ${inp}`);
pre_inp.append(code_inp);
const pre_out = document.createElement("pre");
pre_out.setAttribute("class", "result language-text");
const code_out = document.createElement("code");
code_out.setAttribute("class", "language-text");
code_out.append(`${out}`);
pre_out.append(code_out);
wrap.append(pre_inp);
wrap.append(pre_out);
output.append(wrap);
output.scrollTop = output.scrollHeight;
}
async function sendRequest(exp) {
const scrap = document.scrapscript;
const [result, monad] = document.monad.bind(scrap.parse(scrap.tokenize(exp)));
return {result: scrap.pretty(result), monad, ok: true};
}
let input = document.getElementById("input");
const output = document.getElementById("output");
const history = [];
const clearButton = document.getElementById("clear-local-storage");
const submitButton = document.getElementById("submit-input");
function renderHistory() {
for (const [inp, out] of history) {
updateHistory(inp, out);
}
}
function loadFromLocalStorage() {
history.length = 0;
output.innerHTML = "";
renderHistory();
}
function expandInput() {
if (input.tagName === "TEXTAREA") {
return;
}
const textarea = document.createElement("textarea");
textarea.setAttribute("id", "input");
textarea.setAttribute("rows", "1");
textarea.setAttribute("style", "height: 1.5em;");
textarea.value = input.value;
input.replaceWith(textarea);
input = textarea;
input.focus();
input.addEventListener("keyup", e => inputHandler(e));
}
function shrinkInput() {
if (input.tagName === "INPUT") {
return;
}
const newInput = document.createElement("input");
newInput.setAttribute("id", "input");
newInput.setAttribute("type", "text");
newInput.value = input.value;
input.replaceWith(newInput);
input = newInput;
input.focus();
input.addEventListener("keyup", e => inputHandler(e));
}
async function submitInput() {
const response = await sendRequest(input.value);
if (response.ok) {
const {result, monad} = response;
updateHistory(input.value, result);
history.push([input.value, result]);
input.value = "";
document.monad = monad;
} else {
const msg = await response.text();
updateHistory(input.value, msg);
input.value = "";
}
}
loadFromLocalStorage();
input.focus();
async function inputHandler(e) {
// TODO(max): Make up/down arrow keys navigate history
if (e.key === "Enter") {
if (e.shiftKey && input.tagName === "INPUT") {
// Shift-Enter expands the input to a textarea and does not submit
// input.
expandInput();
return;
}
if (input.tagName === "TEXTAREA") {
// Enter in a textarea should not submit input.
if (e.shiftKey) {
shrinkInput();
}
return;
}
submitInput()
}
}
input.addEventListener("keyup", e => inputHandler(e));
clearButton.addEventListener("click", () => {
window.localStorage.clear();
loadFromLocalStorage();
input.focus();
});
submitButton.addEventListener("click", () => {
submitInput()
input.focus();
});
</script>
<script>
"use strict";
async function main() {
// const scrapscript = await fetch('/scrapscript.py');
let indexURL = "https://cdn.jsdelivr.net/pyodide/v0.25.1/full/";
const urlParams = new URLSearchParams(window.location.search);
const buildParam = urlParams.get("build");
if (buildParam) {
if (["full", "debug", "pyc"].includes(buildParam)) {
indexURL = indexURL.replace(
"/full/",
"/" + urlParams.get("build") + "/",
);
} else {
console.warn(
'Invalid URL parameter: build="' +
buildParam +
'". Using default "full".',
);
}
}
const { loadPyodide } = await import(indexURL + "pyodide.mjs");
const pyodide = await loadPyodide({
stdout: (msg) => console.log(`Pyodide: ${msg}`),
});
await pyodide.runPythonAsync(`
from pyodide.http import pyfetch
response = await pyfetch("/scrapscript.py")
with open("scrapscript.py", "wb") as f:
f.write(await response.bytes())
`)
const scrapscript = await pyodide.pyimport("scrapscript");
document.monad = scrapscript.ScrapMonad(scrapscript.boot_env());
document.scrapscript = scrapscript;
input.removeAttribute("disabled");
input.setAttribute("placeholder", "Enter Scrapscript code here...");
input.focus();
}
main();
</script>
</main>
<script data-goatcounter="https://scrapscript.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
</body>
</html>