esolang

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 traced example below, painted with one square per codel. From 1 to 2 it pushes 3; from 2 to 3 it prints it.

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 changeSame lightness1 step darker2 steps darker
nonepushpop
1addsubtractmultiply
2dividemodnot
3greaterpointerswitch
4duplicaterollin (number)
5in (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 0col 1col 2col 3col 4col 5
row 0light redlight redlight redreddark magentablack
row 1blackblackblackdark magentadark magentablack

Trace, with DP right and CC left:

  1. Start in the block holding the top-left codel: three light red codels.
  2. Step right into red (one codel). Same hue, one step darker: push. The block left behind had 3 codels, so push 3.
  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.
  4. 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#.

Expected: 3

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

  1. Change the example so it prints 5 instead of 3. What is the smallest change?
  2. Draw a program that reads a number and prints it doubled (in, duplicate, add, out).
  3. Hard: print the letter A (65). Hint: multiply two block sizes rather than drawing 65 codels.

Patterns it teaches

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.

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 piet

Source

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