Operators
saQut provides the operators listed below. The parser uses Pratt parsing:
each operator has a precedence level and an associativity, so an expression
like -2 + -5 parses to a single unambiguous tree.
Operator Precedence Table
Section titled “Operator Precedence Table”Higher number = evaluated first.
| Level | Category | Operators | Associativity |
|---|---|---|---|
| 18 | Member access / call | . [ ] ( ) |
Left |
| 17 | Postfix | ++ -- |
Left |
| 16 | Unary prefix | + - ! ~ |
Right |
| 15 | Exponentiation | ** ^ |
Right |
| 14 | Multiply / Divide / Modulo | * / % |
Left |
| 13 | Add / Subtract | + - |
Left |
| 12 | Bitwise shift | << >> |
Left |
| 11 | Relational | < <= > >= |
Left |
| 10 | Equality | == != |
Left |
| 9 | Bitwise AND | & |
Left |
| 8 | Bitwise XOR | ^ |
Left |
| 7 | Bitwise OR | | |
Left |
| 6 | Logical AND | && |
Left |
| 5 | Logical OR | || |
Left |
| 4 | Ternary condition | ? |
Right |
| 3 | Ternary else | : |
Right |
| 2 | Assignment | = += -= *= etc. |
Right |
| 1 | Comma | , |
Left |
Right-associative:
a = b = 5→a = (b = 5)Left-associative:10 - 4 - 3→(10 - 4) - 3= 3
Arithmetic Operators
Section titled “Arithmetic Operators”int sum = 10 + 5; // 15int diff = 10 - 5; // 5int product = 10 * 5; // 50int quotient = 10 / 5; // 2int remainder = 10 % 3; // 1
int neg = -10; // unary minusint pos = +10; // unary plusExponentiation
Section titled “Exponentiation”int a = 2 ** 3; // 8 (2³)int b = 2 ^ 3; // also 8Both ** and ^ mean exponentiation. They are right-associative:
2 ^ 3 ^ 2 → 2 ^ (3 ^ 2) = 2 ^ 9 = 512
Increment & Decrement
Section titled “Increment & Decrement”int x = 5;x++; // x = 6 (postfix, returns old value)++x; // x = 7 (prefix, returns new value)x--; // x = 6--x; // x = 5Note:
++and--work onintandfloattypes. They are statements, not just expressions: you cannot writefoo(x++)yet.
Comparison Operators
Section titled “Comparison Operators”All comparison operators return a bool (true or false).
int a = 5;int b = 10;
bool eq = a == b; // falsebool neq = a != b; // truebool lt = a < b; // truebool lte = a <= b; // truebool gt = a > b; // falsebool gte = a >= b; // falseComparisons work on int, float, and bool (which is stored as int).
string supports == and != (content comparison) but not <, >, etc.
Logical Operators
Section titled “Logical Operators”bool a = true;bool b = false;
bool and = a && b; // falsebool or = a || b; // truebool not = !a; // falseThe && and || operators are short-circuit: they evaluate the right
side only when necessary.
int x = 0;
// Right side never runs because left is falseif (false && (x = 10)) { }// x is still 0
// Right side never runs because left is trueif (true || (x = 20)) { }// x is still 0Bitwise Operators
Section titled “Bitwise Operators”Work on int values, bit-by-bit.
int a = 0b1100; // 12int b = 0b1010; // 10
int and = a & b; // 0b1000 = 8int or = a | b; // 0b1110 = 14int xor = a ^ b; // 0b0110 = 6int not = ~a; // flips all bits
int left = a << 2; // 0b110000 = 48int right = a >> 2; // 0b0011 = 3Assignment Operators
Section titled “Assignment Operators”Simple assignment:
int x = 5;Compound assignments combine an operation with assignment. They are
right-associative: a += b += 5 → a += (b += 5).
int x = 10;
x += 5; // x = 15x -= 3; // x = 12x *= 2; // x = 24x /= 4; // x = 6x %= 4; // x = 2x &= 3; // x = 2x |= 8; // x = 10x ^= 5; // x = 15x <<= 1; // x = 30x >>= 1; // x = 15Ternary Operator
Section titled “Ternary Operator”A compact if-else that returns a value:
int x = 5;string result = x > 0 ? "positive" : "zero or negative";Type Cast Operator (as)
Section titled “Type Cast Operator (as)”The as operator converts between compatible types. It is infix and
left-associative.
float pi = 3.14;int n = pi as int; // 3 (truncates toward zero)
string s = 42 as string; // "42"
int? maybe = "3.14" as int?; // null (conversion failed)Learn more about type casting…
