Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Syntax and Grammar

Verified against crush-ast v0.13.0 (dc0d0278, 2026-12-03): every runnable block on this page was compiled and run with that build.

This chapter covers the lexical structure and grammar that the Crush parser (crush-frontend) accepts today. Every crush example in this guide is run through the real toolchain by scripts/check-examples.py; where something is described in the design but the parser does not accept it yet, the page says so and links the crush-ast ticket.

Status: alpha. The language is small and still moving. If a feature isn’t in this chapter, assume it doesn’t exist yet.

A program

A Crush file is a sequence of statements. There are two styles, and they can be mixed:

  • Script style — bare top-level statements run top to bottom.
  • fn main() — an explicit entry function.

When a file has both, the top-level statements run first and then main’s body runs.

print("top-level runs first")

fn main() {
    print("then main")
}
top-level runs first
then main

Functions can be called before they are defined:

print(seven())

fn seven() {
    return 7
}
7

Comments

Single-line comments start with // or #. There are no block comments — /* ... */ is a parse error.

// a slash comment
# a hash comment
print("hello") // trailing comments work too
hello

Identifiers

Identifiers start with a letter or underscore and continue with letters, digits and underscores. Hyphens are not allowed, and a name can’t be a keyword.

let counter1 = 1
let _private = 2
let my_variable = 3
print(counter1 + _private + my_variable)
6

Keywords

let   fn      if       else    while   for     in
return break  continue struct  new     try     catch
throw  match  use      import  export  capability
async  await  spawn    yield   lang    mut
and    or     not      true    false   null

and, or and not are keyword spellings of &&, || and !. mut and capability are reserved, but let mut x = ... is not accepted (see Variables), and capability has no usable statement form yet.

impl and trait are contextual keywords: they start a declaration only at the top level of a file (see Structs, Methods and Kinds), so a variable may still be called impl.

The lexer also recognises a handful of localised keyword aliases — karya/manau/yadi/natra/farkau/jaba_samma/sahi/galat (Nepali) plus a few Chinese and Japanese forms — which map to fn/let/if/else/return/ while/true/false:

karya sign(x) {
    yadi x > 0 { farkau "positive" } natra { farkau "not positive" }
}
manau r = sign(1)
print(r)
positive

Literals

KindExamplesNotes
Integer42, -7, 0x2A, 0b101hex (0x/0X, any digit case) and binary (0b) literals, 0.9.0; 0o17 and 1_000 do not lex, and 0x with no digits is Invalid number literal
Float3.14, 0.5, 1.5e3, 2.5e+3, 1e-7a decimal exponent, since 0.11 (CRUSH-221); a malformed one (1e, 1.e5) is Invalid number literal
String"hello", 'hello'single or double quotes; escapes \n \t \" \\
Booleantrue, false
Nullnull
Array[1, 2, 3]see Types
Object{"a": 1, b: 2}see Types

Hex and binary literals are ordinary integers, so they mix freely with decimal ones:

print(0xff)
print(0b101)
print(0xFF + 0b11)
print(-0x10)
255
5
258
-16

Triple-quoted strings are parse errors, and bytes literals (b"...") are refused with bytes literals (b"...") are not supported yet. A string literal may contain a raw newline.

let n = 42
let pi = 3.14
let s = "tab:\there"
print(n)
print(pi)
print(s)
print(0x2A + 0b101)
print('single "quotes" work too')
42
3.14
tab:	here
47
single "quotes" work too

Statements and semicolons

Semicolons are optional. A statement ends at a newline, or you may write ; (and a; b on one line) if you prefer. Both of these are the same program:

let a = 1; let b = 2;
print(a + b);
3
let a = 1
let b = 2
print(a + b)
3

An expression cannot continue onto the next line, even inside (), [] or {}: the newline ends it, so a multi-line array, map or argument list is a parse error (unexpected token in expression: newline). Keep such expressions on one line, or build them with several statements. Blocks (fn/if/while bodies, lambda bodies, match arms) do span lines.

let a = [1,
  2]
print(a)

Return values

A function returns a value only through an explicit return. A bare final expression is not an implicit return — it yields null:

fn explicit() { return 5 }
fn implicit() { 5 }
print(explicit())
print(implicit())
5
null

Blocks

Blocks are delimited by { } and are used by functions, if, loops and try/catch. A bare block as a statement is not supported; use if true { ... } if you need one.

Names resolve at function level, not per block: after a let inside an if or loop body, the name refers to that binding for the rest of the function (see Variables). Each execution of a let does create a fresh binding, which is what a closure captures.

Variables and functions

let x = 42
let name: String = "Alice"

fn add(a: Int, b: Int) -> Int {
    return a + b
}

print(add(x, 1))
print(name)
43
Alice

Type annotations on variables, parameters and return values are checked at compile time — see Types.

Operators

Full tables are in Operators. In short: + - * / %, == != < > <= >=, && || ! (also spelled and, or, not), unary -, and the pipeline |>. There is no **, //, +=/-=, or ternary ? :.

Precedence (high → low)

  1. Calls, field access, indexing: f(), obj.field, a[i]
  2. Unary: -, !
  3. Multiplicative: *, /, %
  4. Additive: +, -
  5. Comparison: <, >, <=, >=
  6. Equality: ==, !=
  7. Logical AND: &&
  8. Logical OR: ||
  9. Pipeline: |>
print(1 + 2 * 3)
print((1 + 2) * 3)
print(1 < 2 && 2 < 3)
7
9
true

Control flow

let x = 5
if x > 3 {
    print("big")
} else if x > 1 {
    print("medium")
} else {
    print("small")
}

let i = 0
while i < 3 {
    i = i + 1
}
print(i)

for n in 0..3 {
    print(n)
}
for n in 1..=2 {
    print(n * 10)
}
big
3
0
1
2
10
20

break and continue work in while and for. See Control Flow.

Capability calls

A capability call looks like an ordinary dotted call — no @ prefix:

io.print("Hello, ", "Crush")
Hello, Crush

(io.print accepts any number of arguments and concatenates them with no separator; the built-in print(x) takes exactly one.) Which capabilities exist, and which command-line flags grant them, is covered in Capability System.

Language blocks

Another language’s code is embedded with @language { ... }. The @ here is required; it is what introduces the block. Polyglot execution is off by default and needs --polyglot:

@python {
print("Hello from Python")
}

A block that sends values back to Crush names them after ->: @python -> total { ... }.

Hello from Python

See Polyglot Programming for the supported languages, how variables cross the boundary, and the (important) security caveats.

Imports and exports

import a.b loads a/b.crush at compile time and binds the qualifier b (import a.b as x binds x, import a.b { f, g } binds the bare names). The file is looked for in the importing file’s directory, then each -L DIR, then CRUSH_PATH. Imports go at the top level of a file, and a module contains only definitions. See Modules, Packages and Visibility for the rules, packages and pub. There is no std.* module tree: a module that is not found is a compile error listing the directories searched.

use @lang python "math" is different: it writes import math into every later @python block (see Polyglot).

import std.io
print("never reached")

export name parses and marks an existing variable or function for export. It is not an expression-level declaration:

let result = 6 * 7
export result
print(result)
42

export let x = ... and export fn f() {...} are parse errors. export has nothing to do with modules: every function of a module is importable without it, and a module that contains an export statement is refused.

Annotations

@name also introduces annotations:

  • @capabilities [fs.read, time.now_ms] declares what the program may use, and the compiler enforces it (Capability System).
  • @kind(...) before a struct chooses its object semantics (Structs, Methods and Kinds).
  • @module, @decision, @invariant, @wip, @temporary, @errors are metadata for tools and agents, attached to the next declaration; they don’t change what the program does (Annotations).

Style

  • snake_case for functions and variables, PascalCase for structs.
  • Four-space indentation; keep lines under ~100 characters.
  • Omit semicolons unless you are putting two statements on one line.

Grammar summary

This is the grammar the parser implements, simplified (from crates/crush-frontend/src/parser/mod.rs):

program     = ( statement | impl_block | trait_def | annotation )* ;

statement   = let_stmt | fn_def | struct_def | if_stmt | while_stmt
            | for_stmt | return_stmt | break | continue
            | try_stmt | throw_stmt | export_stmt | import_stmt | use_stmt
            | lang_block | match_expr | expr_stmt ;

let_stmt    = "let" IDENT ( ":" type )? "=" expression ";"? ;
fn_def      = "async"? "fn" IDENT "(" params? ")" ( "->" type )? block ;
struct_def  = kind_annot? "struct" IDENT "{" ( IDENT ( ":" type )? ","? )* "}" ;  (* one line *)
kind_annot  = "@kind" "(" IDENT ":" value ( "," IDENT ":" value )* ")" ;
impl_block  = "impl" ( IDENT "for" )? IDENT "{" fn_def* "}" ;
trait_def   = "trait" IDENT "{" ( "fn" IDENT "(" params ")" ( ";" | block ) )* "}" ;
if_stmt     = "if" expression block ( "else" ( if_stmt | block ) )? ;
while_stmt  = "while" expression block ;
for_stmt    = "for" IDENT "in" expression block ;           (* a..b, a..=b, arrays, strings, maps *)
try_stmt    = "try" block "catch" IDENT block ;
throw_stmt  = "throw" expression ;
export_stmt = "export" IDENT ;
use_stmt    = "use" "@lang" IDENT STRING ( "as" IDENT )? ;
lang_block  = "@" IDENT ( "[" deps "]" )? ( "->" ( IDENT | "(" IDENT ( "," IDENT )* ")" ) )?
              "{" raw source "}" ;
block       = "{" statement* "}" ;

expression  = literal | IDENT | expression binop expression
            | unop expression | call | field | index | slice
            | lambda | struct_lit | "new" IDENT "(" ")" | IDENT "::" IDENT
            | match_expr | "spawn" call | "await" expression ;
lambda      = "|" params? "|" ( expression | block ) ;
struct_lit  = IDENT "{" ( IDENT ":" expression ","? )* "}" ;
match_expr  = "match" expression "{" ( pattern ( "=>" | "->" ) ( expression | block ) ","? )* "}" ;
pattern     = literal | IDENT | "_" | IDENT "{" ( IDENT ":" pattern ","? )* "}" ;
call        = IDENT "(" args? ")" | expression "." IDENT "(" args? ")" ;

A call’s callee must be a name (or value.method): fs[0](1) and f()(x) are parse errors today (crush-ast CRUSH-zorro-24).

Next Steps