share on twitter

Brainfuck interpreter and visualizer

visit me at janetschel.dev

Delay: 100ms


-

+

Output:


A quick Brainfuck guide

Brainfuck is an esoteric programming language created in 1993 by Urban Müller.

The language consists of only eight simple commands and an instruction pointer. While it is fully Turing complete, it is not intended for practical use, but to challenge and amuse programmers. Brainfuck simply requires one to break commands into microscopic steps.

Hence the name Brainfuck.

>

increment the data pointer (to point to the next cell to the right).



<

decrement the data pointer (to point to the next cell to the left).



+

increment (increase by one) the byte at the data pointer.



-

decrement (decrease by one) the byte at the data pointer.



.

output the byte at the data pointer.



,

accept one byte of input, storing its value in the byte at the data pointer.



[

if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching

]

command.



]

if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching

[

command.



These are the basic commands, which operate on an infinitely long tape where cells are incremented and decremented respectively.

All other characters are treated as comments (= ignored).

Implementation details

Since the creater of Brainfuck left some details of the behaviour of the interpreter pretty vague, each compiler/interpreter can (and probably will) handle some details differently.

Here are some important implementation details of this interpreter/visualzier:

Hello World!

Lets look at a simple 'Hello World!' in Brainfuck. Try to figure out what happens at each line.

All steps are annotated with comments.

++++++++

Set Cell #0 to 8


[


>++++

Add 4 to Cell #1; this will always set Cell #1 to 4


[

as the cell will be cleared by the loop


>++

Add 2 to Cell #2


>+++

Add 3 to Cell #3


>+++

Add 3 to Cell #4


>+

Add 1 to Cell #5


<<<<-

Decrement the loop counter in Cell #1


]

Loop till Cell #1 is zero; number of iterations is 4


>+

Add 1 to Cell #2


>+

Add 1 to Cell #3


>-

Subtract 1 from Cell #4


>>+

Add 1 to Cell #6


[<]

Move back to the first zero cell you find


<-

Decrement the loop Counter in Cell #0


]

Loop till Cell #0 is zero; number of iterations is 8



>>.

Cell #2 has value 72 which is 'H'


>---.

Subtract 3 from Cell #3 to get 101 which is 'e'


+++++++..+++.

Likewise for 'llo' from Cell #3


>>.

Cell #5 is 32 for the space


<-.

Subtract 1 from Cell #4 for 87 to give a 'W'


<.

Cell #3 was set to 'o' from the end of 'Hello'


+++.------.--------.

Cell #3 for 'rl' and 'd'


>>+.

Add 1 to Cell #5 gives us an exclamation point


>++.

And finally a newline from Cell #6


This concludes our little 'Hello World!' example in Brainfuck.

You can see it looks pretty intimidating, but it really is not if you once get the hang of it. Other people, for example, have manage to build the game TicTacToe with a full-blown computer AI.

If we shorten the 'Hello World!' code from above and remove all unnecessary characters, this is what we get:

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

Generate your own Brainfuck

If you want to generate your own Brainfuck code, without going through the trouble of typing it out, you can just type any ASCII character in this text-field and Brainfuck code will automatically be generated.

It will (probably) not be perfectly minified Brainfuck code but I tried my best to use as much loops as possible.

Please keep in mind that this tool is only able to translate text into Brainfuck. It can not interpret and do logical things!

You can try it out right here:





Output:

Your output will appear here...

Some more examples

Alphabet

After our little 'Hello World!' Brainfuck program (and maybe your own output-programs) take a look at this Brainfuck code. It will print the entire English alphabet to the screen.

Brainfuck code to do so:

>++++[<++++++>->++++++++>++++++++++++++++>++++++++<<<]<++>>>+<<<[>>>.>[->+<<+>]<.>>[-<+<->>]<<+<.<<-]




Fibonacci Sequence

In contrast to all other examples which were always printing something, this program will not print anything to the output.

Instead the output will be left in the memory once the program exits. For this to be effective, you need to have a big enough monitor to view the tape.

This program will print the first six fibonacci numbers, then exit.

+>+>+++++-[-[->+<]<<[->>>>+>+<<<<<]>[->>>>>+>+<<<<<<]>>>[-<<<<+>>>>]>[-<<<+>>>]>[-<<<<<+>>>>>]>[-<<<<<+>>>>>]<<<<]





For even more Brainfuck files, you can visit this GitHub repository; or you can experiment by yourself in the above interpreter and visualizer.

Any more ideas? You can contact me at my GitHub