Operators: General

edit

Precedence

edit

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;   
    int y = 12/(x-50); 

    declare int x; add int 5 and int 4int 9; multiply int 9 and int 6int 54; store int 54 to x; (note the add is evaluated before the multiply due to the precedence operator)

    declare int y; load from xint 54; subtract int 50 from int 54int 4; divide int 12 by int 4int 3; store int 3 to y; (note the subtract is evaluated before the divide due to the precedence operator)

Function Call

edit

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) { 
          return x + y;
      }
    
    int z = add(1, 2); 

    define function add that returns int and has parameters (int x, int y)

    declare int z; call add with arguments (int 1, int 2) → int 3; store int 3 to z

Cast

edit

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.

Conditional

edit

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;        
    int x = b ? 1 : 2;       
    List y = x > 1 ? new ArrayList() : null; 
    def z = x < 2 ? x : 2.0; 

    declare boolean b; store boolean true to b

    declare int x; load from bboolean true evaluate 1st expression: int 1int 1; store int 1 to x

    declare List y; load from xint 1; int 1 greater than int 1boolean false; evaluate 2nd expression: nullnull; store null to y;

    declare def z; load from xint 1; int 1 less than int 2boolean true; evaluate 1st expression: load from xint 1; promote int 1 and double 2.0: result double; implicit cast int 1 to double 1.0double 1.0; implicit cast double 1.0 to defdef; store def to z;

Assignment

edit

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(); 
    example.x = 1;                   
    example.y = 2.0;                 
    example.z = new ArrayList();     

    declare Example example; allocate Example instance → Example reference; store Example reference to example

    load from exampleExample reference; store int 1 to x of Example reference

    load from exampleExample reference; implicit cast double 2.0 to defdef; store def to y of Example reference

    load from exampleExample reference; allocate ArrayList instance → ArrayList reference; implicit cast ArrayList reference to List referenceList reference; store List reference to z of Example reference

  • A field assignment from a field access.

    Example example = new Example(); 
    example.x = 1;                   
    example.y = example.x;           

    declare Example example; allocate Example instance → Example reference; store Example reference to example

    load from exampleExample reference; store int 1 to x of Example reference

    load from exampleExample reference @0; load from exampleExample reference @1; load from x of Example reference @1int 1; implicit cast int 1 to defdef; store def to y of Example reference @0; (note Example reference @0 and Example reference @1 are the same)

Compound Assignment

edit

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; 
    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; store int 10 to i

    load from iint 10; multiply int 10 and int 2int 20; store int 20 to i; (note this is equivalent to i = i*2)

    load from iint 20; divide int 20 by int 5int 4; store int 4 to i; (note this is equivalent to i = i/5)

    load from iint 4; remainder int 4 by int 3int 1; store int 1 to i; (note this is equivalent to i = i%3)

    load from iint 1; add int 1 and int 5int 6; store int 6 to i; (note this is equivalent to i = i+5)

    load from iint 6; subtract int 5 from int 6int 1; store int 1 to i; (note this is equivalent to i = i-5)

    load from iint 1; left shift int 1 by int 2int 4; store int 4 to i; (note this is equivalent to i = i<<2)

    load from iint 4; right shift int 4 by int 1int 2; store int 2 to i; (note this is equivalent to i = i>>1)

    load from iint 2; unsigned right shift int 2 by int 1int 1; store int 1 to i; (note this is equivalent to i = i>>>1)

    load from iint 1; bitwise and int 1 and int 15int 1; store int 1 to i; (note this is equivalent to i = i&2)

    load from iint 1; bitwise xor int 1 and int 12int 13; store int 13 to i; (note this is equivalent to i = i^2)

    load from iint 13; bitwise or int 13 and int 2int 15; store int 15 to i; (note this is equivalent to i = i|2)

  • Compound assignment for each boolean operator.

    boolean b = true; 
    b &= false;       
    b ^= false;       
    b |= true;        

    declare boolean b; store boolean true in b;

    load from bboolean true; boolean and boolean true and boolean falseboolean false; store boolean false to b; (note this is equivalent to b = b && false)

    load from bboolean false; boolean xor boolean false and boolean falseboolean false; store boolean false to b; (note this is equivalent to b = b ^ false)

    load from bboolean true; boolean or boolean false and boolean trueboolean true; store boolean true to b; (note this is equivalent to b = b || true)

  • A compound assignment with the string concatenation operator.

    String s = 'compound'; 
    s += ' assignment';    

    declare String s; store String 'compound' to s;

    load from sString 'compound'; string concat String 'compound' and String ' assignment''String 'compound assignment'; store String 'compound assignment' to s; (note this is equivalent to s = s + ' assignment')

  • A compound assignment with the def type.

    def x = 1; 
    x += 2;    

    declare def x; implicit cast int 1 to def; store def to x;

    load from xdef; implicit cast def to int 1int 1; add int 1 and int 2int 3; implicit cast int 3 to defdef; store def to x; (note this is equivalent to x = x+2)

  • A compound assignment with an extra implicit cast.

    byte b = 1; 
    b += 2;     

    declare byte b; store byte 1 to x;

    load from xbyte 1; implicit cast byte 1 to `int 1int 1; add int 1 and int 2int 3; implicit cast int 3 to byte 3byte 3; store byte 3 to b; (note this is equivalent to b = b+2)