mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-03 22:02:51 +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
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -45,7 +46,7 @@ struct Assign {
|
||||
|
||||
struct VariableDefinition {
|
||||
static constexpr StatementKind Kind = StatementKind::VariableDefinition;
|
||||
const Expression* pat;
|
||||
const Pattern* pat;
|
||||
const Expression* init;
|
||||
};
|
||||
|
||||
@@ -89,7 +90,7 @@ struct Continue {
|
||||
struct Match {
|
||||
static constexpr StatementKind Kind = StatementKind::Match;
|
||||
const Expression* exp;
|
||||
std::list<std::pair<const Expression*, const Statement*>>* clauses;
|
||||
std::list<std::pair<const Pattern*, const Statement*>>* clauses;
|
||||
};
|
||||
|
||||
struct Continuation {
|
||||
@@ -113,7 +114,7 @@ struct Statement {
|
||||
-> const Statement*;
|
||||
static auto MakeAssign(int line_num, const Expression* lhs,
|
||||
const Expression* rhs) -> const Statement*;
|
||||
static auto MakeVariableDefinition(int line_num, const Expression* pat,
|
||||
static auto MakeVariableDefinition(int line_num, const Pattern* pat,
|
||||
const Expression* init)
|
||||
-> const Statement*;
|
||||
static auto MakeIf(int line_num, const Expression* cond,
|
||||
@@ -129,7 +130,7 @@ struct Statement {
|
||||
static auto MakeContinue(int line_num) -> const Statement*;
|
||||
static auto MakeMatch(
|
||||
int line_num, const Expression* exp,
|
||||
std::list<std::pair<const Expression*, const Statement*>>* clauses)
|
||||
std::list<std::pair<const Pattern*, const Statement*>>* clauses)
|
||||
-> const Statement*;
|
||||
// Returns an AST node for a continuation statement give its line number and
|
||||
// contituent parts.
|
||||
|
||||
Reference in New Issue
Block a user