Purl

From Esolang
Jump to navigation Jump to search

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.

Instructions

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.

Command Knitting Equivalent Description
Kx and Px 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'
BEG Beginning of Row Pop the top value of the stack, and assign it to the current row's value.
EOR End of Row Push the current row's value to the top of the stack.
CONT Continue Pop the top value of the stack, and discard it.
CO Cast On Take an integer as input from the user, and assign it to the current row's value.
MB Make Bobble Take a character as input from the user, and assign its ASCII value to the current row's value.
DROP Drop Stitch Pop the top value of the stack, and output it as an ASCII character.
SL Slip Stitch Pop the top value of the stack, and output it as a number.
TYW Turn Your Work Reverse the order of the stack.
INC Increase Add the top value of the stack to the current row's value.
DEC Decrease Subtract the top value of the stack from the current row's value.
JOIN Join Work Multiply the current row's value by the top value of the stack.
YO Yarn Over Divide the current row's value by the top value of the stack.
REP Repeat Loop over the lines below on the next indentation level until the top value of the stack is 0.
BO Bind Off Unconditionally exit the program.

Example Patterns

Hello, world!

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

Truth Machine

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

4-Function Calculator

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:

  1. operator
  2. operand 1
  3. 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

External resources