quadrate playground

DocumentationDocs

Quick Reference

Show

The Stack

Quadrate is stack-based. Values are pushed onto a stack, and operations consume values from the stack.

5 3 + push 5, push 3, add them → 8
10 print nl push 10, print it, newline

Variables

Use -> to store a value in a variable, then use the variable name to push it back.

42 -> x store 42 in x
x x * push x twice, multiply → 1764

Stack Operations

dup duplicate top: [a] → [a a]
swap swap top two: [a b] → [b a]
drop discard top: [a b] → [a]
over copy second: [a b] → [a b a]
rot rotate third to top: [a b c] → [b c a]
dup2 swap2 drop2 2-element variants
dupd overd nipd operate on element below top

Math & Comparison

+ - * / % arithmetic operators
== < > <= >= != comparisons → true/false

Control Flow

condition if { ... } run block if true
if { ... } else { ... } if-else branch
0 10 1 for i { i print } loop i from 0 to 9

Functions

Define with inputs and outputs separated by --. Named params are auto-bound as locals.

fn add(a:i64 b:i64 -- i64) takes 2 ints, returns 1
fn double(i64 -- i64) unnamed params use the stack
fn greet() { "Hi" print } no inputs, no outputs

Structs & Fields

struct Point { x:f64 y:f64 } define a struct
Point { x = 1.0 y = 2.0 } create an instance
p <<x read field x from p
p 5.0 >>x -> p write field x (returns struct)
p 5.0 >>x! write field x (no return)
Point {} 1.0 >>x 2.0 >>y chained writes

Strings

"Hello" print print a string
$"Hello {name}!" string interpolation
"a" "b" strings::concat concatenate → "ab"

Output

print print top of stack
nl print newline
" " print print space separator

Error Handling

fn divide(i64 i64 -- i64)! fallible function (may fail)
10 0 divide if { ... } else { ... } check success/failure
10 0 divide? propagate error to caller with ?
"msg" -1 panic signal an error

Switch

x switch { 1 { "one" } 2 { "two" } } match value against cases

Generics

struct Box<T> { value:T } generic struct
42 Box -> b type inferred from value

More Loops

loop { ... break } infinite loop, use break
continue skip to next iteration

Types

i64 integer (64-bit)
f64 floating point
str string
ptr pointer
[]i64 typed array ([]i64, []f64, []str)
fn(i64 -- i64) typed function pointer

Comments

// single line comment ignored by compiler
/* multi-line */ block comment

Common Patterns

dup * square a number
2 % 0 == check if even
0 > if { ... } if positive
swap drop discard second (same as nip)

Code

Output

Ctrl/Cmd+Enter