Legato
Legato

GoFiler Legato Script Reference

 

Legato v 1.4j

Application v 5.22b

  

 

Chapter ThreeData Types and Operators (continued)

3.12 Operator Precedence

3.12.1 What Happens First

The order of evaluation of operators is key in determining the result of an expression and it becomes more important as expressions grow in length and complexity. The following table shows the order of operator precedence in Legato:

  Precedence   Operator   Description   Associativity  
    1 (Highest)   ++   Suffix increment   Left-to-right  
    --   Suffix decrement   Left-to-right  
    ()   Function call, precedence group.   Left-to-right  
    []   Array subscript   Left-to-right  
    2   ++   Prefix increment   Right-to-left  
    --   Prefix decrement   Right-to-left  
      +   Unary plus (positive)   Right-to-left  
      -   Unary minus (negative)   Right-to-left  
      !   Logical NOT   Right-to-left  
      ~   Bitwise NOT (One’s Complement)   Right-to-left  
    3   *   Multiplication   Left-to-Right  
      /   Division   Left-to-Right  
      %   Modulo   Left-to-Right  
    4   +   Addition   Left-to-Right  
      -   Subtraction   Left-to-Right  
    5   >>   Bitwise left shift   Left-to-Right  
      <<   Bitwise right shift   Left-to-Right  
    6   <   Less than   Left-to-Right  
      <=   Less than or equal to   Left-to-Right  
      >   Greater than   Left-to-Right  
      =>   Greater than or equal to   Left-to-Right  
    7   ==   Equal to   Left-to-Right  
      !=   Not equal to   Left-to-Right  
    8   &   Bitwise AND   Left-to-Right  
    9   ^   Bitwise XOR   Left-to-Right  
  10   |   Bitwise OR   Left-to-Right  
  11   &&   Logical AND   Left-to-Right  
  12   ||   Logical OR   Left-to-Right  
  13   ?:   Ternary conditional   Right-to-Left  
  14   =   Direct assignment   Right-to-Left  
    +=   Assignment by sum   Right-to-Left  
      -=   Assignment by difference   Right-to-Left  
      *=   Assignment by product   Right-to-Left  
      /=   Assignment by quotient   Right-to-Left  
      %=   Assignment by remainder   Right-to-Left  
      <<=   Assignment by bitwise left shift   Right-to-Left  
      >>=   Assignment by bitwise right shift   Right-to-Left  
      &=   Assignment by bitwise AND   Right-to-Left  
      ^=   Assignment by bitwise XOR   Right-to-Left  
      |=   Assignment by bitwise OR   Right-to-Left  
  15 (Lowest)   ,   Comma   Left-to-Right  

 

3.12.2 A Note About Interpreted Versus Compiled

In the Legato interpreted environment, the engine creates a list by precedence of items to perform and then performs them in order. For example:

if ((a < b) || (b < c)) { something ... }

In this case, the operations are broken down as follows:

a < b  to  result 1

b < c  to  result 2

result 1 || result 2  to   result 3

if (...)   result 3   then   { something ...}

The parentheses in the above case are superfluous but can help with reading clarity. In an optimized compiler environment, the second operation, b < c will never execute if the first condition is true, it is logically unnecessary. However, this leads to some interesting differences. For example:

if ((a < b[i++]) || (b[i++] < c)) { something ... }

In this case, the operations are broken down as follows:

a < b[i]  to  result 1

++   (i = i + 1)

b[i] < c  to  result 2

++   (i = i + 1)

result 1 || result 2  to   result 3

if (...)   result 3   then   { something ...}

Two conditions happen in an optimized compiler. First, i placed in a temporary area so every reference to i and a suffix unary condition will be based on the original value prior to the execution of the expression. Second, and more of a logical oddity, if the second operation, b < c, is not executed, the value of i will be incremented only once.

In Legato, the operations are executed in the order as above. So, i is 0 on start. It will always end as 2. Further, result 2 is based on b[1], not b[0]. In general, the second example is simply bad form and should be avoided.

Another odd example exposing the same unary differences:

while (a < 5) { list[a++] = a; }

The list in this condition will be loaded with 1,2,3,4,5, not 0,1,2,3,4. Again, this practice should be avoided.