This is one of most popular esoteric programming languages - i.e. ones, which were created not for real work but for small puzzles end logic exercises - i.e. for fun and amusement.
Main feature of the language is extreme simplicity. Nevertheles it allows hypotetically to create any program, though the source code could be extremely large and execution time insanely long.
Brainfuck program is a sequence of characters. Each character is a command. In core implementation there are
just 8
commands which work with the following 4
areas of memory:
0
, i.e. the program is started from the beginning;32-bit
ints), initially filled with zeros;So there are 8
commands in total, of them 6
do some action on memory and move the code pointer to the next command:
>
- increases the data pointer (so it points to the next cell in the array);<
- decreases the data pointer;+
- increments the value in the current memory cell (i.e. one pointed by data pointer);-
- decrements the value in the current memory cell;.
- takes the value of current memory cell, converts it to character and prints to output;,
- takes one character from input and puts its ASCII code to the current
memory cell.Two more commands are used to change the code pointer in specific way, to create loops and conditions:
[
- peeks at current cell, if the value here is non-zero, then simply proceeds to the next instruction, but if the
value is 0
, then searches forward for corresponding ]
and sets code pointer immediately after this (i.e. skips
the block within brackets);]
- moves code pointer back, to corresponding [
.I.e. with code like +-[->[>-].].
the first opening bracket makes the interpreter jump to the end, behind last closing
bracket.
Any other characters are usually ignored so it is safe to use verbal comments, spaces and line breaks.
To make the language bit more handy, we will extend it a bit, adding two more input/output commands:
:
- takes the value of current memory cell and prints to output as a decimal integer (followed by space);;
- takes next decimal integer (probably, consisting of several digits) from input and puts it to the current cell.The reason for this addition is obvious - the original version of Brainfuck make it rather difficult to manipulate numbers in human-readable format.
So the program ,:
for example being fed on input with the symbol A
will print to output value 65
(it is the
ascii code of capital A
letter).
We will call this extended variant Brainfuck++ in the problem statements.
Some problems will allow two more operations, assuming that interpreter has additional internal storage - stack:
#
- copies value from the current cell to the top of the stack;$
- removes value from the top of the stack and puts it to the current cell (old value of the cell is lost).So the program ;#>$[-<+>]<:
will double the entered value.
Printing 5
:
+++++:
Printing !
:
++++++++ ++++++++ ++++++++ ++++++++ +.
Printing Hello world
:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.
>---.
+++++++.
.
+++.
>>.
<-.
<.
+++.
------.
--------.
>>+.
>++.
Summing two values:
;>;[-<+>]<: