From 38a24d57067b657cde572f92210c5719091c88ff Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 13 Sep 2026 20:12:08 +0200 Subject: Allow reordering sections --- boot/parser.yy | 70 +++++++++++++++++++++++++++------------------------------- 1 file changed, 32 insertions(+), 38 deletions(-) (limited to 'boot/parser.yy') diff --git a/boot/parser.yy b/boot/parser.yy index 9c9e728..68af60e 100644 --- a/boot/parser.yy +++ b/boot/parser.yy @@ -132,7 +132,7 @@ along with GCC; see the file COPYING3. If not see %type switch_case; %type > switch_cases; %type variable_declaration; -%type > variable_declarations variable_part; +%type > variable_declarations; %type type_expression; %type > type_expressions; %type expression operand simple_expression procedure_return unary_expression; @@ -145,9 +145,9 @@ along with GCC; see the file COPYING3. If not see %type procedure_declaration; %type > procedure_heading; %type return_declaration; -%type > procedure_part; %type > type_declaration; -%type > type_declarations type_part; +%type > type_declarations; +%type > module_declaration module_declarations; %type > procedure_body; %type field_declaration; %type > field_declarations; @@ -167,40 +167,47 @@ along with GCC; see the file COPYING3. If not see %type > array_type_expression; %% program: - import_part type_part variable_part procedure_part "end" "." + import_part module_declarations "end" "." { driver.finished = true; - std::vector declarations = $2; - std::vector variables = $3; - declarations.insert(declarations.end(), - std::make_move_iterator(variables.begin()), std::make_move_iterator(variables.end())); - std::vector appendix = $4; - declarations.insert(declarations.end(), - std::make_move_iterator(appendix.begin()), std::make_move_iterator(appendix.end())); - - boot::unit *tree = new boot::unit(boot::make_position(@$), $1, std::move(declarations)); + boot::unit *tree = new boot::unit(boot::make_position(@$), $1, $2); driver.tree.reset(tree); } - | import_part type_part variable_part procedure_part + | import_part module_declarations "program" "(" optional_identifiers ")" procedure_body "end" "." { driver.finished = true; - std::vector declarations = $2; - std::vector variables = $3; - declarations.insert(declarations.end(), - std::make_move_iterator(variables.begin()), std::make_move_iterator(variables.end())); - std::vector appendix = $4; - declarations.insert(declarations.end(), - std::make_move_iterator(appendix.begin()), std::make_move_iterator(appendix.end())); - boot::unit *tree = new boot::unit(boot::make_position(@$), $1, std::move(declarations), - std::move($7), boot::make_position(@5), std::move(*$9)); + boot::unit *tree = new boot::unit(boot::make_position(@$), $1, $2, + std::move($5), boot::make_position(@3), std::move(*$7)); driver.tree.reset(tree); } +module_declaration: + "type" type_declarations { $$ = $2; } + | "var" variable_declarations + { + std::vector variables = $2; + + $$.insert($$.end(), variables.begin(), variables.end()); + } + | procedure_declaration { $$.emplace_back($1); } +module_declarations: + %empty {} + | module_declaration module_declarations + { + std::vector appendix = $2; + + $$ = $1; + $$.insert($$.end(), appendix.begin(), appendix.end()); + } procedure_body: - variable_part statement_part procedure_return - { $$ = std::make_unique($1, $2, $3); } + "var" variable_declarations statement_part procedure_return + { $$ = std::make_unique($2, $3, $4); } + | statement_part procedure_return + { + $$ = std::make_unique(std::vector(), $1, $2); + } statement_part: %empty {} @@ -256,13 +263,6 @@ procedure_declaration: $$ = new boot::procedure_declaration(boot::make_position(@$), std::move(*$2), $4.release()); $$->parameters = $3; } -procedure_part: - %empty {} - | procedure_declaration procedure_part - { - $$ = $2; - $$.emplace($$.cbegin(), $1); - } call_expression: designator_expression actual_parameter_list { $$ = std::make_unique(boot::make_position(@$), $1, $2); @@ -696,9 +696,6 @@ variable_declarations: $$ = $2; $$.insert(std::cbegin($$), $1); } -variable_part: - %empty {} - | "var" variable_declarations { $$ = $2; } import_declaration: IDENTIFIER "." import_declaration { @@ -737,9 +734,6 @@ type_declarations: $$.insert($$.cbegin(), $1.release()); } | %empty {} -type_part: - %empty {} - | "type" type_declarations { $$ = $2; } actual_parameter_list: "(" optional_expressions ")" { $$ = $2; } %% -- cgit v1.2.3