Stack Up

From Esolang
Jump to navigation Jump to search

Stack Up is a stack-based programming language. Stack Up uses two LIFO data stacks for storing and processing data. Each stack has no maximum depth beyond that which is imposed by hardware and holds 8-bit integers ranging 0 to 255. One stack is known as the Main Stack; the other is the Extra Stack. Except as noted, all commands effect only the Main Stack.

There are 17 commands in Stack Up. A Stack Up program is made of a sequence of commands, which are run in order. Each command is made of 3 letters; commands are case-sensitive. Each command must be on its own line. Anything after the command or any line without a command is treated as a comment and ignored.

The program must have the command END on the last line. All lines after it will be ignored.

Commands

  • NEW: Pushes a 0 onto the stack.
  • CLN: Pushes a copy of the top value onto the stack.
  • DEL: Pops and deletes the top value.
  • SWP: Swaps the top two values of the stack.
  • INC: Increases the top value by 1.
  • DEC: Decreases the top value by 1.
  • ADD: Pops the top two values and pushes their sum onto the stack.
  • DIF: Pops the top two values and pushes their difference onto the stack.
  • PAS: Pops the top value of the main stack and pushes it onto the extra stack.
  • PSB: Pops the top value of the extra stack and pushes it onto the main stack.
  • INI: Has the user input an integer (0-255) and push it onto the stack.
  • INA: Has the user input an ASCII character and push its value onto the stack.
  • OUI: Pops the top value and outputs it as an integer.
  • OUA: Pops the top value and outputs the ASCII character matching it.
  • LOP: If the top value is 0, jumps forward to just after the matching STP command.
  • STP: If the top value isn't 0, jumps backward to the matching LOP command.
  • END: End the Program

Note on Loops

The LOP and STP commands create matching pairs, like parentheses. There is no maximum depth of loops except hardware limitations.

Empty Stack

If a command tries to pop or manipulate a number from an empty stack, it pushes a 0 onto the empty stack before resolving the command.

Sample Program

Truth Machine:

 INI
 LOP
 NEW
 INC
 OUI
 STP
 OUI
 END

Turing-Completeness

Stack Up can be shown to be turing-complete by reduction to brainfuck.

First, use the NEW command 10 times (or more, if desired); each NEW adds one cell to the tape. If additional cells are required, the empty stack handling will create them as needed. Then, replace the 8 commands of brainf**k, as follows:

+: INC
-: DEC
>: PAS
<: PSB
,: DEL INA
.: CLN OUA
[: LOP
]: STP

Put an END command at the end.

External resources

Python Interpeter