diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-09-16 11:49:35 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-09-16 11:49:35 +0200 |
| commit | b575036fe804f14b78da74897a74b010cc94f730 (patch) | |
| tree | 054dcb6e932bc8cf5faf2a0158794913fc6e8be3 /boot | |
| parent | 52c8ca2c06da93c8d14e80a256e8996c1fa1e885 (diff) | |
| download | elna-b575036fe804f14b78da74897a74b010cc94f730.tar.gz | |
Implement named blocks
Diffstat (limited to 'boot')
| -rw-r--r-- | boot/ast.cc | 63 | ||||
| -rw-r--r-- | boot/lexer.ll | 6 | ||||
| -rw-r--r-- | boot/name_analysis.cc | 73 | ||||
| -rw-r--r-- | boot/parser.yy | 6 | ||||
| -rw-r--r-- | boot/symbol.cc | 15 | ||||
| -rw-r--r-- | boot/validation.cc | 9 |
6 files changed, 160 insertions, 12 deletions
diff --git a/boot/ast.cc b/boot/ast.cc index 04e9230..4a596e5 100644 --- a/boot/ast.cc +++ b/boot/ast.cc @@ -112,6 +112,16 @@ namespace elna::boot __builtin_unreachable(); } + void empty_visitor::visit(block_statement *) + { + __builtin_unreachable(); + } + + void empty_visitor::visit(break_statement *) + { + __builtin_unreachable(); + } + void empty_visitor::visit(empty_statement *) { __builtin_unreachable(); @@ -306,12 +316,24 @@ namespace elna::boot void walking_visitor::visit(defer_statement *statement) { - for (auto *block_statement : statement->statements) + for (auto *body_statement : statement->statements) + { + body_statement->accept(this); + } + } + + void walking_visitor::visit(block_statement *statement) + { + for (auto *body_statement : statement->statements) { - block_statement->accept(this); + body_statement->accept(this); } } + void walking_visitor::visit(break_statement *) + { + } + void walking_visitor::visit(empty_statement *) { } @@ -325,16 +347,16 @@ namespace elna::boot { case_label->accept(this); } - for (auto *block_statement : case_block.statements) + for (auto *body_statement : case_block.statements) { - block_statement->accept(this); + body_statement->accept(this); } } if (statement->alternative != nullptr) { - for (auto *block_statement : *statement->alternative) + for (auto *body_statement : *statement->alternative) { - block_statement->accept(this); + body_statement->accept(this); } } } @@ -1171,6 +1193,35 @@ namespace elna::boot } } + block_statement::block_statement(const source_position position, identifier&& name, + std::vector<statement *>&& statements) + : node(position), name(std::move(name)), statements(std::move(statements)) + { + } + + void block_statement::accept(parser_visitor *visitor) + { + visitor->visit(this); + } + + block_statement::~block_statement() + { + for (const statement *body_statement : statements) + { + delete body_statement; + } + } + + break_statement::break_statement(const source_position position, identifier&& label) + : node(position), label(std::move(label)) + { + } + + void break_statement::accept(parser_visitor *visitor) + { + visitor->visit(this); + } + void empty_statement::accept(parser_visitor *visitor) { visitor->visit(this); diff --git a/boot/lexer.ll b/boot/lexer.ll index 1cccd35..3d881e3 100644 --- a/boot/lexer.ll +++ b/boot/lexer.ll @@ -179,6 +179,12 @@ cast { defer { return yy::parser::make_DEFER(this->location); } +block { + return yy::parser::make_BLOCK(this->location); +} +break { + return yy::parser::make_BREAK(this->location); +} case { return yy::parser::make_CASE(this->location); } diff --git a/boot/name_analysis.cc b/boot/name_analysis.cc index 8820684..9ba030c 100644 --- a/boot/name_analysis.cc +++ b/boot/name_analysis.cc @@ -48,8 +48,14 @@ namespace elna::boot return "Trait '#" + this->name + "' not declared"; case undeclared_symbol: return "Symbol '" + this->name + "' not declared"; + case undeclared_label: + return "Label '" + this->name + "' not declared"; case not_a_type: return "'" + this->name + "' is not a type"; + case not_a_label: + return "'" + this->name + "' is not a label"; + case break_leaves_defer: + return "'break' cannot leave a defer statement"; default: __builtin_unreachable(); } @@ -781,9 +787,64 @@ namespace elna::boot void resolving_visitor::visit(defer_statement *statement) { - for (auto *block_statement : statement->statements) + ++this->defer_depth; + for (auto *body_statement : statement->statements) { - block_statement->accept(this); + body_statement->accept(this); + } + --this->defer_depth; + } + + void resolving_visitor::visit(block_statement *statement) + { + const std::string& label_name = statement->name.name(); + + this->bag.enter(); + + auto label_symbol = std::make_shared<label_info>(this->defer_depth); + label_symbol->position.emplace(statement->name.position()); + label_symbol->file = this->module_file; + + if (!this->bag.enter(label_name, label_symbol)) + { + auto original = this->bag.lookup(label_name); + symbol_declaration_error::redefinition original_definition{ + .original = original->position, + .file = this->redefinition_file(original) + }; + add_error<symbol_declaration_error>(statement->name.position(), + label_name, original_definition); + } + for (auto *body_statement : statement->statements) + { + body_statement->accept(this); + } + this->bag.leave(); + } + + void resolving_visitor::visit(break_statement *statement) + { + const std::string& label_name = statement->label.name(); + const source_position position = statement->label.position(); + const std::shared_ptr<info> symbol = this->bag.lookup(label_name); + + if (symbol == nullptr) + { + add_error<symbol_declaration_error>(position, label_name, + symbol_declaration_error::kind::undeclared_label); + return; + } + auto label_symbol = symbol->is_label(); + + if (label_symbol == nullptr) + { + add_error<symbol_declaration_error>(position, label_name, + symbol_declaration_error::kind::not_a_label); + } + else if (label_symbol->defer_depth != this->defer_depth) + { + add_error<symbol_declaration_error>(position, label_name, + symbol_declaration_error::kind::break_leaves_defer); } } @@ -800,16 +861,16 @@ namespace elna::boot { case_label->accept(this); } - for (auto *block_statement : case_block.statements) + for (auto *body_statement : case_block.statements) { - block_statement->accept(this); + body_statement->accept(this); } } if (statement->alternative != nullptr) { - for (auto *block_statement : *statement->alternative) + for (auto *body_statement : *statement->alternative) { - block_statement->accept(this); + body_statement->accept(this); } } } diff --git a/boot/parser.yy b/boot/parser.yy index ab386fe..eaf1a7c 100644 --- a/boot/parser.yy +++ b/boot/parser.yy @@ -112,6 +112,8 @@ along with GCC; see the file COPYING3. If not see BEGIN_BLOCK "begin" END_BLOCK "end" DEFER "defer" + BLOCK "block" + BREAK "break" CASE "case" OF "of" TO "to" @@ -582,6 +584,10 @@ statement: | call_expression { $$ = $1.release(); } | "defer" statements "end" { $$ = new boot::defer_statement(boot::make_position(@$), $2); } + | "block" identifier statements "end" + { $$ = new boot::block_statement(boot::make_position(@$), std::move(*$2), $3); } + | "break" identifier + { $$ = new boot::break_statement(boot::make_position(@$), std::move(*$2)); } | "case" expression "of" switch_cases else_statements "end" { $$ = new boot::case_statement(boot::make_position(@$), $2, $4, $5); } | %empty { $$ = new boot::empty_statement(boot::make_position(@$)); } diff --git a/boot/symbol.cc b/boot/symbol.cc index 55f91ea..58524ad 100644 --- a/boot/symbol.cc +++ b/boot/symbol.cc @@ -310,6 +310,11 @@ namespace elna::boot return nullptr; } + std::shared_ptr<label_info> info::is_label() + { + return nullptr; + } + type_info::type_info(const type& symbol) : symbol(symbol), owner(symbol.get<alias_type>()) { @@ -346,6 +351,16 @@ namespace elna::boot return std::static_pointer_cast<variable_info>(shared_from_this()); } + label_info::label_info(std::size_t defer_depth) + : defer_depth(defer_depth) + { + } + + std::shared_ptr<label_info> label_info::is_label() + { + return std::static_pointer_cast<label_info>(shared_from_this()); + } + static void builtin_integers(const std::shared_ptr<symbol_table>& symbols, const std::array<type_properties, target_integer_count>& properties, const std::string& integer_name) diff --git a/boot/validation.cc b/boot/validation.cc index 2125a99..e628cd8 100644 --- a/boot/validation.cc +++ b/boot/validation.cc @@ -157,6 +157,15 @@ namespace elna::boot visit_statements(statement->statements); } + void validation_visitor::visit(block_statement *statement) + { + visit_statements(statement->statements); + } + + void validation_visitor::visit(break_statement *) + { + } + void validation_visitor::visit(empty_statement *) { } |
