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
inline Requests the compiler to inline a function at call sites
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
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
enum Declares an enumeration type
type Declares a type alias
as Type narrowing cast / import module namespace
null Null pointer literal (0)
$"..." String interpolation
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
}

inline

Requests the compiler to inline a function at call sites, eliminating function call overhead. Can be combined with pub.

The compiler will inline in most cases, but may decline for recursive functions or indirect calls through function pointers. Best suited for small wrapper functions where the call overhead would dominate the function body.

inline fn double(x:i64 -- result:i64) {
    x 2 *
}

pub inline fn inc(x:i64 -- result:i64) {
    x 1 +
}

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 strings

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

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

as

Has two uses:

1. Type narrowing cast — Narrows a ptr value to a specific struct type for field access. This is compile-time only with no runtime cost.

fn get_body(c:ptr -- body:str) {
    -> c
    c as http::Ctx <<body
}

When multiple structs share a field name and the compiler cannot determine the type, as disambiguates:

c as MyStruct <<value      // access <<value on MyStruct specifically
c as MyStruct -> typed_c  // bind as typed local for subsequent accesses
typed_c <<value            // type is known, no ambiguity

2. Import namespace — Specifies the module namespace in an import statement. See import.

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 filter(arr:IntArray pred:Predicate -- result:IntArray) { ... }

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 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
}