From 4867c970d84e623ec6164290aba7fcf04925ed88 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 18 Dec 2025 13:54:43 +0300 Subject: [PATCH] Simplify bison grammar to use precedence rules --- libs/parser/rules/pslang.y | 79 +++++++++++++------------------------- 1 file changed, 26 insertions(+), 53 deletions(-) diff --git a/libs/parser/rules/pslang.y b/libs/parser/rules/pslang.y index bbbbd7f..4729a1f 100644 --- a/libs/parser/rules/pslang.y +++ b/libs/parser/rules/pslang.y @@ -131,6 +131,14 @@ template %token end 0 +%left ampersand vertical_bar circumflex +%left equals not_equals less greater less_equals greater_equals +%nonassoc as +%precedence UMINUS +%left plus minus +%left asterisk slash percent +%precedence NOT + %type indented_statement_list %type indentation %type statement @@ -142,13 +150,6 @@ template %type type_expression %type primitive_type %type expression -%type bool_expression -%type compare_expression -%type as_expression -%type negate_expression -%type sum_expression -%type mult_expression -%type not_expression %type postfix_expression %type base_expression %type > comma_separated_expression_list @@ -235,52 +236,24 @@ primitive_type ; expression -: bool_expression { $$ = $1; } -; - -bool_expression -: compare_expression { $$ = $1; } -| bool_expression ampersand compare_expression { $$ = ast::binary_operation{ast::binary_operation_type::logical_and, std::make_unique($1), std::make_unique($3), @$ }; } -| bool_expression vertical_bar compare_expression { $$ = ast::binary_operation{ast::binary_operation_type::logical_or, std::make_unique($1), std::make_unique($3), @$ }; } -| bool_expression circumflex compare_expression { $$ = ast::binary_operation{ast::binary_operation_type::logical_xor, std::make_unique($1), std::make_unique($3), @$ }; } -; - -compare_expression -: as_expression { $$ = $1; } -| compare_expression equals as_expression { $$ = ast::binary_operation{ast::binary_operation_type::equals, std::make_unique($1), std::make_unique($3), @$ }; } -| compare_expression not_equals as_expression { $$ = ast::binary_operation{ast::binary_operation_type::not_equals, std::make_unique($1), std::make_unique($3), @$ }; } -| compare_expression less as_expression { $$ = ast::binary_operation{ast::binary_operation_type::less, std::make_unique($1), std::make_unique($3), @$ }; } -| compare_expression greater as_expression { $$ = ast::binary_operation{ast::binary_operation_type::greater, std::make_unique($1), std::make_unique($3), @$ }; } -| compare_expression less_equals as_expression { $$ = ast::binary_operation{ast::binary_operation_type::less_equals, std::make_unique($1), std::make_unique($3), @$ }; } -| compare_expression greater_equals as_expression { $$ = ast::binary_operation{ast::binary_operation_type::greater_equals, std::make_unique($1), std::make_unique($3), @$ }; } -; - -as_expression -: negate_expression { $$ = $1; } -| negate_expression as type_expression { $$ = ast::cast_operation{ std::make_unique($1), std::make_unique($3), @$ }; } -; - -negate_expression -: sum_expression { $$ = $1; } -| minus sum_expression { $$ = ast::unary_operation{ast::unary_operation_type::negation, std::make_unique($2), @$ }; } -; - -sum_expression -: mult_expression { $$ = $1; } -| sum_expression plus mult_expression { $$ = ast::binary_operation{ast::binary_operation_type::addition, std::make_unique($1), std::make_unique($3), @$ }; } -| sum_expression minus mult_expression { $$ = ast::binary_operation{ast::binary_operation_type::subtraction, std::make_unique($1), std::make_unique($3), @$ }; } -; - -mult_expression -: not_expression { $$ = $1; } -| mult_expression asterisk not_expression { $$ = ast::binary_operation{ast::binary_operation_type::multiplication, std::make_unique($1), std::make_unique($3), @$ }; } -| mult_expression slash not_expression { $$ = ast::binary_operation{ast::binary_operation_type::division, std::make_unique($1), std::make_unique($3), @$ }; } -| mult_expression percent not_expression { $$ = ast::binary_operation{ast::binary_operation_type::remainder, std::make_unique($1), std::make_unique($3), @$ }; } -; - -not_expression -: postfix_expression -| exclamation postfix_expression { $$ = ast::unary_operation{ast::unary_operation_type::logical_not, std::make_unique($2), @$ }; } +: expression ampersand expression { $$ = ast::binary_operation{ast::binary_operation_type::logical_and, std::make_unique($1), std::make_unique($3), @$ }; } +| expression vertical_bar expression { $$ = ast::binary_operation{ast::binary_operation_type::logical_or, std::make_unique($1), std::make_unique($3), @$ }; } +| expression circumflex expression { $$ = ast::binary_operation{ast::binary_operation_type::logical_xor, std::make_unique($1), std::make_unique($3), @$ }; } +| expression equals expression { $$ = ast::binary_operation{ast::binary_operation_type::equals, std::make_unique($1), std::make_unique($3), @$ }; } +| expression not_equals expression { $$ = ast::binary_operation{ast::binary_operation_type::not_equals, std::make_unique($1), std::make_unique($3), @$ }; } +| expression less expression { $$ = ast::binary_operation{ast::binary_operation_type::less, std::make_unique($1), std::make_unique($3), @$ }; } +| expression greater expression { $$ = ast::binary_operation{ast::binary_operation_type::greater, std::make_unique($1), std::make_unique($3), @$ }; } +| expression less_equals expression { $$ = ast::binary_operation{ast::binary_operation_type::less_equals, std::make_unique($1), std::make_unique($3), @$ }; } +| expression greater_equals expression { $$ = ast::binary_operation{ast::binary_operation_type::greater_equals, std::make_unique($1), std::make_unique($3), @$ }; } +| expression as type_expression { $$ = ast::cast_operation{ std::make_unique($1), std::make_unique($3), @$ }; } +| minus expression %prec UMINUS { $$ = ast::unary_operation{ast::unary_operation_type::negation, std::make_unique($2), @$ }; } +| expression plus expression { $$ = ast::binary_operation{ast::binary_operation_type::addition, std::make_unique($1), std::make_unique($3), @$ }; } +| expression minus expression { $$ = ast::binary_operation{ast::binary_operation_type::subtraction, std::make_unique($1), std::make_unique($3), @$ }; } +| expression asterisk expression { $$ = ast::binary_operation{ast::binary_operation_type::multiplication, std::make_unique($1), std::make_unique($3), @$ }; } +| expression slash expression { $$ = ast::binary_operation{ast::binary_operation_type::division, std::make_unique($1), std::make_unique($3), @$ }; } +| expression percent expression { $$ = ast::binary_operation{ast::binary_operation_type::remainder, std::make_unique($1), std::make_unique($3), @$ }; } +| exclamation expression %prec NOT { $$ = ast::unary_operation{ast::unary_operation_type::logical_not, std::make_unique($2), @$ }; } +| postfix_expression { $$ = $1; } ; postfix_expression