Arithmetic
x = 10 + 3 # 13
x = 10 - 3 # 7
x = 10 * 3 # 30
x = 10 / 3 # 3 (integer division when both are int)
x = 10.0 / 3 # 3.3333... (float division)Division between two int values truncates toward zero. To get a float result, make at least one operand a float:
half = 1.0 / 2 # 0.5Negation uses a leading -:
n = -xComparison
All comparison operators return bool:
x == y # equal
x != y # not equal
x < y # less than
x > y # greater than
x <= y # less than or equal
x >= y # greater than or equalBoolean
a and b # true only if both are true
a or b # true if either is true
not a # inverts a booleanShort-circuit evaluation applies: and stops at the first false, or stops at the first true.
alive = true
health = 0
if alive and health > 0 do
print("still fighting")
endPipe Operator
The |> operator passes the value on its left as the first argument to the call on its right. This makes left-to-right data transformation chains readable:
# Without pipe:
result = clamp(scale(delta, 2.0), 0.0, 100.0)
# With pipe:
result = delta |> scale(2.0) |> clamp(0.0, 100.0)Each step receives the previous result as its first argument, and any additional arguments follow normally.
String Interpolation
Any expression can be embedded inside a string literal using #{}:
name = "world"
score = 42
msg = "Hello, #{name}! Score: #{score}"Nested expressions work too:
label = "HP: #{to_int(@player_hp * 100)}%"The expression is evaluated at runtime and converted to a string automatically.
Operator Precedence
From highest to lowest:
| Precedence | Operators |
|---|---|
| Unary | -, not |
| Multiplicative | *, / |
| Additive | +, - |
| Comparison | ==, !=, <, >, <=, >= |
| Boolean | and, or |
| Pipe | |> |
Use parentheses to override precedence:
result = (a + b) * c
valid = (x > 0) and (y > 0)