7.2. Operator expressions

The following table shows how the standard operators are directly converted into routine calls.
Operator          Routine
expr1 + expr2     expr1.plus(expr2)
expr1 - expr2     expr1.minus(expr2)
expr1 * expr2     expr1.times(expr2)
expr1 / expr2     expr1.div(expr2)
expr1 ^ expr2     expr1.pow(expr2)
expr1 % expr2     expr1.mod(expr2)
expr1 < expr2     expr1.is_lt(expr2)
expr1 = expr2     expr1.is_eq(expr2)
- expr            expr.negate
~ expr            expr.not

In addition to the unary and binary operators, there are additional operators that are defined in terms of a combination of the unary and binary operators
Operator         Translation
expr1 <= expr2   expr2.is_lt(expr1).not
expr1 >= expr2   expr1.is_lt(expr2).not
expr1 /= expr2   expr1.is_eq(expr2).not
expr1 > expr2    expr2.is_lt(expr1)

[1] Earlier versions of Sather 1.0 defined separate routines for each of these operators.

The form '[expression list]' is translated into a call on the routine aget. For instance,
a := [3,5];   -- Equivalent to a := aget(3,5); Used in the array class
f := arr[2];  -- Equivalent to f := arr.aget(2); Used outside the array

This is described in more detail later.


In addition to the above mentioned operators, it is possible to group expressions using plain parentheses, which have the highest precedence.

7.2.1. Operator precedence

The precedence ordering shown below determines the grouping of the syntactic sugar forms. Symbols of the same precedence associate left to right and parentheses may be used for explicit grouping. Evaluation order obeys explicit parenthesis in all cases.
Strongest |  .  ::  []  ()
          |  ^
          |  ~   unary -
          |  *  /  %
          |  +   binary -
          |  <  <=  =  /=  >=  >
Weakest   |  and     or

Points to note

Syntactic sugar example

Here's a formula written with syntactic sugar and the calls it is textually equivalent to. It doesn't matter what the types of the variables are; the sugar ignores types.
-- Written using syntactic sugar
r := (x^2 + y^2).sqrt;

-- Written without sugar
r := (x.pow(2).plus(y.pow(2))).sqrt