piet-0.1: A Piet interpreter

Language.Piet.Commands

Description

This module contains the implementation of the Piet language constructs. Most of the documentation is copied from the Piet specification at http://www.dangermouse.net/esoteric/piet.html.

Synopsis

# Stack access

Pushes the value of the colour block just exited on to the stack. Note that values of colour blocks are not automatically pushed on to the stack - this push operation must be explicitly carried out.

Pops the top value off the stack and discards it.

# Arithmetic operators

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.

# Boolean operations

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.

# Movement

Pops the top value off the stack and rotates the DP clockwise that many steps (anticlockwise if negative).

Pops the top value off the stack and toggles the CC that many times.

# Stack modification

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 n is 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.

In this implementation, "ignored" means that the top two values remain pushed off the stack, while the rest of the stack remains unmodified.

# I/O

Reads a number from STDIN and pushes it on to the stack.

Reads a char from STDIN and pushes it on to the stack.

Pops the top value off the stack and prints it to STDOUT as a number.

Pops the top value off the stack and prints it to STDOUT as a char.