esolang

Kernel

The machine under every Shibboleth language. A language is a text file that says how to read source text into its instructions.

A program

shib run museum/docs/kernel.shib -e "2 3 ADD 48 ADD EMIT"
5

museum/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"
BAA

Arithmetic

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"
A2

Comparison

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"
10

Memory 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"
Hi

Input 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
hello

Loop 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"
01234

Quotation, 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"
Hi
How 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, and plain=off stops plain kernel tokens being read from a program, so only the rules count.
  • A reading rule, "x" => tokens, says that the text x in 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.

readsrunsin words
+PTR LOAD 1 ADD 256 MOD PTR STOREload the cell under the pointer, add 1, keep it in 0 to 255, store it back
-PTR LOAD 255 ADD 256 MOD PTR STOREthe same with 255 added, which is 1 less once kept in 0 to 255
>1 MOVEmove the pointer one cell right
<-1 MOVEmove the pointer one cell left
.PTR LOAD EMITprint the cell under the pointer
,READ 1 ADD DUP IF 1 SUB THEN PTR STOREread a byte and store it; at the end of input (−1) store 0
[PTR LOAD IF BEGINif the cell is 0 jump past the matching ], else start a loop
]PTR LOAD NOT UNTIL THENrepeat while the cell is not 0, then close the IF that [ opened
shib run shibboleth/witness/brainfuck.shib -e "++++++++[>++++++++<-]>+."
A

Deadfish 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 THEN

shib manual lists any language's rules and words. Befunge, Underload, Ook! and others are in shibboleth/witness/.

Other header settings
settingdoes
reader=2dthe program is a page of characters walked by the grid cursor, as in Befunge
reader=rewritethe program is a text read one rule at a time, which REST and REWRITE can read and replace as it runs
size=WxHthe page size for reader=2d
numbers=bigarithmetic on whole numbers of any size, up to the digits in the limits below
fold=casereading rules match in any case
called=plaintext 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">.

exportvaluewhat it bounds
STEP_LIMIT100,000steps in a run, unless the caller gives its own budget (shib run gives 1,000,000, or --steps)
OUTPUT_LIMIT1,024bytes of output; the run ends there
GRID_SIZE32 × 32the grid of PUT, GET, STEP, PEEK and POKE; the cursor wraps round at the edges
BIG_DIGITS10,000digits of a number in a language with numbers=big
LITERALS0 1 2 3 4 5 6 7 8 9 10 16 32 64 100 128the numbers a world writes when it changes a program; by hand any whole number works
PAIRSBEGIN … 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/.

fileholds
language.shibthe language it grew; run programs in it with shib run runs/world-1/language.shib
challenges.jsonevery star, with the program and the generation it came
stats.ndjsonone line of counts each time the world sleeps and makes words
feed.txtits news as it went, such as new words, stars and changes of world
archive.txtevery 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.