diff options
| author | Brian Cully <bjc@spork.org> | 2025-08-23 13:27:50 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-08-23 13:27:50 -0400 |
| commit | 39042a3476ffbdc79d1e49a69cb5103628eb7265 (patch) | |
| tree | 2015faa21c5769640f815564c1021310eec4a378 | |
| parent | 0753ea2b9e10b0f5030b05e84cd8490cc9ec7b34 (diff) | |
| download | automathon-39042a3476ffbdc79d1e49a69cb5103628eb7265.tar.gz automathon-39042a3476ffbdc79d1e49a69cb5103628eb7265.zip | |
render call and data stacks
| -rw-r--r-- | index.html | 17 | ||||
| -rw-r--r-- | main.mjs | 58 | ||||
| -rwxr-xr-x | src/lib.rs | 10 |
3 files changed, 72 insertions, 13 deletions
@@ -18,10 +18,19 @@ </textarea> <button id='compile'>compile</button> <button id='tick'>tick</button> - <button id='run'>run</button> - <div id='datastack'></div> - <div id='callstack'></div> - <div id='wordlist'></div> + <button id='bench'>bench</button> + <fieldset> + <legend>stack</legend> + <ol id='stack'></ol> + </fieldset> + <fieldset> + <legend>call stack</legend> + <ol id='callstack'></ol> + </fieldset> + <fieldset> + <legend>word list</legend> + <div id='wordlist'></div> + </fieldset> <canvas id='arena' width='512' height='512'>no canvas!</canvas> <script src='./main.mjs' type='module'></script> </body> @@ -20,6 +20,47 @@ function selectorForIP(word, offset) { return sel; } +function initWordlist() { + const sel = selectorForIP(0, 0); + document.querySelectorAll(sel).forEach(e => { + e.classList.add('ip') + }); +} + +function renderStack(interp) { + document.querySelectorAll('#stack').forEach(e => { + while (e.lastChild) { + e.removeChild(e.lastChild); + } + interp.stack().reverse() + .forEach(datum => { + console.debug('creating elt for', datum); + const elt = document.createElement('li'); + elt.textContent = datum; + console.debug('elt', elt); + e.appendChild(elt); + return elt; + }); + }); +} + +function renderCallStack(interp) { + document.querySelectorAll('#callstack').forEach(e => { + while (e.lastChild) { + e.removeChild(e.lastChild); + } + interp.callstack().reverse() + .forEach(datum => { + console.debug('creating elt for', datum); + const elt = document.createElement('li'); + elt.textContent = `${datum.word}@${datum.offset}`; + console.debug('elt', elt); + e.appendChild(elt); + return elt; + }); + }); +} + async function loaded() { console.debug('running init'); const mod = await init(); @@ -45,15 +86,14 @@ async function loaded() { if (res) { const wordlist = wordlistElts(interp.wordlist()); wordlist.forEach(elt => wordlistContainer.appendChild(elt)); - const sel = selectorForIP(0, 0); - document.querySelectorAll(sel).forEach(e => { - e.classList.add('ip') - }); + initWordlist(); } }; document.querySelector('#tick').onclick = e => { console.debug('tick clicked', e); - interp.tick(); + if (!interp.tick()) { + interp.reset_ip(); + } console.info('callstack', interp.callstack()); console.info('stack', interp.stack()); @@ -62,11 +102,13 @@ async function loaded() { document.querySelectorAll('#wordlist .ip').forEach(e => e.classList.remove('ip')); const sel = selectorForIP(word, offset); document.querySelectorAll(sel).forEach(e => { - e.classList.add('ip') + e.classList.add('ip'); }); + renderStack(interp); + renderCallStack(interp); }; - document.querySelector('#run').onclick = e => { - console.debug('run clicked', e); + document.querySelector('#bench').onclick = e => { + console.debug('bench clicked', e); const start = performance.now(); let tickCount = 0; for (let i = 0; i < 1_000_000; i++) { @@ -119,11 +119,19 @@ impl ExportedInterp { } pub fn ip(&self) -> ExportedInstructionPointer { - let Some(interp) = self.i.as_ref() else { + let Some(interp) = &self.i else { return ExportedInstructionPointer { word: 0, offset: 0 } }; (&interp.ip).into() } + + pub fn reset_ip(&mut self) { + let Some(interp) = &mut self.i else { + return; + }; + interp.ip.word = 0; + interp.ip.offset = 0; + } } #[wasm_bindgen] |
