summaryrefslogtreecommitdiffstats
path: root/main.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'main.mjs')
-rw-r--r--main.mjs63
1 files changed, 39 insertions, 24 deletions
diff --git a/main.mjs b/main.mjs
index f8c9267..dcd2ec7 100644
--- a/main.mjs
+++ b/main.mjs
@@ -15,9 +15,7 @@ function wordlistElts(wordlist) {
}
function selectorForIP(word, offset) {
- const sel = `#wordlist x-bytecode[x-index='${word}'] x-op[x-index='${offset}']`;
- console.debug('using sel', sel);
- return sel;
+ return `#wordlist x-bytecode[x-index='${word}'] x-op[x-index='${offset}']`;
}
function initWordlist() {
@@ -34,10 +32,8 @@ function renderStack(interp) {
}
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;
});
@@ -51,16 +47,29 @@ function renderCallStack(interp) {
}
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;
});
});
}
+function tick(interp) {
+ if (!interp.tick()) {
+ interp.reset_ip();
+ }
+
+ const { word, offset } = interp.ip();
+ document.querySelectorAll('#wordlist .ip').forEach(e => e.classList.remove('ip'));
+ const sel = selectorForIP(word, offset);
+ document.querySelectorAll(sel).forEach(e => {
+ e.classList.add('ip');
+ });
+ renderStack(interp);
+ renderCallStack(interp);
+}
+
async function loaded() {
console.debug('running init');
const mod = await init();
@@ -75,7 +84,6 @@ async function loaded() {
console.debug('removing child', wordlistContainer.lastChild)
wordlistContainer.removeChild(wordlistContainer.lastChild);
}
- console.debug('container has firstchild', wordlistContainer.firstChild)
// always add a newline until i decide what to do with the parser.
const text = document.querySelector('textarea').value + '\n';
@@ -87,25 +95,13 @@ async function loaded() {
const wordlist = wordlistElts(interp.wordlist());
wordlist.forEach(elt => wordlistContainer.appendChild(elt));
initWordlist();
+ renderStack(interp);
+ renderCallStack(interp);
}
};
document.querySelector('#tick').onclick = e => {
console.debug('tick clicked', e);
- if (!interp.tick()) {
- interp.reset_ip();
- }
- console.info('callstack', interp.callstack());
- console.info('stack', interp.stack());
-
- const { word, offset } = interp.ip();
- console.info('ip', word, offset);
- document.querySelectorAll('#wordlist .ip').forEach(e => e.classList.remove('ip'));
- const sel = selectorForIP(word, offset);
- document.querySelectorAll(sel).forEach(e => {
- e.classList.add('ip');
- });
- renderStack(interp);
- renderCallStack(interp);
+ tick(interp);
};
document.querySelector('#bench').onclick = e => {
console.debug('bench clicked', e);
@@ -115,9 +111,28 @@ async function loaded() {
tickCount += interp.run();
}
const end = performance.now();
- console.info(`run took ${end-start} ms for ${tickCount} ticks (${(end-start)/tickCount * 1000000} µs/tick).`);
+ console.info(`run took ${end-start} ms for ${tickCount} ticks (${(end-start)/tickCount * 1_000_000} ns/tick).`);
console.info('result', interp.stack());
};
+
+ let blinkenRun = false;
+ document.querySelector('#blinken').onclick = e => {
+ console.debug('blinken clicked', e);
+ blinkenRun = !blinkenRun;
+ if (blinkenRun) {
+ e.target.textContent = 'haltenblinken';
+ } else {
+ e.target.textContent = 'blinken';
+ }
+ const frameRate = 6;
+ const onTimeout = _ => {
+ if (blinkenRun) {
+ tick(interp);
+ setTimeout(onTimeout, 1_000 / frameRate);
+ }
+ }
+ setTimeout(onTimeout);
+ }
window.interp = interp;
}