Let variables (#1095)

* Modify parser and AST nodes to include let statement

* Implement let variables

* Remove redundant code in fail_match_choice test

* Add comment for has_value_category()

* Apply suggestions from code review

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>

* clang-format changes

* Update comments for new BindingPattern methods

* Implement nested vars in patterns

* Apply suggestions from @geoffromer's code review

Co-authored-by: Geoff Romer <gromer@google.com>

* Implement changes from code review.

* Remove maybe_var_pattern grammar rule

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Darshal Shetty
2022-03-11 16:30:24 -05:00
committed by GitHub
co-authored by Jon Meow Geoff Romer
parent 93842ad878
commit 20b272446f
20 changed files with 340 additions and 23 deletions
+33 -6
View File
@@ -72,6 +72,7 @@
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/paren_contents.h"
#include "executable_semantics/ast/pattern.h"
#include "executable_semantics/ast/value_category.h"
#include "executable_semantics/common/arena.h"
#include "executable_semantics/common/nonnull.h"
#include "executable_semantics/syntax/bison_wrap.h"
@@ -202,6 +203,7 @@
LEFT_CURLY_BRACE
LEFT_PARENTHESIS
LEFT_SQUARE_BRACKET
LET
LIBRARY
MATCH
MINUS
@@ -573,11 +575,16 @@ non_expression_pattern:
AUTO
{ $$ = arena->New<AutoPattern>(context.source_loc()); }
| binding_lhs COLON pattern
{ $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
{
$$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
std::nullopt);
}
| paren_pattern
{ $$ = $1; }
| postfix_expression tuple_pattern
{ $$ = arena->New<AlternativePattern>(context.source_loc(), $1, $2); }
| VAR non_expression_pattern
{ $$ = arena->New<VarPattern>(context.source_loc(), $2); }
;
binding_lhs:
identifier { $$ = $1; }
@@ -642,7 +649,8 @@ clause:
{
$$ = Match::Clause(arena->New<BindingPattern>(
context.source_loc(), std::string(AnonymousName),
arena->New<AutoPattern>(context.source_loc())),
arena->New<AutoPattern>(context.source_loc()),
ValueCategory::Let),
$3);
}
;
@@ -659,7 +667,15 @@ statement:
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); }
{
$$ = arena->New<VariableDefinition>(context.source_loc(), $2, $4,
ValueCategory::Var);
}
| LET pattern EQUAL expression SEMICOLON
{
$$ = arena->New<VariableDefinition>(context.source_loc(), $2, $4,
ValueCategory::Let);
}
| statement_expression SEMICOLON
{ $$ = arena->New<ExpressionStatement>(context.source_loc(), $1); }
| if_statement
@@ -798,7 +814,10 @@ function_declaration:
}
;
variable_declaration: identifier COLON pattern
{ $$ = arena->New<BindingPattern>(context.source_loc(), $1, $3); }
{
$$ = arena->New<BindingPattern>(context.source_loc(), $1, $3,
std::nullopt);
}
;
alternative:
identifier tuple
@@ -837,10 +856,18 @@ declaration:
| VAR variable_declaration SEMICOLON
{
$$ = arena->New<VariableDeclaration>(context.source_loc(), $2,
std::nullopt);
std::nullopt, ValueCategory::Var);
}
| VAR variable_declaration EQUAL expression SEMICOLON
{ $$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4); }
{
$$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
ValueCategory::Var);
}
| LET variable_declaration EQUAL expression SEMICOLON
{
$$ = arena->New<VariableDeclaration>(context.source_loc(), $2, $4,
ValueCategory::Let);
}
| INTERFACE identifier LEFT_CURLY_BRACE declaration_list RIGHT_CURLY_BRACE
{
auto ty_ty = arena -> New<TypeTypeLiteral>(context.source_loc());