Roll-n-rolling with the Piet ROLL command
Mr. Young and a few others questioned how QuickPiet‘s command worked …. and to be honest, at first I didn’t either. It turns out that the Piet ROLL command works fairly simply and is extremely powerful (according to npiet which I used as the basis for my testing).
The ROLL command pops two values off of the stack puts nothing back. The top value from the stack defines how many “turns” the roll executes. A turn will put the top value on the bottom and shift all other values “up” one place toward the top of the stack. The second value defines the “depth” of the roll or how many elements should be included in each turn starting at the top of the stack.
As the main Piet page suggests, a ROLL to a depth of X and a single turn will effectively bury the top of the stack down X spots. If that is as clear as mud, check out this image which shows two roll actions executed on a already populated stack.
[EDIT: Correction, the last roll in the above image shows a roll of depth 5, 2 turns. Not 3. My bad.)
I used the nPiet command line Win32 interpreter to test this and the following program shown with 1 pixel per codel** and again with 20×20 pixels per codel. nPiet is the most stable interpreter I have found and has some nice features like creating trace output images, tracing/logging statements, and automatically guessing codel sizes. Below is the 20×20 codel version and nPiet trace output.
$ ./npiet.exe -v -t ben/roll-big.gifinfo: verbose set to 1info: trace set to 1info: using file ben/roll-big.gifinfo: got gif image with 320 x 120 pixelinfo: codelsize guessed is 20 pixeltrace: step 0 (0,0/r,l nR -> 1,0/r,l dR):action: push, value 1trace: stack (1 values): 1trace: step 1 (1,0/r,l dR -> 2,0/r,l lR):action: push, value 2trace: stack (2 values): 2 1trace: step 2 (2,0/r,l lR -> 3,0/r,l nR):action: push, value 3trace: stack (3 values): 3 2 1trace: step 3 (3,0/r,l nR -> 4,0/r,l dR):action: push, value 4trace: stack (4 values): 4 3 2 1trace: step 4 (4,0/r,l dR -> 5,0/r,l lR):action: push, value 5trace: stack (5 values): 5 4 3 2 1trace: step 5 (5,0/r,l lR -> 6,0/r,l nR):action: push, value 3trace: stack (6 values): 3 5 4 3 2 1trace: step 6 (6,0/r,l nR -> 7,0/r,l dR):action: push, value 2trace: stack (7 values): 2 3 5 4 3 2 1trace: step 7 (7,0/r,l dR -> 8,0/r,l lB):action: rolltrace: stack (5 values): 3 5 4 2 1trace: step 8 (8,0/r,l lB -> 9,0/r,l nB):action: push, value 3trace: stack (6 values): 3 3 5 4 2 1trace: step 9 (9,0/r,l nB -> 10,0/r,l dB):action: push, value 2trace: stack (7 values): 2 3 3 5 4 2 1trace: step 10 (10,0/r,l dB -> 11,0/r,l lG):action: rolltrace: stack (5 values): 4 3 5 2 1trace: entering white block at 12,0 (like the perl interpreter would)...trace: step 11 (11,0/r,l lG -> 12,0/r,l WW):trace: special case: we at a white codel - continuingtrace: white cell(s) crossed - continuing with no command at 12,2...trace: step 12 (12,0/r,l WW -> 12,2/d,r lG):info: program end
** Since small Piet programs are TINY, instead of using 1×1 pixels as the unit block or codel, Piet interpreters should be able to use codels of larger sizes. Codels of larger sizes allow Piet programs to be easier to see and work with.
Create an interpreter for a new Language based on Piet: QuickPiet
To pair off of Nate’s Rock Paper Scissors challenge, here is one I am offering up to him: create an interpreter which will execute QuickPiet commands. What is QuickPiet? QuickPiet is a very basic language based off of the commands found in the esoteric Piet programming language.
Piet programmers are written as images which can be interpreted as actions based on a stack. Recently I signed up to give a talk at the April Fool’s Day meeting of the Lambda Lounge where several people will be giving short talks on esoteric and useless programming languages. Programming in Piet poses several challenges including working only with a single stack and building up your logic into pixels and colors. I should have a few posts on writing programs in Piet in the next week or so. Until then, I have been mainly struggling with how to accomplish real work with just a single stack and a very limited menu of commands to choose from. QuickPiet is an attempt to make that process easier.
- Write an interpreter for the QuickPiet language I have posted in a new GitHub Gist; http://gist.github.com/332040.
[Edit: The language spec is now in the base QuickPiet repo on GitHub]
- The interpreter should allow the user to enter in QuickPiet commands from a file or GUI text box
- The interpreter should allow the user to enter text to be handled by STDIN and should display to the user any characters sent to STDOUT
- Extra points for GUIs and/or the ability to see how the stack changes through program execution and/or at the end
The “complete” spec can be found on the Gist page, but a very quick rundown is below:
- Program execution is top down, except for GOTO commands
- Each line can be blank (ignored), a label, a comment, or a command
- Only one command per line
- There are no explicit looping or other control structures
- The interpreter maintains a single “infinitely large” stack which different commands can act on
- Example commands include (IN, OUT, PUSH, POP, ADD, SUBTRACT, GOTO, etc)
- Commands are case-insensitive, labels are not
If you have any questions or are willing to venture a solution please leave a comment. I just had this idea this morning so I am very interested in any feedback you may have. I am planning on working on my own solution and hope to have something put together today or tomorrow.