Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
allow "else if (...) then" in expressions #3609
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Nov 21, 2014
1 parent daa5087 commit 65cdc96
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
29 changes: 12 additions & 17 deletions typechecker/Ceylon.g
Original file line number Diff line number Diff line change
Expand Up @@ -1163,10 +1163,7 @@ unambiguousType
;
statement returns [Statement statement]
: /*(IF_CLAUSE conditions THEN_CLAUSE) =>
expressionOrSpecificationStatement
{ $statement = $expressionOrSpecificationStatement.statement; }
|*/ directiveStatement
: directiveStatement
{ $statement = $directiveStatement.directive; }
| controlStatement
{ $statement = $controlStatement.controlStatement; }
Expand Down Expand Up @@ -2092,10 +2089,17 @@ thenElseClauses returns [IfClause ifClause, ElseClause elseClause, ConditionList
(
ELSE_CLAUSE
{ $elseClause = new ElseClause($ELSE_CLAUSE); }
de2=disjunctionExpression
{ Expression e = new Expression(null);
e.setTerm($de2.term);
$elseClause.setExpression(e); }
(
de2=disjunctionExpression
{ Expression e = new Expression(null);
e.setTerm($de2.term);
$elseClause.setExpression(e); }
|
ifExpression
{ Expression e = new Expression(null);
e.setTerm($ifExpression.term);
$elseClause.setExpression(e); }
)
)?
;
Expand Down Expand Up @@ -2211,15 +2215,6 @@ thenElseExpression returns [Term term]
de2=disjunctionExpression
{ $thenElseOperator.operator.setRightTerm($de2.term); }
)*
//TODO: enable this to add support for if (...) and
// given (...) expressions
//TODO: when enabling this, we'll need something
// to distinguish between "if (...) then" and
// the control structure "if (...) { ... }"
/*|
IF_CLAUSE conditions
THEN_CLAUSE disjunctionExpression
(ELSE_CLAUSE disjunctionExpression)?*/
;
thenElseOperator returns [BinaryOperatorExpression operator]
Expand Down
7 changes: 4 additions & 3 deletions typechecker/en/modules/expressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1443,12 +1443,13 @@ Digit{1,2} ":" Digit{2} ( ":" Digit{2} ( ":" Digit{3} )? )?
<literal>then</literal> expression, and an <literal>else</literal>
expression. The <literal>else</literal> expression is not optional.</para>

<synopsis>IfElseExpression: "if" ConditionList ThenExpression ElseExpression</synopsis>
<synopsis>IfElseExpression: "if" ConditionList ThenExpression (ElseExpression | ElseIfExpression)</synopsis>
<synopsis>ElseIfExpression: "else" IfExpression</synopsis>

<para>The type of an <literal>if/then/else</literal> expression with
<literal>then</literal> expression of type <literal>X</literal> and
<literal>else</literal> expression of type <literal>Y</literal> is
<literal>X|Y</literal>.</para>
<literal>else</literal> or <literal>else if</literal> expression of
type <literal>Y</literal> is <literal>X|Y</literal>.</para>

<para>A <literal>switch/case/else</literal> expression has a
<literal>switch</literal> expression or inline variable, a list of
Expand Down
5 changes: 5 additions & 0 deletions typechecker/test/main/inline/inlineExpressions.ceylon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
void inlineExpressions(Integer? arg, Boolean bool, Integer|Float num) {

@type:"String" value numStr =
if (num==0) then "zero"
else if (num==1) then "unit"
else num.string;

@type:"Integer|Float" value someStuff
= if (exists arg)
then arg else 0.0;
Expand Down

0 comments on commit 65cdc96

Please sign in to comment.