Keywords
Language keywords for declarations, control flow, and more.
Overview
| Keyword | Description |
|---|---|
fn |
Declares a function with a stack signature |
pub |
Makes a function, constant, or struct visible to other modules |
def |
Declares a function whose inputs are bound to their names instead of staying on the stack |
inline |
Requests the compiler to inline a function at call sites |
const |
Declares a compile-time constant value |
var |
Declares a module-level mutable global variable |
struct |
Declares a structured data type with named fields |
packed |
Struct modifier: fields laid out adjacent with no padding |
use |
Imports a module |
if |
Conditional execution |
else |
Alternative block when if condition is false |
for |
Iteration with counter |
loop |
Infinite loop |
while |
Leave the loop if the flag is false |
until |
Leave the loop if the flag is true |
break |
Exit loop |
continue |
Skip to next iteration |
return |
Exit function |
defer |
Schedule cleanup code |
switch |
Multi-way branching |
-> |
Variable binding |
test |
Declares a test block |
import |
Imports a native C library |
enum |
Declares an enumeration type |
type |
Declares a type alias |
as |
Names the module in an import |
null |
Null pointer literal (0) |
$"..." |
String interpolation |
Ok |
The status of a call that succeeded (0) |
Err |
The generic error code (1) |
true |
Boolean true (1) |
false |
Boolean false (0) |
Declarations
fn
Declares a function with a stack signature. Its inputs stay on the stack for the body to work on, and the names in the signature are documentation.
fn sum(a:i64 b:i64 -- s:i64) {
+
}
Written without a name it is an anonymous function, and every rule here applies to it unchanged —
the inputs stay on the stack (def binds them), ! makes it fallible, and the body must leave
what the signature declares:
fn main() {
5 fn (x:i64 -- r:i64) { 2 * } call print nl // 10
}
pub
Makes a function, constant, or struct visible to other modules.
pub fn greet() {
"Hello" print nl
}
def
Declares a function like fn, except that each input parameter is bound as a local at entry and
taken off the stack, so the body reads its inputs by name. Every input must be named, since each
is bound, and a def must declare at least one input.
def scaled(value:i64 factor:i64 -- result:i64) {
value factor *
}
fn main() {
7 3 scaled print nl // 21
3 4 def (a:i64 b:i64 -- r:i64) { a b + } call print nl // 7
}
inline
Requests the compiler to inline a function at call sites, eliminating function call overhead. Can be combined with pub.
A direct call by name is replaced by the body. A plain stack fn (not fallible, generic or a
method, with no locals, return or defer) is spliced into the caller before code generation,
so a word factored into smaller words costs what it would written out in one piece. Any other
inline function is marked for LLVM to inline. A recursive call, or a call through a function
value with call, is not inlined. Best suited for small wrapper functions where the call
overhead would dominate the function body.
inline fn double(x:i64 -- result:i64) {
2 *
}
pub inline fn incr(x:i64 -- result:i64) {
1 +
}
const
Declares a compile-time constant value.
const Pi = 3.14159
var
Declares a module-level mutable global variable. The initializer is required; the :type annotation is optional — when omitted, the type is inferred from the initializer. Reads use the bare name; writes use ->. Unlike const, the value can change across function calls. Use pub var to export.
var counter = 0 // inferred i64
var ratio = 3.14 // inferred f64
var label = "hello" // inferred str
pub var magic:i64 = 42 // explicit type still supported
fn bump() {
counter 1 + -> counter
}
Inference rules: integer literal → i64, float literal → f64, string literal → str, const-reference → the const's type (derived from its value shape). Write :type explicitly when the inferred default isn't what you want (e.g. var handle:ptr = null).
Supported types: i64, f64, d128, str, ptr, any struct type, any array type and any function type. Sized integer types are rejected here — a global is a 64-bit slot, so the width would describe nothing (see sized integer types). Initializers accept a literal, a reference to a previously-declared const, a struct construction (var p = Point { x = 1.0 y = 2.0 }), an array literal (var cells:[]f64 = [1024]f64, or var seen = []i64, whose element type says the global's), or a function (var show:fn(i64 --) = &plain). An array or function global needs its type written unless the literal names its element type, and is checked against it: a store of another type is an error, and so is a function that takes or leaves something else. String, struct and array initializers run once before main and are stored as a reference-counted pointer in the global; a str global that has not been assigned reads as the empty string.
pub var exports the global, as pub fn exports a function. The symbol is qd_global_<name>, which is the name assembly writes — useful in --freestanding builds, where a value shared with an interrupt handler or a boot stub can live in Quadrate instead of a .S file:
pub var tick_count:i64 = 0 // assembly refers to it as qd_global_tick_count
Without pub the global has internal linkage and is visible only inside the compiled module. A -> name assignment inside any function always writes to the matching module-level var — locals never shadow globals; pick a different name if you need a local.
struct
Declares a structured data type with named fields.
struct Point {
x:f64
y:f64
}
packed
Struct modifier: fields are laid out back-to-back at their exact widths with no padding between them. Use for parsing on-disk binary formats (WAD lumps, PNG chunks, network protocol headers) where the memory layout must match a byte-exact specification. Combines with pub: pub packed struct ....
packed struct FileLump {
offset:u32
size:u32
name:u64 // 8 raw bytes
}
// sizeof = 16 bytes; a plain `struct` would round each field to 8 bytes for 24.
Without packed, each field is rounded up to an 8-byte slot for uniform alignment. With packed, struct FileLump above is exactly 16 bytes wide.
use
Imports a module, making its functions available with module::function syntax.
use strings
fn main() {
"hello" len print nl // 5
}
Control flow
if
Executes a block if the top of stack is true (non-zero).
5 3 > if {
"yes" print
}
else
Provides an alternative block when the if condition is false.
x 0 > if {
"positive"
} else {
"non-positive"
}
for
Iterates from start to end with a step, binding the iterator variable.
0 10 1 for i {
i print nl
}
loop
Repeats a block indefinitely until break is called.
loop {
"forever"
print nl
}
while
Inside a loop or for: takes a flag and leaves the innermost loop when it is false. The
condition is whatever precedes it, so at the top of the body it makes a classic while loop.
0
loop {
dup 5 < while
dup print nl
1 +
}
drop
until
Inside a loop or for: takes a flag and leaves the innermost loop when it is true. At the end
of the body it makes a do-while loop, which runs at least once.
0
loop {
1 +
dup 3 >= until
}
drop
break
Exits the innermost loop immediately.
loop {
x 10 > if {
break
}
}
continue
Skips to the next iteration of the innermost loop.
0 10 1 for i {
i 5 == if {
continue
}
i print nl
}
return
Exits the current function immediately.
fn do_work( -- ) {
"working" print nl
return
"not reached" print
}
return exits the enclosing function from anywhere — including inside if, else, for,
loop and switch arms — which makes the guard-clause style available:
fn classify(n:i64 -- label:str) {
dup 0 < if {
drop "negative"
return
}
0 == if {
"zero"
return
}
"positive"
}
fn main() {
-5 classify print nl
0 classify print nl
7 classify print nl
}
A function that declares outputs must leave them on the stack before returning, on every path
that returns. Deferred blocks registered before the return still run (see defer).
switch
Branches based on matching the top of stack against case values. Use _ as the default case when no other case matches.
switch {
1 {
"one"
}
2 {
"two"
}
_ {
"other"
}
}
Other keywords
defer
Schedules a block to run when the function exits, in LIFO order.
defer {
file io::close
}
test
Declares a test block. Tests are run with quad test or quadc --test.
use testing
test "addition works" {
1 2 + 3 testing::assert_eq
}
import
Imports a native C library for use in a module. Used with as to specify the module namespace.
import "libmath.a" as "math" {
pub fn sqrt(x:f64 -- result:f64)
}
as
Names the module an import declares its words in. See import. It used to also
narrow a ptr to a struct type; a ptr is raw memory now and never holds one, so that use
has been removed.
enum
Declares an enumeration with named integer constants. Values auto-increment from 0 unless explicitly assigned.
enum Color { Red Green Blue }
enum Token { Int Float Str = 10 Ident }
Access variants with EnumName::Variant. Use pub enum to export from a module.
Color::Red print nl // 0
Color::Blue print nl // 2
type
Declares a type alias. The alias is resolved at compile time with no runtime cost.
type Predicate = fn(i64 -- i64)
type IntArray = []i64
type Number = i64
Use pub type to export from a module.
fn keep_if(arr:IntArray pred:Predicate -- result:[]i64) { ... }
An array or function alias does not yet work as a declared output (the body's []i64 is
reported as not matching IntArray), so the output is written as []i64.
null
Pushes 0 with pointer semantics. Used to represent the absence of a value in pointer fields.
struct Node { value:i64 next:*Node }
Node { value = 10 next = null } -> n
null 0 == if { "true" print nl } // true
String interpolation
The $"..." syntax embeds expressions in strings. Expressions inside {...} are evaluated and converted to strings.
"World" -> name
42 -> age
$"Hello, {name}! Age: {age}" print nl
Desugars to sb::new / sb::append / sb::finish calls. The sb module is auto-imported.
Ok
Literal 0: the status a fallible call leaves when it succeeded. Used in switch to match the
success case.
"test.txt" io::read_file switch {
Ok { print }
_ { drop "error" print nl }
}
Err
Literal 1: the generic error code, which "message" Err panic raises. Used in switch to
match that code.
"test.txt" io::read_file switch {
Ok { print }
Err { drop "generic error" print nl }
_ { drop "error reading file" print nl }
}
arrow (->)
Pops a value from the stack and binds it to a local variable.
42 -> x
true
Pushes 1 onto the stack.
true if {
"yes" print
}
false
Pushes 0 onto the stack.
false if {
} else {
"no" print
}