aboutsummaryrefslogtreecommitdiff
path: root/boot/ast.cc
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-09-27 11:23:17 +0200
committerEugen Wissner <belka@caraus.de>2026-09-27 11:23:17 +0200
commitc57a24b9fe37e233e7ec0e2e65405738d9c09c6e (patch)
tree99b5f4dc22d27d8159c3ba76f2cf3a455ed37fb6 /boot/ast.cc
parentd048b633dd02014f8bff1b512ce4d5ae2b8b7903 (diff)
downloadelna-c57a24b9fe37e233e7ec0e2e65405738d9c09c6e.tar.gz
Reuse container visiting logic
Diffstat (limited to 'boot/ast.cc')
-rw-r--r--boot/ast.cc93
1 files changed, 26 insertions, 67 deletions
diff --git a/boot/ast.cc b/boot/ast.cc
index cd2ac55..87d6313 100644
--- a/boot/ast.cc
+++ b/boot/ast.cc
@@ -243,7 +243,11 @@ namespace elna::boot
}
if (declaration->body.has_value())
{
- traverse_body(this, declaration->body.value());
+ traverse_block(this, declaration->body.value());
+ if (declaration->body.value().return_expression != nullptr)
+ {
+ declaration->body.value().return_expression->accept(this);
+ }
}
}
@@ -253,66 +257,54 @@ namespace elna::boot
statement->rvalue().accept(this);
}
- void walking_visitor::traverse_block(block& body)
- {
- for (variable_declaration *const variable : body.variables)
- {
- variable->accept(this);
- }
- for (statement *const body_statement : body.statements)
- {
- body_statement->accept(this);
- }
- }
-
void walking_visitor::visit(if_statement *statement)
{
statement->branch().prerequisite().accept(this);
- traverse_block(statement->branch().body);
+ traverse_block(this, statement->branch().body);
for (conditional_statements& branch : statement->branches)
{
branch.prerequisite().accept(this);
- traverse_block(branch.body);
+ traverse_block(this, branch.body);
}
if (statement->alternative != nullptr)
{
- traverse_block(*statement->alternative);
+ traverse_block(this, *statement->alternative);
}
}
void walking_visitor::visit(while_statement *statement)
{
statement->branch().prerequisite().accept(this);
- traverse_block(statement->branch().body);
+ traverse_block(this, statement->branch().body);
for (conditional_statements& branch : statement->branches)
{
branch.prerequisite().accept(this);
- traverse_block(branch.body);
+ traverse_block(this, branch.body);
}
}
void walking_visitor::visit(repeat_statement *statement)
{
statement->condition().accept(this);
- traverse_block(statement->body);
+ traverse_block(this, statement->body);
}
void walking_visitor::visit(for_statement *statement)
{
statement->range().accept(this);
- traverse_block(statement->body);
+ traverse_block(this, statement->body);
}
void walking_visitor::visit(defer_statement *statement)
{
- traverse_block(statement->body);
+ traverse_block(this, statement->body);
}
void walking_visitor::visit(block_statement *statement)
{
- traverse_block(statement->body);
+ traverse_block(this, statement->body);
}
void walking_visitor::visit(break_statement *)
@@ -326,50 +318,30 @@ namespace elna::boot
void walking_visitor::visit(case_statement *statement)
{
statement->condition().accept(this);
- for (switch_case& case_block : statement->cases)
+ for (const switch_case& case_block : statement->cases)
{
- for (expression *case_label : case_block.labels)
- {
- case_label->accept(this);
- }
- traverse_block(case_block.body);
+ visit_all(this, case_block.labels);
+ traverse_block(this, case_block.body);
}
if (statement->alternative != nullptr)
{
- traverse_block(*statement->alternative);
+ traverse_block(this, *statement->alternative);
}
}
void walking_visitor::visit(procedure_call *call)
{
call->callable().accept(this);
- for (expression *argument : call->arguments)
- {
- argument->accept(this);
- }
+ visit_all(this, call->arguments);
}
void walking_visitor::visit(unit *unit)
{
- for (import_declaration *_import : unit->imports)
- {
- _import->accept(this);
- }
- for (declaration *const unit_declaration : unit->declarations)
- {
- unit_declaration->accept(this);
- }
+ visit_all(this, unit->imports);
+ visit_all(this, unit->declarations);
if (unit->entry_point.has_value())
{
- visit_entry_point(unit);
- }
- }
-
- void walking_visitor::visit_entry_point(unit *unit)
- {
- for (statement *body_statement : unit->entry_point.value())
- {
- body_statement->accept(this);
+ visit_all(this, unit->entry_point.value());
}
}
@@ -452,10 +424,7 @@ namespace elna::boot
void walking_visitor::visit(array_constructor_expression *expression)
{
- for (auto *element : expression->elements)
- {
- element->accept(this);
- }
+ visit_all(this, expression->elements);
}
void walking_visitor::visit(slicing_expression *expression)
@@ -1115,20 +1084,10 @@ namespace elna::boot
delete this->return_expression;
}
- void traverse_body(parser_visitor *visitor, const procedure_body& body)
+ void traverse_block(parser_visitor *visitor, const block& 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);
- }
+ visit_all(visitor, body.variables);
+ visit_all(visitor, body.statements);
}
unit::unit(const source_position position,