📜 ⬆️ ⬇️

Quine on Brainfuck, Tutorial

On Habré there are already many articles about quains and technologies of their writing. Quine lovers can get bored - there is a template, you follow it, you get a quine. It is also easy to write a multilanguage quine, even with the participation of esoteric languages, and there are at least three articles about this. This is where quine entirely written in esoteric languages ​​comes to the rescue, returning the bored programmer with interest.

I'll try to talk about the process of writing quine on Brainfuck.


I will quote my article Multilingual Quine
')
A quay-like construction ( hereinafter, it is only about languages ​​where data and code can be separated ) can be divided into two parts:
1) the data segment (important point, this part remains empty until the code is completed, after that it is encoded).
2) code segment
A data segment is a kind of cipher that can be decrypted in two ways. As a result of decryption, the first method will result in a string
representation of the actual data segment; decryption in the second way will result in a string representation of the code segment. Combining the results of these transcripts, we get a string that contains all the source code of the program.

Pay attention to the selected text. There are no variables in the brainfack, there is only and exclusively executable code. On the other hand, there is a tape that can and should be used to store information.
Therefore, for a brainfack, the Quine scheme will look like this.

1) Code that writes data to tape
2) A code that reads data from a tape and prints a code from item 1
3) A code that reads data from a tape and displays the code from clause 2 and clause 3

Accordingly, the code from item 1 can be obtained by “encrypting” the code from item 2 and item 3, when it is written.

How to write data to tape? It begs to write in the form of ascii-character codes.

We will write a function on Python that receives a string and returns a program on the brainfix that writes this string to the tape.
def to_brainfuck(st): return '>'+''.join('+'*ord(c)+'>' for c in st) 


 >>> print to_brainfuck('ku') >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++> 


Pay attention to the ">" at the beginning of the program, an empty cell at the beginning of the tape is needed in case you have to return there. A blank cell is the only way to stop the Brainfack cycle. If the implementation of the brainfack works with an infinite tape in both directions, the ">" at the beginning can be omitted.

Now back to the beginning of the tape.

 <[<] 


and print its contents:

 >[.>] 


The program will display "ku"

We already know how to write the code for item 1 and item 3 of our scheme.
Now p.2, the hardest part.

So, you need to write a program that, using the contents of the tape, will output the line that the print command to_brainfuck ('ku') printed, actually an analogue of the Python function to_brainfuck on the brainfack.
Brainfack can print only those characters that are on the tape. Command "." prints the contents of the cell on which the carriage stands. Therefore, we need to write the characters "+" and ">" to tape.

 >+++++++++++++++++++++++++++++++++++++++++++> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++. 


Pay attention to the ">" at the beginning - we leave an empty marker cell between what is already on the tape and any service information that we write there. Also pay attention to the "." in the end. Remember the ">" at the very beginning of the program? Here we will derive it.

back to the beginning of the tape

 [<]<[<]> 


Now let's write a nested loop. the inner loop “plucks” one by one from the contents of the next cell and at each such pinching displays “+”, the outer loop outputs “>” and proceeds to the next cell

 [ [ [>]>      ,   "+" .  "+" <<[<]>-        ] >[>]>>  ,   ,   ,   ">" .  ">" [<]<[<]>     ] 


So, we can write program A, the filling tape and program B, running along the filled tape and outputting program A.
True, program B has one drawback, after its activity there is nothing left on the tape, but we need the contents of the tape for subsequent printing.

We modify program B so that with each decrease in the contents of the cell at one end of the tape, it increases the contents of the cell at the other end. I put the new code under the old one and align it so that the changes made are visible:
 [[[>]>. << [<]>-]>[>]>>. [<]<[<]>]  [[[>]>. [>]<+[<]< [<]>-]>[>]>>. [>]+ [<]<[<]>]  


After executing the program, the contents of the cells with a copy of 1 more than you need. We will not redo anything, just fix it and print it.

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


I don’t really like this code, it seems to be much shorter, something like:

 >>>[-.>] 


only without removing ascii-0 at the end, but it is not invented immediately.
We put everything together

   "k"  "u"   >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>   "+"  ">"  ,     ">" >+++++++++++++++++++++++++++++++++++++++++++> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.   ,      >+     [<]<[<]>    ,    [[[>]>.[>]<+[<]<[<]>-]>[>]>>.[>]+[<]<[<]>]          >>>[->]<<[<]>>>[.>] 


So, we have a program that clogs the character codes “k” and “u” onto a tape, then prints out the part that clogs them, then prints the characters themselves. Those. at the moment it is almost quine, the program prints its first part, and instead of the second it prints "ku". And why? Because the string “ku” we fed at the beginning of the Python function to_brainfuck. And should have the text of the program. But he was not there yet. But now there is!
Now is the most pleasant moment. We take that part of the program that we wrote with our hands and fed the to_brainfuck functions. Combine the two parts and quine ready:

 >>> s=""">+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+[<]<[<]>[[[>]>.[>]<+[<]<[<]>-]>[>]>>.[>]+[<]<[<]>]>>>[->]<<[<]>>>[.>]""" >>> print to_brainfuck(s)+s 


Quine code
 >++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+[<]<[<]>[[[>]>.[>]<+[<]<[<]>-]>[>]>>.[>]+[<]<[<]>]>>>[->]<<[<]>>>[.>] 



The resulting quine is not too compact due to the chosen coding method, for each character of the “code segment” there are about 50 characters of the “data segment”. You can significantly reduce the data segment if you resort to other coding methods, of course, in this case, the part of the program that you need to write with your hands will greatly increase.

Yes, another funny moment. Since Brainfack ignores all characters except those eight that are its commands, you can somehow interestingly format the code and / or add some inscriptions there before encryption . Quine from this will not cease to be Quine.

 >+++++++++++++++++++++++++++++++++++++++++++> + + + Quine for Habrahabr + + + + + +++++++++++++++++++++++++++++++++++++++++++++ + + + + + + + + + . > + [ < ] < [ < ] > [ [ [ > ] > . [ > ] < + [ < ] < [<]>-]>[>]>>.[>]+[<]<[<]>]>>>[->]<<[<]>>>[.>] 


Inside the square, you can add something else. I do not give the full text of Quine, because such games with formatting significantly increase the size of the “data segment” and quine becomes very huge. Thanks for attention.
Has anyone read to the end? :) Update: wow! someone even likes it! Thank!
It seems that I managed to write an article that can claim the title of the most useless tutorial Habr (and perhaps not only Habr)!

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


All Articles