Piet
Piet is an esoteric programming language designed by David Morgan-Mar, whose programs are bitmaps that look like abstract art.
A Piet program that prints either “Piet” or “Hello, world!” depending on the codel size specified.
Brainfuck interpreter in Piet
Input format: the Brainfuck program (which must not contain the pipe character "|"), followed by a pipe character, followed by the input for the Brainfuck program (may be empty).
How Brainfuck programs are stored on the stack
Let C = c0c1…cn−1 be a Brainfuck program of length n. Let s−k, s−k+1, …, s−1, s0, s1, …, sk−1, sk be the memory cells used by C (or a superset thereof), so that 2k+1 is the number of memory cells that may hold a nonzero value. Let m be the index of the memory cell currently pointed to by the Brainfuck data pointer. Let p be the index of the next Brainfuck command cp to execute.
The Brainfuck program and its runtime state are stored on the Piet stack as follows (from bottom to top):
cn−1, cn−2, …, c1, c0, 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 onto the stack, and counts them. When it encounters the pipe symbol, it reverses their order on the stack, then pushes s0 = 0, n, p = 0, 2k+1 = 1, and m = 0.
The interpreter then begins executing the Brainfuck program.
Its fetch module retrieves cp from the stack and compares it against the ASCII codes of <, >, +, -, ., ,, [, and ].
The corresponding command is then executed.
Finally, p is incremented so the next command can be fetched.
If p = n, the fetch module terminates the interpreter.
![[Module diagram of the Brainfuck interpreter]](piet_bfi_modules.gif)
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.
See also
- Piet programming language by David Morgan-Mar
- npiet – a Piet interpreter by Erik Schoenfelder
- Brainfuck-to-Piet compiler written in Ruby by Yusuke Endoh
- Brainfuck interpreter in Piet by Allan Wirth
- Piet Quine by Yusuke Endoh