Piet
Programs are pictures that look like Mondrian paintings. A command is not a colour but a change of colour.
David Morgan-Mar, 2001 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 4 examples and 7 known programs · Hello World on Try It Online: identical · Test method
Run it
Example
Expected output
HE
Output
The idea
David Morgan-Mar designed Piet in 2001 and named it after the painter Piet Mondrian. A program is an image made of blocks of colour. The interpreter moves from block to block, and what it does depends on how the colour changes on the way.
Read the story
The trick that makes it work is relative encoding. There are six hues in a cycle and three lightness levels in a cycle. Stepping from one block to the next, the interpreter counts how many hue steps and how many lightness steps it took. That pair picks one of 17 commands. The same command can be written with any starting colour, so a programmer has real freedom to make the picture look good.
Numbers come from size. When the interpreter pushes a number, it pushes the number of pixels in the block it just left.
The machine
Colours. 18 colours in a 6 × 3 grid (red, yellow, green, cyan, blue, magenta, each light, normal or dark), plus white and black. White is free space the pointer slides through; black is a wall.
Pointers. A direction pointer (DP: right, down, left or up) and a codel chooser (CC: left or right). From the current block, the interpreter takes the edge furthest in the DP direction, then the codel on that edge furthest in the CC direction, and steps out of it. If it hits black or the image edge, it first flips the CC, then turns the DP clockwise, and tries again. After eight failed tries the program stops.
Commands, by hue steps (rows) and lightness steps (columns):
| Hue change | Same lightness | 1 step darker | 2 steps darker |
|---|---|---|---|
| none | push | pop | |
| 1 | add | subtract | multiply |
| 2 | divide | mod | not |
| 3 | greater | pointer | switch |
| 4 | duplicate | roll | in (number) |
| 5 | in (char) | out (number) | out (char) |
pointer and switch pop a number and rotate the DP or flip the CC that many times; that is how Piet branches. roll is the general stack shuffle. A command that cannot run (popping an empty stack) is skipped.
Example
A program that prints 3, written as a grid of colours.
A program that prints 3, written as a grid of colours. Each cell is one codel.
| col 0 | col 1 | col 2 | col 3 | col 4 | col 5 | |
|---|---|---|---|---|---|---|
| row 0 | light red | light red | light red | red | dark magenta | black |
| row 1 | black | black | black | dark magenta | dark magenta | black |
Trace, with DP right and CC left:
- Start in the block holding the top-left codel: three light red codels.
- Step right into red (one codel). Same hue, one step darker: push. The block left behind had 3 codels, so push 3.
- Step right into dark magenta (three codels, an L shape). Red to magenta is 5 hue steps; normal to dark is one step darker: out (number). It prints
3. - Now try to leave the dark magenta block. Right is black (both codels of the right edge), down is the image edge, left of its leftmost codel is black, up is the image edge. All eight DP/CC tries fail, and the program stops.
The L shape is the point of step 4. Red sits in the notch of the L, touching two of its codels, but neither of them is ever picked as an exit towards red, so the program cannot wander back.
This was traced by hand from the specification's rules. This implementation writes each codel as a letter (R light red, r red, 6 dark magenta, # black), so the grid above is the two lines RRRr6# and ###66#.
Checked examples
Example
Expected output
4242
Output
Example
Expected output
231
Output
Example
Expected output
(nothing)
Output
Quirks
- A run stops after 3,000,000 steps.
Try this
- Change the example so it prints
5instead of3. What is the smallest change? - Draw a program that reads a number and prints it doubled (in, duplicate, add, out).
- Hard: print the letter
A(65). Hint: multiply two block sizes rather than drawing 65 codels.
Patterns it teaches
- Two-dimensional control flow: the path is chosen by block geometry plus two small pointers.
- Stack machines: all 17 commands work on one stack;
rollmakes it usable. - Camouflage: a program that is also a painting.
- Encoding tricks: commands are differences, numbers are areas.
Further
- esolangs.org/wiki/Piet
- David Morgan-Mar's Piet page at dangermouse.net, with the specification and many sample programs.
- npiet, a widely used interpreter, which can also trace a program step by step.
See also
Non-textual
Two-dimensional languages
Write your own in Run Convert a program from Piet
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: piet.
Command line
shib run piet -e 'rR'shib manual pietSource
- Module
langs/grid/index.js- Interpreter
langs/grid/piet.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org