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

Quick Reference

Cheat sheets for the toolchain, the Crush language and CASM. Every code block that can run is run by the guide’s checker against crush-ast dc0d0278. Details live in the chapters linked from each heading.

Command line (Getting Started)

CommandDoes
crush run FILE [flags]run a .crush, .cast.json, .casm (CASM JSON), .cvms (assembly) or .cvm1 file
crush build FILE.crushcompile to .cvm1
crush-run run FILE [flags]same as crush run; all the capability flags live here
crush-run caps [FILE]list the capabilities of your build, or what FILE needs (runs nothing)
crushc FILE.crush -o OUT.cvm1compile; --emit casm|ast|types prints a layer; -C only checks
crush-compile FILE.cvms -o OUT.cvm1 --cap NAMEassemble CVM1 assembly text (or CASM JSON .casm)
crush repl / crush-replinteractive REPL (.help, .quit)
crush doctorwhich polyglot interpreters are on PATH
crush debug FILE --step --inspectbreakpoints, step, watch, print (Debugging)
crush gui run FILE --uia program that draws a window; needs the separate crush-gui (Windows)
crush-aotc run FILE.crushahead-of-time compile via rustc (--backend gcc) and run
crush-walk-run FILE.pywalk a Python/JS/Rust/… file to CAST and run it

Capability flags (everything is off unless you pass it, except the standard library, which --no-stdlib removes): --fs --fs-root DIR --env --time --net (HTTP) --tcp --tcp-listen (raw sockets) --process --crypto --db PATH --graphics --bus --task --akg --polyglot --cap NAME. A program that needs an ungranted capability is refused before it runs; --message-format json makes errors one JSON object per line (Diagnostics). Quotas: --max-steps (default 1,000,000, or unlimited), --max-wall-ms (30,000 for the whole run, or unlimited), --max-stack (4,096), --max-output (1 MiB), --max-call-depth (256).

Language (Syntax)

Variables and functions

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

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

fn greet(who) {
    print("Hello, " + who)
}

greet(name)
print(add(x, 1))
Hello, Alice
43
  • Semicolons are optional. Comments are // and #. Strings use "…" or '…'.
  • A function returns a value only with return; falling off the end yields null.
  • print(x) takes exactly one argument; io.print(a, b, …) takes any number.
  • No let mut, no T? types, no working import (CRUSH-110).

Closures (Closures)

fn make_counter() {
    let c = 0
    return || {
        c = c + 1
        return c
    }
}
let next = make_counter()
next()
print(next())
let sq = |x| x * x
print(5 |> sq)
2
25

Control flow (Control Flow)

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

let i = 0
while i < 2 {
    i = i + 1
}

for k in 0..3 {
    print(k)
}

for item in ["a", "b"] {
    print(item)
}
medium
0
1
2
a
b

0..3 excludes the end, 0..=3 includes it. for also walks a string’s characters and a map’s keys. break and continue work in while and for. match v { 1 => "one", P { x: a } => a, _ => "other" } matches literals, bindings and struct patterns; return match … returns its value.

Operators (Operators)

+ - * / % · == != < > <= >= · && || ! or and or not (short-circuit) · unary - · pipeline |> · ranges a..b, a..=b. There is no **, //, +=, bitwise operator or ternary. Int overflow and division by zero are run-time errors.

Errors

try {
    throw "disk full"
} catch e {
    print("caught: " + e)
}
caught: disk full

Structs and objects (Types)

struct Point { x: Int, y: Int }
impl Point {
    fn sum(self) { return self.x + self.y }
}
let p = Point { x: 3, y: 4 }
let o = {"name": "crush"}
o["lang"] = "en"
print(p.sum())
print(o.name + "/" + o["lang"])
7
crush/en

Structs are sealed (no undeclared fields), references by default (@kind(semantics: value(copy)) for values), and == compares fields. See Structs, Methods and Kinds.

Capabilities (Capabilities)

Capability calls are dotted and have no @. They exist only if you enabled them:

if fs.exists("notes.txt") {
    print(fs.read("notes.txt"))
} else {
    print("no notes")
}
no notes
io.print("a", "b", 3)
print(str.len("héllo"))
ab3
6

Polyglot blocks (Polyglot)

@python -> out { }, @javascript -> out { } and @bash { } run as host subprocesses, only with --polyglot, and with the host’s full authority (no sandbox by default). Names a block reads are passed in; the names after -> come back. Without the flag the program is refused:

@python {
    print("hello from python")
}

Types (Types)

TypeExampleNotes
Int42, 0x2A, 0b10164-bit, checked overflow
Float3.14, 1.5e3exponent form since 0.11
String"hello", 'hello'UTF-8; len counts bytes
Booltrue
Nullnull
array[1, 2, 3]
object{"key": "value"}insertion-ordered
structstruct P { x, y }one line; P { x: 1, y: 2 }
function|x| x + 1, fn namesvalues; closures capture by reference

Annotations (: Int, -> Int) are checked at compile time and are optional.

Capabilities at a glance

Always onio.print io.read str.concat str.len conv.chr conv.ord
Standard library (default)str.* math.* conv.* collections.* json.* path.* regex.* sys.args …
--fsfs.read fs.write fs.exists fs.list fs.mkdir …
--envenv.get env.all env.home_dir
--timetime.now time.now_ms time.now_iso time.elapsed time.sleep
--netnet.http_get net.http_post
--processprocess.exec
--cryptocrypto.sha256 crypto.random
--db PATHdb.query db.execute
--polyglot@python @javascript @bash

CASM text assembly (CASM)

.func main
    PUSH 40
    PUSH 2
    ADD
    CAP_CALL "io.print" 1
    HALT
42
GroupMnemonics
StackPUSH n PUSH_F64 x PUSH_STR "s" PUSH_BOOL 0|1 PUSH_NULL POP DUP SWAP ROT PICK n ROLL n
VariablesLOAD slot STORE slot
ArithmeticADD SUB MUL DIV MOD NEG
Compare / logicEQ NE LT GT LE GE AND OR NOT
BitsBITAND BITOR BITXOR BITNOT SHL SHR
ControlJMP label JZ label JNZ label CALL name RET HALT ENTER_TRY label EXIT_TRY THROW
CapabilitiesCAP_CALL "name" argc
CollectionsNEW_ARRAY n ARR_GET ARR_SET ARR_LEN ARR_PUSH ARR_POP NEW_OBJ GET_FIELD "f" SET_FIELD "f"
Strings / mathSTR_CONTAINS STR_SPLIT STR_REPLACE STR_JOIN STR_TRIM STR_TO_UPPER MATH_POW MATH_SQRT …

Stack effects and the JSON-IR names are in the Instruction Reference. Remember: arguments are pushed first argument first (PUSH_STR "a", PUSH_STR "b", CAP_CALL "io.print" 2 prints ab), and an assembly file (.cvms) needs --cap (or a .cap line) for every capability it calls.