Add compound assignment operators

This commit is contained in:
Nikita Lisitsa 2026-03-30 13:33:16 +03:00
parent 171e07c58d
commit 6d69f846f4
2 changed files with 21 additions and 0 deletions

View file

@ -63,6 +63,13 @@ f64 { return bp::make_f64(ctx.location); }
")" { return bp::make_rparen(ctx.location); } ")" { return bp::make_rparen(ctx.location); }
"[" { return bp::make_lbracket(ctx.location); } "[" { return bp::make_lbracket(ctx.location); }
"]" { return bp::make_rbracket(ctx.location); } "]" { return bp::make_rbracket(ctx.location); }
"+=" { return bp::make_plus_assignment(ctx.location); }
"-=" { return bp::make_minus_assignment(ctx.location); }
"*=" { return bp::make_asterisk_assignment(ctx.location); }
"/=" { return bp::make_slash_assignment(ctx.location); }
"%=" { return bp::make_percent_assignment(ctx.location); }
">>=" { return bp::make_right_shift_assignment(ctx.location); }
"<<=" { return bp::make_left_shift_assignment(ctx.location); }
"+" { return bp::make_plus(ctx.location); } "+" { return bp::make_plus(ctx.location); }
"-" { return bp::make_minus(ctx.location); } "-" { return bp::make_minus(ctx.location); }
"*" { return bp::make_asterisk(ctx.location); } "*" { return bp::make_asterisk(ctx.location); }

View file

@ -105,6 +105,13 @@ template <typename T>
%token less_equals "<=" %token less_equals "<="
%token greater_equals ">=" %token greater_equals ">="
%token arrow "->" %token arrow "->"
%token plus_assignment "+="
%token minus_assignment "-="
%token asterisk_assignment "*="
%token slash_assignment "/="
%token percent_assignment "%="
%token left_shift_assignment "<<="
%token right_shift_assignment ">>="
%token <std::string> lit_i8 %token <std::string> lit_i8
%token <std::string> lit_u8 %token <std::string> lit_u8
@ -222,6 +229,13 @@ optional_comment
statement statement
: expression { $$ = std::make_unique<ast::expression>($1); } : expression { $$ = std::make_unique<ast::expression>($1); }
| expression assignment expression { $$ = ast::assignment{ std::make_unique<ast::expression>($1), std::make_unique<ast::expression>($3), @$ }; } | expression assignment expression { $$ = ast::assignment{ std::make_unique<ast::expression>($1), std::make_unique<ast::expression>($3), @$ }; }
| expression plus_assignment expression { auto lhs = std::make_shared<ast::expression>($1); $$ = ast::assignment{ lhs, std::make_shared<ast::expression>(ast::binary_operation{ ast::binary_operation_type::addition, lhs, std::make_unique<ast::expression>($3), @$ }), @$ }; }
| expression minus_assignment expression { auto lhs = std::make_shared<ast::expression>($1); $$ = ast::assignment{ lhs, std::make_shared<ast::expression>(ast::binary_operation{ ast::binary_operation_type::subtraction, lhs, std::make_unique<ast::expression>($3), @$ }), @$ }; }
| expression asterisk_assignment expression { auto lhs = std::make_shared<ast::expression>($1); $$ = ast::assignment{ lhs, std::make_shared<ast::expression>(ast::binary_operation{ ast::binary_operation_type::multiplication, lhs, std::make_unique<ast::expression>($3), @$ }), @$ }; }
| expression slash_assignment expression { auto lhs = std::make_shared<ast::expression>($1); $$ = ast::assignment{ lhs, std::make_shared<ast::expression>(ast::binary_operation{ ast::binary_operation_type::division, lhs, std::make_unique<ast::expression>($3), @$ }), @$ }; }
| expression percent_assignment expression { auto lhs = std::make_shared<ast::expression>($1); $$ = ast::assignment{ lhs, std::make_shared<ast::expression>(ast::binary_operation{ ast::binary_operation_type::remainder, lhs, std::make_unique<ast::expression>($3), @$ }), @$ }; }
| expression left_shift_assignment expression { auto lhs = std::make_shared<ast::expression>($1); $$ = ast::assignment{ lhs, std::make_shared<ast::expression>(ast::binary_operation{ ast::binary_operation_type::left_shift, lhs, std::make_unique<ast::expression>($3), @$ }), @$ }; }
| expression right_shift_assignment expression { auto lhs = std::make_shared<ast::expression>($1); $$ = ast::assignment{ lhs, std::make_shared<ast::expression>(ast::binary_operation{ ast::binary_operation_type::right_shift, lhs, std::make_unique<ast::expression>($3), @$ }), @$ }; }
| variable_declaration { $$ = $1; } | variable_declaration { $$ = $1; }
| if expression colon { $$ = ast::if_block{std::make_unique<ast::expression>($2), @$}; } | if expression colon { $$ = ast::if_block{std::make_unique<ast::expression>($2), @$}; }
| else colon { $$ = ast::else_block{@$}; } | else colon { $$ = ast::else_block{@$}; }