diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-08-13 22:55:06 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-08-13 22:55:06 +0200 |
| commit | bf7416a3ef9f0b787dcaebf816f3f3e6e385ff42 (patch) | |
| tree | 26359c705133dbd8e98f06483008a720d0dae9d7 /boot/parser.yy | |
| parent | ae29de96c94e5c582f4424804464cdd29bf2a013 (diff) | |
| download | elna-bf7416a3ef9f0b787dcaebf816f3f3e6e385ff42.tar.gz | |
Implement fixed-size integers
Diffstat (limited to 'boot/parser.yy')
| -rw-r--r-- | boot/parser.yy | 116 |
1 files changed, 104 insertions, 12 deletions
diff --git a/boot/parser.yy b/boot/parser.yy index a28b6cb..b48510b 100644 --- a/boot/parser.yy +++ b/boot/parser.yy @@ -78,8 +78,10 @@ along with GCC; see the file COPYING3. If not see %token <std::string> IDENTIFIER %token <std::string> TRAIT -%token <std::int64_t> INTEGER -%token <std::uint64_t> WORD +%token <std::pair<std::uint64_t, elna::boot::integer_sign>> INTEGER INTEGER64 WORD WORD64 +%token <std::pair<std::uint8_t, elna::boot::integer_sign>> INTEGER8 WORD8 +%token <std::pair<std::uint16_t, elna::boot::integer_sign>> INTEGER16 WORD16 +%token <std::pair<std::uint32_t, elna::boot::integer_sign>> INTEGER32 WORD32 %token <double> FLOAT %token <std::string> CHARACTER %token <std::string> STRING @@ -130,8 +132,7 @@ along with GCC; see the file COPYING3. If not see %type <std::vector<elna::boot::variable_declaration *>> variable_declarations variable_part; %type <elna::boot::type_expression *> type_expression; %type <std::vector<elna::boot::type_expression *>> type_expressions; -%type <elna::boot::expression *> expression operand simple_expression procedure_return; -%type <elna::boot::unary_expression *> unary_expression; +%type <elna::boot::expression *> expression operand simple_expression procedure_return unary_expression; %type <elna::boot::binary_expression *> binary_expression; %type <std::vector<elna::boot::expression *>> expressions actual_parameter_list; %type <elna::boot::designator_expression *> designator_expression; @@ -237,13 +238,86 @@ procedure_return: "return" expression { $$ = $2; } | "return" { $$ = nullptr; } literal: - INTEGER { $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), boot::integer_literal::from($1)); } - | WORD { $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), boot::integer_literal::from($1)); } - | FLOAT { $$ = new boot::literal<double>(boot::make_position(@$), $1); } - | BOOLEAN { $$ = new boot::literal<bool>(boot::make_position(@$), $1); } - | CHARACTER { $$ = new boot::literal<unsigned char>(boot::make_position(@$), $1.at(0)); } - | "nil" { $$ = new boot::literal<std::nullptr_t>(boot::make_position(@$), nullptr); } - | STRING { $$ = new boot::literal<std::string>(boot::make_position(@$), $1); } + INTEGER + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed, false); + } + | INTEGER8 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | INTEGER16 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | INTEGER32 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | INTEGER64 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed, false); + } + | WORD8 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD16 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD32 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD64 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | FLOAT + { + $$ = new boot::literal<double>(boot::make_position(@$), $1, boot::integer_sign::unmarked); + } + | BOOLEAN + { + $$ = new boot::literal<bool>(boot::make_position(@$), $1, boot::integer_sign::_unsigned); + } + | CHARACTER + { + $$ = new boot::literal<unsigned char>(boot::make_position(@$), $1.at(0), boot::integer_sign::_unsigned); + } + | "nil" + { + $$ = new boot::literal<std::nullptr_t>(boot::make_position(@$), nullptr, boot::integer_sign::_unsigned); + } + | STRING + { + $$ = new boot::literal<std::string>(boot::make_position(@$), $1, boot::integer_sign::_unsigned); + } simple_expression: literal { $$ = $1; } | designator_expression { $$ = $1; } @@ -348,7 +422,25 @@ unary_expression: } | "-" operand { - $$ = new boot::unary_expression(boot::make_position(@$), $2, boot::unary_operator::minus); + auto operand = $2; + auto integer_operand = operand->is_literal() == nullptr + ? nullptr + : operand->is_literal()->is_a<boot::integer_literal>(); + + if (integer_operand != nullptr + && integer_operand->wants_signed == boot::integer_sign::unmarked) + { + // Rebuild the literal with a position spanning the minus and the + // integer token. + $$ = new boot::literal<boot::integer_literal>(boot::make_position(@$), + integer_operand->value, boot::integer_sign::negative, + integer_operand->has_explicit_size); + delete integer_operand; + } + else + { + $$ = new boot::unary_expression(boot::make_position(@$), operand, boot::unary_operator::minus); + } } | "+" operand { |
