Language reference
Types
MUL has three types: i32, bool and string. Writing any other type
name, such as i64, is an error (MUL-T002). There are no conversions
between them: if 1 { } and 1 + true are type errors.
i32
A 32-bit signed integer in two's complement, range
-2147483648..=2147483647. A variable's type annotation (let x: i32 = 1)
is optional.
bool
true or false. Comparisons and logical operators produce it, and if
conditions require it. It can be a parameter, a variable or a return value.
// expect: 1
module demo
fn between(x: i32, low: i32, high: i32) -> bool {
return low <= x && x <= high
}
pub fn main() -> i32 {
let inside: bool = between(5, 1, 9)
if inside && !between(0, 1, 9) {
return 1
}
return 0
}
Type checking
Every operand, initializer, assigned value, argument, returned value and
condition must have the expected type, otherwise compilation fails with
MUL-T012 (type mismatch: expected X, found Y):
| Operator | Operands | Result |
|---|---|---|
+ - * / %, unary - |
i32 |
i32 |
< <= > >= |
two i32 |
bool |
== != |
two values of the same type | bool |
&& || ! |
bool |
bool |
// expect-error: MUL-T012
module demo
pub fn main() -> i32 {
let ready = true
return ready + 1
}
Literals
- Decimal digits only: no
0x, no_separators, no suffixes. - A literal must fit in
0..=2147483647(MUL-T003). The one exception is-2147483648, which is read asi32::MIN. - A negative number is unary minus applied to a literal.
// expect: -2147483648
module demo
pub fn main() -> i32 {
return -2147483648
}
Arithmetic
All arithmetic is checked. A result outside the i32 range stops the program
with MUL-R002; it never wraps around.
// expect-error: MUL-R002
module demo
pub fn main() -> i32 {
return 2147483647 + 1
}
Overflow also covers -(-2147483648), -2147483648 / -1 and -2147483648 % -1.
Division truncates toward zero, and the remainder has the sign of the dividend:
// expect: -31
module demo
pub fn main() -> i32 {
return (-7 / 2) * 10 + (-7 % 2)
}
Division or remainder by zero stops the program with MUL-R003. The zero
check comes first: -2147483648 / 0 is MUL-R003. Each operation is checked
on its own, so 2147483647 + 1 - 1 traps although the final value would fit.
Evaluation order
Statements run in order. Operands are evaluated left to right, the left
operand fully before the right one. && and || evaluate the right operand
only when it decides the result, so a trap there is skipped when it is not
needed:
// expect: 0
module demo
fn fails() -> bool {
return 1 / 0 == 0
}
pub fn main() -> i32 {
if false && fails() {
return 1
}
return 0
}
Every other operand runs. Call arguments are evaluated left to right
before the call. Every initializer and assigned value is evaluated even when
nothing reads it. The order is observable through print:
// expect: left
// expect: right
// expect: 3
module demo
fn say(text: string, n: i32) -> i32 io {
print(text)
return n
}
pub fn main() -> i32 io {
return say("left", 1) + say("right", 2)
}
and through which trap is reported, since the first one in this order wins:
// expect-error: MUL-R003
module demo
pub fn main() -> i32 {
return (1 / 0) + (2147483647 + 1)
}
string
Immutable UTF-8 text, written as a literal in double quotes
(syntax). A string can be a parameter, a variable or a return
value, and print writes it with a line feed. Nothing else works on strings
yet: == and != are MUL-T015, + and < take i32 (MUL-T012).
// expect: Ada says hi
// expect: 0
module demo
fn greeting(polite: bool) -> string {
if polite {
return "Ada says hi"
}
return "hi"
}
pub fn main() -> i32 io {
let line: string = greeting(true)
print(line)
return 0
}
Conversions
There are none, neither implicit nor explicit, and a type name is not a
function (i32(true) is MUL-T008). To turn a bool into an i32, use
if; to turn an i32 into a bool, compare it (n != 0).
Normative rules
RFC-003 states every rule on this page as
a numbered SEM-NN rule, and
tests/conformance/ checks each one in
every backend.
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.