cookieundercover cookieundercover - 1 month ago 10
Brainfuck Question

Why do I get a Perl error in this brainfuck program?

When I run the following Brainfuck program (source)...

>+>++>+++>++++>+++++>++++++>+++++++>++++++++>++++++++++++++++++++++++++++++++<<<<<<<<[>>>>>>>>+.<<<<<<<<]


... through the interpreter I use (a small one written in Perl) it does not what it is supposed to do ("you'll find that it loops forever and prints out each character, starting with space (32), in ASCIIbetical order"), but instead prints out, looping:

Wide character in print at (eval 1) line 1, <> chunk 1.


Yes, the code contains redundancies. It is autogenerated from, out of all things, Haskell.

Answer

Your interpreter likely uses a Perl function such as “chr” to convert cell values to characters upon output, and does not limit cell values to [0;255].

While this can be useful in some calculations, the Brainfuck output is normally bytes, whereas Unicode codepoints are usually encoded as UTF-8, and even if not, those above 255 need more than one byte anyway, naturally.

I'm sure you first do get all those characters, and only then the warnings, once the cell value gets too big. Maybe, if you let it run for a loooong time, the cell values wrap around (at 32767, or 2147483647 probably), then you have negative values, and eventually 32 again, repeating the cycle.