📜 ⬆️ ⬇️

Brainfuck Interpreter on Brainfuck

After finding a number of posts on Habré related to Brainfuck, including its interpretation, I had a desire to write my own Brainfuck interpreter. But in order to satisfy the necessary sensations that the language itself brings to us, it was necessary to write it in Brainfuck. And I partially got it. Immediately make a reservation about what I don’t have: this interpreter currently does not support loops and input data input (in the case of input data, there’s no where to read them, since the Brainfuck program is input) , if in a nutshell, the " [ ", " ] " and " , ".

Restrictions


Of the limitations (besides what the interpreter does not support) there are only two of the following:


Speed


I think you have already suggested, but I still clarify that you should not write interpreters on Brainfuck and the speed of work leaves much to be desired.

Obfuscated interpreter source code


If you still have a desire to experience what I did, then here is the source code itself:
,[>,]>>>>++<<<<<[<]>[[[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]>++++++++++[<++++>-]<+++<[>-<-]+>[<->[-]]<[->>>[<+<<+>>>-]<[>+<-]<<[>>>>>>>>>[>>->>]<<<<[<<<<]<<<<<-]>>>>>>>>>>>[>>>>]<+<[-]<<<<[<<<<]<<[<+<<+>>>-]<[>+<-]<<[>>>>>>>>>>>[>>>>]<<+<<<<[<<<<]<<<<<-]>>>>>>>>>[>>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]>]<<<<[<<<<]<<<<<]<<[<]>[[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]>+++++++++[<+++++>-]<<[>-<-]+>[<->[-]]<[->>>[<+<<+>>>-]<[>+<-]<<[>>>>>>>>>[>>->>]<<<<[<<<<]<<<<<-]>>>>>>>>>>>[>>>>]<-<[-]<<<<[<<<<]<<[<+<<+>>>-]<[>+<-]<<[>>>>>>>>>>>[>>>>]<<+<<<<[<<<<]<<<<<-]>>>>>>>>>[>>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]>]<<<<[<<<<]<<<<<]<<[<]>[[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]>+++++++++[<+++++>-]<+<[>-<-]+>[<->[-]]<[->>>[<+<<+>>>-]<[>+<-]<<[>>>>>>>>>[>>->>]<<<<[<<<<]<<<<<-]>>>>>>>>>>>[>>>>]<.<<<<<[<<<<]>>>>[>>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]>]<<<<[<<<<]<<<<<]<<[<]>[[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]>++++++++++[<++++++>-]<<[>-<-]+>[<->[-]]<[->>>-<<<]<<[<]>[[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]>++++++++++[<++++++>-]<++<[>-<-]+>[<->[-]]<[->>>+<<<]<<[<]>[-]>]

I will once again make a reservation that this interpreter does not support loops or loops, so that you do not have to test this interpreter for the correct operation of these capabilities.
')
You can execute Brainfuck code on brainfuck.tk , and use the following code as an example of input data:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+.+.>++++++++++.<.-.-.
which should output the next two lines:
ABC
CBA


Source


Before the source code I will clarify some points:

ASCII character codes used:

Source:
 ,[>,]  brainfuck  >>>>++<<<<       (N  4) <[<]>    :   N   0 [  [[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]      (N  1) >++++++++++[<++++>-]<+++   43  (N  2)  <[>-<-]       43 +>[<->[-]]<[-     "";  : (N  1) >>>[<+<<+>>>-]<[>+<-]<<   (N  4)    (N  1) [>>>>>>>>>[>>->>]<<<<[<<<<]<<<<<-]         >>>>>>>>>>>[>>>>]<+<[-]    <<<<[<<<<]<<[<+<<+>>>-]<[>+<-]<<   (N  4)   (N  1) [>>>>>>>>>>>[>>>>]<<+<<<<[<<<<]<<<<<-]    >>>>>>>>>[>>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]>]    <<<<[<<<<]<<<<<    (N  1) ] <<[<]>    :   (N  1)   0  [[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]      (N  1) >+++++++++[<+++++>-]<   45  (N  2)  <[>-<-]       45 +>[<->[-]]<[-     "";  : (N  1) >>>[<+<<+>>>-]<[>+<-]<<   (N  4)    (N  1) [>>>>>>>>>[>>->>]<<<<[<<<<]<<<<<-]         >>>>>>>>>>>[>>>>]<-<[-]    <<<<[<<<<]<<[<+<<+>>>-]<[>+<-]<<   (N  4)   (N  1) [>>>>>>>>>>>[>>>>]<<+<<<<[<<<<]<<<<<-]    >>>>>>>>>[>>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]>]    <<<<[<<<<]<<<<<    (N  1) ] <<[<]>    :   (N  1)   0  [[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]      (N  1) >+++++++++[<+++++>-]<+   46  (N  2)  <[>-<-]       46 +>[<->[-]]<[-     "";  : (N  1) >>>[<+<<+>>>-]<[>+<-]<<   (N  4)    (N  1) [>>>>>>>>>[>>->>]<<<<[<<<<]<<<<<-]         >>>>>>>>>>>[>>>>]<.<    <<<<[<<<<]>>>>[>>[-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]>]    <<<<[<<<<]<<<<<    (N  1) ] <<[<]>    :   (N  1)   0  [[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]      (N  1) >++++++++++[<++++++>-]<   60  (N  2)  <[>-<-]       60 +>[<->[-]]<[-     "";  : (N  1) >>>-<<<     1 ] <<[<]>    :   (N  1)   0  [[>]>+>+<<<[<]>-]+[>]>>-[<<<[<]>+[>]>>-]      (N  1) >++++++++++[<++++++>-]<++   62  (N  2)  <[>-<-]       62 +>[<->[-]]<[-     "";  : (N  1) >>>+<<<     1 ] <<[<]>[-]>          (  ) ] 

Instead of conclusion


It would also be interesting to see working cycles, but at the moment I do not have a complete picture of how they should be implemented, so as soon as I have a fully functional implementation of the interpreter, I will update this article.

PS
Thanks to habrachelovek iSage for providing links to Brainfuck interpreters on Brainfuck.

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


All Articles