From 532948d9a56bcb823d86fc502507ec91336b18ac Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Mon, 27 Sep 2021 12:10:16 -0700 Subject: [PATCH] Miscellaneous tuple simplifications (#857) - Drop unnecessary special-casing of 0-tuples. - Drop unused constructor. --- executable_semantics/ast/pattern.cpp | 10 ------ executable_semantics/ast/pattern.h | 6 ---- .../interpreter/interpreter.cpp | 33 +++---------------- 3 files changed, 5 insertions(+), 44 deletions(-) diff --git a/executable_semantics/ast/pattern.cpp b/executable_semantics/ast/pattern.cpp index 455915ffc803..c30efe341fce 100644 --- a/executable_semantics/ast/pattern.cpp +++ b/executable_semantics/ast/pattern.cpp @@ -54,16 +54,6 @@ void Pattern::Print(llvm::raw_ostream& out) const { } } -TuplePattern::TuplePattern(Nonnull arena, - Nonnull tuple_literal) - : Pattern(Kind::TuplePattern, tuple_literal->SourceLoc()) { - const auto& tuple = cast(*tuple_literal); - for (const FieldInitializer& init : tuple.Fields()) { - fields.push_back( - Field(init.name, arena->New(init.expression))); - } -} - auto PatternFromParenContents(Nonnull arena, SourceLocation loc, const ParenContents& paren_contents) -> Nonnull { diff --git a/executable_semantics/ast/pattern.h b/executable_semantics/ast/pattern.h index 1bb702890d96..7e2a5a1eadef 100644 --- a/executable_semantics/ast/pattern.h +++ b/executable_semantics/ast/pattern.h @@ -109,12 +109,6 @@ class TuplePattern : public Pattern { TuplePattern(SourceLocation loc, std::vector fields) : Pattern(Kind::TuplePattern, loc), fields(std::move(fields)) {} - // Converts tuple_literal to a TuplePattern, by wrapping each field in an - // ExpressionPattern. - // - // REQUIRES: tuple_literal->Tag() == Expression::Kind::TupleLiteral - TuplePattern(Nonnull arena, Nonnull tuple_literal); - static auto classof(const Pattern* pattern) -> bool { return pattern->Tag() == Kind::TuplePattern; } diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index e12b83394497..188eae4d6b8d 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -413,14 +413,8 @@ auto Interpreter::StepLvalue() -> Transition { } } case Expression::Kind::TupleLiteral: { - if (act->Pos() == 0) { - // { {(f1=e1,...) :: C, E, F} :: S, H} - // -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H} - Nonnull e1 = - cast(*exp).Fields()[0].expression; - return Spawn{arena->New(e1)}; - } else if (act->Pos() != - static_cast(cast(*exp).Fields().size())) { + if (act->Pos() < + static_cast(cast(*exp).Fields().size())) { // { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S, // H} // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S, @@ -485,18 +479,8 @@ auto Interpreter::StepExp() -> Transition { } } case Expression::Kind::TupleLiteral: { - if (act->Pos() == 0) { - if (cast(*exp).Fields().size() > 0) { - // { {(f1=e1,...) :: C, E, F} :: S, H} - // -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H} - Nonnull e1 = - cast(*exp).Fields()[0].expression; - return Spawn{arena->New(e1)}; - } else { - return Done{CreateTuple(act, exp)}; - } - } else if (act->Pos() != - static_cast(cast(*exp).Fields().size())) { + if (act->Pos() < + static_cast(cast(*exp).Fields().size())) { // { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S, // H} // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S, @@ -672,14 +656,7 @@ auto Interpreter::StepPattern() -> Transition { } case Pattern::Kind::TuplePattern: { const auto& tuple = cast(*pattern); - if (act->Pos() == 0) { - if (tuple.Fields().empty()) { - return Done{TupleValue::Empty()}; - } else { - Nonnull p1 = tuple.Fields()[0].pattern; - return Spawn{(arena->New(p1))}; - } - } else if (act->Pos() != static_cast(tuple.Fields().size())) { + if (act->Pos() < static_cast(tuple.Fields().size())) { // { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S, // H} // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,