Stacky is a simple stack-oriented programming language.
Stacky is a stack-oriented, minimal programming language designed for learning how stack machines work. It provides a clear syntax based on common stack machine concepts so you can learn the execution model by writing and running code.
You can try Stacky quickly using the web-based Stacky Playground:
You can also install a CLI tool to run a REPL or execute .stacky
files via cargo:
$ cargo install stacky-cli
Run the following example:
; hello.stacky
push "hello, world!"
println
In the Stacky Playground, press the Run button to execute the code. From the CLI, run:
stacky hello.stacky
The program should print hello, world!
.
Stacky supports a variety of instructions. Here's a slightly more complex example:
; let i = 0
push 0
store i
start:
; i++
load i
push 1
add
store i
; if i == 100 return
load i
push 100
eq
br end
; if i % 15 == 0
load i
push 15
mod
push 0
eq
br fizz_buzz
; if i % 3 == 0
load i
push 3
mod
push 0
eq
br fizz
; if i % 5 == 0
load i
push 5
mod
push 0
eq
br buzz
goto other
fizz_buzz:
println "fizz buzz"
goto start
fizz:
println "fizz"
goto start
buzz:
println "buzz"
goto start
other:
load i
println
goto start
end:
Stacky parses code line by line. Each line can contain one command or label.
push 1
push 2
add
println
Comments are written with ;
.
; this is comment
push "comment"
println
Stacky values have one of the following types:
- nil
- int
- float
- string
- bool
These values can be pushed onto the stack using the push
command.
push nil
push 10
push 1.2
push "foo"
push true
The store
/load
commands allow you to save values to registers independent of the stack.
; i = 2
push 2
store i
; println(i + 3)
load i
push 3
add
println
You can transfer program control using labels and the goto
command. Labels can be defined with label:
.
goto label
push "unreachable"
println
label:
You can also use the br
command to transfer to a label only if the top of the stack is true
.
; a = 1
push 1
store a
; b = 2
push 2
store b
; a == b
load a
load b
eq
br then
goto else
then:
println "a is equal to b"
goto end
else:
println "a is not equal to b"
goto end
end:
Commands pop arguments from the stack by default.
; println "hello"
push "hello"
println
; 1 + 2
push 1
push 2
add
If the arguments are constants, you can write them inline.
push "hello"
add 1 2
This repository is under the MIT License.