-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
423 lines (400 loc) · 12 KB
/
Copy pathindex.html
File metadata and controls
423 lines (400 loc) · 12 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>MoonScript in the browser — PoC</title>
<style>
:root {
--bg: #0f1115;
--panel: #171a21;
--panel-2: #1e222b;
--border: #2a2f3a;
--text: #d7dce5;
--muted: #8b93a3;
--accent: #9b6dff;
--accent-2: #6ea8fe;
--err: #ff6b6b;
--warn: #f0b429;
--ok: #4ade80;
--mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
}
body {
display: flex;
flex-direction: column;
background: var(--bg);
color: var(--text);
font: 14px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}
header {
display: flex;
align-items: center;
gap: 12px;
padding: 10px 16px;
background: var(--panel);
border-bottom: 1px solid var(--border);
flex: none;
}
header h1 {
font-size: 15px;
margin: 0;
font-weight: 600;
}
.tag {
font: 11px/1 var(--mono);
color: var(--muted);
border: 1px solid var(--border);
border-radius: 999px;
padding: 4px 8px;
white-space: nowrap;
}
.spacer {
flex: 1;
}
.toolbar {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 16px;
background: var(--panel);
border-bottom: 1px solid var(--border);
flex: none;
flex-wrap: wrap;
}
button,
select {
font: inherit;
color: var(--text);
background: var(--panel-2);
border: 1px solid var(--border);
border-radius: 6px;
padding: 6px 12px;
cursor: pointer;
}
button:hover,
select:hover {
border-color: #3a4152;
}
button.primary {
background: linear-gradient(180deg, var(--accent), #7c4dff);
border-color: #7c4dff;
color: #fff;
font-weight: 600;
}
button:disabled {
opacity: 0.5;
cursor: default;
}
kbd {
font: 11px var(--mono);
background: #0b0d11;
border: 1px solid var(--border);
border-bottom-width: 2px;
border-radius: 4px;
padding: 1px 5px;
color: var(--muted);
}
.hint {
color: var(--muted);
}
main {
flex: 1;
display: grid;
grid-template-columns: 1fr 1fr;
min-height: 0;
}
.pane {
display: flex;
flex-direction: column;
min-width: 0;
min-height: 0;
}
.pane + .pane {
border-left: 1px solid var(--border);
}
.label {
flex: none;
padding: 6px 14px;
font: 11px var(--mono);
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--muted);
background: var(--panel);
border-bottom: 1px solid var(--border);
}
textarea {
flex: 1;
width: 100%;
resize: none;
border: 0;
outline: none;
background: var(--bg);
color: var(--text);
font: 14px/1.6 var(--mono);
padding: 14px;
tab-size: 2;
white-space: pre;
overflow: auto;
}
#output {
flex: 1;
margin: 0;
padding: 14px;
overflow: auto;
font: 13px/1.6 var(--mono);
white-space: pre-wrap;
word-break: break-word;
}
#output .err {
color: var(--err);
}
#output .warn {
color: var(--warn);
}
#output .muted {
color: var(--muted);
}
/* the <div id="dom-target"> the DOM interop example writes into */
#dom-target {
flex: none;
margin: 0 14px 10px;
padding: 8px 12px;
border: 1px dashed var(--border);
border-radius: 6px;
font: 12px/1.5 var(--mono);
color: var(--muted);
}
details {
flex: none;
border-top: 1px solid var(--border);
background: var(--panel);
max-height: 40%;
display: flex;
flex-direction: column;
min-height: 0;
}
details[hidden] {
display: none;
}
summary {
cursor: pointer;
padding: 6px 14px;
font: 11px var(--mono);
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--muted);
}
#compiled {
margin: 0;
padding: 0 14px 12px;
overflow: auto;
font: 12px/1.6 var(--mono);
white-space: pre;
color: var(--accent-2);
}
#status {
flex: none;
padding: 6px 14px;
font: 11px var(--mono);
color: var(--muted);
background: var(--panel);
border-top: 1px solid var(--border);
}
#status.ok {
color: var(--ok);
}
#status.err {
color: var(--err);
}
@media (max-width: 760px) {
main {
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr;
}
.pane + .pane {
border-left: 0;
border-top: 1px solid var(--border);
}
}
</style>
</head>
<body>
<header>
<h1>MoonScript in the browser</h1>
<span class="tag">proof of concept</span>
<span class="spacer"></span>
<span class="tag" id="versions">MoonScript → Lua · no server</span>
</header>
<div class="toolbar">
<button id="run" class="primary">▶ Run</button>
<button id="clear">Clear output</button>
<select id="examples" title="Load an example">
<option value="">Load example…</option>
</select>
<span class="spacer"></span>
<span class="hint"><kbd>Ctrl</kbd>/<kbd>Cmd</kbd> + <kbd>Enter</kbd> to run</span>
</div>
<main>
<section class="pane">
<div class="label">MoonScript</div>
<textarea id="editor" spellcheck="false" autocomplete="off" autocapitalize="off"></textarea>
</section>
<section class="pane">
<div class="label">Output</div>
<pre id="output"></pre>
<div id="dom-target">DOM target — untouched</div>
<details id="compiled-wrap" hidden>
<summary>Compiled Lua</summary>
<pre id="compiled"></pre>
</details>
<div id="status">Ready.</div>
</section>
</main>
<!--
Fengari is Lua 5.3 written in JavaScript. It is the same runtime LiveCodes
uses to run Lua (and the Fennel and Teal compilers). The MoonScript
compiler is a WebAssembly build loaded lazily by moonscript.js, because
the real MoonScript parser needs LPeg.
-->
<script src="https://cdn.jsdelivr.net/npm/fengari-web@0.1.4/dist/fengari-web.js"></script>
<script type="module">
import * as moon from './moonscript.js';
const editor = document.getElementById('editor');
const output = document.getElementById('output');
const compiled = document.getElementById('compiled');
const compiledWrap = document.getElementById('compiled-wrap');
const statusEl = document.getElementById('status');
const examplesEl = document.getElementById('examples');
const runButton = document.getElementById('run');
const versionsEl = document.getElementById('versions');
const domTarget = document.getElementById('dom-target');
const state = document.documentElement.dataset;
let runs = 0;
function write(text, cls) {
const span = document.createElement('span');
if (cls) span.className = cls;
span.textContent = text;
output.appendChild(span);
output.scrollTop = output.scrollHeight;
}
function clearOutput() {
output.textContent = '';
}
function setStatus(message, kind) {
statusEl.textContent = message;
statusEl.className = kind || '';
state.status = message;
}
async function run() {
clearOutput();
setStatus('Running…');
state.status = 'running';
runButton.disabled = true;
const started = performance.now();
let result;
try {
result = await moon.run(editor.value, { onPrint: (line) => write(line + '\n') });
} catch (err) {
runButton.disabled = false;
write((err && err.message ? err.message : String(err)) + '\n', 'err');
setStatus('Failed to load the compiler', 'err');
return;
}
// Anything the compiler itself printed (warnings, mostly).
for (const line of result.log || []) {
write(line + '\n', 'warn');
}
if (result.error) {
write(result.error.message.replace(/\n?$/, '\n'), 'err');
}
if (result.lua) {
compiled.textContent = result.lua;
compiledWrap.hidden = false;
} else {
compiled.textContent = '';
compiledWrap.hidden = true;
}
runs += 1;
const elapsed = Math.round(performance.now() - started);
state.runs = String(runs);
state.runMs = String(elapsed);
runButton.disabled = false;
if (result.error) {
setStatus(
result.error.kind === 'compile' ? 'Compile error' : 'Runtime error',
'err',
);
} else {
setStatus('Done in ' + elapsed + ' ms.', 'ok');
}
}
document.getElementById('run').addEventListener('click', run);
document.getElementById('clear').addEventListener('click', () => {
clearOutput();
compiled.textContent = '';
compiledWrap.hidden = true;
domTarget.innerHTML = 'DOM target — untouched';
setStatus('Ready.');
});
editor.addEventListener('keydown', (event) => {
if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
event.preventDefault();
run();
}
if (event.key === 'Tab') {
event.preventDefault();
const start = editor.selectionStart;
const end = editor.selectionEnd;
editor.value = editor.value.slice(0, start) + ' ' + editor.value.slice(end);
editor.selectionStart = editor.selectionEnd = start + 2;
}
});
for (const example of moon.examples) {
const option = document.createElement('option');
option.value = example.name;
option.textContent = example.name;
examplesEl.appendChild(option);
}
examplesEl.addEventListener('change', () => {
const chosen = moon.examples.find((example) => example.name === examplesEl.value);
if (!chosen) return;
editor.value = chosen.code;
examplesEl.value = '';
run();
});
editor.value = moon.examples[0].code;
window.addEventListener('load', async () => {
try {
const { moonscript, lua, lpeg } = await moon.versions();
// Compiling and running use two different Lua versions, which is a
// real property of this approach and worth showing rather than hiding.
const runtime = (window.fengari?.lua?.LUA_RELEASE) || 'Lua 5.3';
versionsEl.textContent = 'MoonScript ' + moonscript + ' · Fengari (' + runtime + ')';
versionsEl.title =
'Compiled by a WebAssembly build of ' +
lua +
' + ' +
lpeg +
', then run on Fengari (' +
runtime +
').';
} catch (err) {
// Offline, or the CDN is unreachable. Run will report it properly.
console.error(err);
}
run();
});
// Element ids and run state are exposed for scripted checks, so a check
// never has to match on rendered text.
Object.assign(window, { moon, __run: run, __editor: editor, __output: output });
</script>
</body>
</html>