Syntactic sugar expressions

Sather - Language Specification
Prev | Chapter 7. Basic Expressions | Next

# 7.11. Syntactic sugar expressions

Example 7-13. Examples:

    a+b
    x<7

    sugar_expression ==>
            expression binary_op expression | - expression |  [ expression ]  [ expression_list ] | ( expression )

    binary_op ==>
            + | - | * | / | ^ | % | ~ | < | <= | = | /= | > | >=

As shown in the following table, several Sather constructs are simply syntactic sugar for corresponding routine calls. 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. Note that 'and' and 'or' are not listed as syntactic sugar for operations in 'BOOL'; this allows short-circuiting the evaluation of subexpressions. The '<=' and '>' expressions do not reverse the original left to right order of argument evaluation. Ambiguity between unary minus and negative literals must be resolved with explicit parenthesis.

Table 7-2.

Sugar form | Translation | Sugar form | Translation
--- | --- | --- | ---
expr1 + expr2 | expr1.plus(expr2) | expr1 /= expr2 | expr1.is_eq(expr2).not
expr1 - expr2 | expr1.minus(expr2) | expr1 > expr2 | expr2.is_lt(expr1)
expr1 * expr2 | expr1.times(expr2) | expr1 >= expr2 | expr1.is_lt(expr2).not
expr1 / expr2 | expr1.div(expr2) | - expr | expr.negate
expr1 ^ expr2 | expr1.pow(expr2) | ~ expr | expr.not
expr1 % expr2 | expr1.mod(expr2) | [expr_list] | aget(expression_list)
expr1 < expr2 | expr1.is_lt(expr2) | expr1[expression_list] | expr1.aget(expression_list)
expr1 <= expr2 | expr2.is_lt(expr1).not | (expression) | expression
expr1 = expr2 | expr1.is_eq(expr2) |  |

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.

Table 7-3.

Strongest | . :: [] ()
| ^
| ~ Unary -
| * / %
| + Binary -
| < <= = /= >= >
Weakest | and or

## 7.11.1. Syntactic sugar example

Example 7-14. 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.

    r := (x^2 + y^2).sqrt;
       -- Written using syntactic sugar

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

Prev | Home | Next
or expressions | Up | Loops and Iterators