summaryrefslogtreecommitdiffstats
path: root/site
diff options
context:
space:
mode:
authorBrian Cully <bjc@spork.org>2025-08-25 13:35:12 -0400
committerBrian Cully <bjc@spork.org>2025-08-25 13:57:21 -0400
commit37c0a0b5e4d3939656cac8d103b5a22963fbc9dd (patch)
tree8843628b991ac39991d22f80df9aa1d2a22cffc4 /site
parentf2bcc1df6ef43a6021adfcd380fc899ca8e6533a (diff)
downloadautomathon-37c0a0b5e4d3939656cac8d103b5a22963fbc9dd.tar.gz
automathon-37c0a0b5e4d3939656cac8d103b5a22963fbc9dd.zip
load forth from server
Diffstat (limited to 'site')
-rw-r--r--site/index.html17
-rw-r--r--site/main.mjs26
-rw-r--r--site/samples/slo-fac.fs20
3 files changed, 41 insertions, 22 deletions
diff --git a/site/index.html b/site/index.html
index 6583568..5939d37 100644
--- a/site/index.html
+++ b/site/index.html
@@ -10,20 +10,13 @@
<div id='editor'>
<div id='code'>
+ <select id='src-select'>
+ <option>fac.fs</option>
+ <option>slo-fac.fs</option>
+ </select>
<button id='compile'>compile</button>
<br>
- <div id='src' contenteditable='plaintext-only'>: fac ( n -- n )
- dup ( n n )
- 1 > if ( n flag )
- dup ( n n )
- 1 - ( n n-1 )
- fac ( n fac-n )
- * ( n )
- then ( n )
-;
-
-5 fac
-drop</div>
+ <div id='src' contenteditable='plaintext-only'></div>
</div>
<div id='state-container'>
diff --git a/site/main.mjs b/site/main.mjs
index 3add1fe..22d1f96 100644
--- a/site/main.mjs
+++ b/site/main.mjs
@@ -85,12 +85,38 @@ function tick(vm) {
renderTextHighlight(vm);
}
+function loadForth(taintedPath) {
+ // ascii only + ‘-’, ‘_’, ‘.’, and ‘/’, but no ‘../’
+ const path =
+ taintedPath
+ .replace(/[^-_A-Za-z./]/g, '')
+ .replace(/\.\.\//g, '');
+ fetch(`./samples/${path}`)
+ .then(resp => {
+ if (!resp.ok) {
+ throw `http status ${resp.status}`
+ }
+ return resp.text()
+ })
+ .then(text => {
+ document.querySelector('#src').textContent = text;
+ })
+ .catch(e => {
+ console.error(`couldn't fetch ‘${path}’`, e);
+ });
+}
+
async function loaded() {
console.debug('running init');
const mod = await init();
console.debug('init done', mod);
const vm = make_vm();
+ document.querySelectorAll('#src-select').forEach(async sel => {
+ sel.onchange = _ => loadForth(sel.value);
+ loadForth(sel.value);
+ });
+
document.querySelector('#compile').onclick = e => {
console.debug('compile clicked', e);
diff --git a/site/samples/slo-fac.fs b/site/samples/slo-fac.fs
index 0ba50ca..1de4dc8 100644
--- a/site/samples/slo-fac.fs
+++ b/site/samples/slo-fac.fs
@@ -8,19 +8,19 @@
\ and be something nice to look at with blinken
: count-to ( n -- n n-1 ... 1 )
- dup
- 1 > if
- dup 1 - count-to
- then
+ dup
+ 1 > if
+ dup 1 - count-to
+ then
;
: stack-mul ( n-x ... n x -- v )
- dup
- 1 > if
- rot rot * swap 1 - stack-mul
- else
- drop
- then
+ dup
+ 1 > if
+ rot rot * swap 1 - stack-mul
+ else
+ drop
+ then
;
5 count-to