Control Flow
Open as a notebook (.crush-nb, how to run it)
Verified against crush-ast
v0.13.0(dc0d0278, 2026-12-03).
Crush has if/else, while, for, break/continue, a match expression,
and try/catch/throw. Conditions are type-checked and must be Bool.
If
let score = 85
if score >= 90 {
print("A")
} else if score >= 80 {
print("B")
} else {
print("C or below")
}
B
if is a statement, not an expression: let y = if c { 1 } else { 2 } is a
parse error. Use a function or match to select a value.
While
let i = 0
while i < 3 {
print(i)
i = i + 1
}
0
1
2
Loop with break
let counter = 0
while true {
if counter >= 3 {
break
}
counter = counter + 1
}
print(counter)
3
For
Over an array
for n in [10, 20, 30] {
print(n)
}
10
20
30
Over a range
a..b is half-open (excludes b); a..=b includes b. There is no range()
function:
for i in 0..3 {
print(i)
}
let items = ["x", "y"]
for i in 0..len(items) {
print(items[i])
}
0
1
2
x
y
for i in 1..=3 {
print(i)
}
1
2
3
Over a string or a map
A string yields its characters; a map yields its keys, in insertion order:
for c in "abc" {
print(c)
}
let ages = {"ada": 36, "alan": 41}
for name in ages {
print(name + " is " + ages[name])
}
a
b
c
ada is 36
alan is 41
The loop variable is a local like any let, so methods work on it:
for s in shapes { print(s.area()) }, or row.push(x) on each row of an array of
arrays. (Before crush-ast #256
such a call was compiled as a capability named s.area and the program was
refused.)
Break and continue
continue skips to the next iteration; break leaves the innermost loop:
for i in 0..5 {
if i % 2 == 0 {
continue
}
print(i)
}
for i in 0..10 {
if i == 3 {
break
}
}
1
3
Match
match selects among arms by comparing a value to literal patterns, with _ as
the wildcard. It is an expression: bind its result with let.
let v = 2
let word = match v {
1 => "one",
2 => "two",
_ => "many"
}
print(word)
two
Supported patterns: integer, string and bool literals, _, a bare identifier
that binds the scrutinee (n => "got " + n), and struct patterns that test a
value’s struct type and bind or compare its fields (Point { x: 0, y: y } => ...,
see Structs, Methods and Kinds). Arm bodies
can be an expression or a { ... } block whose last expression is the value:
let n = 5
let msg = match n {
0 => "zero",
other => "got " + other
}
print(msg)
got 5
match also works as a statement, with arms that only have side effects:
let v = 2
match v {
1 => print("one")
2 => { print("two"); print("still two") }
_ => print("other")
}
two
still two
A match can be returned directly (before crush-ast
#253, return match ... silently
returned null):
fn name(v) {
return match v {
0 => "zero",
_ => "other"
}
}
print(name(0))
print(name(3))
zero
other
Current limitations (alpha):
- A
matchwith no matching arm evaluates tonull; there is no exhaustiveness error. - No guards (
x if x > 3 => ...), no range patterns (0..3 => ...), no alternatives (1 | 2 => ...), no negative or float literal patterns, and no comma after a{ ... }arm body (2 => { 7 }, _ => 0is a parse error; the last three are listed in crush-ast’s CRUSH-192 ticket).
Errors: try / catch / throw
throw raises any value; catch name { ... } receives it. There is no
finally, and the parenthesised form catch (e) is not accepted.
fn divide(a, b) {
if b == 0 {
throw "division by zero"
}
return a / b
}
try {
print(divide(4, 2))
print(divide(1, 0))
print("not reached")
} catch err {
print("Error: " + err)
}
print("done")
2
Error: division by zero
done
A throw crosses function-call boundaries (fixed in crush-ast CRUSH-126),
catch can rethrow, and thrown values can be any type:
try {
try {
throw "inner"
} catch e {
throw "re-" + e
}
} catch outer {
print(outer)
}
try {
throw {"code": 7}
} catch e {
print(e.code)
}
re-inner
7
An uncaught throw ends the program with [runtime] uncaught error: <value>.
What catch catches
try/catch handles values raised with throw, and, since crush-ast
CRUSH-393 (v0.12.0 and later), also runtime faults: division by zero, an out-of-range index, a
runtime type error, a failing host capability (a missing file for fs.read, a
network timeout). The caught value is an error value that prints as
error(<message>):
try {
print(1 / 0)
} catch e {
print("caught: " + e)
}
try {
print([1][3])
} catch e {
print(conv.type_of(e))
}
print("after")
caught: error(division by zero)
error
after
Some failures are not catchable and still end the program even inside try:
running out of a quota (--max-steps, --max-stack, --max-output,
--max-wall-ms, call depth), a capability the run does not grant (refused before
anything runs), sys.exit, an uncaught throw, and a polyglot block that raises:
try {
let i = 0
while true { i = i + 1 }
} catch e {
print("never reached")
}