One of the most famous esolangs.
Brainfuck operates on an array of memory cells, each initially set to zero. A pointer is initially pointing to the first memory cell. All characters that are no operations should be ignored, and considered to be comments.
> | Move the pointer to the right |
< | Move the pointer to the left |
+ | Increment the memory cell at the pointer |
- | Decrement the memory cell at the pointer |
. | Output the character signified by the cell at the pointer |
, | Input a character and store it in the cell at the pointer |
[ | Jump past the matching ] if the cell at the pointer is 0 |
] | Jump back to the matching [ if the cell at the pointer is nonzero |
Interpreter
Memory should normally consist of 8 bit cells, and wrap on overflow and underflow. Negative memory addresses should NOT be assumed to exist, however, an interpreter may provide some. Memory should consist of at least 30000 cells, some existing brainfuck programs do need more so this should be configurable or unbounded.
Evaluation
Evaluation consists of moving along the memory an incrementing or decrementing the value stored in the 8-bit cells.
+++ Store 3 at 0 >> Move to 2 + Store 1 at 2
Loop
A simple loop is a matter of decrementing a counter in a cell:
+++++ Store 5 at 0 [-] Decrement
IO
The output operator emits the ASCII value at the cell.
++++++++
++++++++
++++++++
++++++++
++++++++
. Write 0x28, or '('
Compiler
Brainfuck was invented by Urban Müller in 1993, in an attempt to make a language for which he could write the smallest possible compiler for the Amiga OS, version 2.0. He managed to write a 240-byte compiler.
Here is a compiler from Brainfuck to Uxn bytecode, it will emit an hexadecimal string that can be copy-pasted back into the interpreter and evaluated natively:
incoming: uxn devlog 2021