Language reference

Syntax

Formal grammar: spec/grammar.ebnf. Normative sources: RFC-001, RFC-002, RFC-005, RFC-006, RFC-007, RFC-009, RFC-010, RFC-011.

Source text

Layout

One program has two layouts that differ only in whitespace (RFC-011, ADR-013). The compact layout is the canonical one, written by mul fmt and used by AI agents: a space only between two word tokens, a line feed only after a comment, nothing else. The pretty layout, written by mul fmt --pretty, is for people: one statement per line, four-space indentation, spaces around operators, a blank line before every function. This reference shows the pretty layout; the compact guide shows the compact one.

// expect: 55
module demo fn fib(n:i32)->i32{if n<2{return n}return fib(n-1)+fib(n-2)}pub fn main()->i32{return fib(10)}
// expect: 55
module demo

fn fib(n: i32) -> i32 {
    if n < 2 {
        return n
    }
    return fib(n - 1) + fib(n - 2)
}

pub fn main() -> i32 {
    return fib(10)
}

Module

A file starts with module and a dotted name, followed by functions in any order.

// expect: 3
module shop.pricing

pub fn main() -> i32 {
    return fee() + 1
}

fn fee() -> i32 {
    return 2
}

The module name has no effect on resolution yet (there are no imports), but it appears in the IR dump and in native symbol names (mul.<module>.<function>).

Functions

[pub] fn name(param: type, ...) -> type [pure | io] { body }

Body and statements

A body is a block: statements in braces, run in order. Statements need no separator; line breaks carry no meaning.

Statement Meaning
let name = e, let name: i32 = e new immutable variable
var name = e, var name: i32 = e new mutable variable
name = e store a new value in a var
{ ... } nested block with its own scope
if c { ... } else if d { ... } else { ... } run the first block whose condition is true
while c { ... } run the block again and again while c is true
return e end the function with the value of e
// expect: 7
module demo

pub fn main() -> i32 {
    let base: i32 = 3
    var total = base * 2
    {
        let bonus = 1
        total = total + bonus
    }
    return total
}

Rules:

// expect: 12
module demo

pub fn main() -> i32 {
    {
        let step = 5
    }
    let step = 6
    {
        return step * 2
    }
}

if and else

// expect: 7
module demo

fn clamp(value: i32, low: i32, high: i32) -> i32 {
    var result = value
    if value < low {
        result = low
    } else if value > high {
        result = high
    }
    return result
}

pub fn main() -> i32 {
    return clamp(-4, 0, 10) + clamp(7, 0, 10)
}
// expect: 3628800
module demo

fn factorial(n: i32) -> i32 {
    if n <= 1 {
        return 1
    }
    return n * factorial(n - 1)
}

pub fn main() -> i32 {
    return factorial(10)
}

while

// expect: 55
module demo

fn sumTo(n: i32) -> i32 {
    var i = 1
    var total = 0
    while i <= n {
        total = total + i
        i = i + 1
    }
    return total
}

pub fn main() -> i32 {
    return sumTo(10)
}
// expect: 7
module demo

fn smallestDivisor(n: i32) -> i32 {
    var d = 2
    while d * d <= n {
        if n % d == 0 {
            return d
        }
        d = d + 1
    }
    return n
}

pub fn main() -> i32 {
    return smallestDivisor(91)
}

Expressions

Form Meaning
42 integer literal (decimal digits only)
true, false bool literals
x a parameter or a variable in scope
f(a, b) call of function f with arguments evaluated left to right
-e, !e negation of an i32, logical not of a bool
a * b, a / b, a % b multiplication, division, remainder
a + b, a - b addition, subtraction
a == b, a != b, a < b, a <= b, a > b, a >= b comparison, result bool
a && b true when both are; b runs only when a is true
a || b true when either is; b runs only when a is false
(e) grouping

Precedence, from strongest: unary - and !, then * / %, then + -, then comparisons, then &&, then ||. Binary operators associate to the left: 10 - 3 - 2 is (10 - 3) - 2. Comparisons do not chain: a < b < c is MUL-S005; write a < b && b < c.

// expect: 5
module demo

pub fn main() -> i32 {
    return 10 - 3 - 2 + 2 * -(-0)
}

Functions and values live in separate namespaces: x always names a parameter or variable and x(...) always calls a function.

// expect: 12
module demo

fn twice(twice: i32) -> i32 {
    return twice * 2
}

pub fn main() -> i32 {
    return twice(6)
}

Limits

This page is docs/language/ in the repository, published as it is. Every mul code block on it is compiled and run by the test suite.