Tzopilotl by Example
9. Operators
9.1 Arithmetic
3 + 4 -- addition
10 - 3 -- subtraction
6 * 7 -- multiplication
1 / 3 -- division (int/int produces fraction)
10 // 3 -- integer (floor) division: 3
10 % 3 -- modulo: 1
-x -- negation
9.2 Comparison
a == b -- equal
a != b -- not equal
a < b -- less than
a <= b -- less or equal
a > b -- greater than
a >= b -- greater or equal
-- Comparison works on strings (lexicographic)
"apple" < "banana" -- true
"abc" >= "abc" -- true
-- == and != work on ALL types (structural equality)
(1, 2) == (1, 2) -- true
[1, 2, 3] == [1, 2, 3] -- true
'foo == 'foo -- true
-- User-defined == overloads take priority
struct Vec2 { x Float, y Float }
fn ==(a Vec2, b Vec2) Bool {
let dx = a.x - b.x;
let dy = a.y - b.y;
dx * dx + dy * dy < 0.001
}
9.3 Logical
a && b -- logical and
a || b -- logical or
!a -- logical not
9.4 Bitwise
255 & 15 -- bitwise and: 15
240 | 15 -- bitwise or: 255
255 ^ 15 -- bitwise xor: 240
~0 -- bitwise not: -1
1 << 10 -- left shift: 1024
1024 >> 5 -- arithmetic right shift: 32
(-1) >>> 1 -- unsigned right shift: 9223372036854775807
9.5 Concatenation ($)
-- String concatenation
"Hello" $ ", " $ "World" -- "Hello, World"
-- Array concatenation (same element type)
[1, 2] $ [3, 4] -- [1, 2, 3, 4]
-- List concatenation (lazy)
List(1, 2) $ List(3, 4) -- List(1, 2, 3, 4)
-- Tuple concatenation
(1, 2) $ (3, 4) -- (1, 2, 3, 4)
9.6 Operator Precedence (low to high)
<- -> — ref assignment (right-associative)
|> — pipeline
|| — logical or
&& — logical and
:: — cons (right-associative)
== != — equality
< <= > >= — comparison
| — bitwise or
^ — bitwise xor
& — bitwise and
<< >> >>> — shifts
+ - $ — additive / concatenation
* / % // — multiplicative