Language selection: Deutsch / English

Brainfuck interpreter in Piet

[Image: Brainfuck interpreter in Piet] Input: Brainfuck program (must not contain the pipe character "|"), then a pipe, then (if not empty) the input for the brainfuck program.
1 pixel per codel. Tested with npiet.

Convert Brainfuck programs to Piet

You can generate a Brainfuck interpreter in Piet with hard coded Brainfuck code. Just enter a Brainfuck program below to generate an equivalent Piet program.

Enter your Brainfuck program here:

Functional principle in general

How Brainfuck programs are stored at the stack

Let C=c0c1…cn−1 be a Brainfuck programm of size n. Let s−k, s−k+1, …, s−1, s0, s1, …, sk−1, sk be the memory cells used by C (or more memory cells) and 2k+1 be the number of the memory cells that may contain a nonzero value. Let m be the index of the memory cell the Brainfuck data pointer points to. Finally, let p be the index of the Brainfuck command cp that should be executed next.

The Brainfuck program and its state is stored at the Piet stack as follows (from the bottom to the top of the stack).
cn, cn−1, …, c2, c1, s0, s−1, s1, s−2, s2, …, s−k, sk, n, p, 2k+1, m

How the Brainfuck interpreter works

See Figure 1 for the modules of the Brainfuck interpreter. The reader module reads characters from STDIN, pushes them at the stack, and counts them. When the pipe symbol is read, it flips their order at the stack. Afterwards, it pushes s0=0, n, p=0, 2k+1=1, and m=0.

Now, the interpreter can start executing the Brainfuck programm. Its fetch module fetches cp from the stack and compares it with the ASCII codes of < > + - . , [ and ]. Then, the interpreter can execute cp. Finally, p is incremented, so the next command can be fetched. If p=n, the fetch module will terminate the interpreter.

Figure 1: Modules of the Brainfuck interpreter. Top: The reader module. Right: The fetch module. Bottom: The command execution module. Left: The module for incrementing p.