The hook
const kernel = require('./shibboleth/kernel');
const result = kernel.run(program, input, steps, { watch: s => { /* look at s */ } });
run in shibboleth/kernel.js takes an options object as its fourth argument. When watch is a function, the kernel calls it after every instruction with one object. The run is the same with or without it, with the same output and the same step count. A watch that throws ends the run with that error.
The object has 15 fields.
| field | is |
|---|---|
ins | the instruction just run, with op (a number, OP_NAMES[op] is its name), arg, at (its place in the code it came from) |
frame | the code being run, with code, pc (the next instruction), loops (open repeats, each { count, index }) |
depth | how many frames deep the run is; 1 is the program itself |
top | the place in the program of the instruction running at the top level |
stack | the stack, bottom first |
inPos | how many input bytes have been read |
out | the output bytes so far |
memory | the memory, cells numbered 0 up, or null until the first STORE |
ptr | the tape pointer, the cell that PTR puts |
grid | the grid cells row by row, or null until the grid is used |
gx | the grid column it stands on |
gy | the grid row it stands on |
gw | the grid width |
gh | the grid height |
dict | the compiled language, with names, bodies (each word's code), rules |
stack, out, memory and grid are the run's own arrays, not copies, and they keep changing. Copy what you want to keep. The memory has 1024 cells.
A tape heatmap
museum/docs/watch-heatmap.js runs a brainfuck hello world through shibboleth/witness/brainfuck.shib, the brainfuck front end written as reading rules, and counts how many steps the pointer spends on each cell.
// node museum/docs/watch-heatmap.js: a brainfuck program on the v3 kernel, with a watch that counts the steps spent on each tape cell
'use strict';
const fs = require('fs');
const path = require('path');
const kernel = require('../../shibboleth/kernel');
const ROOT = path.join(__dirname, '..', '..');
const language = kernel.parseLanguage(fs.readFileSync(path.join(ROOT, 'shibboleth/witness/brainfuck.shib'), 'utf8'));
const hello = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.';
const visits = [];
const watch = s => { visits[s.ptr] = (visits[s.ptr] || 0) + 1; };
const result = kernel.run(kernel.compile(language, hello), [], kernel.STEP_LIMIT, { watch });
const most = Math.max(...visits.filter(Boolean));
const SHADES = ' .:-=+*#%@';
console.log(`output: ${JSON.stringify(Buffer.from(result.out).toString())}, ${result.steps} steps`);
console.log('tape: ' + Array.from(visits, (n = 0) => SHADES[Math.ceil((n / most) * (SHADES.length - 1))]).join(''));
visits.forEach((n = 0, cell) => console.log(`cell ${cell} ${String(n).padStart(5)} ${'#'.repeat(Math.round((n / most) * 40))}`));
node museum/docs/watch-heatmap.js
output: "Hello World!\n", 4640 steps
tape: :**@%-.
cell 0 182 ######
cell 1 818 #########################
cell 2 773 ########################
cell 3 1292 ########################################
cell 4 1041 ################################
cell 5 402 ############
cell 6 132 ####The counts are kernel steps, not brainfuck commands. Each + is several instructions (PTR LOAD 1 ADD 256 MOD PTR STORE), so the busy cells are the ones the loops add to.
One trace for every machine
museum/server/trace.js turns a run into one shape that every machine shares, so one viewer can show them all. It has an adapter per machine, v2, micro, v3. Each runs the program with a watch and keeps a step for every instruction, up to 4,000 steps.
const trace = require('./museum/server/trace');
const t = trace.v3(kernel, language, source, input);
source is whatever kernel.compile takes, the program text for a language read by rules, or a list of tokens. The result has these fields.
machine,tokens,words,input,out,halted, andtotal, the run's step countparts, what this machine has, in the order a viewer shows it, each{ id, kind, label, tone }; withv3a part shows only when the run touches itsteps, one per instruction,{ at, w, wat, op, says, lit, v };atis the program token,wthe word it is inside,saysthe instruction in plain words,litthe parts it touched,veach part's value after it
This command prints one for +>++..
node -e 'const k = require("./shibboleth/kernel"), { v3 } = require("./museum/server/trace"), fs = require("fs"); const t = v3(k, k.parseLanguage(fs.readFileSync("shibboleth/witness/brainfuck.shib", "utf8")), "+>++.", []); console.log(t.machine, "halted:", t.halted, "steps:", t.total, "parts:", t.parts.map(p => p.id).join(" ")); console.log(JSON.stringify(t.steps.find(s => s.op === "STORE")))'
version 3 halted: true steps: 29 parts: stack mem read print
{"at":0,"w":null,"wat":0,"op":"STORE","says":"write to memory","lit":["mem"],"v":{"stack":{"top":[],"n":0},"loop":null,"mem":{"w":[0,1],"p":0},"grid":{"w":null,"x":0,"y":0},"read":0,"print":0}}The comment at the top of museum/server/trace.js describes every part kind's value.
In the browser
museum/server/watch.js defines <shib-watch>, the step-by-step viewer on Shibboleth's Train and Ask pages. It takes a trace. el.show(trace, { play }) draws the program with the running token lit, what the instruction does, and every part after the step; el.stop() stops one that plays. Its styles are WATCH_CSS in museum/server/pages.js.
The language pages of ///, Befunge, Brainfuck, Deadfish, Ook! and Underload use it too. Their Watch the machine button plays a trace of the page's first example, made with trace.v3 when the site is built.