Expressions
Expressions in Pix define conditions over the coordinate variables
x and y. They are evaluated for every pixel
position on the grid to determine which pixels should be drawn.
Coordinate variables
x is the horizontal position (increases rightward from 0) and
y is the vertical position (increases downward from 0). These
are the only variables available in expressions.
Operator precedence
From lowest to highest:
| Precedence | Operator | Associativity |
|---|---|---|
| 1 (lowest) | or | Left |
| 2 | and | Left |
| 3 | not | Unary prefix |
| 4 | = < > <= >= | Binary |
| 5 | + - | Left |
| 6 | * / | Left |
| 7 (highest) | ^ | Right |
Arithmetic operators
These operate on numbers and produce numbers. All arithmetic uses signed 64-bit integers.
x + 5 // addition
x - 8 // subtraction (can produce negative values)
2 * x // multiplication
x / 2 // integer division (truncates toward zero)
x ^ 2 // exponentiation (right-associative) Division by zero and negative exponents produce errors.
Comparison operators
Comparisons take two numbers and return a boolean.
x = 3 // equal
x < 5 // less than
x > 0 // greater than
x <= 5 // less than or equal
x >= 0 // greater than or equal Logical operators
Logical operators combine boolean values.
x = 3 and y = 5 // both must be true
x = 0 or y = 0 // either can be true
not (x = 0) // negation Parentheses
Use parentheses to group sub-expressions and override precedence.
(x - 8) ^ 2 + (y - 8) ^ 2 < 16 // circle equation
(x > 5 and y < 10) or (x = 0) // grouped logic Type rules
Operators require specific types. There is no implicit type coercion.
| Operator | Operand types | Result type |
|---|---|---|
+ - * / ^ | Number, Number | Number |
= < > <= >= | Number, Number | Boolean |
and or | Boolean, Boolean | Boolean |
not | Boolean | Boolean |
The condition in a draw or erase statement must
evaluate to a boolean. Using a number expression as a condition produces an
error.