Previous Up Next

Very short BF interpreter written in BF

Of course, it could be expected that someone would come up with a shorter (and much faster) BF interpreter written in BF than the one I wrote. On November 4, 2002, I received the following email from NYYRIKKI:


Hi,

I saw your BrainFuck interpreter for BrainFuck and it encouraged me to write my own version of it. I now write to you as I thought, you might be interested about it.

I wanted to write as fast version as possible so I wrote it directly with Brainfuck. Here is a list of key tricks, that I used to get maximum performance:

This program works same way as yours. Only difference is, that program termination character is ":" instead of "!"

No more explaining... here is the code:

>>>+[,>+++++++[<------>-]<[->+>+<<]>>[-<<+>>]<->+<[>-<[-]]>[-<<[-]++++
+>>>>>]<<[->+>+<<]>>[-<<+>>]<-->+<[>-<[-]]>[-<<[-]++++++++>>>>>]<<[->+
>+<<]>>[-<<+>>]<--->+<[>-<[-]]>[-<<[-]++++++>>>>>]<<[->+>+<<]>>[-<<+>>
]<---->+<[>-<[-]]>[-<<[-]+++++++>>>>>]<<[->+>+<<]>>[-<<+>>]++++++[<---
>-]+<[>-<[-]]>[-<<[-]++++>>>>>]<<[->+>+<<]>>[-<<+>>]+++++[<---->-]+<[>
-<[-]]>[-<<[-]+++>>>>>]<<[->+>+<<]>>[-<<+>>]+++++++[<------->-]+<[>-<[
-]]>[-<<[-]+>>>>>]<<[->+>+<<]>>[-<<+>>]+++++++[<------->-]<-->+<[>-<[-
]]>[-<<[-]++>>>>>]<++++[<---->-]<]<<<[<<<]>>>
[-->+<[>-]>[>]<<++>[-<<<<
<+[>-->+<[>-]>[-<<+>>>]<<+>+<[>-]>[-<<->>>]<<+<[-<<<+>>>]<<<]>>>>>]<->
+<[>-]>[>]<<+>[-<->>>[>>>]>[->+>>+<<<]>[-<+>]>>[-[->>+<<]+>>]+>[->+<]>
[-<+>>>[-]+<<]+>>[-<<->>]<<<<[->>[-<<+>>]<<<<]>>[-<<<<<+>>>>>]<<<<<<<[
>>[-<<<+>>>]<<<<<]+>>[>-->+<[>-]>[-<<->>>]<<+>+<[>-]>[-<<+>>>]<<+<[->>
>+<<<]>>>]<]<--->+<[>-]>[->>[>>>]>+<<<<[<<<]>>]<<->+<[>-]>[->>[>>>]>-<
<<<[<<<]>>]<<->+<[>-]>[->>[>>>]>[->+>>+<<<]>[-<+>]>>[-[->>+<<]+>>]+>+<
[-<<]<<<<<[<<<]>>]<<->+<[>-]>[->>[>>>]>[->+>>+<<<]>[-<+>]>>[-[->>+<<]+
>>]+>-<[-<<]<<<<<[<<<]>>]<<->+<[>-]>[->>[>>>]>[->+>>+<<<]>[-<+>]>>[-[-
>>+<<]+>>]+>.<[-<<]<<<<<[<<<]>>]<<->+<[>-]>[->>[>>>]>[->+>>+<<<]>[-<+>
]>>[-[->>+<<]+>>]+>,<[-<<]<<<<<[<<<]>>]<<++++++++>>>]
Thanx for giving me inspiration! :-)

~NYYRIKKI


Bit Doubler

But NYYRIKKI decided to make something usefull out of the above BF interpreter in BF. The original BF interpreter was working on an array of bytes (0-255). When the following interpreter is executed on such a BF interpreter, each memory location can hold a word (0-65535). Actually, it will always "square" the capacity of the underlying BF interpreter. Hence its name.
>>>+[,>+++++++[<------>-]<[->+>+<<]>>[-<<+>>]<->+<[>-<[-]]>[-<<[-]+++++
>>>>>]<<[->+>+<<]>>[-<<+NNNNNN>>]NNN<NNNNNNNN-->+<[>-<[-]]>[-<<[-]+++++
+++>>>>>]<<[->+>+<<]>>[-NNNNNNN<<NNN+NNNNNNNN>>]<--->+<[>-<[-]]>[-<<[-]
++++++>>>>>]<<[->+>+<<]>NNN>[NN-<NNN<+>>NN]<---->+<[>-<[-]]>[-<<[-]++++
+++>>>>>]<<[->+>+<<]>>[-NNN<NN<+>NNN>]++NN++++[<--->-]+<[>-<[-]]>[-<<[-
]++++>>>>>]<<[->+>+<<]>>NNNNN[-<<NNN+>>]NN+++++[<---->-]+<[>-<[-]]>[-<<
[-]+++>>>>>]<<[->+>+<<]>NNN>[NN-<NNN<+>>NN]+++++++[<------->-]+<[>-<[-]
]>[-<<[-]+>>>>>]<<[->+>+NNN<<NN]>NNN>[-<NN<+>>]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]<<<-[->+<]+[-<<<]<<<<<[<<<]>>]<<->
+<[>-]>[->>[>>>][  <-- Made By NYYRIKKI 2002 -->  ]>[->+>>>+<<<<]>[-<+>
]>>>[-[->>>+<<<]+>>>]+>.<[-<<<]<<<<<[<<<]>>]<<->+<[>-]>[->>[>>>]>[->+>>
>+<<<<]>[-<+>]>>>[-[->>>+<<<]+>>>]+>,<[-<<<]<<<<<[<<<]>>]<<++++++++>>>]

(On November 13, 2002, I recieved an email from Daniel B. Cristofani pointing to his even shorter interpreter.)


Brainf***