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:
Geoff Romer
2021-07-30 12:24:12 -07:00
committed by GitHub
co-authored by Jon Meow
parent b08f6bb0f1
commit 6ac3adfa53
31 changed files with 1302 additions and 575 deletions
+12 -1
View File
@@ -9,6 +9,7 @@
#include "common/ostream.h"
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/pattern.h"
#include "executable_semantics/ast/statement.h"
#include "executable_semantics/interpreter/stack.h"
#include "executable_semantics/interpreter/value.h"
@@ -19,6 +20,7 @@ namespace Carbon {
enum class ActionKind {
LValAction,
ExpressionAction,
PatternAction,
StatementAction,
ValAction,
};
@@ -33,6 +35,11 @@ struct ExpressionAction {
const Expression* exp;
};
struct PatternAction {
static constexpr ActionKind Kind = ActionKind::PatternAction;
const Pattern* pattern;
};
struct StatementAction {
static constexpr ActionKind Kind = ActionKind::StatementAction;
const Statement* stmt;
@@ -46,6 +53,7 @@ struct ValAction {
struct Action {
static auto MakeLValAction(const Expression* e) -> Action*;
static auto MakeExpressionAction(const Expression* e) -> Action*;
static auto MakePatternAction(const Pattern* p) -> Action*;
static auto MakeStatementAction(const Statement* s) -> Action*;
static auto MakeValAction(const Value* v) -> Action*;
@@ -53,6 +61,7 @@ struct Action {
auto GetLValAction() const -> const LValAction&;
auto GetExpressionAction() const -> const ExpressionAction&;
auto GetPatternAction() const -> const PatternAction&;
auto GetStatementAction() const -> const StatementAction&;
auto GetValAction() const -> const ValAction&;
@@ -75,7 +84,9 @@ struct Action {
std::vector<const Value*> results;
private:
std::variant<LValAction, ExpressionAction, StatementAction, ValAction> value;
std::variant<LValAction, ExpressionAction, PatternAction, StatementAction,
ValAction>
value;
};
} // namespace Carbon