Keywords and Operators#
ZScript has a number of keywords, reserved for various purposes. Any keywords listed here that would otherwise be valid identifiers, are not usable as identifiers, due to being reserved.
Misc#
misc keyword |
description |
---|---|
true false |
boolean literal values |
|
used to declare scripts |
if unless |
|
while until do |
|
|
|
|
Inclusion, used in for-each loops |
|
|
|
|
break continue |
|
switch case |
|
|
Used for defaults, both in switch statements and in function prototypes |
|
Used to return values from functions.
Also used to return early, even in |
|
Used to define custom type aliases |
|
used to declare enumerations |
|
used to declare classes |
|
Class member modifier for static uses |
|
used when declaring namespaces |
|
used to bring remote symbols into scope via using statements |
|
modifier for using statements |
|
Variant of the |
|
Code repetition compiler directive |
|
Constant compile assertion directive |
|
Option reading compiler directive |
|
Include checking compiler directive |
|
Modifier used for internal engine bindings |
|
Modifier that indicates a function is optimized to be lightweight.
Only usable by |
|
Modifier used for functions that can return a compile-time constant value,
if all parameters used to call it are also compile-time constant.
Only usable by |
asm zasm try catch |
reserved, no present use |
Note
The names of primitive types are also reserved keywords.
Operators#
Operators come in both keyword and syntactical varieties, listed here alongside any alternate forms.
Most operators are left-associative, except assignment operators and the Ternary operator.
Note
Example key:
expr
means ‘any expression’type
means ‘an identifier which resolves to the name of a type’var
means ‘an identifier which resolves to the name of a variable’func
means ‘an identifier which resolves to the name of a function’boolean
means ‘an expression evaluating to true or false’
priority |
operator |
example |
description |
---|---|---|---|
1 |
|
( expr ) |
Grouping - allows specifying precedence of an expression. |
2 |
|
< type > expr |
Casting - allows changing the type of an expression |
3 |
|
var++ |
Post-increment. Returns the variable’s value, then adds 1 to the variable.
Related: Pre- vs Post- increment/decrement
|
3 |
|
var-- |
Post-decrement. Returns the variable’s value, then subtracts 1 from the
variable.
Related: Pre- vs Post- increment/decrement
|
3 |
|
func() |
Function call |
3 |
|
new class() |
Constructor Function Call |
3 |
|
delete expr |
Deprecated; used to be used to delete objects. Deletion now handled automatically. |
3 |
|
array[expr] |
Indexing- used to access indexes of an array |
3 |
|
expr->identifier |
Arrow - used to access members of objects |
4 |
|
++var |
Pre-increment. Adds 1 to the variable, then returns that value.
Related: Pre- vs Post- increment/decrement
|
4 |
|
--var |
Pre-decrement. Subtracts 1 from the variable, then returns that value.
Related: Pre- vs Post- increment/decrement
|
4 |
|
-expr |
Negation. Inverts the sign of the expression. |
4 |
! not |
!expr
not expr
|
Boolean Not. Returns ‘true’ if the expression was ‘false’ or ‘0’/’NULL’, otherwise returns ‘false’ |
4 |
~ bitnot compl |
~expr
bitnot expr
compl expr
|
Bitwise Not. Returns the bitwise inverse of the expression (flipping every ‘0’ bit to ‘1’, and vice-versa) |
5 |
|
expr ^^^ expr |
Exponentiation. Raises the first expression to the power of the second expression. |
6 |
|
expr * expr |
Multiplication. Multiplies the two expressions together. |
6 |
|
expr / expr |
Division. Divides the first expression by the second expression. |
6 |
|
expr % expr |
Modulo. Divides the first expression by the second expression, returning only the remainder of the division. |
7 |
|
expr + expr |
Addition. Adds the two expressions together. |
7 |
|
expr - expr |
Subtraction. Subtracts the second expression from the first. |
8 |
<< >> |
expr << expr
expr >> expr
|
Bitwise shift (left and right). Shifts the bits of the left expression, by the number of places in the right expression (left or right respectively). Right expr is always an int. |
9 |
|
expr < expr |
Returns true if the left expr is less than the right expr. |
9 |
|
expr > expr |
Returns true if the left expr is greater than the right expr. |
9 |
|
expr <= expr |
Returns true if the left expr is less than or equal the right expr. |
9 |
|
expr >= expr |
Returns true if the left expr is greater than or equal the right expr. |
10 |
== equals |
expr == expr
expr equals expr
|
Returns true if the two expressions are exactly equal to each other. |
10 |
!= <> not_eq not_equal |
expr != expr
expr <> expr
expr not_eq expr
expr not_equal expr
|
Returns false if the two expressions are exactly equal to each other. |
10 |
~~ appx_eq appx_equal |
expr ~~ expr
expr appx_eq expr
expr appx_equal expr
|
Returns true if the two expressions are close to equal. How close numbers need to be to be considered “close to equal” is determined by the APPROX_EQUAL_MARGIN option. |
10 |
^^ xor |
expr ^^ expr
expr xor expr
|
Converts both expressions to boolean values (via ‘!= 0’). Then returns true if EXACTLY one value is true. |
11 |
& bitand |
expr & expr
expr bitand expr
|
Performs a boolean ‘and’ operation, on each binary bit of the expressions. |
12 |
^ bitxor |
expr ^ expr
expr bitxor expr
|
Performs a boolean ‘xor’ operation, on each binary bit of the expressions. |
13 |
| bitor |
expr | expr
expr bitor expr
|
Performs a boolean ‘or’ operation, on each binary bit of the expressions. |
14 |
&& and |
expr && expr
expr and expr
|
Boolean And. Returns true if both expressions are true. |
15 |
|| or |
expr || expr
expr or expr
|
Boolean Or. Returns true if at least one expression is true. |
16 |
|
boolean ? expr : expr |
Ternary Expression. If the boolean is true, return the first expression. Otherwise return the second expression. |
17 |
|
delete expr |
Deprecated as of 3.0. See: class/object allocation |
18 |
= := |
var = expr
var := expr
|
Sets the variable to the value of the expression. |
18 |
|
var += expr |
Same as ‘var = var + expr’ |
18 |
|
var -= expr |
Same as ‘var = var - expr’ |
18 |
|
var *= expr |
Same as ‘var = var * expr’ |
18 |
|
var /= expr |
Same as ‘var = var / expr’ |
18 |
|
var %= expr |
Same as ‘var = var % expr’ |
18 |
|
var <<= expr |
Same as ‘var = var << expr’ |
18 |
|
var >>= expr |
Same as ‘var = var >> expr’ |
18 |
&= and_eq and_equal |
var &= expr
var and_eq expr
var and_equal expr
|
Same as ‘var = var & expr’ |
18 |
|= or_eq or_equal |
var |= expr
var or_eq expr
var or_equal expr
|
Same as ‘var = var | expr’ |
18 |
^= xor_eq xor_equal |
var ^= expr
var xor_eq expr
var xor_equal expr
|
Same as ‘var = var ^ expr’ |
18 |
|
var ~= expr |
Same as ‘var = var & (~expr)’ |
18 |
|
var &&= expr |
Same as ‘var = var && expr’ |
18 |
|
var ||= expr |
Same as ‘var = var || expr’ |
Caution
Some operators behavior may not be intuitive. Notable behaviors:
- Exponentiation
^^^
If either parameter is type
long
, a long exponentiation will be performed. Ex.2L ^^^ 2L == 4L
.Per our definition,
0^^^0 == 1
. With long type,0L ^^^ 0L == 1L
.If exponentiation overflows, the minimum representable value (
-214748.3648
or-2147483648L
) will be returned.
- Exponentiation
Bitwise operators
~ << >> & ^ |
where either parameter is typelong
will act on the full 32-bits. Otherwise, they will first truncate the value, and access the remaining ~~18 bits only.% 0
and/ 0
will produce a script error.% 0
, after producing the error, will return the value of% 1
./ 0
, after producing the error, will return either214748.3647
or-214748.3647
, matching the sign of the dividend.
As the
int
type is not a real integer, ‘Integer Division’ does not exist- i.e.5 / 2 == 2.5
, instead of5 / 2 == 2
.Short-circuit for boolean operations is the default, though can be disabled with a compiler option.
Type Casting#
The casting operator can be used to forcibly change the type of a value. This can be used to pass it to a function it otherwise would not be compatible with, to access a member function of it as a pointer to a particular class, or cause a function to treat it differently via function overloads.
int x = 5;
Trace(x); // prints '5.0000'
Trace(<long>x); // prints '50000', due to long type overload
Syntax#
While not technically ‘keywords’, this table shows reserved syntactical symbols/notations.
syntax |
description |
---|---|
|
delimiter |
|
delimiter, decimal point |
|
line-ender |
|
scope resolution |
|
delimiter, misc |
|
Parentheses |
|
Brackets |
|
Braces |
|
Start of many compiler directive |
|
Arrow, for member access |
=..= =.. ..= .. ... |
|
|
|
/* */ |
|
|
Handle for annotations |
!! %% !% $$ ** @@ $ |
Reserved, no current use |