Lab · Playground
Esoteric languages: Brainfuck and Befunge
Two famous esoteric languages to run in your browser — Brainfuck's eight commands on a byte tape, and Befunge's program counter wandering a 2-D grid.
Esoteric languages are made to make a point, not to get work done: how little a language needs to be universal, or how strange control flow can get and still compute. Both of these date from 1993, and both have interpreters small enough to live inside this page’s playground script, so they run instantly with nothing to download. Every block has a stdin button for the input commands.
Brainfuck
Urban Müller designed Brainfuck to have the smallest possible compiler (his was 240 bytes on the Amiga). There is a tape of 30 000 byte cells, a pointer into it, and eight commands:
|
move the pointer right / left |
|
add / subtract one in the current cell (bytes wrap: 255 + 1 is 0) |
|
write the cell as a character / read one character into it (0 at the end of input) |
|
while the current cell is not zero, run what is between them |
Every other character is a comment, which is how the programs below explain themselves.
Cell 0 is a loop counter; each pass adds to cells 1 and 2
++++++++++ c0 = 10
[ >+++++++ >+++ <<- ] c1 = 70 c2 = 30 (c0 back to 0)
>++ . c1 = 72 'H'
+++++++++++++++++++++++++++++++++ . c1 = 105 'i'
>+++ . c2 = 33 '!'
c0 = 10 (the counter) c1 = 57 (the character '9') c2 = 32 (a space)
++++++++++ [ >+++++ >+++ <<- ] >+++++++ >++ <<
++++++++++
[ >. >. <- <- ] print c1 and a space; decrease the digit and the counter
Press stdin and type some lowercase letters (no spaces); then Run
, read a character
[ until the end of input (where reading gives 0)
>++++ [ <-------- >- ] subtract 4 × 8 = 32: 'a' (97) becomes 'A' (65)
< . , print it and read the next
]
Type two digits into stdin (like 34) and Run; the sum must be under 10
, >, c0 = first digit c1 = second (as characters)
>++++++[ <-------- >- ] c1 = c1 minus 48: from the character '4' to the number 4
<[ <+ >- ] move it onto c0: now c0 is the character for the sum
<. print it
Befunge
Chris Pressey designed Befunge-93 to be as hard to compile as possible. The program is a grid of
80 × 25 characters, and the instruction pointer moves across it in a direction — right at first.
> < ^ v steer it, _ and | turn left/right or up/down depending on the value they pop,
? picks a random direction, and # jumps over the next cell. Numbers, arithmetic and output use a
stack: digits push themselves, + - * / % work on the top two items, . prints a number and
, a character, : duplicates, \ swaps, $ drops, and " toggles string mode, which pushes
each character it passes over. @ ends the program.
"!egnufeB morf olleH">:#,_@
" pushes the message last letter first, so the first letter ends up on top. Then : duplicates
the top, pops the copy: not zero, so the pointer turns left, , prints the letter, # hops over
:, and > sends it right again. When the stack is empty a pop gives 0, sends the pointer
right, and @ stops.
>:.1+:55+`#@_
There is no jump back to the start: while the count is not above 10, _ sends the pointer right,
it runs off the end of the line, and it reappears at the left edge. (The first number printed is
0 — popping an empty stack gives 0.)
&:." +",,&:." =",,+.55+,@
Press stdin, type two numbers such as 6 7, and Run: & reads a number, and . prints one.
&>:1-:v
^ _$>\:v
^ *_$.@
Give it a number from 1 to 12 on stdin. The top two rows count down, leaving n, n−1, … 1 on the stack; the loop on the right multiplies them together until it finds the empty stack underneath.
v>"H",@
>?< <
>"T",^
? sends the pointer off in a random direction. Left and right bounce straight back into it; up
reaches the > that prints H and stops; down prints T and loops back round. Run it a few times.