Stacker

From Esolang
Jump to: navigation, search

Stacker is an esoteric programming language created by Fletcher Cutting (User:DatCodingGuy) and takes inspiration from multiple languages including Stack Up, Calcutape and Befunge while adding its own unique features.

Stacker started as Fletcher making an interpreter for Stack Up but realizing that it's extremely painful to write code in and doesn't have much functionality. Throughout making the interpreter he decided to start adding more functionality and it eventually reached the point of it being different enough to be called a separate language.

Overview

Stacker has two stacks, the main stack and the extra stack. Most of the commands affect the main stack, the extra stack is just there to store data that you want to use later on and don't want it to be affected. Stacker also includes a flag register that is either true or false. This flag is used for commands such as if and loop and can be manipulated by the compare and flip commands.

Commands

Each command consists of a single character (being either a digit, letter or symbol), there are a total of 33 commands:

Command Description
0-9 Push a single digit on to the main stack
$ Pops the top value off the main stack and discards it
: Duplicates the top value of the main stack
_ Swaps the top two values of the main stack
i Increments the top value of the main stack by 1
d Decrements the top value of the main stack by 1
+ Pops the top two values off the main stack and pushes their sum
- Pops the top two values off the main stack and pushes their difference (1st - 2nd)
* Pops the top two values off the main stack and pushes their product
/ Pops the top two values off the main stack and pushes their quotient
^ Squares the top value of the main stack
> Pops the top value off the main stack and pushes it on to the extra stack
< Pops the top value off the extra stack and pushes it on to the main stack
} Pops the top value off the main stack (N) and then pops Nth amount of values off the main stack and pushes them on to the extra stack in the same order
{ Pops the top value off the main stack (N) and then pops Nth amount of values off the extra stack and pushes them on to the main stack in the same order
& Takes input from the user in the form of a 32bit integer and pushes it on to the main stack
, Takes input from the user in the form of an ASCII character and pushes its value on to the main stack
% Pops the top value off the main stack and prints it as an integer
. Pops the top value off the main stack and prints it as a character
[ If the flag is true then start the loop, otherwise jump to the matching ]
] If the flag is true then exit the loop, otherwise jump back to the matching [
( If the flag is true then continue executing the code, otherwise jump to the matching )
) Does nothing other than symbolize the end of an if statement
= Pops the top value off the main stack (N) and depending on N it does something different. If N is 0 then it pops the top two values off the main stack (N1 and N2) and if N1 equals N2 then the flag is set to true, otherwise the flag is set to false. If N is 1 then it pops the top two values off the stack (N1 and N2) and if N1 is greater than N2 then it sets the flag to true, otherwise the flag is false. If N is -1 then it pops two values off the main stack (N1 and N2) and if N1 is less than N2 then is sets the flag to true, otherwise the flag is set to false.
e Pops the top value off the main stack (N) and executes the value N as an opcode
! Terminates the program immediately
? Sets the program counter to 0 and continues executing commands from the start of the program
h Halts the program by entering an infinite loop
# If the flag is true then it skips the next command
| Reverses the direction of the PC
f Flips the flag (if the flag is true it will be set to false, if the flag is false it will be set to true)

Notes

You can find a list of opcode values here

By default input is not displayed to the console as it is being inputted, please look at the examples for a way to work around this.

The order in which you push multiple values on to the main stack and extra stack remains the same. If the main stack is [0, 1, 2, 3] and you use the } command the top four values on the extra stack will be [0, 1, 2, 3]. If the four commands starting at position 3 are [4, 5, *, ^] and you use the " (giving it the arguments 3 and 4) command the first four values on the main stack will be [4, 5, 17, 19] (values that are pushed are the opcode values of the commands, not integer values of the character).

Examples

Hello World:

4 8 * 1 + 5 2 *:* 5 2 *:* 8 + 5 2 *:* 9 + 5 + 52 *:* 9 + 2 + 9 9 * 6 + 4 8 * 5 2 *:* 9 + 2 + 5 2 *:* 8 + 5 2 *:* 1 +
8 9 * ........... h

Cat (inputs integer outputs integer):

&%h

Cat (inputs a single character, outputs it as an integer):

,%h

Cat (inputs a single character, outputs it as a character):

,.h

By default input is not displayed in the console as it is being inputted, one way to work around this is by doing this:

,:.

This works because it takes input as a character, duplicates it and then prints the top value leaving a copy of the character on the stack. If you want to input a string then turn it in to a loop like this:

[,:.]

A simple while loop:

9[::%0.00=d]h

All this does is loop through a number (number 9 is pushed at the start), checks to see if it equals 0 and if it doesn't it decrements the number and loops again.

Computational Class

Stacker is Turing-complete, meaning that it has the same computational class as universal Turing machines. This is proven by a process of deduction when comparing Stacker with Brainfuck. Stacker has all of the commands that Brainfuck does which shows it is turing complete. To simulate Brainfuck's tape with our stacks you can push as many 0s as you need to and use the > and < commands to move values from one stack to the other emulating the tape.

Implementation

You can find the official interpreter here