Refactor FunctionDefinition towards instance methods (#654)

It felt weird to have a `Make` method in this case.
This commit is contained in:
Jon Meow
2021-07-15 14:55:44 -07:00
committed by GitHub
parent d2afd45214
commit ef785bb6e3
5 changed files with 28 additions and 35 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
namespace Carbon {
void FunctionDeclaration::Print() const { PrintFunDef(definition); }
void FunctionDeclaration::Print() const { definition.Print(); }
void StructDeclaration::Print() const {
std::cout << "struct " << *definition.name << " {" << std::endl;
@@ -8,32 +8,18 @@
namespace Carbon {
auto MakeFunDef(int line_num, std::string name, const Expression* ret_type,
const Expression* param_pattern, const Statement* body)
-> FunctionDefinition {
FunctionDefinition f;
f.line_num = line_num;
f.name = std::move(name);
f.return_type = ret_type;
f.param_pattern = param_pattern;
f.body = body;
return f;
}
void PrintFunDefDepth(const FunctionDefinition& f, int depth) {
std::cout << "fn " << f.name << " ";
PrintExp(f.param_pattern);
void FunctionDefinition::PrintDepth(int depth) const {
std::cout << "fn " << name << " ";
PrintExp(param_pattern);
std::cout << " -> ";
PrintExp(f.return_type);
if (f.body) {
PrintExp(return_type);
if (body) {
std::cout << " {" << std::endl;
PrintStatement(f.body, depth);
PrintStatement(body, depth);
std::cout << std::endl << "}" << std::endl;
} else {
std::cout << ";" << std::endl;
}
}
void PrintFunDef(const FunctionDefinition& f) { PrintFunDefDepth(f, -1); }
} // namespace Carbon
+13 -6
View File
@@ -11,6 +11,19 @@
namespace Carbon {
struct FunctionDefinition {
FunctionDefinition() = default;
FunctionDefinition(int line_num, std::string name,
const Expression* param_pattern,
const Expression* return_type, const Statement* body)
: line_num(line_num),
name(std::move(name)),
param_pattern(param_pattern),
return_type(return_type),
body(body) {}
void Print() const { PrintDepth(-1); }
void PrintDepth(int depth) const;
int line_num;
std::string name;
const Expression* param_pattern;
@@ -18,12 +31,6 @@ struct FunctionDefinition {
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);
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_