Expressions
Hybrix expressions are made of variables, literal values, and operators. Operators accept operands (depicted as __ in the table below), allowing them to be combined to make compound expressions. For example, X + 1 is an expression that uses the + operator. The operands are the variable X and the literal value 1.
Literal values
| Example | Purpose |
|---|---|
123 $0000_FF23 | integer literal |
"HELLO{N}" | string literal |
@MY_LABEL | data label |
NULL | null object |
SELF | object self-reference |
TRUE FALSE | boolean literal |
Operators
The table below lists all the expression operators for the Hybrix language, in order of decreasing precedence according to the order of operations. For example, AND has a higher precedence than OR, therefore A OR B AND C OR D will be parsed as A OR (B AND C) OR D.
| Operator group | Example | Purpose |
|---|---|---|
NEW __ ( __ ) CLASS_ID( __ ) (literal value) | NEW INT[](3) CLASS_ID(MY_CLASS) -123 | create a new object get the class ID (see table above) |
__ . __ __ :: __ __ ( __ ) __ [ __ ] | MY_OBJECT.MEMBER MY_MODULE::MEMBER MY_FUNC(X, Y)MY_ARRAY[3] | access a class member access a module member call a function access an array element |
- __ | -X + -5 | negate a number |
__ * __ __ / __ __ % __ __ // __ __ %% __ | X * Y / 2 | multiply, divide, remainder |
__ + __ __ - __ | 2 + 2 | add and subtract numbers |
__ & __ | "HELLO" & "WORLD" | append text strings ⚠️ not implemented yet |
__ AS __ | TARGET AS PLAYER | type cast for class objects |
__ <= __ __ < __ __ = __ __ > __ __ >= __ __ <> __ | X <> 3 | compare two numbers |
NOT __ | NOT X > 3 | boolean NOT |
__ AND __ | X < 0 AND X > 3 | boolean AND |
__ OR __ | X < 0 OR X > 3 | boolean OR |
Division and remainder
The // and %% operators perform Euclidean integer division and remainder (modulus). For example, 17 divided by 5 equals 3 with a remainder of 2. Therefore 17 / 5 returns 3, and 17 % 5 returns remainder 2. For negative numbers, the Euclidean approach ensures that the remainder is always a positive number.
The Euclidean approach is the most mathematically correct way to divide; however, for historical reasons, most programming languages such as C and Rust instead use truncated division, where the remainder can be a negative number if the dividend is negative. For consistency with those languages, Hybrix also provides / and % operators which implement truncated integer division and remainder.
| Truncated quotient | Truncated remainder |
|---|---|
17 / 5 = 3 | 17 % 5 = 2 |
-17 / 5 = -3 | -17 % 5 = -2 |
17 / -5 = -3 | 17 % -5 = 2 |
-17 / -5 = 3 | -17 % -5 = -2 |
| Euclidean quotient | Euclidean remainder |
|---|---|
17 // 5 = 3 | 17 %% 5 = 2 |
-17 // 5 = -4 | -17 %% 5 = 3 |
17 // -5 = -3 | 17 %% -5 = 2 |
-17 // -5 = 4 | -17 %% -5 = 3 |
Function-like operators
The type conversion operators look like regular function calls, but their names are reserved words such as TO_INT.
| Example | Purpose |
|---|---|
TO_BYTE(X) | convert a numeric value X to a BYTE type |
TO_PAIR(X) | convert a numeric value X to a PAIR type |
TO_INT(X) | convert a numeric value X to a INT type |
TO_ADDRESS(P) | convert a pointer value P its memory address as an INT value |
Important points:
- Type conversions can cause overflow. For example,
TO_BYTE(257)will produce aBYTEwith value 1. - To convert between classes types, use the AS operator instead.
TO_ADDRESS()produces the memory address of the object being pointed to (not the address of the variable itself). Thus it requires a heap-allocated type such as an array, object, or function pointer.