Today I took a look at the probably most famous esoteric programming language, brainfuck (https://en.wikipedia.org/wiki/Brainfuck). Now you might say why would you waste time on such a language but I was always interested and wanted to at least understand the "Hello World" program of the language. My goal was to write "Hello DEV.to" based on this.
Basics
There are only eight commands in the language, all of them consist of a single character.
Character | Command |
---|---|
> | increment the pointer (move to the right) |
< | decrement the pointer (move to the left) |
+ | increment the byte at the position of the pointer |
- | decrement the byte at the position of the pointer |
. | output the byte at the position of the pointer |
, | accept one byte of input, stored in the cell at the position of the pointer |
[ | starting a "while" loop if the byte at the position of the pointer is zero then the loop gets skipped otherwise it iterates through the loop till the byte is zero |
] | closing the loop |
As an interpreter, I used the following website https://fatiherikli.github.io/brainfuck-visualizer/ which also shows you the position of the pointer and the current value.
Hello World!
First I took the "Hello World!" example from the German Wikipedia https://de.wikipedia.org/wiki/Brainfuck#Hello_World! to see what actually is happens.
++++++++++
[
>+++++++>++++++++++>+++>+<<<<-
]
>++.
>+.
+++++++.
.
+++.
>++.
<<+++++++++++++++.
>.
+++.
------.
--------.
>+.
>.
+++.
If you run this it will print "Hello World!", but what is actually happening here?
Let's look at the first line, this sets the content of the first cell to 10. This is followed by a loop which results in:
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 70 | 100 | 30 | 10 |
As written in the basics the loop runs till the content at the current cell is zero. In our case, the pointer is at cell one which had 10 as content before.
When we take a closer look at the line in the loop we see the pointer moves to cell two and increases the content in that cell 7 times, then the pointer goes to cell three and increases that 10 times. The pointer moves again to cell four and increases by 3, in cell five the content gets increased by 1. After that, the pointer moves back four cells (so we are in cell one again) and decreases the content by 1, which means the loop runs 10 times.
>+++++++>++++++++++>+++>+<<<<-
After the loop the next line prints the first ASCII character of our output "H".
>++.
So the pointer moves one cell to the right, to cell two and increases the content by 2 and prints the ASCII character the decimal code 72. Our cell content is now:
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 72 | 100 | 30 | 10 |
The next letter we need is the "e" so we need a decimal 101 to print that value, the closest we have is the 100 in cell three . So we move the pointer there, increase by one and print.
>+.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 72 | 101 | 30 | 10 |
For the "l" we need 108 and again cell number three is the closest, so we increase the value to 108 and print the ASCII character.
+++++++.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 72 | 108 | 30 | 10 |
The next letter is also "l" so we just print the same value again.
.
By now the output shows "Hell", the missing "o" is ASCII character 111 so we can stay in the current cell and increase the value by 3.
+++.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 72 | 111 | 30 | 10 |
I guess the concept is clear now so I will not describe the rest of the "Hello World" program.
Hello DEV.to
Now let us see how we can write "Hello DEV.to", we start with the same word so the first part of the program is the same as above
++++++++++
[
>+++++++>++++++++++>+++>+<<<<-
]
>++.
>+.
+++++++.
.
+++.
Now we need a space, decimal 32, closest is the 30 in cell number four so we move the pointer there, increase the value and print it.
>++.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 72 | 111 | 32 | 10 |
Now we start with "DEV.to", "D" is 68 so the closest is cell two, again we move the pointer (2x) and this time decrement the content.
<<----.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 68 | 111 | 32 | 10 |
"E" is next to "D" so we only need to increase the value by 1.
+.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 69 | 111 | 32 | 10 |
For the "V" (86) we stay in the same cell and just increase the value.
+++++++++++++++++.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 86 | 111 | 32 | 10 |
Now we move the pointer to cell four and increase the 32 to 46, the decimal representation of the ASCII character ".".
>>++++++++++++++.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 86 | 111 | 46 | 10 |
The "t" (116) and "o" (111) are closest to cell 3, so we move the pointer back and increase to 116, print "t" and then decrement again to 111 for the "o".
<+++++.
-----.
Cell 1 | Cell 2 | Cell 3 | Cell 4 | Cell 5 |
---|---|---|---|---|
0 | 86 | 111 | 46 | 10 |
Other ways to write "Hello DEV.to"
++++++++++
[
>+++++++>+++++++++++>++++>+<<<<-
]
>++.
>---------.
+++++++.
.
+++.
>--------.
<<----.
+.
+++++++++++++++++.
>>++++++++++++++.
<+++++.
-----.
++++++++++
[
>+++++++>+++++++++++>++++>+<<<<-
]
>++.
>---------.
+++++++.
.
+++.
>--------.
<<----.
+.
<++++
[>++++<-]
>+.
<++++++++
[>-----<-]
>.
>+++++.
-----.
++++++++++
[>++++++++++>++++++++++<<-]
+++
[>----------<-]
>++.
>+.
+++++++.
.
+++.
<<+++
[>>>+++++++++++<<<-]
>>>-.
>>+++++++
[<++++++++++>-]
<--.
+.
>++
[<++++++++>-]
<+.
>++++
[<---------->-]
<.
<<+++++.
-----.
Top comments (1)
the syntax really brainfuck