aboutsummaryrefslogtreecommitdiff
path: root/boot/ast.cc
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-07-13 18:14:45 +0200
committerEugen Wissner <belka@caraus.de>2026-07-13 18:14:45 +0200
commit500c0676b3f6cd5a2297987d5b0dc7ccf34a28d9 (patch)
treea394c73de9273489e6045f3d252714b5ac69b17c /boot/ast.cc
parent97741a01323021ccec8b0b60c6f8318c88ac373a (diff)
downloadelna-500c0676b3f6cd5a2297987d5b0dc7ccf34a28d9.tar.gz
Make return statement part of the body and not a statement
Diffstat (limited to 'boot/ast.cc')
-rw-r--r--boot/ast.cc128
1 files changed, 42 insertions, 86 deletions
diff --git a/boot/ast.cc b/boot/ast.cc
index fbecf11..118d92c 100644
--- a/boot/ast.cc
+++ b/boot/ast.cc
@@ -84,11 +84,6 @@ namespace elna::boot
__builtin_unreachable();
}
- void empty_visitor::visit(return_statement *)
- {
- __builtin_unreachable();
- }
-
void empty_visitor::visit(defer_statement *)
{
__builtin_unreachable();
@@ -205,18 +200,22 @@ namespace elna::boot
}
if (declaration->body.has_value())
{
- for (constant_declaration *const constant : declaration->body.value().constants())
+ for (constant_declaration *const constant : declaration->body.value().constants)
{
constant->accept(this);
}
- for (variable_declaration *const variable : declaration->body.value().variables())
+ for (variable_declaration *const variable : declaration->body.value().variables)
{
variable->accept(this);
}
- for (statement *const statement : declaration->body.value().statements())
+ for (statement *const statement : declaration->body.value().entry_point)
{
statement->accept(this);
}
+ if (declaration->body.value().return_expression != nullptr)
+ {
+ declaration->body.value().return_expression->accept(this);
+ }
}
}
@@ -269,11 +268,6 @@ namespace elna::boot
}
}
- void walking_visitor::visit(return_statement *statement)
- {
- statement->return_expression().accept(this);
- }
-
void walking_visitor::visit(defer_statement *statement)
{
for (struct statement *const block_statement : statement->statements)
@@ -340,12 +334,13 @@ namespace elna::boot
{
procedure->accept(this);
}
- if (unit->entry_point.has_value())
+ for (statement *const entry_statement : unit->entry_point)
{
- for (statement *const entry_statement : unit->entry_point.value())
- {
- entry_statement->accept(this);
- }
+ entry_statement->accept(this);
+ }
+ if (unit->return_expression != nullptr)
+ {
+ unit->return_expression->accept(this);
}
}
@@ -773,70 +768,54 @@ namespace elna::boot
}
procedure_body::procedure_body(std::vector<constant_declaration *>&& constants,
- std::vector<variable_declaration *>&& variables, std::vector<statement *>&& statements)
- : m_variables(std::move(variables)), m_constants(std::move(constants)), m_statements(std::move(statements))
- {
- }
-
- procedure_body::procedure_body(std::vector<constant_declaration*>&& constants,
- std::vector<variable_declaration *>&& variables)
- : procedure_body(std::move(constants), std::move(variables), std::vector<statement *>{})
+ std::vector<variable_declaration *>&& variables, std::vector<statement *>&& entry_point,
+ expression *return_expr)
+ : constants(std::move(constants)), variables(std::move(variables)),
+ entry_point(std::move(entry_point)), return_expression(return_expr)
{
}
procedure_body::procedure_body(procedure_body&& that)
- : m_variables(std::move(that.m_variables)), m_constants(std::move(that.m_constants)),
- m_statements(std::move(that.m_statements))
- {
- }
-
- procedure_body& procedure_body::operator=(procedure_body&& that)
- {
- std::swap(m_variables, that.m_variables);
- std::swap(m_constants, that.m_constants);
- std::swap(m_statements, that.m_statements);
-
- return *this;
- }
-
- const std::vector<variable_declaration *>& procedure_body::variables()
- {
- return m_variables;
- }
-
- const std::vector<constant_declaration *>& procedure_body::constants()
- {
- return m_constants;
- }
-
- const std::vector<statement *>& procedure_body::statements()
+ : constants(std::move(const_cast<std::vector<constant_declaration *>&>(that.constants))),
+ variables(std::move(const_cast<std::vector<variable_declaration *>&>(that.variables))),
+ entry_point(std::move(const_cast<std::vector<statement *>&>(that.entry_point))),
+ return_expression(that.return_expression)
{
- return m_statements;
}
procedure_body::~procedure_body()
{
- for (statement *body_statement : this->statements())
+ for (statement *body_statement : this->entry_point)
{
delete body_statement;
}
- for (variable_declaration *variable : this->variables())
+ for (variable_declaration *variable : this->variables)
{
delete variable;
}
- for (constant_declaration *constant : this->constants())
+ for (constant_declaration *constant : this->constants)
{
delete constant;
}
}
unit::unit(const struct position position)
- : node(position)
+ : node(position), procedure_body(std::vector<constant_declaration *>{},
+ std::vector<variable_declaration *>{}, std::vector<statement *>{}, nullptr)
{
}
- unit::unit(const struct position position, std::vector<statement *>&& entry_point)
- : node(position), entry_point(std::make_optional<std::vector<statement *>>(std::move(entry_point)))
+ unit::unit(const struct position position,
+ std::vector<import_declaration *>&& imports,
+ std::vector<constant_declaration *>&& constants,
+ std::vector<type_declaration *>&& types,
+ std::vector<variable_declaration *>&& variables,
+ std::vector<procedure_declaration *>&& procedures,
+ std::vector<statement *>&& entry_point,
+ expression *const return_expression)
+ : node(position),
+ procedure_body(std::move(constants), std::move(variables), std::move(entry_point), return_expression),
+ imports(std::move(imports)), types(std::move(types)), procedures(std::move(procedures))
{
}
@@ -845,24 +824,21 @@ namespace elna::boot
visitor->visit(this);
}
+ bool unit::has_body() const
+ {
+ return !this->entry_point.empty() || this->return_expression != nullptr;
+ }
+
unit::~unit()
{
for (procedure_declaration *procedure : this->procedures)
{
delete procedure;
}
- for (variable_declaration *variable : this->variables)
- {
- delete variable;
- }
for (type_declaration *type : this->types)
{
delete type;
}
- for (constant_declaration *constant : this->constants)
- {
- delete constant;
- }
for (import_declaration *declaration : this->imports)
{
delete declaration;
@@ -1206,26 +1182,6 @@ namespace elna::boot
}
}
- return_statement::return_statement(const struct position position, expression *return_expression)
- : node(position), m_return_expression(return_expression)
- {
- }
-
- void return_statement::accept(parser_visitor *visitor)
- {
- visitor->visit(this);
- }
-
- expression& return_statement::return_expression()
- {
- return *m_return_expression;
- }
-
- return_statement::~return_statement()
- {
- delete m_return_expression;
- }
-
case_statement::case_statement(const struct position position,
expression *condition, std::vector<switch_case>&& cases, std::vector<statement *> *alternative)
: node(position), m_condition(condition), cases(std::move(cases)), alternative(alternative)