A program
shib run museum/docs/kernel.shib -e "2 3 ADD 48 ADD EMIT"
5museum/docs/kernel.shib is a language with no rules of its own, so a program in it is plain kernel instructions and numbers, separated by spaces.
#! shibboleth 3 seed=0 syllables=
The kernel alone: no reading rules and no words, so a program is kernel instructions and numbers.
The kernel is shibboleth/kernel.js, one file in plain Node. A run has a stack of values (whole numbers and texts), a memory of cells with a pointer, a 32 by 32 grid with a cursor, the input bytes and the output bytes.
The lessons end on the kernel, from its stack to a language of your own, and the Studio steps through any program one instruction at a time.
Instructions
49 instructions, by what they work on. Each is written with its stack effect; a b -- a+b takes a and b (b on top) and leaves their sum. Each heading is a link target, kernel.html#op-DUP; the brackets are #op-QUOTE and #op-UNQUOTE.
Stack
DUP a -- a a
Copy the top value.
DROP a --
Throw away the top value.
SWAP a b -- b a
Swap the top two values.
OVER a b -- a b a
Copy the second value onto the top.
shib run museum/docs/kernel.shib -e "65 66 OVER SWAP EMIT EMIT EMIT"
BAAArithmetic
ADD a b -- a+b
Add the top two numbers.
SUB a b -- a−b
Subtract the top number from the one below it.
MUL a b -- a×b
Multiply the top two numbers.
DIV a b -- a÷b
Divide the second number by the top, rounding toward 0 (dividing by 0 gives 0).
MOD a b -- a mod b
The remainder of the second number divided by the top (by 0 gives 0).
ROLL n -- r
A random number from 0 to top − 1, the same in every run with the same seed.
shib run museum/docs/kernel.shib -e "7 6 MUL 23 ADD EMIT 100 7 MOD 48 ADD EMIT"
A2Comparison
EQ a b -- a=b
1 if the top two values are equal, else 0.
LT a b -- a<b
1 if the second number is less than the top, else 0.
NOT a -- a=0
1 if the top is 0, else 0.
shib run museum/docs/kernel.shib -e "3 5 LT 48 ADD EMIT 3 3 EQ NOT 48 ADD EMIT"
10Memory or tape
LOAD addr -- v
Push the value in memory cell number <top> (1,024 cells, all 0 at the start).
STORE v addr --
Put the second value into memory cell number <top>.
PTR -- ptr
Push the memory pointer.
MOVE n --
Move the memory pointer by the top value (it wraps round at 1,024).
shib run museum/docs/kernel.shib -e "72 0 STORE 105 1 STORE 0 LOAD EMIT 1 LOAD EMIT"
HiInput and output
READ -- byte
Read one byte of input; −1 when the input is used up.
LEFT -- n
How many input bytes are still unread.
EMIT v --
Print the top value as a byte (a text prints as its characters).
TONE pitch ms --
Play a tone, given the pitch (a MIDI number) and the length in milliseconds.
shib run museum/docs/kernel.shib -e "LEFT TIMES READ EMIT END" --input hello
helloLoop or branch
IF flag --
Take the top value; if it is not 0 run on, else jump past ELSE or THEN.
ELSE --
The other branch of an IF.
THEN --
The end of an IF.
BEGIN --
The start of a loop that repeats until UNTIL finds a value that is not 0.
UNTIL flag --
Take the top value; if it is 0 go back to BEGIN, else leave the loop.
TIMES n --
Take the top value n and run everything up to END n times.
END --
The end of a TIMES loop.
I -- i
The round number of the innermost TIMES loop, counting from 0.
HALT --
Stop the program here.
shib run museum/docs/kernel.shib -e "5 TIMES I 48 ADD EMIT END"
01234Quotation, code as text
[ -- text
Start a quotation; the text up to the matching ] is pushed as one value.
] --
End a quotation.
CALL text -- …
Read the top value as program text and run it.
DIP x text -- … x
Run the top text with the value below it set aside, then put that value back.
CAT a b -- ab
Join the top two values into one text.
UNCONS text -- rest c
Split the first character off a text; pushes the rest, then that character (−1 when empty).
SIZE v -- n
The length of the top text (a number counts as 1).
STRING c -- c1 c2 …
String mode, push every character read until the character numbered <top> comes again.
REST -- text
Push the rest of the program text, the part not read yet.
REWRITE text --
Replace the rest of the program text with the top text.
FIND text part -- i
Where the top text first occurs in the text below it (−1 if nowhere).
SPLIT text i -- head tail
Cut the second text at position <top>: pushes the head, then the tail.
shib run museum/docs/kernel.shib -e "[ Hi ] EMIT [ 33 EMIT ] CALL"
Hi!Defining words
DEFINE name body --
While running, bind a name to a body; a lowercase name becomes a word, anything else a reading rule.
shib run museum/docs/kernel.shib -e "[ star ] [ 42 EMIT ] DEFINE [ star star star ] CALL"
***Grid
PUT v --
Write the top value into the grid cell under the cursor.
GET -- v
Push the value of the grid cell under the cursor.
STEP --
Move the grid cursor one cell the way it faces.
FACE dir --
Turn the grid cursor to 0 east, 1 south, 2 west, 3 north.
PEEK x y -- v
Push the program page cell at column x, row y (y on top).
POKE v x y --
Write a value into the program page at column x, row y, so the program changes itself.
shib run museum/docs/kernel.shib -e "72 PUT STEP 105 PUT 0 0 PEEK EMIT 1 0 PEEK EMIT"
HiHow the instructions behave at the edges
- An instruction that needs more values than the stack holds reads 0 for the missing ones.
- A text where a number is wanted counts as its length; a number where a text is wanted is the one character with that code.
- A word made with DEFINE is known to program text read after it, such as text run by CALL or DIP, as in the example above. The program around it was read before the word existed.
- The same program with the same input and seed gives the same output, ROLL included.
The .shib format
A language is a header line, then any number of reading rules and words. Every other line is a comment.
#! shibboleth 3 seed=brainfuck syllables= plain=off
brainfuck as a front end: eight reading rules over the pointer and memory; [ and ] are halves of IF/BEGIN pairs
"+" => PTR LOAD 1 ADD 256 MOD PTR STORE
"-" => PTR LOAD 255 ADD 256 MOD PTR STORE
">" => 1 MOVE
"<" => -1 MOVE
"." => PTR LOAD EMIT
"," => READ 1 ADD DUP IF 1 SUB THEN PTR STORE
"[" => PTR LOAD IF BEGIN
"]" => PTR LOAD NOT UNTIL THEN
- The header,
#! shibboleth 3, names the kernel;seed=names the language,syllables=is for languages a world grows, andplain=offstops plain kernel tokens being read from a program, so only the rules count. - A reading rule,
"x" => tokens, says that the textxin a program means those kernel tokens. The longest rule that matches wins; text no rule matches is skipped. - A word,
name => tokens, with a name of lowercase letters, is a name for a run of tokens. Rules and other words can use it.
The Studio's Make a language tab writes this format with a form.
The brainfuck language file, rule by rule.
| reads | runs | in words |
|---|---|---|
+ | PTR LOAD 1 ADD 256 MOD PTR STORE | load the cell under the pointer, add 1, keep it in 0 to 255, store it back |
- | PTR LOAD 255 ADD 256 MOD PTR STORE | the same with 255 added, which is 1 less once kept in 0 to 255 |
> | 1 MOVE | move the pointer one cell right |
< | -1 MOVE | move the pointer one cell left |
. | PTR LOAD EMIT | print the cell under the pointer |
, | READ 1 ADD DUP IF 1 SUB THEN PTR STORE | read a byte and store it; at the end of input (−1) store 0 |
[ | PTR LOAD IF BEGIN | if the cell is 0 jump past the matching ], else start a loop |
] | PTR LOAD NOT UNTIL THEN | repeat while the cell is not 0, then close the IF that [ opened |
shib run shibboleth/witness/brainfuck.shib -e "++++++++[>++++++++<-]>+."
ADeadfish keeps its accumulator in memory cell 0 and has two words. fix is Deadfish's reset of 256 and −1 to 0, num turns a number into its decimal digits.
#! shibboleth 3 seed=deadfish syllables= plain=off
Deadfish as a front end: the accumulator is memory cell 0; fix resets 256 and -1 to 0; num writes a number in decimal
"i" => 0 LOAD 1 ADD fix 0 STORE
"d" => 0 LOAD 1 SUB fix 0 STORE
"s" => 0 LOAD DUP MUL fix 0 STORE
"o" => 0 LOAD num EMIT 10 EMIT
fix => DUP 256 EQ OVER -1 EQ ADD IF DROP 0 THEN
num => [ ] SWAP BEGIN DUP 10 MOD 48 ADD rot CAT SWAP 10 DIV DUP NOT UNTIL DROP
rot => [ SWAP ] DIP SWAP
shib run shibboleth/witness/deadfish.shib -e "iisiiiisiiiiiiiio"
72
shib manual shibboleth/witness/brainfuck.shib
shibboleth/witness/brainfuck.shib: shibboleth 3
#! shibboleth 3 seed=brainfuck syllables= plain=off
reading rules
"+" => PTR LOAD 1 ADD 256 MOD PTR STORE
"-" => PTR LOAD 255 ADD 256 MOD PTR STORE
">" => 1 MOVE
"<" => -1 MOVE
…
"]" => PTR LOAD NOT UNTIL THENshib manual lists any language's rules and words. Befunge, Underload, Ook! and others are in shibboleth/witness/.
Other header settings
| setting | does |
|---|---|
reader=2d | the program is a page of characters walked by the grid cursor, as in Befunge |
reader=rewrite | the program is a text read one rule at a time, which REST and REWRITE can read and replace as it runs |
size=WxH | the page size for reader=2d |
numbers=big | arithmetic on whole numbers of any size, up to the digits in the limits below |
fold=case | reading rules match in any case |
called=plain | text run by CALL, DIP or DEFINE reads plain kernel tokens only, never the rules |
Limits
Kernel 01a56c547be1b62280eddea845ce969d7536b07b9fc5fddc4bc1981872df8565, the SHA-256 of shibboleth/kernel.js. A page that runs the kernel in the browser carries the same hash in <meta name="kernel-hash">.
| export | value | what it bounds |
|---|---|---|
STEP_LIMIT | 100,000 | steps in a run, unless the caller gives its own budget (shib run gives 1,000,000, or --steps) |
OUTPUT_LIMIT | 1,024 | bytes of output; the run ends there |
GRID_SIZE | 32 × 32 | the grid of PUT, GET, STEP, PEEK and POKE; the cursor wraps round at the edges |
BIG_DIGITS | 10,000 | digits of a number in a language with numbers=big |
LITERALS | 0 1 2 3 4 5 6 7 8 9 10 16 32 64 100 128 | the numbers a world writes when it changes a program; by hand any whole number works |
PAIRS | BEGIN … UNTIL, IF … THEN, TIMES … END, [ … ] | the brackets; one left open or unmatched does nothing |
shib run museum/docs/kernel.shib -e "BEGIN 0 UNTIL" --steps 1000
(stopped: steps)Worlds, challenges, stars
- A world is one seeded run of the search. Each generation it changes programs a little, runs them, and keeps the shortest program for each new behaviour. Programs that keep coming back become words of its language.
- A challenge is a task given by examples, these inputs and these outputs. There are 48 (
shibboleth/base/tasks.js), in the groups first, output, input, arith, text, logic, tricky. - A star is a challenge a world solves without being shown it.
How it works shows one generation step by step and puts the search against random search.
Run a world
shib evolve 1 300 --out runs
It prints each challenge as the world finds it, then writes runs/world-1/.
| file | holds |
|---|---|
language.shib | the language it grew; run programs in it with shib run runs/world-1/language.shib |
challenges.json | every star, with the program and the generation it came |
stats.ndjson | one line of counts each time the world sleeps and makes words |
feed.txt | its news as it went, such as new words, stars and changes of world |
archive.txt | every program it kept, with two of its inputs and outputs |
shib evolve is the plain search on the earlier kernel, shibboleth/base/kernel.js, whose instructions are the first 25 above. The models on Shibboleth's Train page run the same code in the browser, and Models lists them.