Skip to main content

Operators

This document describes all operators in Mux, including their precedence, associativity, and behavior.

Operator Precedence

Higher precedence operators are evaluated first.

PrecedenceOperatorsAssociativity
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
9inLeft-to-right

Arithmetic Operators

Binary Arithmetic

OperatorDescriptionTypesExample
+Additionint, float, string5 + 3, "a" + "b"
-Subtractionint, float10 - 4
*Multiplicationint, float6 * 7
/Divisionint, float15 / 3
%Moduloint, float10 % 3 (result: 1)
**Exponentiationint, float2 ** 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

snippet.mux
Loading...

Properties:

  • Right-associative: a ** b ** c = a ** (b ** c)
  • Higher precedence than *, /, %

Increment and Decrement

OperatorDescriptionRestrictions
++Postfix incrementint only, standalone statement
--Postfix decrementint only, standalone statement

Usage Rules

snippet.mux
Loading...

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

OperatorDescriptionTypes
==EqualityAll types
!=InequalityAll types
<Less thanint, float, string
<=Less than or equalint, float, string
>Greater thanint, float, string
>=Greater than or equalint, float, string

Comparison Rules

  • Both operands must have the same type
  • No implicit conversion between numeric types
  • String comparison is lexicographic (Unicode codepoint order)
snippet.mux
Loading...

Logical Operators

OperatorDescriptionBehavior
&&Logical ANDShort-circuit evaluation
||Logical ORShort-circuit evaluation
!Logical NOTUnary negation

Short-Circuit Evaluation

snippet.mux
Loading...

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

OperatorDescriptionTypes
inMembership testT in list<T>, T in set<T>, string in string, char in string
snippet.mux
Loading...

Collection Operators

Concatenation with +

TypesOperationresult
list<T> + list<T>ConcatenationCombined list
map<K,V> + map<K,V>MergeCombined map
set<T> + set<T>UnionSet with all elements
string + stringConcatenationCombined string
snippet.mux
Loading...

Compound Assignment Operators

OperatorExpansionTypes
+=a = a + bint, float, string
-=a = a - bint, float
*=a = a * bint, float
/=a = a / bint, float
%=a = a % bint, float
snippet.mux
Loading...

Reference Operators

OperatorDescriptionExample
&Create referenceauto r = &x
*Dereference*r = 42
snippet.mux
Loading...

See Also