Piet::Interpreter - Interpreter for the Piet programming language
use Piet::Interpreter; my $p = Piet::Interpreter->new(image => 'my_code.gif'); $p->run;
Piet is a programming language in which programs look like abstract paintings. The language is named after Piet Mondrian, who pioneered the field of geometric abstract art. The language is fully described at http://www.physics.usyd.edu.au/~mar/esoteric/piet.html. A Piet program is an image file, usually a gif, which uses a set of 20 colors and the transitions between blocks of those colors to define a series of instructions and program flow. See the above URL for more details. (Note: some sample programs there may not work, as they were constructed before a working interpreter was available.)
Since Piet is a visual language, an image parsing mechanism is required. This module uses Image::Magick, so it would be to your advantage to download, install, and test that module and its related stuff before trying to use this one.
Instantiates and returns a new Piet::Interpreter object. Valid arguments are:
Specifies the program image file to load into the interpreter.
Tells the interpreter how large a codel is, in pixels. Defaults to 1.
Sets the behavior of non-standard colored codels to either 'white' or 'black'. Defaults to 'white'.
Turns on debugging information, including warnings.
Turns on warnings only.
Turns on program tracing, which only outputs instructions and values.
Resets the PVM (Piet Virtual Machine) back to the default state. After a reset, the current x and y should both be 0, the DP points to the right, the CC points to the left, and the stack should be empty.
Loads in a program image from the specified file. The interpreter was designed and tested using gif images, but any format that is supported by Image::Magick should work just fine. Once the file has been loaded, it is inspected and processed, creating a run-matrix and determining some useful properties from the image.
Note: Be sure to set the codel size, if needed, before loading the image. Otherwise, a size of 1 will be assumed, and the codel columns and rows will not be calculated correctly, causing pain and irritation.
Starts the Piet interpreter running from the upper-left codel. Program execution is described under "Language Concepts", below.
Performs one "step" of a Piet program, where a step is one transition from one codel block to the next. A failed transition (trying to go out of bounds, or onto black) is not considered a step, but a slide into or out of a while block is. Returns the step count number, or undef if the step terminates the program.
Turns debugging information on or off.
Turns warnings on or off.
Turns program instruction tracing on or off.
Sets or returns the codel size for the program image.
Sets the behavior of non-standard codels to 'white' or 'black'.
Returns the number of codel rows in the program image.
Returns the number of codel columns in the program image.
Returns the name of the file from which the program image was loaded.
Prints detailed information about the state of the PVM, with an optional label. Information reported includes the filename, number of codel columns and rows, which debugging, warning, or tracing flags are set, how non-standard colored codels are handled, the step number, the current x and y position of the pointer, the directions of the DP and CC, the last color visited, and the values currently on the stack.
Returns a nicely formatted text version of the program image's codel matrix, with the filename, codel size, and column/row information.
Piet uses 20 distinct colors, 18 of which are related cyclically in two ways:
Red -> Yellow -> Cyan -> Blue -> Magenta -> Red
Light -> Normal -> Dark -> Light
Note that "light" is considered to be one step "darker" than "dark", and vice versa. White and black do not fall into either cycle.
Additional colors (such as orange or brown) may also be used. In the default case, non-standard colors are treated by the PVM (Piet Virtual Machine) as the same as white, so may be used freely wherever white is used. You may also use the nonstandard() method to tell the PVM to treat them the same as black.
Piet code takes the form of an image made up of the recognised colors. Individual pixels of color are significant in the language, so it is common for programs to be enlarged for viewing so that the details are easily visible. In such enlarged programs, the term "codel" is used to mean a block of color equivalent to a single pixel of code, to avoid confusion with the actual pixels of the enlarged graphic, of which many may make up one codel.
Piet uses a stack for storage of all data values. Data values exist only as integers, though they may be read in or printed as Unicode character values with the appropriate commands.
The Piet language interpreter begins executing a program in the color block which includes the upper left codel of the program. The interpreter maintains a Direction Pointer (DP), initially pointing to the right. The DP may point either right, left, down or up. The interpreter also maintains a Codel Chooser (CC), initially pointing left. The CC may point either left or right. The directions of the DP and CC will often change during program execution. As it executes the program, the interpreter traverses the color blocks of the program under the following rules:
The interpreter continues doing this until the program terminates.
Each non-black, non-white color block in a Piet program represents an integer equal to the number of codels in that block. Note that non-positive integers cannot be represented, although they can be constructed with operators. When the interpreter encounters a number, it does not necessarily do anything with it. In particular, it is not automatically pushed on to the stack - there is an explicit command for that.
Black color blocks and the edges of the program restrict program flow. If the Piet interpreter attempts to move into a black block or off an edge, it is stopped and the CC is toggled. The interpreter then attempts to move from its current block again. If it fails a second time, the DP is moved clockwise one step. These attempts are repeated, with the CC and DP being changed between alternate attempts. If, after eight attempts the interpreter cannot leave its current color block, there is no way out and the program terminates.
White color blocks are "free" zones through which the interpreter passes unhindered. If it moves from a color block into a white area, the interpreter "slides" through the white codels in the direction of the DP until it reaches a non-white color block. If the interpreter slides into a black block or an edge, it is considered restricted (see above), otherwise it moves into the color block so encountered. Sliding across white blocks does not cause a command to be executed.
Commands are defined by the transition of color from one color block to the next as the interpreter travels through the program. The number of steps along the Hue Cycle and Lightness Cycle in each transition determine the command executed, as shown in the table below. If the transition between color blocks occurs via a slide across a white block, no command is executed.
Pushes the value of the color block just exited on to the stack. Note: values are not automatically pushed onto the stack - the push operation must be explicitly carried out.
Pops the top value off the stack and discards it.
Pops the top two values off the stack, adds them, and pushes the result back on the stack.
Pops the top two values off the stack, subtracts the top value from the second top value, and pushes the result back on the stack.
Pops the top two values off the stack, multiplies them, and pushes the result back on the stack.
Pops the top two values off the stack, calculates the integer division of the second top value by the top value, and pushes the result back on the stack.
Pops the top two values off the stack, calculates the second top value modulo the top value, and pushes the result back on the stack.
Replaces the top value of the stack with 0 if it is non-zero, and 1 if it is zero.
Pops the top two values off the stack, and pushes 1 on to the stack if the second top value is greater than the top value, and pushes 0 if it is not greater.
Pops the top value off the stack and rotates the DP clockwise that many steps, or counterclockwise if it is negative.
Pops the top value off the stack and toggles the CC that many times.
Pushes a copy of the top value on the stack on to the stack.
Pops the top two values off the stack and "rolls" the remaining stack entries to a depth equal to the second value popped, by a number of rolls equal to the first value popped. A single roll to depth nis defined as burying the top value on the stack n deep and bringing all values above it up by 1 place. A negative number of rolls rolls in the opposite direction. A negative depth is an error and the command is ignored.
Reads a character from STDIN as a number, and pushes it on to the stack.
Reads a value from STDIN as a character, and pushes it on to the stack.
Pops the top value off the stack and prints it to STDOUT as a number.
Reads a value from STDIN as a character, and pushes it on to the stack.
Any operations which cannot be performed (such as popping values when not enough are on the stack) are simply ignored.
Marc Majcher (email@example.com)