This document describes all operators in Mux, including their precedence, associativity, and behavior.
Operator Precedence
Higher precedence operators are evaluated first.
| Precedence | Operators | Associativity |
|---|
| 1 (highest) | . (member access), (), [] | Left-to-right |
| 2 | ** | Right-to-left |
| 3 | ! | Right-to-left |
| 4 | *, /, % | Left-to-right |
| 5 | +, - (binary) | Left-to-right |
| 6 | <, <=, >, >= | Left-to-right |
| 7 | ==, != | Left-to-right |
| 8 | &&, || | Left-to-right |
| 9 | in | Left-to-right |
Arithmetic Operators
Binary Arithmetic
| Operator | Description | Types | Example |
|---|
+ | Addition | int, float, string | 5 + 3, "a" + "b" |
- | Subtraction | int, float | 10 - 4 |
* | Multiplication | int, float | 6 * 7 |
/ | Division | int, float | 15 / 3 |
% | Modulo | int, float | 10 % 3 (result: 1) |
** | Exponentiation | int, float | 2 ** 3 (result: 8) |
Arithmetic Rules
- All arithmetic operators require both operands to have the same type
- No implicit type conversion between
int and float
- Division by zero is a runtime error for
int, returns inf for float
- Modulo with negative numbers follows C semantics
- Arithmetic operators are builtin-only for primitive numeric types (not interface-dispatched)
Exponentiation Details
Properties:
- Right-associative:
a ** b ** c = a ** (b ** c)
- Higher precedence than
*, /, %
Increment and Decrement
| Operator | Description | Restrictions |
|---|
++ | Postfix increment | int only, standalone statement |
-- | Postfix decrement | int only, standalone statement |
Usage Rules
Rationale
The postfix-only, standalone-only design prevents ambiguity and side-effect confusion that can occur with prefix operators or expression-embedded increments.
Comparison Operators
| Operator | Description | Types |
|---|
== | Equality | All types |
!= | Inequality | All types |
< | Less than | int, float, string |
<= | Less than or equal | int, float, string |
> | Greater than | int, float, string |
>= | Greater than or equal | int, float, string |
Comparison Rules
- Both operands must have the same type
- No implicit conversion between numeric types
- String comparison is lexicographic (Unicode codepoint order)
Logical Operators
| Operator | Description | Behavior |
|---|
&& | Logical AND | Short-circuit evaluation |
|| | Logical OR | Short-circuit evaluation |
! | Logical NOT | Unary negation |
Short-Circuit Evaluation
Implementation
The && and || operators use LLVM control flow for short-circuit evaluation:
For `a && b`:
1. Evaluate a
2. If a is false, return false (b not evaluated)
3. If a is true, evaluate b and return result
Phi nodes merge results from different branches.
Membership Operator
| Operator | Description | Types |
|---|
in | Membership test | T in list<T>, T in set<T>, string in string, char in string |
Collection Operators
Concatenation with +
| Types | Operation | result |
|---|
list<T> + list<T> | Concatenation | Combined list |
map<K,V> + map<K,V> | Merge | Combined map |
set<T> + set<T> | Union | Set with all elements |
string + string | Concatenation | Combined string |
Compound Assignment Operators
| Operator | Expansion | Types |
|---|
+= | a = a + b | int, float, string |
-= | a = a - b | int, float |
*= | a = a * b | int, float |
/= | a = a / b | int, float |
%= | a = a % b | int, float |
Reference Operators
| Operator | Description | Example |
|---|
& | Create reference | auto r = &x |
* | Dereference | *r = 42 |
See Also