-
Notifications
You must be signed in to change notification settings - Fork 89
Secondary expressions (ref)
This section describes expressions that are in fact just syntactic sugar over Core Expressions. We just present a translation of Secondary Expressions into Core Expressions.
secondary_expr =
conditional_expr
| while_loop
| do_while_loop
| when_expr
| unless_expr
| lambda_expr
| list_constructor
| swap_operator
'if' '(' expr ')' expr 'else' expr
A standard branch, which executes and returns value of first expression if the condition evaluates to true or second elsewhere.
Internally it is translated into
match (cond) {
| true => expr1
| false => expr2
}
while_loop =
'while' '(' expr ')' expr
A loop, executing body expression as long as the condition is true. Its value is always checked before the execution of body and if it evaluates to false, then the loop ends. The body must be of type void.
The loop is translated internally into the following code
def loop () {
if (cond)
{ body; loop () }
else
()
};
loop ()
do_while_loop =
'do' expr 'while' '(' expr ')'
This loop is similar to while loop, but body is executed before the first time the condition is checked.
when_expr =
'when' '(' expr ')' expr
A version of the if condition, but having only one branch -- execution of body only when the condition is satisfied. If its value if false, then nothing is done (i. e. () is returned).
Its semantics is the same as
if (cond) body else ()
'unless' '(' expr ')' expr
An opposite version of when. It executes and returns value of body only if conditions are not satisfied (i. e. evaluates to false).
Its semantics is the same as
if (cond) () else body
lambda_expr =
'fun' [ method_type_parameters ]
'(' method_parameters ')' [ ':' type ] [ where_constraints ] block
Lambda expressions can be thought as of anonymous local functions. This construct defines such a function and returns it as a functional value. This value can be used just like the name of a regular local function.
Example:
List.Iter (intList, fun (x) { printf ("%d\n", x) })
is equivalent to
def tmpfunc (x) { printf ("%d\n", x) };
List.Iter (intList, tmpfunc)
list_constructor =
'[' [ { expr ',' } expr ] ']'
[1, 2, 3]
is translated to list.Cons (1, list.Cons (2,
list.Cons (3, list.Nil ())))
.
swap_operator =
expr '<->' expr
Infix operator <->
is used to swap values of two assignable
expressions. You can think about it as being translated to
def temp = e2;
e2 = e1;
e1 = temp
except that subexpressions of e1 and e2 are evaluated only once (there is a special macro to assure this), so
a[x.Next()] <-> a[x.Next()]