From 47521ad6d85f9bd6caee39696ce40dff82cfa50d Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 2 Sep 2026 13:31:04 +0200 Subject: Implement program entry point --- boot/ast.cc | 59 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'boot/ast.cc') diff --git a/boot/ast.cc b/boot/ast.cc index d4f015b..b8647f4 100644 --- a/boot/ast.cc +++ b/boot/ast.cc @@ -232,18 +232,7 @@ namespace elna::boot } if (declaration->body.has_value()) { - for (variable_declaration *variable : declaration->body.value().variables) - { - variable->accept(this); - } - for (auto *statement : declaration->body.value().entry_point) - { - statement->accept(this); - } - if (declaration->body.value().return_expression != nullptr) - { - declaration->body.value().return_expression->accept(this); - } + traverse_body(this, declaration->body.value()); } } @@ -376,9 +365,9 @@ namespace elna::boot { procedure->accept(this); } - for (auto *entry_statement : unit->entry_point) + if (unit->entry_point.has_value()) { - entry_statement->accept(this); + traverse_body(this, unit->entry_point.value()); } } @@ -989,20 +978,20 @@ namespace elna::boot procedure_body::procedure_body(std::vector&& variables, std::vector&& entry_point, expression *return_expression) : variables(std::move(variables)), - entry_point(std::move(entry_point)), return_expression(return_expression) + statements(std::move(entry_point)), return_expression(return_expression) { } procedure_body::procedure_body(procedure_body&& that) noexcept : variables(std::move(const_cast&>(that.variables))), - entry_point(std::move(const_cast&>(that.entry_point))), + statements(std::move(const_cast&>(that.statements))), return_expression(that.return_expression) { } procedure_body::~procedure_body() { - for (const statement *body_statement : this->entry_point) + for (const statement *body_statement : this->statements) { delete body_statement; } @@ -1012,9 +1001,30 @@ namespace elna::boot } } - unit::unit(const source_position position) + void traverse_body(parser_visitor *visitor, const procedure_body& body) + { + for (variable_declaration *variable : body.variables) + { + variable->accept(visitor); + } + for (statement *body_statement : body.statements) + { + body_statement->accept(visitor); + } + if (body.return_expression != nullptr) + { + body.return_expression->accept(visitor); + } + } + + unit::unit(const source_position position, + std::vector&& imports, + std::vector&& types, + std::vector&& variables, + std::vector&& procedures) : node(position), - procedure_body(std::vector{}, std::vector{}, nullptr) + imports(std::move(imports)), types(std::move(types)), procedures(std::move(procedures)), + variables(std::move(variables)) { } @@ -1023,10 +1033,10 @@ namespace elna::boot std::vector&& types, std::vector&& variables, std::vector&& procedures, - std::vector&& entry_point) + std::optional&& body) : node(position), - procedure_body(std::move(variables), std::move(entry_point)), - imports(std::move(imports)), types(std::move(types)), procedures(std::move(procedures)) + imports(std::move(imports)), types(std::move(types)), procedures(std::move(procedures)), + variables(std::move(variables)), entry_point(std::move(body)) { } @@ -1035,11 +1045,6 @@ namespace elna::boot visitor->visit(this); } - bool unit::has_body() const - { - return !this->entry_point.empty(); - } - unit::~unit() { for (const procedure_declaration *procedure : this->procedures) -- cgit v1.2.3