Operator expressions

Sather - A Language Tutorial
Prev | Chapter 7. Operator Redefinition | Next

# 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]

> [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.

## Grouping

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

- The >, >= and /= operators are not directly translated into their own routine. Rather, they are defined in terms of is_lt and is_eq.

- Each of these transformations is applied after the component expressions have themselves been transformed.

- '`out`' and '`inout`' modes may not be used with the syntactic sugar expressions.

- The '<=' and '>' expressions do not reverse the original left to right order of argument evaluation.

- `and`' and `or`' are not listed as syntactic sugar for operations in `BOOL`'; this allows short-circuiting the evaluation of subexpression.

- The aget and aset routines are meant to support array like indexed access and require at least one index argument.

## 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

Prev | Home | Next
Operator Redefinition | Up | Array Access Routines