From f7b035a505e86ad916a43ab1dc1a662a6066b011 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 24 Sep 2026 00:39:51 +0200 Subject: Move module heading to the top --- boot/parser.yy | 81 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 37 deletions(-) (limited to 'boot/parser.yy') diff --git a/boot/parser.yy b/boot/parser.yy index 01dfa80..1e732c6 100644 --- a/boot/parser.yy +++ b/boot/parser.yy @@ -95,12 +95,11 @@ along with GCC; see the file COPYING3. If not see EXCLAMATION "!" ARROW "->" AT "@" HAT "^" COLON ":" SEMICOLON ";" DOT "." COMMA "," -%token PROGRAM "program" +%token PROGRAM "program" MODULE "module" CAST "cast" NIL "nil" CONST "const" - VAR "var" - THREADVAR "threadvar" + VAR "var" THREADVAR "threadvar" PROCEDURE "proc" TYPE "type" RECORD "record" @@ -135,7 +134,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; @@ -144,14 +143,15 @@ along with GCC; see the file COPYING3. If not see %type designator_expression; %type > call_expression; %type statement; -%type > statements statement_part; +%type > statements statement_part optional_begin_statements; %type block; %type procedure_declaration; %type > procedure_heading; %type return_declaration; %type > type_declaration; -%type > type_declarations; -%type > module_declaration module_declarations; +%type > type_declarations sections section; +%type , std::vector>> + required_declarations optional_declarations; %type > procedure_body; %type field_declaration; %type > field_declarations; @@ -167,31 +167,38 @@ along with GCC; see the file COPYING3. If not see %type > import_declaration; %type > required_identifiers optional_identifiers generic_parameters; %type name_reference; -%type > import_declarations import_part; +%type > import_declarations; %type > array_type_expression; +%type > unit; %% -program: - import_part module_declarations "end" "." +program: unit { - driver.finished = true; + driver.tree = $1; + } +unit: + "module" optional_declarations "end" "." + { + auto [modules, sections] = $2; - boot::unit *tree = new boot::unit(boot::make_position(@$), $1, $2); - driver.tree.reset(tree); + $$ = std::make_unique(boot::make_position(@$), std::move(modules), std::move(sections)); } - | import_part module_declarations - "program" "(" optional_identifiers ")" procedure_body "end" "." + | "program" "(" optional_identifiers ")" required_declarations statement_part "end" "." { - driver.finished = true; + auto [modules, sections] = $5; - 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); + $$ = std::make_unique(boot::make_position(@$), std::move(modules), + std::move(sections), $3, boot::make_position(@1), $6); } -module_declaration: + | "program" "(" optional_identifiers ")" optional_begin_statements "end" "." + { + $$ = std::make_unique(boot::make_position(@$), std::vector{}, + std::vector{}, $3, boot::make_position(@1), $5); + } +section: "type" type_declarations { $$ = $2; } - | variable_part + | "var" variable_declarations { - std::vector variables = $1; + std::vector variables = $2; $$.insert($$.end(), variables.begin(), variables.end()); } @@ -206,33 +213,36 @@ module_declaration: $$.insert($$.end(), variables.begin(), variables.end()); } | procedure_declaration { $$.emplace_back($1); } -module_declarations: +required_declarations: + sections { $$ = { {}, $1 }; } + | "import" import_declarations { $$ = { $2, {} }; } + | "import" import_declarations sections { $$ = { $2, $3 }; } +optional_declarations: %empty {} - | module_declaration module_declarations + | required_declarations { $$ = $1; } +sections: + section sections { std::vector appendix = $2; $$ = $1; $$.insert($$.end(), appendix.begin(), appendix.end()); } + | section { $$ = $1; } procedure_body: block procedure_return { $$ = std::make_unique(std::move($1), $2); } - -variable_part: - "var" variable_declarations { $$ = $2; } - block: - variable_part statement_part - { $$ = boot::block(std::move($1), std::move($2)); } - | "begin" statements - { $$ = boot::block(std::vector(), std::move($2)); } - | statements + "var" variable_declarations statement_part + { $$ = boot::block(std::move($2), std::move($3)); } + | optional_begin_statements { $$ = boot::block(std::vector(), std::move($1)); } - statement_part: %empty {} | "begin" statements { $$ = $2; } +optional_begin_statements: + "begin" statements { $$ = $2; } + | statements { $$ = $1; } identifier: IDENTIFIER { $$ = std::make_unique($1, boot::make_position(@1)); } identifier_definition: @@ -739,9 +749,6 @@ import_declarations: { $$.emplace_back(new boot::import_declaration(boot::make_position(@$), $1)); } -import_part: - %empty {} - | "import" import_declarations { $$ = $2; } type_declaration: identifier_definition generic_parameters "=" type_expression { $$ = std::make_unique(boot::make_position(@$), std::move(*$1), $4); -- cgit v1.2.3