Operators: General
Use the precedence operator '()'
to guarantee the order of evaluation for an expression. An expression encapsulated by the precedence operator (enclosed in parentheses) overrides existing precedence relationships between operators and is evaluated prior to other expressions in inward-to-outward order.
Grammar
precedence: '(' expression ')';
Examples
Precedence with numeric operators.
int x = (5+4)*6; 1 int y = 12/(x-50); 2
- declare
int x
; addint 5
andint 4
→int 9
; multiplyint 9
andint 6
→int 54
; storeint 54
tox
; (note the add is evaluated before the multiply due to the precedence operator) - declare
int y
; load fromx
→int 54
; subtractint 50
fromint 54
→int 4
; divideint 12
byint 4
→int 3
; storeint 3
toy
; (note the subtract is evaluated before the divide due to the precedence operator)
- declare
Use the function call operator ()
to call an existing function. A function call is defined within a script.
Grammar
function_call: ID '(' ( expression (',' expression)* )? ')'';
Examples
A function call.
int add(int x, int y) { 1 return x + y; } int z = add(1, 2); 2
- define function
add
that returnsint
and has parameters (int x
,int y
) - declare
int z
; calladd
with arguments (int 1
,int 2
) →int 3
; storeint 3
toz
- define function
An explicit cast converts the value of an original type to the equivalent value of a target type forcefully as an operation. Use the cast operator '()'
to specify an explicit cast. Refer to casting for more information.
A conditional consists of three expressions. The first expression is evaluated with an expected boolean result type. If the first expression evaluates to true then the second expression will be evaluated. If the first expression evaluates to false then the third expression will be evaluated. The second and third expressions will be promoted if the evaluated values are not the same type. Use the conditional operator '? :'
as a shortcut to avoid the need for a full if/else branch in certain expressions.
Errors
- If the first expression does not evaluate to a boolean type value.
- If the values for the second and third expressions cannot be promoted.
Grammar
conditional: expression '?' expression ':' expression;
Promotion
byte | short | char | int | long | float | double | Reference | def | |
byte | int | int | int | int | long | float | double | - | def |
short | int | int | int | int | long | float | double | - | def |
char | int | int | int | int | long | float | double | - | def |
int | int | int | int | int | long | float | double | - | def |
long | long | long | long | long | long | float | double | - | def |
float | float | float | float | float | float | float | double | - | def |
double | double | double | double | double | double | double | double | - | def |
Reference | - | - | - | - | - | - | - | Object @ | def |
def | def | def | def | def | def | def | def | def | def |
@ If the two reference type values are the same then this promotion will not occur.
Examples
Evaluation of conditionals.
boolean b = true; 1 int x = b ? 1 : 2; 2 List y = x > 1 ? new ArrayList() : null; 3 def z = x < 2 ? x : 2.0; 4
- declare
boolean b
; storeboolean true
tob
- declare
int x
; load fromb
→boolean true
evaluate 1st expression:int 1
→int 1
; storeint 1
tox
- declare
List y
; load fromx
→int 1
;int 1
greater thanint 1
→boolean false
; evaluate 2nd expression:null
→null
; storenull
toy
; - declare
def z
; load fromx
→int 1
;int 1
less thanint 2
→boolean true
; evaluate 1st expression: load fromx
→int 1
; promoteint 1
anddouble 2.0
: resultdouble
; implicit castint 1
todouble 1.0
→double 1.0
; implicit castdouble 1.0
todef
→def
; storedef
toz
;
- declare
Use the assignment operator '='
to store a value in a variable or reference type member field for use in subsequent operations. Any operation that produces a value can be assigned to any variable/field as long as the types are the same or the resultant type can be implicitly cast to the variable/field type.
See variable assignment for examples using variables.
Errors
- If the type of value is unable to match the type of variable or field.
Grammar
assignment: field '=' expression
Examples
The examples use the following reference type definition:
name:
Example
non-static member fields:
* int x
* def y
* List z
Field assignments of different type values.
Example example = new Example(); 1 example.x = 1; 2 example.y = 2.0; 3 example.z = new ArrayList(); 4
- declare
Example example
; allocateExample
instance →Example reference
; storeExample reference
toexample
- load from
example
→Example reference
; storeint 1
tox
ofExample reference
- load from
example
→Example reference
; implicit castdouble 2.0
todef
→def
; storedef
toy
ofExample reference
- load from
example
→Example reference
; allocateArrayList
instance →ArrayList reference
; implicit castArrayList reference
toList reference
→List reference
; storeList reference
toz
ofExample reference
- declare
A field assignment from a field access.
Example example = new Example(); 1 example.x = 1; 2 example.y = example.x; 3
- declare
Example example
; allocateExample
instance →Example reference
; storeExample reference
toexample
- load from
example
→Example reference
; storeint 1
tox
ofExample reference
- load from
example
→Example reference @0
; load fromexample
→Example reference @1
; load fromx
ofExample reference @1
→int 1
; implicit castint 1
todef
→def
; storedef
toy
ofExample reference @0
; (noteExample reference @0
andExample reference @1
are the same)
- declare
Use the compound assignment operator '$='
as a shortcut for an assignment where a binary operation would occur between the variable/field as the left-hand side expression and a separate right-hand side expression.
A compound assignment is equivalent to the expression below where V is the variable/field and T is the type of variable/member.
V = (T)(V op expression);
Operators
The table below shows the available operators for use in a compound assignment. Each operator follows the casting/promotion rules according to their regular definition. For numeric operations there is an extra implicit cast when necessary to return the promoted numeric type value to the original numeric type value of the variable/field and can result in data loss.
Operator | Compound Symbol |
Multiplication | *= |
Division | /= |
Remainder | %= |
Addition | += |
Subtraction | -= |
Left Shift | <<= |
Right Shift | >>= |
Unsigned Right Shift | >>>= |
Bitwise And | &= |
Boolean And | &= |
Bitwise Xor | ^= |
Boolean Xor | ^= |
Bitwise Or | |= |
Boolean Or | |= |
String Concatenation | += |
Errors
- If the type of value is unable to match the type of variable or field.
Grammar
compound_assignment: ( ID | field ) '$=' expression;
Note the use of the $=
represents the use of any of the possible binary operators.
Examples
Compound assignment for each numeric operator.
int i = 10; 1 i *= 2; 2 i /= 5; 3 i %= 3; 4 i += 5; 5 i -= 5; 6 i <<= 2; 7 i >>= 1; 8 i >>>= 1; 9 i &= 15; 10 i ^= 12; 11 i |= 2; 12
- declare
int i
; storeint 10
toi
- load from
i
→int 10
; multiplyint 10
andint 2
→int 20
; storeint 20
toi
; (note this is equivalent toi = i*2
) - load from
i
→int 20
; divideint 20
byint 5
→int 4
; storeint 4
toi
; (note this is equivalent toi = i/5
) - load from
i
→int 4
; remainderint 4
byint 3
→int 1
; storeint 1
toi
; (note this is equivalent toi = i%3
) - load from
i
→int 1
; addint 1
andint 5
→int 6
; storeint 6
toi
; (note this is equivalent toi = i+5
) - load from
i
→int 6
; subtractint 5
fromint 6
→int 1
; storeint 1
toi
; (note this is equivalent toi = i-5
) - load from
i
→int 1
; left shiftint 1
byint 2
→int 4
; storeint 4
toi
; (note this is equivalent toi = i<<2
) - load from
i
→int 4
; right shiftint 4
byint 1
→int 2
; storeint 2
toi
; (note this is equivalent toi = i>>1
) - load from
i
→int 2
; unsigned right shiftint 2
byint 1
→int 1
; storeint 1
toi
; (note this is equivalent toi = i>>>1
) - load from
i
→int 1
; bitwise andint 1
andint 15
→int 1
; storeint 1
toi
; (note this is equivalent toi = i&2
) - load from
i
→int 1
; bitwise xorint 1
andint 12
→int 13
; storeint 13
toi
; (note this is equivalent toi = i^2
) - load from
i
→int 13
; bitwise orint 13
andint 2
→int 15
; storeint 15
toi
; (note this is equivalent toi = i|2
)
- declare
Compound assignment for each boolean operator.
boolean b = true; 1 b &= false; 2 b ^= false; 3 b |= true; 4
- declare
boolean b
; storeboolean true
inb
; - load from
b
→boolean true
; boolean andboolean true
andboolean false
→boolean false
; storeboolean false
tob
; (note this is equivalent tob = b && false
) - load from
b
→boolean false
; boolean xorboolean false
andboolean false
→boolean false
; storeboolean false
tob
; (note this is equivalent tob = b ^ false
) - load from
b
→boolean true
; boolean orboolean false
andboolean true
→boolean true
; storeboolean true
tob
; (note this is equivalent tob = b || true
)
- declare
A compound assignment with the string concatenation operator.
String s = 'compound'; 1 s += ' assignment'; 2
- declare
String s
; storeString 'compound'
tos
; - load from
s
→String 'compound'
; string concatString 'compound'
andString ' assignment''
→String 'compound assignment'
; storeString 'compound assignment'
tos
; (note this is equivalent tos = s + ' assignment'
)
- declare
A compound assignment with the
def
type.def x = 1; 1 x += 2; 2
- declare
def x
; implicit castint 1
todef
; storedef
tox
; - load from
x
→def
; implicit castdef
toint 1
→int 1
; addint 1
andint 2
→int 3
; implicit castint 3
todef
→def
; storedef
tox
; (note this is equivalent tox = x+2
)
- declare
A compound assignment with an extra implicit cast.
byte b = 1; 1 b += 2; 2
- declare
byte b
; storebyte 1
tox
; - load from
x
→byte 1
; implicit castbyte 1 to
int 1→
int 1; add
int 1and
int 2→
int 3; implicit cast
int 3to
byte 3→
byte 3; store
byte 3to
b; (note this is equivalent to
b = b+2`)
- declare