# Stack-based

*Not to be confused with Stack based.*

Stack-based is a stack-based esolang invented by User:None1.

Paradigm(s) | procedural, imperative |
---|---|

Designed by | User:None1 |

Appeared in | 2023 |

Memory system | |

Dimensions | one-dimensional |

Computational class | Turing complete |

Reference implementation | Official interpreter, Online interpreter |

File extension(s) | `.stb` |

## Commands

They are case-insensitive.

Legend: *x*, *y* and *z* are variables, *integer* is an integer literal, *string* is a string literal without quotes.

### Declaration

VARx

Declare a variable named *x* with initial value 0. All values in this esolang are unsigned bignums.

### I/O

EOF returns 0 in all input commands.

Ix

Input *x* as decimal.

Ox

Output *x* as decimal.

ICx

Input *x* as Unicode.

OCx

Output *x* as Unicode.

P "string"

Output *string* (escape is not allowed).

### Arithmetic

Sxinteger

Store *integer* in *x*.

Axyz

Let *z* be *x*+*y*.

SUxyz

Let *z* be *x*-*y*.

Mxyz

Let *z* be *x***y*.

Qxyz

Let *z* be the quotient of *x*/*y*.

Rxyz

Let *z* be the remainder of *x*/*y*.

Cxy

Let *y* be *x*.

### Stack operations

PUSHx

Push *x* onto the stack.

POPx

Pop stack and store in *x*.

POP

Pop stack and discard.

TOPx

Store top of stack to *x*.

SZx

Store number of elements in the stack to *x*.

### Comparison

LTxyz

Set *z* to 1 if *x* < *y*, otherwise 0.

LExyz

Set *z* to 1 if *x* <= *y*, otherwise 0.

GTxyz

Set *z* to 1 if *x* > *y*, otherwise 0.

GExyz

Set *z* to 1 if *x* >= *y*, otherwise 0.

EQxyz

Set *z* to 1 if *x* = *y*, otherwise 0.

NExyz

Set *z* to 1 if *x* != *y*, otherwise 0.

### Jump

JAxinteger

Jump *integer* commands forward if *x* is nonzero.

JBxinteger

Jump *integer* commands backward if *x* is nonzero.

NOP

NOP.

HALT

Quit.

### Random

RNDx

Randomize *x* to integer between 0 and 1.

### Logic operations

ANDxyz

Let *z* be 1 if *x* and *y* are both nonzero, otherwise 0.

ORxyz

Let *z* be 0 if *x* and *y* are both zero, otherwise 1.

NOTxy

Let *y* be 1 if x is zero, otherwise 0.

### Bitwise operations

BNDxyz

Let *z* be the bitwise AND result of *x* and *y*.

BORxyz

Let *z* be the bitwise OR result of *x* and *y*.

XORxyz

Let *z* be the bitwise XOR result of *x* and *y*.

### Comments

Like most assembly-like languages, Stack-based uses `;`

as comments, comments comment until the next line and are not parsed or executed.

code;commentmore code

### Errors

Syntax error

When the syntax is invalid.

Undefined variable

When you attempt to use an undefined variable.

Division error

Division by 0 (modulo by 0 returns 0 in this esolang, so it won't raise an error).

Negative error

When attempting to create a negative value.

Empty error

When popping or accessing top of stack when the stack is empty.

### Example programs

#### Hello World!

P "Hello World!"

#### Cat program

VAR x IC x OC x JB x 2

Bug: Prints an extra NUL character in the end, which is visible in online interpreter. Please try to fix it.

#### A+B Problem

VAR x I x VAR y I y VAR z A x y z O z

#### Truth Machine

VAR x I x O x JB x 1

#### Fibonacci

VAR x VAR y VAR z VAR i S x 0 S y 1 S i 1 O x A x y z C y x C z y JB i 4

#### Reverse Cat program

VAR x IC x PUSH x JB x 2 VAR y VAR z SZ y NOT y z JA z 3 POP x OC x JB y 5

Bug: Prints an extra NUL character in front, which is visible in online interpreter. Please try to fix it.

#### looping counter

VAR x VAR y VAR z VAR one S x 1 S one 1 S z 10 C x y P "*" SU y one y JB y 2 OC z A x one x JB one 6

#### XKCD Random Number

VAR four S four 4 O four

#### Coin

VAR x RND x JA x 3 P "Tails" HALT P "Heads"

## Turing completeness

A conversion from BinaryLanguage is possible, so it is Turing complete.

## External resources

- Official interpreter in Python.
- Online interpreter based on the official interpreter.