Purl is a stack-based esoteric programming language developed by User:GotCubes. Programs written in Purl called "Patterns", since they are designed to mimic knitting patterns. However, Purl patterns may result in rather awkward knitted works.
Purl is comprised of 15 instructions, which each have an equivalent in knitting terminology. Each line of a Purl program is treated as its own "row", with an initial value of 0.
||Knit and Purl||Assign the value of the current row to a number defined by a binary sequence.|
Knits equate to 1's, while Purls equate to 0's. For example: K1 P2 K1 P3 = 0b1001000 = 72 = 'H'
||Beginning of Row||Pop the top value of the stack, and assign it to the current row's value.|
||End of Row||Push the current row's value to the top of the stack.|
||Continue||Pop the top value of the stack, and discard it.|
||Cast On||Take an integer as input from the user, and assign it to the current row's value.|
||Make Bobble||Take a character as input from the user, and assign its ASCII value to the current row's value.|
||Drop Stitch||Pop the top value of the stack, and output it as an ASCII character.|
||Slip Stitch||Pop the top value of the stack, and output it as a number.|
||Turn Your Work||Reverse the order of the stack.|
||Increase||Add the top value of the stack to the current row's value.|
||Decrease||Subtract the top value of the stack from the current row's value.|
||Join Work||Multiply the current row's value by the top value of the stack.|
||Yarn Over||Divide the current row's value by the top value of the stack.|
||Repeat||Loop over the lines below on the next indentation level until the top value of the stack is 0.|
||Bind Off||Unconditionally exit the program.|
This pattern pushes the characters of the string "Hello, world!" onto the stack in reverse order. Then, the pattern drops stitches to print the characters off of the stack from top to bottom.
K1 P4 K1 EOR K2 P2 K1 P2 EOR K2 P1 K2 P2 EOR K3 P2 K1 P1 EOR K2 P1 K4 EOR K3 P1 K3 EOR K1 P5 EOR K1 P1 K2 P2 EOR K2 P1 K4 EOR K2 P1 K2 P2 EOR K2 P1 K2 P2 EOR K2 P2 K1 P1 K1 EOR K1 P2 K1 P3 EOR DROP DROP DROP DROP DROP DROP DROP DROP DROP DROP DROP DROP DROP
This pattern begins by accepting a number as input from the user, and pushing it to the stack. If the input is 0, the repeat block is skipped entirely, and 0 is printed a single time before exiting. If the input is 1 (or non-zero), the stack top is duplicated using
INC EOR, which adds the stack top to the current row's value (0), and pushes it to the stack. Then, the duplicate value is popped back off of the stack and printed, and the pattern loops back to the repeat instruction.
CO EOR REP INC EOR SL SL
This pattern begins by pushing operand 1 as an integer, a character operator (from the set
[+, -, *, /]), and operand 2 as an integer to the stack.
TYW instructions are used to manipulate the stack such that the final order of the stack from top to bottom is:
- operand 1
- operand 2
The pattern then pops the operator, subtracts 43 (the ASCII value of '+') from the operand, and pushes it to the stack. Because Purl has no way to branch based on a non-zero value at the top of the stack, the logic for addition is added after the repeat block. Therefore, if the value at the top of the stack is non-zero (i.e. the user did not select the '+' operator), the code block inside of the repeat instruction is executed. In the code block, the pattern determines if the user selected '-', and skips the next repeat block if so. This logic is continued for '*' and '/'. After the pattern has successfully executed the operation that the user selected, a
BO instruction is executed in order to unconditionally exit from the program, such that no extra operations are performed.
CO EOR MB EOR TYW CO EOR TYW P2 K1 P1 K1 P1 K2 DEC EOR REP CONT P2 K1 P1 K2 P1 K1 DEC EOR REP CONT P2 K1 P1 K1 P1 K1 P1 DEC EOR REP CONT P2 K1 P1 K4 DEC EOR REP BO CONT CONT BEG YO EOR SL BO CONT CONT BEG JOIN EOR SL BO CONT CONT BEG DEC EOR SL BO CONT CONT BEG INC EOR SL