From 4ad6052aa2816f8296e62569272e99fc0d2fd3c1 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 3 Jul 2026 08:53:03 +0200 Subject: Merge variable_expression and named_type_expression --- frontend/parser.yy | 97 +++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 48 deletions(-) (limited to 'frontend/parser.yy') diff --git a/frontend/parser.yy b/frontend/parser.yy index bace8d7..5e3837b 100644 --- a/frontend/parser.yy +++ b/frontend/parser.yy @@ -102,8 +102,6 @@ along with GCC; see the file COPYING3. If not see ELSE "else" ELSIF "elsif" RETURN "return" - PROGRAM "program" - MODULE "module" IMPORT "import" BEGIN_BLOCK "begin" END_BLOCK "end" @@ -142,11 +140,12 @@ along with GCC; see the file COPYING3. If not see %type call_expression; %type return_statement; %type statement; -%type > required_statements optional_statements statement_part; +%type > required_statements optional_statements; +%type >> statement_part; %type procedure_declaration; %type , elna::frontend::procedure_type_expression *>> procedure_heading; %type return_declaration; -%type > procedure_declarations procedure_part; +%type > procedure_part; %type type_declaration; %type > type_declarations type_part; %type > block; @@ -162,43 +161,47 @@ along with GCC; see the file COPYING3. If not see %type > import_declarations import_part; %% program: - "program" ";" import_part constant_part type_part variable_part procedure_part statement_part "end" "." + import_part constant_part type_part variable_part procedure_part statement_part "end" "." { - auto tree = new frontend::program(frontend::make_position(@1)); - - std::swap(tree->imports, $3); - std::swap(tree->constants, $4); - std::swap(tree->types , $5); - std::swap(tree->variables, $6); - std::swap(tree->procedures, $7); - std::swap(tree->body, $8); - - driver.tree.reset(tree); - } - | "module" ";" import_part constant_part type_part variable_part procedure_part "end" "." - { - auto tree = new frontend::unit(frontend::make_position(@1)); - - std::swap(tree->imports, $3); - std::swap(tree->constants, $4); - std::swap(tree->types , $5); - std::swap(tree->variables, $6); - std::swap(tree->procedures, $7); + if ($6) + { + frontend::program *tree = new frontend::program(frontend::make_position(@1)); + std::swap(tree->body, *$6.release()); - driver.tree.reset(tree); + driver.tree.reset(tree); + } + else + { + driver.tree.reset(new frontend::unit(frontend::make_position(@1))); + } + std::swap(driver.tree->imports, $1); + std::swap(driver.tree->constants, $2); + std::swap(driver.tree->types , $3); + std::swap(driver.tree->variables, $4); + std::swap(driver.tree->procedures, $5); } block: constant_part variable_part statement_part "end" { - $$ = std::make_unique(std::move($1), std::move($2), std::move($3)); + if ($3) + { + $$ = std::make_unique(std::move($1), std::move($2), std::move(*$3.release())); + } + else + { + $$ = std::make_unique(std::move($1), std::move($2)); + } } statement_part: /* no statements */ {} - | "begin" required_statements { std::swap($$, $2); } - | return_statement { $$.push_back($1); } + | "begin" required_statements { $$ = std::make_unique>(std::move($2));; } + | return_statement + { + $$ = std::make_unique>(std::vector{ $1 }); + } | "begin" required_statements ";" return_statement { - std::swap($$, $2); - $$.push_back($4); + $$ = std::make_unique>(std::move($2)); + $$->push_back($4); } identifier_definition: IDENTIFIER "*" { $$ = frontend::identifier_definition{ $1, true }; } @@ -224,26 +227,24 @@ procedure_heading: formal_parameter_list return_declaration } } procedure_declaration: - "proc" identifier_definition procedure_heading ";" block ";" + "proc" identifier_definition procedure_heading block { - $$ = new frontend::procedure_declaration(frontend::make_position(@1), std::move($2), $3.second, std::move(*$5)); + $$ = new frontend::procedure_declaration(frontend::make_position(@1), std::move($2), $3.second, + std::move(*$4)); std::swap($3.first, $$->parameter_names); } - | "proc" identifier_definition procedure_heading ";" "extern" ";" + | "proc" identifier_definition procedure_heading "extern" { $$ = new frontend::procedure_declaration(frontend::make_position(@1), std::move($2), $3.second); std::swap($3.first, $$->parameter_names); } -procedure_declarations: - procedure_declaration procedure_declarations +procedure_part: + /* no procedure declarations */ {} + | procedure_declaration procedure_part { std::swap($$, $2); $$.emplace($$.cbegin(), std::move($1)); } - | procedure_declaration { $$.emplace_back(std::move($1)); } -procedure_part: - /* no procedure definitions */ {} - | procedure_declarations { std::swap($$, $1); } call_expression: designator_expression actual_parameter_list { $$ = new frontend::procedure_call(frontend::make_position(@1), $1); @@ -401,7 +402,7 @@ designator_expression: | simple_expression "^" { $$ = new frontend::dereference_expression(frontend::make_position(@1), $1); } | IDENTIFIER - { $$ = new frontend::variable_expression(frontend::make_position(@1), $1); } + { $$ = new frontend::named_expression(frontend::make_position(@1), $1); } statement: designator_expression ":=" expression { $$ = new frontend::assign_statement(frontend::make_position(@1), $1, $3); } @@ -487,7 +488,7 @@ type_expression: } | IDENTIFIER { - $$ = new frontend::named_type_expression(frontend::make_position(@1), $1); + $$ = new frontend::named_expression(frontend::make_position(@1), $1); } identifiers: IDENTIFIER "," identifiers @@ -497,18 +498,18 @@ identifiers: } | IDENTIFIER { $$.emplace_back(std::move($1)); } variable_declaration: - identifier_definitions ":" type_expression ";" + identifier_definitions ":" type_expression { std::shared_ptr shared_type{ $3 }; $$ = new frontend::variable_declaration( frontend::make_position(@2), std::move($1), shared_type); } - | identifier_definitions ":" type_expression ":=" "extern" ";" + | identifier_definitions ":" type_expression ":=" "extern" { std::shared_ptr shared_type{ $3 }; $$ = new frontend::variable_declaration( frontend::make_position(@2), std::move($1), shared_type, std::monostate{}); } - | identifier_definitions ":" type_expression ":=" expression ";" + | identifier_definitions ":" type_expression ":=" expression { std::shared_ptr shared_type{ $3 }; $$ = new frontend::variable_declaration( frontend::make_position(@2), std::move($1), shared_type, $5); @@ -523,7 +524,7 @@ variable_declarations: variable_part: /* no variable declarations */ {} | "var" variable_declarations { std::swap($$, $2); } -constant_declaration: identifier_definition ":=" expression ";" +constant_declaration: identifier_definition ":=" expression { $$ = new frontend::constant_declaration(frontend::make_position(@1), std::move($1), $3); } @@ -556,8 +557,8 @@ import_declarations: } import_part: /* no import declarations */ {} - | "import" import_declarations ";" { std::swap($$, $2); } -type_declaration: identifier_definition "=" type_expression ";" + | "import" import_declarations { std::swap($$, $2); } +type_declaration: identifier_definition "=" type_expression { $$ = new frontend::type_declaration(frontend::make_position(@1), std::move($1), $3); } -- cgit v1.2.3