Operators: General
editOperators: General
editPrecedence
editUse 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.
declare
int x; addint 5andint 4→int 9; multiplyint 9andint 6→int 54; storeint 54tox; (note the add is evaluated before the multiply due to the precedence operator)declare
int y; load fromx→int 54; subtractint 50fromint 54→int 4; divideint 12byint 4→int 3; storeint 3toy; (note the subtract is evaluated before the divide due to the precedence operator)
Function Call
editUse the function call operator () to call an existing function. A
function call is defined within a script.
Grammar
function_call: ID '(' ( expression (',' expression)* )? ')'';
Examples
Cast
editAn 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.
Conditional
editA 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; int x = b ? 1 : 2; List y = x > 1 ? new ArrayList() : null; def z = x < 2 ? x : 2.0;
declare
boolean b; storeboolean truetobdeclare
int x; load fromb→boolean trueevaluate 1st expression:int 1→int 1; storeint 1toxdeclare
List y; load fromx→int 1;int 1greater thanint 1→boolean false; evaluate 2nd expression:null→null; storenulltoy;declare
def z; load fromx→int 1;int 1less thanint 2→boolean true; evaluate 1st expression: load fromx→int 1; promoteint 1anddouble 2.0: resultdouble; implicit castint 1todouble 1.0→double 1.0; implicit castdouble 1.0todef→def; storedeftoz;
Assignment
editUse 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.
declare
Example example; allocateExampleinstance →Example reference; storeExample referencetoexampleload from
example→Example reference; storeint 1toxofExample referenceload from
example→Example reference; implicit castdouble 2.0todef→def; storedeftoyofExample referenceload from
example→Example reference; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetoList reference→List reference; storeList referencetozofExample reference -
A field assignment from a field access.
declare
Example example; allocateExampleinstance →Example reference; storeExample referencetoexampleload from
example→Example reference; storeint 1toxofExample referenceload from
example→Example reference @0; load fromexample→Example reference @1; load fromxofExample reference @1→int 1; implicit castint 1todef→def; storedeftoyofExample reference @0; (noteExample reference @0andExample reference @1are the same)
Compound Assignment
editUse 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; i *= 2; i /= 5; i %= 3; i += 5; i -= 5; i <<= 2; i >>= 1; i >>>= 1; i &= 15; i ^= 12; i |= 2;
declare
int i; storeint 10toiload from
i→int 10; multiplyint 10andint 2→int 20; storeint 20toi; (note this is equivalent toi = i*2)load from
i→int 20; divideint 20byint 5→int 4; storeint 4toi; (note this is equivalent toi = i/5)load from
i→int 4; remainderint 4byint 3→int 1; storeint 1toi; (note this is equivalent toi = i%3)load from
i→int 1; addint 1andint 5→int 6; storeint 6toi; (note this is equivalent toi = i+5)load from
i→int 6; subtractint 5fromint 6→int 1; storeint 1toi; (note this is equivalent toi = i-5)load from
i→int 1; left shiftint 1byint 2→int 4; storeint 4toi; (note this is equivalent toi = i<<2)load from
i→int 4; right shiftint 4byint 1→int 2; storeint 2toi; (note this is equivalent toi = i>>1)load from
i→int 2; unsigned right shiftint 2byint 1→int 1; storeint 1toi; (note this is equivalent toi = i>>>1)load from
i→int 1; bitwise andint 1andint 15→int 1; storeint 1toi; (note this is equivalent toi = i&2)load from
i→int 1; bitwise xorint 1andint 12→int 13; storeint 13toi; (note this is equivalent toi = i^2)load from
i→int 13; bitwise orint 13andint 2→int 15; storeint 15toi; (note this is equivalent toi = i|2) -
Compound assignment for each boolean operator.
declare
boolean b; storeboolean trueinb;load from
b→boolean true; boolean andboolean trueandboolean false→boolean false; storeboolean falsetob; (note this is equivalent tob = b && false)load from
b→boolean false; boolean xorboolean falseandboolean false→boolean false; storeboolean falsetob; (note this is equivalent tob = b ^ false)load from
b→boolean true; boolean orboolean falseandboolean true→boolean true; storeboolean truetob; (note this is equivalent tob = b || true) -
A compound assignment with the string concatenation operator.
-
A compound assignment with the
deftype. -
A compound assignment with an extra implicit cast.