From 275fa4bff6d153019b6914fed7127a7d919ca177 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 23 Jul 2026 01:01:03 +0200 Subject: Implement for loop --- boot/ast.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'boot/ast.cc') diff --git a/boot/ast.cc b/boot/ast.cc index ec5b27d..a50d3d3 100644 --- a/boot/ast.cc +++ b/boot/ast.cc @@ -91,6 +91,11 @@ namespace elna::boot __builtin_unreachable(); } + void empty_visitor::visit(for_statement *) + { + __builtin_unreachable(); + } + void empty_visitor::visit(defer_statement *) { __builtin_unreachable(); @@ -286,6 +291,20 @@ namespace elna::boot } } + void walking_visitor::visit(for_statement *statement) + { + statement->initial_value().accept(this); + statement->final_value().accept(this); + if (statement->step != nullptr) + { + statement->step->accept(this); + } + for (auto *body_statement : statement->body) + { + body_statement->accept(this); + } + } + void walking_visitor::visit(defer_statement *statement) { for (auto *block_statement : statement->statements) @@ -1522,6 +1541,41 @@ namespace elna::boot } } + for_statement::for_statement(const source_position position, identifier&& control_variable, + expression *initial_value, expression *final_value, + std::vector&& body, expression *step) + : node(position), m_initial_value(initial_value), m_final_value(final_value), + control_variable(std::move(control_variable)), body(std::move(body)), step(step) + { + } + + for_statement::~for_statement() + { + delete this->m_initial_value; + delete this->m_final_value; + delete this->step; + + for (const statement *body_statement : this->body) + { + delete body_statement; + } + } + + void for_statement::accept(parser_visitor *visitor) + { + visitor->visit(this); + } + + expression& for_statement::initial_value() + { + return *this->m_initial_value; + } + + expression& for_statement::final_value() + { + return *this->m_final_value; + } + const char *print_binary_operator(const binary_operator operation) { switch (operation) -- cgit v1.2.3