From da1e84e33de1a9c2cc1745b8f42bc7f0cee8fc5c Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 9 Oct 2026 23:35:03 +0200 Subject: Add const sections --- boot/parser.yy | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'boot/parser.yy') diff --git a/boot/parser.yy b/boot/parser.yy index 9628443..fa701ec 100644 --- a/boot/parser.yy +++ b/boot/parser.yy @@ -132,6 +132,10 @@ along with GCC; see the file COPYING3. If not see %type > switch_cases; %type variable_declaration; %type > variable_declarations; +%type const_declaration; +%type > const_declarations; +%type , std::vector>> + block_declaration block_declarations; %type type_expression return_declaration; %type > type_expressions; %type expression operand simple_expression procedure_return unary_expression; @@ -201,6 +205,12 @@ section: $$.insert($$.end(), variables.begin(), variables.end()); } + | "const" const_declarations + { + std::vector constants = $2; + + $$.insert($$.end(), constants.begin(), constants.end()); + } | "threadvar" variable_declarations { std::vector variables = $2; @@ -231,11 +241,39 @@ sections: procedure_body: block procedure_return { $$ = std::make_unique(std::move(*$1), $2); } +block_declaration: + "const" const_declarations + { + $$ = { std::move($2), {} }; + } + | "var" variable_declarations + { + $$ = { {}, std::move($2) }; + } +block_declarations: + block_declaration block_declarations + { + auto [constants, variables] = std::move($1); + auto [more_constants, more_variables] = std::move($2); + + constants.insert(constants.end(), more_constants.begin(), more_constants.end()); + variables.insert(variables.end(), more_variables.begin(), more_variables.end()); + $$ = { std::move(constants), std::move(variables) }; + } + | block_declaration + { + $$ = std::move($1); + } block: - "var" variable_declarations statement_part - { $$ = std::make_unique($2, $3); } + block_declarations statement_part + { + auto [constants, variables] = std::move($1); + $$ = std::make_unique(std::move(constants), std::move(variables), $2); + } | optional_begin_statements - { $$ = std::make_unique(std::vector(), $1); } + { + $$ = std::make_unique(std::vector(), $1); + } statement_part: %empty {} | "begin" statements { $$ = $2; } @@ -749,6 +787,18 @@ variable_declarations: $$ = $2; $$.insert(std::cbegin($$), $1); } +const_declaration: + identifier_definition "=" expression + { + $$ = new boot::constant_declaration(boot::make_position(@$), std::move(*$1), $3); + } +const_declarations: + %empty {} + | const_declaration const_declarations + { + $$ = $2; + $$.insert(std::cbegin($$), $1); + } import_path: IDENTIFIER "." import_path { -- cgit v1.2.3