aboutsummaryrefslogtreecommitdiff
path: root/boot/ast.cc
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-07-24 03:11:32 +0200
committerEugen Wissner <belka@caraus.de>2026-07-24 03:11:32 +0200
commitbb4f474cb2247e0e314beab5d7d72e673ff4e8b6 (patch)
tree4809b414bffa8d422ef8c1b6fe65dcdd4dce65d8 /boot/ast.cc
parent275fa4bff6d153019b6914fed7127a7d919ca177 (diff)
downloadelna-bb4f474cb2247e0e314beab5d7d72e673ff4e8b6.tar.gz
Implement a repeat loop
Diffstat (limited to 'boot/ast.cc')
-rw-r--r--boot/ast.cc65
1 files changed, 46 insertions, 19 deletions
diff --git a/boot/ast.cc b/boot/ast.cc
index a50d3d3..69d72fb 100644
--- a/boot/ast.cc
+++ b/boot/ast.cc
@@ -91,6 +91,11 @@ namespace elna::boot
__builtin_unreachable();
}
+ void empty_visitor::visit(repeat_statement *)
+ {
+ __builtin_unreachable();
+ }
+
void empty_visitor::visit(for_statement *)
{
__builtin_unreachable();
@@ -291,14 +296,18 @@ namespace elna::boot
}
}
- void walking_visitor::visit(for_statement *statement)
+ void walking_visitor::visit(repeat_statement *statement)
{
- statement->initial_value().accept(this);
- statement->final_value().accept(this);
- if (statement->step != nullptr)
+ statement->condition().accept(this);
+ for (auto *body_statement : statement->body)
{
- statement->step->accept(this);
+ body_statement->accept(this);
}
+ }
+
+ void walking_visitor::visit(for_statement *statement)
+ {
+ statement->range().accept(this);
for (auto *body_statement : statement->body)
{
body_statement->accept(this);
@@ -1541,19 +1550,42 @@ namespace elna::boot
}
}
+ repeat_statement::repeat_statement(const source_position position, std::vector<statement *>&& body,
+ expression *condition)
+ : node(position), m_condition(condition), body(std::move(body))
+ {
+ }
+
+ repeat_statement::~repeat_statement()
+ {
+ delete this->m_condition;
+ for (const statement *body_statement : this->body)
+ {
+ delete body_statement;
+ }
+ }
+
+ void repeat_statement::accept(parser_visitor *visitor)
+ {
+ visitor->visit(this);
+ }
+
+ expression& repeat_statement::condition()
+ {
+ return *this->m_condition;
+ }
+
for_statement::for_statement(const source_position position, identifier&& control_variable,
- expression *initial_value, expression *final_value,
- std::vector<statement *>&& 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)
+ expression *range, std::vector<statement *>&& body, identifier *const counter = nullptr)
+ : node(position), m_range(range), control_variable(std::move(control_variable)),
+ counter(counter), body(std::move(body))
{
}
for_statement::~for_statement()
{
- delete this->m_initial_value;
- delete this->m_final_value;
- delete this->step;
+ delete this->m_range;
+ delete this->counter;
for (const statement *body_statement : this->body)
{
@@ -1566,14 +1598,9 @@ namespace elna::boot
visitor->visit(this);
}
- expression& for_statement::initial_value()
- {
- return *this->m_initial_value;
- }
-
- expression& for_statement::final_value()
+ expression& for_statement::range()
{
- return *this->m_final_value;
+ return *this->m_range;
}
const char *print_binary_operator(const binary_operator operation)