This repository was archived by the owner on Mar 21, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 11 files changed +183
-0
lines changed
language/syntactic-structure Expand file tree Collapse file tree 11 files changed +183
-0
lines changed Original file line number Diff line number Diff line change 4
4
break-expression ::= 'break' break-expression-result?
5
5
break-expression-result ::= 'as' expression
6
6
```
7
+
8
+ Stops the evaluation of the enclosing [ ` while ` expression] ( while.md ) or
9
+ [ ` for ` expression] ( for.md ) . In other words, a ` break ` expression is semantically
10
+ equivalent to transferring control to just after the final statement of the
11
+ * loop body* , and then performing no further iterations. If a
12
+ ` break-expression-result ` is present, the [ ` expression ` ] ( ../../expressions.md )
13
+ (the * result* ) becomes the result of the enclosing ` while ` expression or ` for `
14
+ expression.
15
+
16
+ Any [ ` defer ` statements] ( ../../statements/defer.md ) that would go out of scope
17
+ due to the control transfer are executed before stopping the loop.
18
+
19
+ It is a semantic error for a ` break ` expression to appear outside of the * loop
20
+ body* of a ` while ` expression or ` for ` expression. If a ` break ` expression
21
+ appears in the * body* of a ` defer ` statement, it cannot bind to a ` while `
22
+ expression or ` for ` expression outside of the * body* .
23
+
24
+ A ` break ` expression has no result value.
Original file line number Diff line number Diff line change 4
4
condition-expression ::= 'cond' '{' condition-expression-arm (',' condition-expression-arm)* ','? '}'
5
5
condition-expression-arm ::= expression '->' expression
6
6
```
7
+
8
+ For each ` condition-expression-arm ` , in lexical order, evaluates the first
9
+ [ ` expression ` ] ( ../../expressions.md ) (the * condition* ). The first * condition*
10
+ that tests as [ truthy] ( if.md#truthiness ) causes the associated second
11
+ ` expression ` (the * body* ) to be evaluated and the evaluation of arms to stop. If
12
+ no * body* is evaluated during this process, a panic occurs.
13
+
14
+ <!-- TODO: Link to panic definition. -->
15
+
16
+ The whole expression results in the value of the evaluated * body* .
Original file line number Diff line number Diff line change 4
4
if-expression ::= 'if' expression block-expression if-expression-else?
5
5
if-expression-else ::= 'else' block-expression
6
6
```
7
+
8
+ Evaluates the [ ` expression ` ] ( ../../expressions.md ) (the * condition* ). If the
9
+ result tests as [ truthy] ( #truthiness ) , the first
10
+ [ ` block-expression ` ] ( ../block.md ) (the * then body* ) is evaluated. Otherwise, if
11
+ an ` if-expression-else ` clause is present, the second ` block-expression ` (the
12
+ * else body* ) is evaluated.
13
+
14
+ The whole expression results in one of the following values, in order:
15
+
16
+ 1 . The result of the * then body* .
17
+ 2 . The result of the * else body* , if an ` if-expression-else ` is present.
18
+ 3 . The [ nil value] ( ../../../lexical-structure/literals.md#nil-literal ) .
19
+
20
+ ## Truthiness
21
+
22
+ <!-- TODO: Move this section elsewhere? -->
23
+
24
+ Almost all values are considered truthy, with the following exceptions:
25
+
26
+ * The [ nil value] ( ../../../lexical-structure/literals.md#nil-literal ) .
27
+ * The
28
+ [ ` false ` Boolean value] ( ../../../lexical-structure/literals.md#boolean-literal ) .
Original file line number Diff line number Diff line change 3
3
``` ebnf
4
4
next-expression ::= 'next'
5
5
```
6
+
7
+ Skips the remaining portion of the * loop body* of the enclosing
8
+ [ ` while ` expression] ( while.md ) or [ ` for ` expression] ( for.md ) and starts a new
9
+ iteration. In other words, a ` next ` expression is semantically equivalent to
10
+ transferring control to just after the final statement of the * loop body* .
11
+
12
+ Any [ ` defer ` statements] ( ../../statements/defer.md ) that would go out of scope
13
+ due to the control transfer are executed before the next loop iteration is
14
+ started.
15
+
16
+ It is a semantic error for a ` next ` expression to appear outside of the * loop
17
+ body* of a ` while ` expression or ` for ` expression. If a ` next ` expression
18
+ appears in the * body* of a ` defer ` statement, it cannot bind to a ` while `
19
+ expression or ` for ` expression outside of the * body* .
20
+
21
+ A ` next ` expression has no result value.
Original file line number Diff line number Diff line change 3
3
``` ebnf
4
4
return-expression ::= 'tail'? 'ret' expression
5
5
```
6
+
7
+ Evaluates [ ` expression ` ] ( ../../expressions.md ) (the * result* ), explicitly
8
+ returns * result* from the current
9
+ [ ` fn ` declaration] ( ../../declarations/function.md ) or
10
+ [ lambda expression] ( ../values/lambda.md ) , and transfers control to the caller.
11
+
12
+ Any [ ` defer ` statements] ( ../../statements/defer.md ) that would go out of scope
13
+ due to the control transfer are executed after evaluating * result* , but before
14
+ returning it to the caller.
15
+
16
+ It is a semantic error for a ` ret ` expression to appear outside of an ` fn `
17
+ declaration or lambda expression. A ` ret ` expression cannot appear in the * body*
18
+ of a ` defer ` statement, unless it is nested in a lambda expression.
19
+
20
+ A ` ret ` expression has no result value.
21
+
22
+ <!-- TODO: Document tail calls. -->
Original file line number Diff line number Diff line change 4
4
while-expression ::= 'while' expression block-expression while-expression-else?
5
5
while-expression-else ::= 'else' block-expression
6
6
```
7
+
8
+ Evaluates the first [ ` block-expression ` ] ( ../block.md ) (the * loop body* )
9
+ repeatedly until the [ ` expression ` ] ( ../../expressions.md ) (the * condition* ) no
10
+ longer tests as [ truthy] ( if.md#truthiness ) . The * condition* is evaluated at the
11
+ beginning of every iteration. If no iterations run and a
12
+ ` while-expression-else ` is present, the second ` block-expression ` (the * else
13
+ body* ) is evaluated once.
14
+
15
+ The whole expression results in one of the following values, in order:
16
+
17
+ 1 . The result of any [ ` break as ` expression] ( break.md ) within the * loop body* .
18
+ 2 . The result of the * loop body* , if at least one iteration completes.
19
+ 3 . The result of the * else body* , if a ` while-expression-else ` is present.
20
+ 4 . The [ nil value] ( ../../../lexical-structure/literals.md#nil-literal ) .
Original file line number Diff line number Diff line change 3
3
``` ebnf
4
4
assert-statement ::= 'assert' expression
5
5
```
6
+
7
+ Evaluates [ ` expression ` ] ( ../expressions.md ) (the * condition* ). If the result
8
+ does not test as [ truthy] ( ../expressions/control/if.md#truthiness ) , a panic
9
+ occurs. An ` assert ` statement is always executed; it is not conditional on build
10
+ flags or similar.
11
+
12
+ <!-- TODO: Link to panic definition. -->
13
+
14
+ An ` assert ` statement is typically used in a
15
+ [ ` test ` declaration] ( ../declarations/test.md ) to verify the outcome of a unit
16
+ test, but it can also be used in regular code. The runtime system is allowed to
17
+ optimize with the assumption that * condition* holds after the ` assert `
18
+ statement.
Original file line number Diff line number Diff line change 3
3
``` ebnf
4
4
defer-statement ::= 'defer' expression
5
5
```
6
+
7
+ Defers evaluation of [ ` expression ` ] ( ../expressions.md ) (the * body* ) until
8
+ control leaves the enclosing [ block expression] ( ../expressions/block.md ) .
9
+ Multiple ` defer ` statements are evaluated in reverse lexical order.
10
+
11
+ ` defer ` statements are typically used to reliably clean up resources regardless
12
+ of how control leaves a block expression.
Original file line number Diff line number Diff line change 3
3
``` ebnf
4
4
expression-statement ::= expression
5
5
```
6
+
7
+ Evaluates [ ` expression ` ] ( ../expressions.md ) (the * result* ). If the expression
8
+ statement is the final statement of the enclosing
9
+ [ block expression] ( ../expressions/block.md ) , * result* becomes the result of the
10
+ block expression.
Original file line number Diff line number Diff line change 3
3
``` ebnf
4
4
let-statement ::= 'let' pattern '=' expression
5
5
```
6
+
7
+ Evaluates [ ` expression ` ] ( ../expressions.md ) (the * initializer* ) and matches it
8
+ against [ ` pattern ` ] ( ../patterns.md ) . If the match fails, a panic occurs.
9
+
10
+ <!-- TODO: Link to panic definition. -->
11
+
12
+ Any [ bindings] ( ../patterns.md#pattern-bindings ) in ` pattern ` are available for
13
+ the remainder of the enclosing [ block expression] ( ../expressions/block.md ) . Any
14
+ in ` pattern ` that existed prior to the ` let ` statement are shadowed for the
15
+ remainder of the enclosing block expression.
You can’t perform that action at this time.
0 commit comments