Support for if ... then ... else following #911. (#944)

This commit is contained in:
Richard Smith
2022-03-03 16:34:34 -08:00
committed by GitHub
parent 4a98acc3ea
commit 2325c50b3b
9 changed files with 113 additions and 3 deletions
+2
View File
@@ -88,6 +88,7 @@ RUN "__run"
SEMICOLON ";"
SLASH "/"
STRING "String"
THEN "then"
TRUE "true"
TYPE "Type"
UNDERSCORE "_"
@@ -185,6 +186,7 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\"
{SEMICOLON} { return SIMPLE_TOKEN(SEMICOLON); }
{SLASH} { return SIMPLE_TOKEN(SLASH); }
{STRING} { return SIMPLE_TOKEN(STRING); }
{THEN} { return SIMPLE_TOKEN(THEN); }
{TRUE} { return SIMPLE_TOKEN(TRUE); }
{TYPE} { return SIMPLE_TOKEN(TYPE); }
{UNDERSCORE} { return SIMPLE_TOKEN(UNDERSCORE); }
+14 -3
View File
@@ -134,6 +134,8 @@
%type <Nonnull<Expression*>> and_expression
%type <Nonnull<Expression*>> or_lhs
%type <Nonnull<Expression*>> or_expression
%type <Nonnull<Expression*>> statement_expression
%type <Nonnull<Expression*>> if_expression
%type <Nonnull<Expression*>> expression
%type <Nonnull<GenericBinding*>> generic_binding
%type <std::vector<Nonnull<AstNode*>>> deduced_params
@@ -216,6 +218,7 @@
SEMICOLON
SLASH
STRING
THEN
TRUE
TYPE
UNDERSCORE
@@ -477,12 +480,20 @@ or_expression:
std::vector<Nonnull<Expression*>>({$1, $3}));
}
;
expression:
statement_expression:
ref_deref_expression
| predicate_expression
| and_expression
| or_expression
;
if_expression:
statement_expression
| IF expression THEN if_expression ELSE if_expression
{ $$ = arena->New<IfExpression>(context.source_loc(), $2, $4, $6); }
;
expression:
if_expression
;
designator: PERIOD identifier { $$ = $2; }
;
paren_expression: paren_expression_base
@@ -645,11 +656,11 @@ clause_list:
}
;
statement:
expression EQUAL expression SEMICOLON
statement_expression EQUAL expression SEMICOLON
{ $$ = arena->New<Assign>(context.source_loc(), $1, $3); }
| VAR pattern EQUAL expression SEMICOLON
{ $$ = arena->New<VariableDefinition>(context.source_loc(), $2, $4); }
| expression SEMICOLON
| statement_expression SEMICOLON
{ $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
| if_statement
{ $$ = $1; }