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
const Declares a compile-time constant value
struct Declares a structured data type with named fields
use Imports a module
if Conditional execution
else Alternative block when if condition is false
for Iteration with counter
while Conditional loop
loop Infinite loop
break Exit loop
continue Skip to next iteration
return Exit function
defer Schedule cleanup code
switch Multi-way branching
ctx Context variable access
-> Variable binding
test Declares a test block
import Imports a native C library
as Specifies module name in import
Ok Success literal (1) for switch matching
Err Error literal (0) for switch matching
true Boolean true (1)
false Boolean false (0)

Declarations

fn

Declares a function with a stack signature.

fn add(a:i64 b:i64 -- sum:i64) {
    +
}

pub

Makes a function, constant, or struct visible to other modules.

pub fn greet() {
    "Hello" print nl
}

const

Declares a compile-time constant value.

const Pi = 3.14159

struct

Declares a structured data type with named fields.

struct Point {
    x:f64
    y:f64
}

use

Imports a module, making its functions available with module::function syntax.

use str

fn main() {
    "hello" str::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
}

while

Repeats a block while the condition is true. The condition is evaluated before entering and at the end of each iteration.

0 -> i
i 5 < while {
    i print nl
    i 1 + -> i
    i 5 <          // condition for next iteration
}

loop

Repeats a block indefinitely until break is called.

loop {
    "forever"
    print nl
}

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
}

!!! note return only works at the function body's top level. It cannot be used inside if, else, loop, or other blocks.

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
}

ctx

Creates a new isolated stack context. The child starts with a copy of the parent stack but cannot modify parent variables.

ctx {
    // child context
}

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 "libqdmath.a" as "math" {
    pub fn sqrt(x:f64 -- result:f64)
}

as

Specifies the module namespace in an import statement. See import.

Ok

Literal 1 (same as true). Used in switch to match the success case after a fallible function call.

"test.txt" io::read_file switch {
    Ok { -> content content print }
    _ { "error" print nl }
}

Err

Literal 0 (same as false). Used in switch to match the error case after a fallible function call.

"test.txt" io::read_file switch {
    Ok { -> content content print }
    Err { "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
}