Operators Priority
Operators have precedence and, in the case of binary operators, associativity.
Precedence can be altered using parentheses (...).
For example, a + b + c is equivalent to (a + b) + c.
In general, Ad Astra's operator precedence is similar to the rules in Rust and many other languages with C-like syntax.
| Operators | Precedence | Associativity |
|---|---|---|
Assignment: a = b, a += b, etc | 1 | Right-to-Left |
Binary disjunction: a || b | 2 | Left-to-Right |
Binary conjunction: a && b | 3 | Left-to-Right |
Equality and ordering: a == b, a > b, etc | 4 | Left-to-Right |
Range operator: 10..20 | 5 | Left-to-Right |
Bitwise disjunction: a | b | 6 | Left-to-Right |
Bitwise exclusive disjunction: a ^ b | 7 | Left-to-Right |
Bitwise conjunction: a & b | 8 | Left-to-Right |
Bitwise shift: a << b and a >> b | 9 | Left-to-Right |
Additive: a + b and a - b | 10 | Left-to-Right |
Multiplicative: a * b, a / b, and a % b | 11 | Left-to-Right |
Unary Left: -a, *a, !a | 12 | Left-to-Right |
Unary Right: a?, a(arg), a[idx], a.field | 13 | Left-to-Right |
Atomic operand: ident, crate, self, max | 14 | Left-to-Right |
Operators with a higher precedence number take priority over those with a lower
precedence number: a + b * c means a + (b * c), because multiplicative
precedence is higher than additive.
Associativity indicates the typical order of operand evaluation. In the
expression a = b + c, the b + c expression is evaluated before a.