From 853360345e8350d3fd46dbd0b1bd4f09d3f22b76 Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Fri, 26 Feb 2021 13:12:09 -0800 Subject: [PATCH] Update executable semantics for `choice` to match final state of p0157 (#304) * Make parentheses optional for alternatives with no arguments. * Use `,` instead of `;` as separator for alternatives, and make the trailing separator optional. --- executable_semantics/syntax.ypp | 18 +++++++++++++++--- executable_semantics/testdata/choice1.6c | 8 ++++---- executable_semantics/testdata/choice1.golden | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/executable_semantics/syntax.ypp b/executable_semantics/syntax.ypp index c7c3ecdf60ba..38ca71feffb6 100644 --- a/executable_semantics/syntax.ypp +++ b/executable_semantics/syntax.ypp @@ -289,14 +289,26 @@ member_list: { $$ = $2; $$->push_front($1); } ; alternative: - identifier tuple ';' + identifier tuple { $$ = new std::pair($1, $2); } +| identifier + { + $$ = new std::pair( + $1, Carbon::MakeTuple( + yylineno, + new std::vector>())); + } ; alternative_list: // Empty { $$ = new std::list>(); } -| alternative alternative_list - { $$ = $2; $$->push_front(*$1); } +| alternative + { + $$ = new std::list>(); + $$->push_front(*$1); + } +| alternative ',' alternative_list + { $$ = $3; $$->push_front(*$1); } ; declaration: function_definition diff --git a/executable_semantics/testdata/choice1.6c b/executable_semantics/testdata/choice1.6c index b031f59e639b..29c68baeec97 100644 --- a/executable_semantics/testdata/choice1.6c +++ b/executable_semantics/testdata/choice1.6c @@ -3,9 +3,9 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception choice Ints { - None(); - One(Int); - Two(Int,Int); + None, + One(Int), + Two(Int,Int) } fn main() -> Int { @@ -13,7 +13,7 @@ fn main() -> Int { var auto: y = Ints.One(42); var auto: n = 0; match (y) { - case Ints.None() => + case Ints.None => n = n + 2; case Ints.One(auto: x) => n = x + 1 - 42; diff --git a/executable_semantics/testdata/choice1.golden b/executable_semantics/testdata/choice1.golden index 052a6d3ade66..94e198ca614b 100644 --- a/executable_semantics/testdata/choice1.golden +++ b/executable_semantics/testdata/choice1.golden @@ -9,7 +9,7 @@ var auto: x = Ints.None(); var auto: y = Ints.One(0 = 42); var auto: n = 0; match (y) { -case Ints.None() => +case Ints.None => n = (n + 2); case Ints.One(0 = auto: x) => n = ((x + 1) - 42);