From bf7416a3ef9f0b787dcaebf816f3f3e6e385ff42 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 13 Aug 2026 22:55:06 +0200 Subject: Implement fixed-size integers --- boot/parser.yy | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 104 insertions(+), 12 deletions(-) (limited to 'boot/parser.yy') 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 IDENTIFIER %token TRAIT -%token INTEGER -%token WORD +%token > INTEGER INTEGER64 WORD WORD64 +%token > INTEGER8 WORD8 +%token > INTEGER16 WORD16 +%token > INTEGER32 WORD32 %token FLOAT %token CHARACTER %token STRING @@ -130,8 +132,7 @@ along with GCC; see the file COPYING3. If not see %type > variable_declarations variable_part; %type type_expression; %type > type_expressions; -%type expression operand simple_expression procedure_return; -%type unary_expression; +%type expression operand simple_expression procedure_return unary_expression; %type binary_expression; %type > expressions actual_parameter_list; %type designator_expression; @@ -237,13 +238,86 @@ procedure_return: "return" expression { $$ = $2; } | "return" { $$ = nullptr; } literal: - INTEGER { $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from($1)); } - | WORD { $$ = new boot::literal(boot::make_position(@$), boot::integer_literal::from($1)); } - | FLOAT { $$ = new boot::literal(boot::make_position(@$), $1); } - | BOOLEAN { $$ = new boot::literal(boot::make_position(@$), $1); } - | CHARACTER { $$ = new boot::literal(boot::make_position(@$), $1.at(0)); } - | "nil" { $$ = new boot::literal(boot::make_position(@$), nullptr); } - | STRING { $$ = new boot::literal(boot::make_position(@$), $1); } + INTEGER + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed, false); + } + | INTEGER8 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | INTEGER16 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | INTEGER32 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | INTEGER64 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed, false); + } + | WORD8 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD16 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD32 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | WORD64 + { + auto [magnitude, wants_signed] = $1; + $$ = new boot::literal(boot::make_position(@$), + boot::integer_literal::from(magnitude), wants_signed); + } + | FLOAT + { + $$ = new boot::literal(boot::make_position(@$), $1, boot::integer_sign::unmarked); + } + | BOOLEAN + { + $$ = new boot::literal(boot::make_position(@$), $1, boot::integer_sign::_unsigned); + } + | CHARACTER + { + $$ = new boot::literal(boot::make_position(@$), $1.at(0), boot::integer_sign::_unsigned); + } + | "nil" + { + $$ = new boot::literal(boot::make_position(@$), nullptr, boot::integer_sign::_unsigned); + } + | STRING + { + $$ = new boot::literal(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(); + + 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::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 { -- cgit v1.2.3