[Home]MoonShadow/Piet

38.107.191.xxx | ToothyWiki | MoonShadow | RecentChanges | Login

[This] is an assembler for [Piet]. It reads from standard input a file containing Piet instructions (push, pop, add, sub, mul, div, mod, not, gt, ptr, switch, dup, roll, inn, in, outn, out) and assembler directives documented below, and produces on standard output a [netpbm] image of the corresponding Piet program.

A sample program is [here]; [this] is the corresponding image (converted to PNG using pnmtopng; tested against [Sylvain Tynitillier's interpreter] and npiet).



Directives


identifiers consist of the characters a-zA-Z0-9_

macro  ... identifier def
henceforth, whenever identifier is seen, it is replaced with ... before further parsing. (Macros may consist of multiple lines. def must be the last thing on its line.)

identifier:
declares a label

halt
this instruction causes execution to halt

br, bz, bnz, bgt, ble
these are branch instructions. br is an unconditional branch. bz and bnz pop the top item off the stack and branch if it is zero or nonzero respectively, otherwise execution continues. bgt executes a gt instruction and branches if the result is 1; ble, accordingly, 0. The destination label identifier is appended to the instruction, separated by a ., e.g. br.label0, bz.1 etc. If the same label is declared multiple times within the code, the direction of the branch is assumed to be backwards; for numeric labels, the direction may be stated explicitly by appending a b or f as appropriate, e.g. ble.2f A single piet instruction may be appended following another ., e.g. bnz.somewhere.pop - this instruction is executed iff the branch is taken.

.btbl
this is a branch table. This must be the first thing on the line. The rest of the line is treated as a list of labels. The top element is popped off the stack; if it is zero, a branch is taken to the first label in the list, otherwise it is decremented and the next label considered, until either a branch is taken or no more labels are left (in which case execution continues with the instruction following the .btbl directive).

#
comments out the rest of the line

constants
integers are pushed onto the stack as they are encountered. When a quoted string is encountered, its characters are pushed onto the stack in reverse order, followed by the length of the string (a macro for printing a string in this form is supplied in the sample above). When a quoted string preceded by @ is encountered, it is printed without affecting the stack; e.g. @"Hello World!\n" will generate a Piet implementation of Hello World.


tracks
the Piet program consists of a series of tracks. Instructions initially go into track 0, with execution flow from left to right along the top of the image. The .track command begins generating a new track. This command must occur on its own line. Tracks after the first are placed vertically in the image, with execution proceeding from top to bottom of each track. A label of the form _track_N is placed in the scope of track 0 for each other track. The label _track_0 is placed in the scope of each track other than 0. Code in track 0 may branch to the start of any other track using the appropriate branch instruction. Similarly, code in the other tracks may branch to track 0. The  label _track_0 must be explicitly declared within track 0, exactly once, if this feature is used; then, branches from other tracks will enter at that point. See the sample above for example usage.



Todo:

(at what point does this become a statement about art?)

38.107.191.xxx | ToothyWiki | MoonShadow | RecentChanges | Login
Edit this page | View other revisions | Recently used referrers
Last edited March 1, 2010 6:30 pm (viewing revision 5, which is the newest) (diff)
Search: