Operators

Arithmetic, comparison, boolean, pipe, and string interpolation operators in Chasm.

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.5

Negation uses a leading -:

n = -x

Comparison

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 equal

Boolean

a and b    # true only if both are true
a or b     # true if either is true
not a      # inverts a boolean

Short-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")
end

Pipe 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:

PrecedenceOperators
Unary-, not
Multiplicative*, /
Additive+, -
Comparison==, !=, <, >, <=, >=
Booleanand, or
Pipe|>

Use parentheses to override precedence:

result = (a + b) * c
valid  = (x > 0) and (y > 0)