Skip to content

Commit 4434cdf

Browse files
committed
Spec: Syntax of control structures.
1 parent c97a3bc commit 4434cdf

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

docs/_spec/06-expressions.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ chapter: 6
1010
Expr ::= (Bindings | id | ‘_’) ‘=>’ Expr
1111
| Expr1
1212
Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr]
13+
| ‘if‘ Expr ‘then‘ Expr [[semi] ‘else‘ Expr]
1314
| ‘while’ ‘(’ Expr ‘)’ {nl} Expr
14-
| ‘try’ Expr [‘catch’ Expr] [‘finally’ Expr]
15-
| ‘for’ (‘(’ Enumerators ‘)’ | ‘{’ Enumerators ‘}’) {nl} [‘yield’] Expr
15+
| ‘while’ Expr ‘do’ Expr
16+
| ‘try’ Expr [Catches] [‘finally’ Expr]
17+
| ‘for’ (‘(’ Enumerators ‘)’ | ‘{’ Enumerators ‘}’) {nl} [‘do‘ | ‘yield’] Expr
18+
| ‘for’ Enumerators (‘do‘ | ‘yield’) Expr
1619
| ‘throw’ Expr
1720
| ‘return’ [Expr]
1821
| [SimpleExpr ‘.’] id ‘=’ Expr
@@ -46,6 +49,7 @@ ResultExpr ::= Expr1
4649
Ascription ::= ‘:’ InfixType
4750
| ‘:’ Annotation {Annotation}
4851
| ‘:’ ‘_’ ‘*’
52+
Catches ::= ‘catch‘ (Expr | ExprCaseClause)
4953
```
5054

5155
Expressions are composed of operators and operands.
@@ -680,6 +684,7 @@ def matmul(xss: Array[Array[Double]], yss: Array[Array[Double]]) = {
680684

681685
```ebnf
682686
Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr]
687+
| ‘if‘ Expr ‘then‘ Expr [[semi] ‘else‘ Expr]
683688
```
684689

685690
The _conditional expression_ `if (´e_1´) ´e_2´ else ´e_3´` chooses one of the values of ´e_2´ and ´e_3´, depending on the value of ´e_1´.
@@ -698,6 +703,7 @@ The conditional expression `if (´e_1´) ´e_2´` is evaluated as if it was `if
698703

699704
```ebnf
700705
Expr1 ::= ‘while’ ‘(’ Expr ‘)’ {nl} Expr
706+
| ‘while’ Expr ‘do’ Expr
701707
```
702708

703709
The _while loop expression_ `while (´e_1´) ´e_2´` is typed and evaluated as if it was an application of `whileLoop (´e_1´) (´e_2´)` where the hypothetical method `whileLoop` is defined as follows.
@@ -868,15 +874,19 @@ The type of a throw expression is `scala.Nothing`.
868874
## Try Expressions
869875

870876
```ebnf
871-
Expr1 ::= ‘try’ Expr [‘catch’ Expr] [‘finally’ Expr]
877+
Expr1 ::= ‘try’ Expr [Catches] [‘finally’ Expr]
878+
879+
Catches ::= ‘catch‘ (Expr | ExprCaseClause)
872880
```
873881

874-
A _try expression_ is of the form `try { ´b´ } catch ´h´` where the handler ´h´ is usually a [pattern matching anonymous function](08-pattern-matching.html#pattern-matching-anonymous-functions)
882+
A _try expression_ is of the form `try ´b´ catch ´h´` where the handler ´h´ is usually a [pattern matching anonymous function](08-pattern-matching.html#pattern-matching-anonymous-functions)
875883

876884
```scala
877885
{ case ´p_1´ => ´b_1´ ... case ´p_n´ => ´b_n´ }
878886
```
879887

888+
If the handler is a single `ExprCaseClause`, it is a shorthand for that `ExprCaseClause` wrapped in a pattern matching anonymous function.
889+
880890
This expression is evaluated by evaluating the block ´b´.
881891
If evaluation of ´b´ does not cause an exception to be thrown, the result of ´b´ is returned.
882892
Otherwise the handler ´h´ is applied to the thrown exception.
@@ -885,22 +895,22 @@ If the handler contains no case matching the thrown exception, the exception is
885895
More generally, if the handler is a `PartialFunction`, it is applied only if it is defined at the given exception.
886896

887897
Let ´\mathit{pt}´ be the expected type of the try expression.
888-
The block ´b´ is expected to conform to ´\mathit{pt}´.
898+
The expression ´b´ is expected to conform to ´\mathit{pt}´.
889899
The handler ´h´ is expected conform to type `scala.Function[scala.Throwable, ´\mathit{pt}\,´]`.
890900
The type of the try expression is the [weak least upper bound](03-types.html#weak-conformance) of the type of ´b´ and the result type of ´h´.
891901

892-
A try expression `try { ´b´ } finally ´e´` evaluates the block ´b´.
902+
A try expression `try ´b´ finally ´e´` evaluates the expression ´b´.
893903
If evaluation of ´b´ does not cause an exception to be thrown, the expression ´e´ is evaluated.
894904
If an exception is thrown during evaluation of ´e´, the evaluation of the try expression is aborted with the thrown exception.
895905
If no exception is thrown during evaluation of ´e´, the result of ´b´ is returned as the result of the try expression.
896906

897907
If an exception is thrown during evaluation of ´b´, the finally block ´e´ is also evaluated.
898908
If another exception ´e´ is thrown during evaluation of ´e´, evaluation of the try expression is aborted with the thrown exception.
899909
If no exception is thrown during evaluation of ´e´, the original exception thrown in ´b´ is re-thrown once evaluation of ´e´ has completed.
900-
The block ´b´ is expected to conform to the expected type of the try expression.
910+
The expression ´b´ is expected to conform to the expected type of the try expression.
901911
The finally expression ´e´ is expected to conform to type `Unit`.
902912

903-
A try expression `try { ´b´ } catch ´e_1´ finally ´e_2´` is a shorthand for `try { try { ´b´ } catch ´e_1´ } finally ´e_2´`.
913+
A try expression `try ´b´ catch ´e_1´ finally ´e_2´` is a shorthand for `try { try ´b´ catch ´e_1´ } finally ´e_2´`.
904914

905915
## Anonymous Functions
906916

docs/_spec/08-pattern-matching.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ Therefore, the right hand side of the case clause, `y.n`, of type `Int`, is foun
489489
Expr ::= PostfixExpr ‘match’ ‘{’ CaseClauses ‘}’
490490
CaseClauses ::= CaseClause {CaseClause}
491491
CaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Block
492+
ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
492493
```
493494

494495
A _pattern matching expression_

0 commit comments

Comments
 (0)