mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 13:40:12 +01:00
Factor out a Pattern sum type from Expression (#685)
`Pattern` is intended to pilot some changes I would like to apply to all our sum types: - The alternatives are expressed as derived classes rather than members of a `std::variant`. - The alternatives are classes in the [style guide sense](https://google.github.io/styleguide/cppguide.html#Structs_vs._Classes), meaning they can have invariants, but can't have public data members. - Creating an object is expressed using a constructor rather than a factory function. - Accessing an alternative is expressed as a cast (using LLVM's RTTI system) rather than `std::get` or a `Get` method. Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
co-authored by
Jon Meow
parent
b08f6bb0f1
commit
6ac3adfa53
@@ -0,0 +1,130 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/syntax/paren_contents.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
|
||||
namespace Carbon {
|
||||
namespace {
|
||||
|
||||
using llvm::cast;
|
||||
using llvm::isa;
|
||||
using testing::ElementsAre;
|
||||
using testing::IsEmpty;
|
||||
|
||||
// Matches a TuplePattern::Field named `name` whose `pattern` is an
|
||||
// `AutoPattern`.
|
||||
MATCHER_P(AutoFieldNamed, name, "") {
|
||||
return arg.name == std::string(name) && isa<AutoPattern>(arg.pattern);
|
||||
}
|
||||
|
||||
TEST(PatternTest, EmptyAsPattern) {
|
||||
ParenContents<Pattern> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(), IsEmpty());
|
||||
}
|
||||
|
||||
TEST(PatternTest, EmptyAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
EXPECT_THAT(tuple->Fields(), IsEmpty());
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryNoCommaAsPattern) {
|
||||
// Equivalent to a code fragment like
|
||||
// ```
|
||||
// (
|
||||
// auto
|
||||
// )
|
||||
// ```
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/2)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 2);
|
||||
ASSERT_TRUE(isa<AutoPattern>(pattern));
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryNoCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/2)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryWithCommaAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/2)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryWithCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/2)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, BinaryAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/2)},
|
||||
{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/3)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, BinaryAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/2)},
|
||||
{.name = std::nullopt,
|
||||
.term = new AutoPattern(/*line_num=*/3)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
EXPECT_THAT(tuple->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon
|
||||
Reference in New Issue
Block a user