brainfuck X
2017-08-26, post № 178
brainfuck, programming, Python, #ANSI, #argparse, #braindraw, #color, #esoteric, #interpreter, #PPM, #tape, #Turing machine
While browsing StackExchange PCG [1] questions and answers, I came across a challenge regarding drawing the swiss flag. In particular, I was interested in benzene’s answer, in which they showcased a brainfuck dialect capable of creating two-dimensional 𝟤𝟦-bit color images. In this post I present this dialect with slight changes of my own, as well as an interpreter I wrote in Python 2.7 (source code is listed below and can also be downloaded).
Urban Müller’s original brainfuck (my vanilla brainfuck post can be found here) works similar to a Turing machine, in that the memory consists of a theoretically infinitely large tape with individual cells which can be modified. What allows brainfuck X (or braindraw, as benzene called their dialect) to create color images is, that instead of a one-dimensional tape, a three-dimensional tape is used. This tape extends infinitely in two spacial dimensions and has three color planes. Each cell’s value is limited to a byte (an integer value from 𝟢 to 𝟤𝟧𝟧) which results in a 𝟤𝟦-bit color depth.
Adding to brainfuck’s eight commands (+-<>[].,
), there are two characters to move up and down the tape (^v
) and one character to move forwards in the color dimension (*
). Starting on the red color plane, continuing with the green and ending in the blue. After the blue color plane, the color planes cycle and the red color plane is selected. benzene’s original language design which I altered slightly had three characters (rgb
) to directly select a color plane. Whilst this version is supported by my interpreter )the flag --colorletters
is necessary for that functionality(, I find my color star more brainfucky — directly calling color planes by their name seems nearly readable.
brainfuck’s vanilla eight characters still work in the same way, brainfuck X can thereby execute any vanilla brainfuck program [2]. Also, there still is a plaintext output — the tape’s image is a program’s secondary output.
Having executed the final brainfuck instruction, the interpreter prints out the tape to the terminal — using ANSI escape codes. Because of this, the color depth is truncated in the terminal view, as there are only 𝟤𝟣𝟨 colors supported. [3]
For the full 𝟤𝟦-bit color depth output, I use the highly inefficient Portable Pixmap Format (.ppm
) as an output image file format. To open .ppm
files, I recommend using the GNU Image Manipulation Program; specifying the output file name is done via the --output
flag.
The Swiss flag image above was generated by benzene’s braindraw code (see their StackExchange answer linked to above); the resulting .ppm
file was then scaled and converted using GIMP.
Interpreter command: python brainfuckx.py swiss.bfx -l -o swiss.ppm
Usage
- Being written in pure Python, the interpreter is completely controlled via the command line. The basic usage is
python brainfuck-x.py <source code file>
; by using certain flags the functionality can be altered. --input <input string>
,-i <input string>
specifies brainfuck’s input and is given as a byte stream (string).--simplify
,-s
outputs the source code’s simplified version; the source code with all unnecessary characters removed.--colorstar
selects the color star color plane change model which is the default.--colorletters
,-l
selects the color letter color plane change model.--silent stops the interpreter from outputting warnings, infos and the final tape.
--maxcycles <cycles>
,-m <cycles>
defines the maximum number of cycles the brainfuck program can run; the default is one million.--watch
,-w
allows the user to watch the program’s execution.--watchdelay <delay>
defines the time in seconds the interpreter sleeps between each watch frame.--watchskip <N>
tells the interpreter to only show every 𝑁th cycle of the execution.--output <output file name>
,-o
<output file name> saves the final tape as a.ppm
image file.