2013-09-01 05:38:33 +04:00
|
|
|
---
|
2018-03-14 05:55:50 +03:00
|
|
|
language: bf
|
2018-03-13 23:27:08 +03:00
|
|
|
filename: bf.bf
|
2013-09-01 05:38:33 +04:00
|
|
|
contributors:
|
2013-09-04 12:24:15 +04:00
|
|
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
|
|
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
2023-02-16 07:51:31 +03:00
|
|
|
- ["rilysh", "https://github.com/rilysh"]
|
2013-09-01 05:38:33 +04:00
|
|
|
---
|
|
|
|
|
2013-09-04 12:24:15 +04:00
|
|
|
Brainfuck (not capitalized except at the start of a sentence) is an extremely
|
|
|
|
minimal Turing-complete programming language with just 8 commands.
|
2013-09-01 05:38:33 +04:00
|
|
|
|
2023-02-16 07:51:31 +03:00
|
|
|
You can try brainfuck on your browser with [brainfuck-online](https://copy.sh/brainfuck/).
|
2015-03-24 12:11:14 +03:00
|
|
|
|
2017-01-01 02:25:24 +03:00
|
|
|
```bf
|
2013-09-01 05:38:33 +04:00
|
|
|
Any character not "><+-.,[]" (excluding quotation marks) is ignored.
|
|
|
|
|
|
|
|
Brainfuck is represented by an array with 30,000 cells initialized to zero
|
|
|
|
and a data pointer pointing at the current cell.
|
|
|
|
|
|
|
|
There are eight commands:
|
|
|
|
+ : Increments the value at the current cell by one.
|
|
|
|
- : Decrements the value at the current cell by one.
|
|
|
|
> : Moves the data pointer to the next cell (cell on the right).
|
|
|
|
< : Moves the data pointer to the previous cell (cell on the left).
|
|
|
|
. : Prints the ASCII value at the current cell (i.e. 65 = 'A').
|
|
|
|
, : Reads a single input character into the current cell.
|
|
|
|
[ : If the value at the current cell is zero, skips to the corresponding ] .
|
|
|
|
Otherwise, move to the next instruction.
|
|
|
|
] : If the value at the current cell is zero, move to the next instruction.
|
|
|
|
Otherwise, move backwards in the instructions to the corresponding [ .
|
|
|
|
|
|
|
|
[ and ] form a while loop. Obviously, they must be balanced.
|
|
|
|
|
2013-09-04 12:24:15 +04:00
|
|
|
Let's look at some basic brainfuck programs.
|
2013-09-01 05:38:33 +04:00
|
|
|
|
|
|
|
++++++ [ > ++++++++++ < - ] > +++++ .
|
|
|
|
|
|
|
|
This program prints out the letter 'A'. First, it increments cell #1 to 6.
|
|
|
|
Cell #1 will be used for looping. Then, it enters the loop ([) and moves
|
|
|
|
to cell #2. It increments cell #2 10 times, moves back to cell #1, and
|
|
|
|
decrements cell #1. This loop happens 6 times (it takes 6 decrements for
|
|
|
|
cell #1 to reach 0, at which point it skips to the corresponding ] and
|
|
|
|
continues on).
|
|
|
|
|
|
|
|
At this point, we're on cell #1, which has a value of 0, while cell #2 has a
|
|
|
|
value of 60. We move on cell #2, increment 5 times, for a value of 65, and then
|
|
|
|
print cell #2's value. 65 is 'A' in ASCII, so 'A' is printed to the terminal.
|
|
|
|
|
|
|
|
|
|
|
|
, [ > + < - ] > .
|
|
|
|
|
2013-09-04 12:24:15 +04:00
|
|
|
This program reads a character from the user input and copies the character into
|
|
|
|
cell #1. Then we start a loop. Move to cell #2, increment the value at cell #2,
|
|
|
|
move back to cell #1, and decrement the value at cell #1. This continues on
|
|
|
|
until cell #1 is 0, and cell #2 holds cell #1's old value. Because we're on
|
|
|
|
cell #1 at the end of the loop, move to cell #2, and then print out the value
|
|
|
|
in ASCII.
|
2013-09-01 05:38:33 +04:00
|
|
|
|
2013-09-20 23:17:53 +04:00
|
|
|
Also keep in mind that the spaces are purely for readability purposes. You
|
2013-09-04 12:24:15 +04:00
|
|
|
could just as easily write it as:
|
2013-09-01 05:38:33 +04:00
|
|
|
|
|
|
|
,[>+<-]>.
|
|
|
|
|
|
|
|
Try and figure out what this program does:
|
|
|
|
|
2013-09-20 23:17:53 +04:00
|
|
|
,>,< [ > [ >+ >+ << -] >> [- << + >>] <<< -] >>
|
2013-09-01 05:38:33 +04:00
|
|
|
|
|
|
|
This program takes two numbers for input, and multiplies them.
|
|
|
|
|
|
|
|
The gist is it first reads in two inputs. Then it starts the outer loop,
|
|
|
|
conditioned on cell #1. Then it moves to cell #2, and starts the inner
|
2013-09-20 23:17:53 +04:00
|
|
|
loop conditioned on cell #2, incrementing cell #3. However, there comes a
|
2013-09-21 00:01:47 +04:00
|
|
|
problem: At the end of the inner loop, cell #2 is zero. In that case,
|
|
|
|
inner loop won't work anymore since next time. To solve this problem,
|
2013-09-01 05:38:33 +04:00
|
|
|
we also increment cell #4, and then recopy cell #4 into cell #2.
|
2013-09-21 00:01:47 +04:00
|
|
|
Then cell #3 is the result.
|
2013-09-01 05:38:33 +04:00
|
|
|
```
|
|
|
|
|
2013-09-20 23:17:53 +04:00
|
|
|
And that's brainfuck. Not that hard, eh? For fun, you can write your own
|
|
|
|
brainfuck programs, or you can write a brainfuck interpreter in another
|
2013-09-01 05:38:33 +04:00
|
|
|
language. The interpreter is fairly simple to implement, but if you're a
|
2013-09-04 12:24:15 +04:00
|
|
|
masochist, try writing a brainfuck interpreter… in brainfuck.
|
2023-02-16 07:51:31 +03:00
|
|
|
|
|
|
|
## Further Reading
|
|
|
|
* [esolang-wiki](https://esolangs.org/wiki/Brainfuck)
|
|
|
|
* [learn brainfuck](http://cydathria.com/bf/brainfuck.html)
|
|
|
|
* [other resources](http://www.muppetlabs.com/~breadbox/bf/)
|