A stack-based language implemented in OCaml and compiled to WebAssembly
Stackl is a stack-based programming language designed to be simple yet powerful. It is implemented in OCaml and compiled to WebAssembly, allowing it to run efficiently in web environments.
Function | Description | Arguments | Example |
---|---|---|---|
dup | Duplicate the top value on the stack. | None | 3 dup |
exch | Exchange the top two values on the stack. | None | 3 4 exch |
puts | Print the top value on the stack. | None | 3 puts |
load | Load a variable from the environment. | Symbol | x load |
pi | Push the value of pi onto the stack. | None | pi |
save | Save the current drawing state. | None | save |
restore | Restore the last saved drawing state. | None | restore |
translate | Translate the drawing context. | x, y | 10 20 translate |
rotate | Rotate the drawing context. | angle | 45 rotate |
begin_path | Begin a new path for drawing. | None | begin_path |
move_to | Move the drawing cursor to a new position. | x, y | 100 150 move_to |
line_to | Draw a line to a new position. | x, y | 200 250 line_to |
stroke | Stroke the current path. | None | stroke |
set_fill_style | Set the fill style for drawing. | r, g, b | 255 0 0 set_fill_style |
Stackl supports basic arithmetic operations such as addition, subtraction, multiplication, and division.
Operation | Description | Example |
---|---|---|
Addition (+) | Adds the top two values on the stack. | 3 4 + results in 7 |
Subtraction (-) | Subtracts the top value from the second top value on the stack. | 10 4 - results in 6 |
Multiplication (*) | Multiplies the top two values on the stack. | 3 4 * results in 12 |
Division (div) | Divides the second top value by the top value on the stack. | 12 4 div results in 3 |
Stackl supports conditional execution using the if
statement.
Example: { 1 2 < } { 10 puts } { 20 puts } if
will print 10
if the condition { 1 2 < }
is true, otherwise 20
.
Define and call functions using the def
keyword. Loops can be
implemented using recursive functions.
Example: /square { dup * } def
defines a function square
that squares a number. Use it as 5 square
.
Example: /loop { /i exch def { i 5 < } { i puts i 1 + loop } { } if } def
. Use
it as 0 loop
.
Stackl provides various operations to manipulate the stack.
Example: 3 4 exch
swaps the top two values, resulting in 4 3
.
The exch
operation can also be used for variable definition by
swapping the top two values on the stack. This is useful for assigning values to variables.
Example: /x exch def
assigns the last value pushed to the stack
to the variable
x
.
Explore the source code on GitHub.
View on GitHub