Skip to content

stacky-language/stacky

Repository files navigation

Stacky logo

Stacky

Stacky is a simple stack-oriented programming language.

Overview

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.

Getting Started

Installation

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

Hello, World

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!.

FizzBuzz

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:

Reference

Basic Syntax

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

Value Types

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

Variables

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

Control Structure

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:

Inline arguments

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

License

This repository is under the MIT License.

About

Stacky is a simple stack-oriented programming language

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages