Implement the sizeof operator

This commit is contained in:
2025-01-29 12:55:52 +01:00
parent be1a56a557
commit 5178027d9f
7 changed files with 106 additions and 12 deletions

View File

@ -47,6 +47,11 @@ namespace source
expression->value().accept(this);
}
void empty_visitor::visit(size_of_expression *expression)
{
expression->body().accept(this);
}
void empty_visitor::visit(expression_statement *statement)
{
statement->body().accept(this);
@ -754,6 +759,26 @@ namespace source
delete m_value;
}
size_of_expression::size_of_expression(const struct position position, type_expression *body)
: expression(position), m_body(body)
{
}
void size_of_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
type_expression& size_of_expression::body()
{
return *m_body;
}
size_of_expression::~size_of_expression()
{
delete m_body;
}
expression_statement::expression_statement(const struct position position, expression *body)
: statement(position), m_body(body)
{

View File

@ -112,6 +112,9 @@ cast {
as {
return yy::parser::make_AS(this->location);
}
sizeof {
return yy::parser::make_SIZEOF(this->location);
}
[A-Za-z_][A-Za-z0-9_]* {
return yy::parser::make_IDENTIFIER(yytext, this->location);
}

View File

@ -67,7 +67,7 @@
%token CONST VAR PROCEDURE ARRAY OF TYPE RECORD POINTER TO UNION
%token BEGIN_BLOCK END_BLOCK EXTERN
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
%token AND OR NOT CAST AS
%token AND OR NOT CAST AS SIZEOF
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
%token PLUS MINUS MULTIPLICATION DIVISION REMAINDER
%token ASSIGNMENT COLON HAT AT
@ -252,9 +252,13 @@ literal:
pointer:
literal { $$ = $1; }
| designator_expression { $$ = $1; }
| SIZEOF LEFT_PAREN type_expression RIGHT_PAREN
{
$$ = new elna::source::size_of_expression(elna::source::make_position(@1), $3);
}
| cast_expression { $$ = $1; }
| call_expression { $$ = $1; }
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }
| LEFT_PAREN expression RIGHT_PAREN { $$ = $2; }
summand:
factor { $$ = std::move($1); }
| factor MULTIPLICATION factor