Reference · 03

Operators & Precedence

Complete operator precedence table, associativity, short-circuit rules, and compound assignment semantics in Ochre.

Operators are listed from highest precedence (tightest binding) to lowest precedence. ochre fmt automatically preserves parenthesization whenever grouping overrides default precedence.

Precedence Table

LevelOperatorsDescriptionAssociativity
1(...), f(...), a.b, a[i], e?Grouping, call, field access, indexing, try propagationLeft-to-right
2-x, !xUnary negation (Int/Float), logical NOT (Bool)Right-to-left
3*, /, %Multiplication, division, remainderLeft-to-right
4+, -Addition / Str concatenation, subtractionLeft-to-right
5..Exclusive integer range (start..end)Non-associative
6<, <=, >, >=Ordering comparison (Int, Float, Str)Left-to-right
7==, !=Equality / inequality (matching types)Left-to-right
8&&Short-circuit logical AND (Bool)Left-to-right
9`\\`Short-circuit logical OR (Bool)Left-to-right
10??Fallback coalescing (Option/Result)Left-to-right
11=, +=, -=, *=, /=Assignment & compound assignment (statements)Statement

Operator Rules

  • No implicit numeric coercion: Int + Float is a compile-time error. Use float(a) + b or a + int(b).
  • String concatenation: + concatenates Str + Str, returning Str.
  • Short-circuit guarantees: Both && and || skip evaluating the right-hand operand when the left-hand operand determines the outcome — identically in both the bytecode VM and Cranelift AOT.
  • *Compound assignments (+=, -=, =, /=): Valid on Int and Float targets (including nested var indices/fields like rows[i][j] += 5). Every object and index expression on the left-hand side is evaluated exact once**.
fn main() {
    let a = (1 + 2) * 3
    let b = 1 + 2 * 3
    let c = 10 - (4 - 1)
    println("a={a}, b={b}, c={c}")
}

Expected Output

a=9, b=7, c=7