mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 14:51:03 +01:00
Migrate declaration parsing to value semantics (#614)
* Use Expression by value in FunctionDefinition * Migrate choice declarations to value semantics * Migrate Declaration parsing to value semantics
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
|
||||
namespace Carbon {
|
||||
using AST = std::list<Carbon::Declaration>*;
|
||||
using AST = std::list<Carbon::Declaration>;
|
||||
}
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_ABSTRACT_SYNTAX_TREE_H_
|
||||
|
||||
@@ -20,9 +20,9 @@ void StructDeclaration::Print() const {
|
||||
|
||||
void ChoiceDeclaration::Print() const {
|
||||
std::cout << "choice " << name << " {" << std::endl;
|
||||
for (auto& alternative : alternatives) {
|
||||
std::cout << "alt " << alternative.first << " ";
|
||||
PrintExp(alternative.second);
|
||||
for (const auto& [name, signature] : alternatives) {
|
||||
std::cout << "alt " << name << " ";
|
||||
PrintExp(&signature);
|
||||
std::cout << ";" << std::endl;
|
||||
}
|
||||
std::cout << "}" << std::endl;
|
||||
|
||||
@@ -13,6 +13,10 @@
|
||||
#include "executable_semantics/ast/struct_definition.h"
|
||||
#include "executable_semantics/interpreter/dictionary.h"
|
||||
|
||||
namespace yy {
|
||||
class parser;
|
||||
}
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
struct Value;
|
||||
@@ -56,7 +60,7 @@ class Declaration {
|
||||
// Add an entry in the compile time global symbol tables for this declaration.
|
||||
auto TopLevel(TypeCheckContext& e) const -> void { return box->TopLevel(e); }
|
||||
|
||||
private: // types
|
||||
private:
|
||||
// A base class that erases the type of a `Boxed<Content>`, where `Content`
|
||||
// satisfies the Declaration concept.
|
||||
struct Box {
|
||||
@@ -95,15 +99,21 @@ class Declaration {
|
||||
}
|
||||
};
|
||||
|
||||
private: // data members
|
||||
// Constructs an instance in a "partially formed" state, which can only be
|
||||
// assigned to or destroyed.
|
||||
Declaration() = default;
|
||||
|
||||
// Give Bison access to the default constructor.
|
||||
friend class yy::parser;
|
||||
|
||||
// Note: the pointee is const as long as we have no mutating methods. When
|
||||
std::shared_ptr<const Box> box;
|
||||
};
|
||||
|
||||
struct FunctionDeclaration {
|
||||
const FunctionDefinition* definition;
|
||||
explicit FunctionDeclaration(const FunctionDefinition* definition)
|
||||
: definition(definition) {}
|
||||
FunctionDefinition definition;
|
||||
explicit FunctionDeclaration(FunctionDefinition definition)
|
||||
: definition(std::move(definition)) {}
|
||||
|
||||
auto Print() const -> void;
|
||||
auto Name() const -> std::string;
|
||||
@@ -127,12 +137,11 @@ struct StructDeclaration {
|
||||
struct ChoiceDeclaration {
|
||||
int line_num;
|
||||
std::string name;
|
||||
std::list<std::pair<std::string, const Expression*>> alternatives;
|
||||
std::list<std::pair<std::string, Expression>> alternatives;
|
||||
|
||||
ChoiceDeclaration(
|
||||
int line_num, std::string name,
|
||||
std::list<std::pair<std::string, const Expression*>> alternatives)
|
||||
: line_num(line_num), name(name), alternatives(alternatives) {}
|
||||
ChoiceDeclaration(int line_num, std::string name,
|
||||
std::list<std::pair<std::string, Expression>> alternatives)
|
||||
: line_num(line_num), name(name), alternatives(std::move(alternatives)) {}
|
||||
|
||||
void Print() const;
|
||||
auto Name() const -> std::string;
|
||||
|
||||
@@ -8,32 +8,32 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto MakeFunDef(int line_num, std::string name, const Expression* ret_type,
|
||||
const Expression* param_pattern, const Statement* body)
|
||||
-> struct FunctionDefinition* {
|
||||
auto* f = new struct FunctionDefinition();
|
||||
f->line_num = line_num;
|
||||
f->name = std::move(name);
|
||||
f->return_type = ret_type;
|
||||
f->param_pattern = param_pattern;
|
||||
f->body = body;
|
||||
auto MakeFunDef(int line_num, std::string name, Expression ret_type,
|
||||
Expression param_pattern, const Statement* body)
|
||||
-> FunctionDefinition {
|
||||
FunctionDefinition f;
|
||||
f.line_num = line_num;
|
||||
f.name = std::move(name);
|
||||
f.return_type = std::move(ret_type);
|
||||
f.param_pattern = std::move(param_pattern);
|
||||
f.body = body;
|
||||
return f;
|
||||
}
|
||||
|
||||
void PrintFunDefDepth(const FunctionDefinition* f, int depth) {
|
||||
std::cout << "fn " << f->name << " ";
|
||||
PrintExp(f->param_pattern);
|
||||
void PrintFunDefDepth(const FunctionDefinition& f, int depth) {
|
||||
std::cout << "fn " << f.name << " ";
|
||||
PrintExp(&f.param_pattern);
|
||||
std::cout << " -> ";
|
||||
PrintExp(f->return_type);
|
||||
if (f->body) {
|
||||
PrintExp(&f.return_type);
|
||||
if (f.body) {
|
||||
std::cout << " {" << std::endl;
|
||||
PrintStatement(f->body, depth);
|
||||
PrintStatement(f.body, depth);
|
||||
std::cout << std::endl << "}" << std::endl;
|
||||
} else {
|
||||
std::cout << ";" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void PrintFunDef(const FunctionDefinition* f) { PrintFunDefDepth(f, -1); }
|
||||
void PrintFunDef(const FunctionDefinition& f) { PrintFunDefDepth(f, -1); }
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -13,16 +13,15 @@ namespace Carbon {
|
||||
struct FunctionDefinition {
|
||||
int line_num;
|
||||
std::string name;
|
||||
const Expression* param_pattern;
|
||||
const Expression* return_type;
|
||||
Expression param_pattern;
|
||||
Expression return_type;
|
||||
const Statement* body;
|
||||
};
|
||||
|
||||
auto MakeFunDef(int line_num, std::string name, const Expression* ret_type,
|
||||
const Expression* param, const Statement* body)
|
||||
-> FunctionDefinition*;
|
||||
void PrintFunDef(const FunctionDefinition*);
|
||||
void PrintFunDefDepth(const FunctionDefinition*, int);
|
||||
auto MakeFunDef(int line_num, std::string name, Expression ret_type,
|
||||
Expression param, const Statement* body) -> FunctionDefinition;
|
||||
void PrintFunDef(const FunctionDefinition&);
|
||||
void PrintFunDefDepth(const FunctionDefinition&, int);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
|
||||
Reference in New Issue
Block a user