📜 ⬆️ ⬇️

Hello, Brainfuck!

Brainfuck - Turing is a complete language, that is, you can write any program on it that you can write at all :) And we will write “Hello World”, or rather several;)

Introduction


Brainfuck consists of only 8 teams:



Interpreters


')
For the execution of programs written in Brainfuck, you can use:


Hello, Brainfuck!



Let's write our first Brainfuck program that will display the text “Hello World!”. The algorithm is simple:
1. Increase the value in the cell until it is equal to the desired character.
2. Print the value in the cell (our symbol)
3. Go to the next cell.
4. Repeat until all characters are displayed.

Result:
++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++.> ++++++++
++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
+++++++++++++.> +++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++.> +++++++++
++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
++++++++++++++++++++++> ++++++++++++++++++++++++++ ++++++.> ++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++ ++++++++++++++.> ++++++++++++++++
++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
+++++++++++++++.> +++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++ .> +++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++>
++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
+++++++++++++++++++. +++++++++++++++++++++++++++++ ++++.


We implement the algorithm and on php - the script will generate the source code on Brainfuck, which displays the necessary text:
$ text = “Hello, world!”;
$ length = strlen ($ text);
for ($ i = 0; $ i <$ length; $ i ++) {
$ curr = ord ($ text [$ i]);
for ($ j = 1; $ j <= $ curr; $ j ++) {
echo "+";
if ($ j == $ curr & $ i! = $ length-1) {
echo ".>";
} elseif ($ j == $ curr) {
echo ".";
}
}


“Hello World!” Turned out to be quite large in size, we will try to optimize it. It is not necessary to go to a new cell each time and start the “path” to the desired symbol from scratch - we can use the current cell value. New algorithm:
1. Increase the value in the cell until it is equal to the desired character.
2. Print the value in the cell (our symbol)
3. If the next required symbol is greater than the current one, then we increase the value in the cell, if it is less, we decrease it until we get the required one.
4. Print the value in the cell (our symbol)
5. Repeat, starting with paragraph 3, until all the characters will not be vyvdeny

Result:
++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++ ++++++++
++++++++++++++++++++++ ++++++++ +++ .--------------- -------------------------------
---------------------------------. ++++++++++++++++ +++++++++++++++++++++++++++++++
++++++++. ++++++++++++++++++++++++ +++. ----- ---.---------------------------
----------------------------------------.


Already less :) Here is the php code that generates a similar source:
$ text = “Hello, world!”;
$ length = strlen ($ text);
$ curr = 0;
for ($ i = 0; $ i <$ length; $ i ++) {
$ delta = ord ($ text [$ i]) - $ curr;
$ curr = ord ($ text [$ i]);
if ($ delta <0) {
$ op = "-";
$ delta = abs ($ delta);
} else {
$ op = "+";
}
for ($ j = 1; $ j <= $ delta; $ j ++) {
echo $ op;
}
echo ".";
}


Can I still optimize the resulting code on Brainfuck? Can! Now we will again use several cells, but we realize and use multiplication. And previously unused teams [and] will help in this. Multiplication - using the sum, 5 * 10 = 10 + 10 + 10 + 10 + 10. Ie, we need to repeat the addition of 10 to the cell 5 times. Algorithm of the program:
1. Increase the value in the cell until it is 10 - the counter for the cycle, the multiplier
2. The cycle begins [
3. Increase the value in the cell until it is equal to n - the result of the integer division of the code of the desired character by 10
4. Go to the next cell.
5. Repeat step 3-4 for all characters.
6. Reduce the loop count (first cell)
7. The cycle is over]
8. Go to the next cell after the counter.
9. Add to the cell the remainder of the integer division of the code of the desired character by 10 (for “H” in the cell we will have 70, add 2 = 72% 10. Ie we will get “H” = (72/10) + (72% ten))
11. Print the value in the cell (our symbol) and go to the next cell.
12. Repeat p.9-11 for all characters

Result:
++++++++++ [> +++++++> +++++++++> +++++++++ +++++++++ ++> +++++++++++> +++> ++++++++> +++
++++++++> +++++++++++> +++++++++> +++++++++ +++ << <<< <<<<<<<<< -]> ++.> +.> ++++++++.> ++++++++.> +.> ++.> +++++ ++.> +.> ++++.> ++++++++.>.> +++.

Voila! Php generator code:
$ text = “Hello, world!”;
$ length = strlen ($ text);
echo "+++++++++++ [";
for ($ i = 0; $ i <$ length; $ i ++) {
echo ">";
$ ops = floor (ord ($ text [$ i]) / 10);
for ($ j = 1; $ j <= $ ops; $ j ++) {
echo "+";
}
}
for ($ i = 0; $ i <$ length; $ i ++) {
echo "<";
}
echo "-]";
for ($ i = 0; $ i <$ length; $ i ++) {
echo ">";
$ ops = ord ($ text [$ i])% 10;
for ($ j = 1; $ j <= $ ops; $ j ++) {
echo "+";
}
echo ".";
}


Total



Here is such an interesting and wonderful language Brainfuck :) In conclusion, I’ll give a generator written by me on Brainfuck itself. This program reads the entered text and generates Brainfuck code (optimized), which this text will output.
>>>>>>>> +++++++++++++++++++++++++++++++++++++++++++ +>
+++++++++++++++++++++++++++++++++++++ ++
++++++++++++++++++++++++++++++++++++++++ ++
++++++++++++++++++++++++++++++++++++++++++++++++ ++
++++++++++++++++++++++++++++++++++++++++ ++
+++++++++++++++++++++++> ++++++++++++++++++++++++++ ++
++++++++++++++++++++++++++++++++++++++++++++++++ ++
+++++++++++++++++++++++++++++++++++++++++++++++ ++
+++++++++++++++++++++++++++++++++++++++ <<<<<<<<<<<<< <<
, ---------- [++++++++++ >>>>>>>> .......... >>. >> <<<< <<
<<<< [-] ++++++++++ << [> + >>>> + <<<<< -] >>>>> [<<<< >>> >>> > -
] <<<< >> [-]> [-] << [>> +> + <<< -] >>> [<<< + >>> -] >> [-] <<<<< <[
>>>> + >> + <<<<<<<] >>>>>> [<<<<<< >>>>>> -] <<< [>> +> + <<< -]
>>> [<<< + >>> -] [-] << [>> [-] <[> [-] + <[-]] <[-]]> [-]> [<<< <<
<- >>> - >>> [-] <<<<<< [>>>> + >> + <<<<<<] >>>>>> [<<<<<< +> >>
>>> -] <<< [>> +> + <<< -] >>> [<<< + >>> -] [-] << [>> [-] <[> [-] + <[
-]] <[-]]> [-]>] <<<<<< [>> + <[>> +> + <<< -] >>> [<<< >>> >>>]] > [
-] <<<<<< [>>>> + >> + <<<<<< -] >>>>>> [<<<<<< >>>>>> -] <<< [>
> +> + <<< -] >>> [<<< + >>> -] [-] << [>> [-] <[> [-] + <[-]] <[-]] > [
-]> [<<<<<< - >>> - >>> [-] <<<<<< [>>>> + >> + <<<<<< -] >>>>>> [<
<<<<< + >>>>>> -] <<< [>> +> + <<< -] >>> [<<< + >>> -] [-] << [>> [ -]
<[> [-] + <[-]] <[-]]> [-]>] <<<<<] >>> [> +> + << -] >> [<< + >> -]
[-] + <[> [-] << [<< - >> -] << [>> + << -] >>> [-]]> [<<< <<[-] >>> > [
-]] <<< [- >>>>>. <<<<<] >>>>>>>>>>. <<<<. >>.>. <<<<<<< [[ ->
>>>. <<<<] >>>>>>>>>>. <<<<. <. >>. >>. <<<<<<<<<<<<<< --- -
-----]

Source: https://habr.com/ru/post/24605/


All Articles