Stack-based

From Esolang
Jump to navigation Jump to search
Not to be confused with Stack based.

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

Stack-based
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

VAR x

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.

I x

Input x as decimal.

O x

Output x as decimal.

IC x

Input x as Unicode.

OC x

Output x as Unicode.

P "string"

Output string (escape is not allowed).

Arithmetic

S x integer

Store integer in x.

A x y z

Let z be x+y.

SU x y z

Let z be x-y.

M x y z

Let z be x*y.

Q x y z

Let z be the quotient of x/y.

R x y z

Let z be the remainder of x/y.

C x y

Let y be x.

Stack operations

PUSH x

Push x onto the stack.

POP x

Pop stack and store in x.

POP

Pop stack and discard.

TOP x

Store top of stack to x.

SZ x

Store number of elements in the stack to x.

Comparison

LT x y z

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

LE x y z

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

GT x y z

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

GE x y z

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

EQ x y z

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

NE x y z

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

Jump

JA x integer

Jump integer commands forward if x is nonzero.

JB x integer

Jump integer commands backward if x is nonzero.

NOP

NOP.

HALT

Quit.

Random

RND x

Randomize x to integer between 0 and 1.

Logic operations

AND x y z

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

OR x y z

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

NOT x y

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

Bitwise operations

BND x y z

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

BOR x y z

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

XOR x y z

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 ; comment
more 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

See also