Operators
Zeus provides a familiar set of operators for arithmetic, comparison, and logical operations.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition (numbers) or concatenation (strings) | 5 + 3 → 8, "a" + "b" → "ab" |
- | Subtraction | 5 - 3 → 2 |
* | Multiplication | 5 * 3 → 15 |
/ | Division | 15 / 3 → 5 |
% | Modulo (remainder) | 7 % 3 → 1 |
** | Power (exponentiation) | 2.0 ** 3.0 → 8.0 |
Examples
function main(): i32 { let a: i32 = 10; let b: i32 = 3;
let sum: i32 = a + b; // 13 let diff: i32 = a - b; // 7 let product: i32 = a * b; // 30 let quotient: i32 = a / b; // 3 (integer division)
return sum + diff + product + quotient;}Integer Division
Division between integers produces an integer result:
let result: i32 = 7 / 2; // 3, not 3.5For floating-point division, use float types:
let result: f64 = 7.0 / 2.0; // 3.5Modulo Operator
The modulo operator returns the remainder of division:
let remainder: i32 = 17 % 5; // 2let isEven: boolean = (10 % 2) == 0; // truePower Operator
The power operator performs exponentiation. Note that it currently works with floating-point numbers:
let squared: f64 = 2.0 ** 2.0; // 4.0let cubed: f64 = 2.0 ** 3.0; // 8.0Unary Minus
Negate a value:
let positive: i32 = 42;let negative: i32 = -positive; // -42Comparison Operators
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 → true |
!= | Not equal to | 5 != 3 → true |
< | Less than | 3 < 5 → true |
<= | Less than or equal | 5 <= 5 → true |
> | Greater than | 5 > 3 → true |
>= | Greater than or equal | 5 >= 5 → true |
Examples
function compare(a: i32, b: i32): i32 { if (a == b) { return 0; // Equal } if (a < b) { return -1; // a is smaller } return 1; // a is larger}
function main(): i32 { return compare(10, 5); // Returns 1}Comparing Different Types
Comparison works between compatible types:
let intVal: i32 = 5;let floatVal: f64 = 5.0;
// This works because i32 can be implicitly converted to f64let equal: boolean = floatVal == intVal; // trueString Operators
Zeus supports operators for string manipulation:
| Operator | Description | Example |
|---|---|---|
+ | Concatenation | "Hello" + " World" → "Hello World" |
== | Equal to | "abc" == "abc" → true |
!= | Not equal to | "abc" != "xyz" → true |
< | Lexicographically less | "apple" < "banana" → true |
<= | Less than or equal | "abc" <= "abc" → true |
> | Lexicographically greater | "banana" > "apple" → true |
>= | Greater than or equal | "abc" >= "abc" → true |
String Concatenation
let greeting: string = "Hello" + " " + "World";log(greeting); // "Hello World"String Comparison
let a: string = "apple";let b: string = "banana";let c: string = "apple";
if (a == c) { log("a equals c"); // This executes}
if (a < b) { log("apple comes before banana"); // This executes}Logical Operators
| Operator | Description | Example |
|---|---|---|
! | Logical NOT | !true → false |
&& | Logical AND | true && false → false |
|| | Logical OR | true || false → true |
Examples
function main(): i32 { let a: boolean = true; let b: boolean = false;
let notA: boolean = !a; // false let andResult: boolean = a && b; // false let orResult: boolean = a || b; // true
// Short-circuit evaluation if (a || expensiveCheck()) { // expensiveCheck() may not be called if a is true }
return 0;}Combining Conditions
function isInRange(x: i32, min: i32, max: i32): boolean { return x >= min && x <= max;}
function isValidInput(value: i32): boolean { return value > 0 || value == -1; // -1 is a special "any" value}Assignment Operators
Basic Assignment
The = operator assigns a value to a variable:
let x: i32 = 10; // Initial assignmentx = 20; // Reassignmentx = x + 5; // Update based on current valueCompound Assignment Operators
Zeus supports compound assignment operators that combine arithmetic and assignment:
| Operator | Description | Equivalent |
|---|---|---|
+= | Add and assign | x = x + y |
-= | Subtract and assign | x = x - y |
*= | Multiply and assign | x = x * y |
/= | Divide and assign | x = x / y |
%= | Modulo and assign | x = x % y |
function main(): i32 { let x: i32 = 10;
x += 5; // x is now 15 x -= 3; // x is now 12 x *= 2; // x is now 24 x /= 4; // x is now 6 x %= 4; // x is now 2
return x;}Increment and Decrement Operators
Zeus supports both prefix and postfix increment/decrement operators:
| Operator | Description | Example |
|---|---|---|
++x | Prefix increment (increment, then return new value) | let y: i32 = ++x; |
x++ | Postfix increment (return old value, then increment) | let y: i32 = x++; |
--x | Prefix decrement (decrement, then return new value) | let y: i32 = --x; |
x-- | Postfix decrement (return old value, then decrement) | let y: i32 = x--; |
Prefix vs Postfix
function main(): i32 { let a: i32 = 5; let b: i32 = ++a; // a is 6, b is 6 (increment first)
let c: i32 = 5; let d: i32 = c++; // c is 6, d is 5 (return old value first)
return 0;}Common Usage in Loops
function main(): i32 { let sum: i32 = 0;
for (let i: i32 = 0; i < 10; i++) { sum += i; }
return sum; // 45}Operator Precedence
Operators follow standard mathematical precedence:
| Precedence | Operators | Description |
|---|---|---|
| Highest | ++, -- (postfix) | Postfix increment/decrement |
., [] | Member access, indexing | |
() | Function call | |
++, -- (prefix), !, - | Prefix increment/decrement, NOT, negation | |
** | Power (right-associative) | |
*, /, % | Multiplication, division, modulo | |
+, - | Addition, subtraction | |
<, <=, >, >= | Comparison | |
==, != | Equality | |
&& | Logical AND | |
|| | Logical OR | |
| Lowest | =, +=, -=, *=, /=, %= | Assignment |
Examples
let result: i32 = 2 + 3 * 4; // 14 (not 20)let result2: i32 = (2 + 3) * 4; // 20 (parentheses first)let result3: i32 = 10 - 5 - 2; // 3 (left to right)Practical Examples
Absolute Value
function abs(n: i32): i32 { if (n < 0) { return -n; } return n;}Distance Formula (simplified)
function manhattanDistance(x1: i32, y1: i32, x2: i32, y2: i32): i32 { let dx: i32 = x2 - x1; let dy: i32 = y2 - y1;
if (dx < 0) { dx = -dx; } if (dy < 0) { dy = -dy; }
return dx + dy;}Min and Max
function min(a: i32, b: i32): i32 { if (a < b) { return a; } return b;}
function max(a: i32, b: i32): i32 { if (a > b) { return a; } return b;}
function clamp(value: i32, minVal: i32, maxVal: i32): i32 { return max(minVal, min(value, maxVal));}