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
+7 -4
View File
@@ -11,6 +11,7 @@
#include "common/ostream.h"
#include "executable_semantics/ast/function_definition.h"
#include "executable_semantics/ast/member.h"
#include "executable_semantics/ast/pattern.h"
#include "executable_semantics/ast/struct_definition.h"
#include "executable_semantics/interpreter/address.h"
#include "executable_semantics/interpreter/dictionary.h"
@@ -58,8 +59,10 @@ struct ChoiceDeclaration {
struct VariableDeclaration {
static constexpr DeclarationKind Kind = DeclarationKind::VariableDeclaration;
int source_location;
std::string name;
const Expression* type;
// TODO: split this into a non-optional name and a type, initialized by
// a constructor that takes a BindingPattern and handles errors like a
// missing name.
const BindingPattern* binding;
const Expression* initializer;
};
@@ -74,8 +77,8 @@ class Declaration {
int line_num, std::string name,
std::list<std::pair<std::string, const Expression*>> alternatives)
-> const Declaration;
static auto MakeVariableDeclaration(int source_location, std::string name,
const Expression* type,
static auto MakeVariableDeclaration(int source_location,
const BindingPattern* binding,
const Expression* initializer)
-> const Declaration;