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
- A source file is UTF-8, one module per file, extension
.mul. - ASCII whitespace separates tokens; line breaks and indentation carry no meaning. It is required only between two word tokens (keywords, names, numbers) and as the line feed that ends a comment; see Layout.
//starts a comment that runs to the end of the line. There are no block comments.- Identifiers are ASCII: a letter or
_, then letters, digits or_. Non-ASCII characters outside comments and string literals are an error (MUL-S001). - A string literal is text in double quotes,
"Hello", on one line. Escapes:\n,\t,\\,\". Any other escape, a raw tab or other control character, or a missing closing quote isMUL-S006. - Keywords:
module,pub,fn,return,let,var,if,else,while,true,false,pure,io. They cannot be used as names. - Punctuation:
( ) { } -> + - * / % . , : = == != < <= > >= && || !. Nothing else is a token; a lone&or|isMUL-S001.
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)
}
- No statement needs a separator, and the grammar never depends on a line feed: a program without comments is valid on one line.
- Both layouts keep every token and comment; converting either way and back changes nothing. Only a program without syntax errors can be formatted.
- An error in a one-line file is located by column and byte offset (errors).
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 }
- Every function declares a return type:
i32orbool. - Parameters are
name: type, separated by commas; no trailing comma. - An optional effect follows the return type:
pure(the default when none is written) orio. Apurefunction cannot call anioone (MUL-T013); see effects. pubmarks a function as public. With a single module it changes nothing yet; write it onmainand on functions meant to be called from other modules later.- Function names are unique in the module (
MUL-T001); parameter names are unique in a function (MUL-T006). - A function may call itself and functions defined later in the file.
mul runandmul buildstart atmain, which must befn() -> i32(MUL-R006otherwise).
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:
- Every variable has an initializer. The type annotation (
: i32or: bool) is optional; without it the variable takes the initializer's type. - A variable is visible from the next statement to the end of its block. Parameters are visible in the whole body.
- No shadowing: a declaration may not reuse a visible name, parameter or
variable (
MUL-T010). Once a block ends, its names can be declared again. - Only a
varcan be assigned; assigning to aletor a parameter isMUL-T011. Assignment is a statement, never an expression. - A call can stand alone as a statement:
print("hi")orf(x). A function's value is then discarded. No other expression can:x + 1orf(x) + 1alone isMUL-S002. A statement that starts with a name is an assignment (name = ...) or a call (name(...)). - Every path through the body must end with
return(MUL-T004). A statement terminates when it is areturn, a block ending with a terminating statement, or anifwith anelsewhose every arm terminates. Awhilenever terminates. Nothing may follow a terminating statement in the same block (MUL-T005).
// 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)
}
- The condition must be a
bool(MUL-T012); it needs no parentheses, and every arm needs braces. - Conditions are tested in order; the first
trueone runs its block and the rest are skipped. With noelse, nothing runs when all arefalse. - Every arm is a block with its own scope: a name declared in an arm ends
with it. A
varassigned in an arm keeps the value of the path that ran. ifis a statement, not an expression; there is no?:.- An
ifwithoutelsenever terminates, so areturnmust follow it.
// 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)
}
- The condition must be a
bool(MUL-T012) and is tested before every iteration, including the first, so the body may run zero times. It needs no parentheses; the body needs braces. - The body is a block with its own scope, opened anew for every iteration: a name declared in it is not visible in the condition or after the loop.
- A
vardeclared before the loop and assigned in the body keeps the value of the last completed iteration. - The loop ends when the condition is
falseor when the body runsreturn, which leaves the whole function. There is nobreakorcontinue. - A
whilenever terminates, evenwhile true: areturnmust follow it (MUL-T004). - There is no iteration limit; a condition that stays
trueloops forever.
// 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
- One expression may contain at most 256 operators, calls and parentheses
combined (
MUL-S003). Split larger expressions withletor helper functions. - Blocks nest at most 64 levels deep, counting the function body (
MUL-S004).
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.