Cast expressions

This commit is contained in:
2025-01-28 11:21:02 +01:00
parent 18d8ded239
commit be1a56a557
9 changed files with 183 additions and 136 deletions

View File

@ -33,14 +33,20 @@ namespace source
definition->body().accept(this);
}
void empty_visitor::visit(call_expression *statement)
void empty_visitor::visit(call_expression *expression)
{
for (auto& argument : statement->arguments())
for (auto& argument : expression->arguments())
{
argument->accept(this);
}
}
void empty_visitor::visit(cast_expression *expression)
{
expression->target().accept(this);
expression->value().accept(this);
}
void empty_visitor::visit(expression_statement *statement)
{
statement->body().accept(this);
@ -185,50 +191,6 @@ namespace source
{
}
operand::~operand()
{
}
integer_operand::integer_operand(const std::int32_t value)
: m_value(value)
{
}
std::int32_t integer_operand::value() const
{
return m_value;
}
variable_operand::variable_operand(const std::string& name)
: m_name(name)
{
}
const std::string& variable_operand::name() const
{
return m_name;
}
temporary_variable::temporary_variable(const std::size_t counter)
: m_counter(counter)
{
}
std::size_t temporary_variable::counter() const
{
return m_counter;
}
label_operand::label_operand(const std::size_t counter)
: m_counter(counter)
{
}
std::size_t label_operand::counter() const
{
return m_counter;
}
node::node(const struct position position)
: source_position(position)
{
@ -766,6 +728,32 @@ namespace source
}
}
cast_expression::cast_expression(const struct position position, type_expression *target, expression *value)
: expression(position), m_target(target), m_value(value)
{
}
void cast_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
type_expression& cast_expression::target()
{
return *m_target;
}
expression& cast_expression::value()
{
return *m_value;
}
cast_expression::~cast_expression()
{
delete m_target;
delete m_value;
}
expression_statement::expression_statement(const struct position position, expression *body)
: statement(position), m_body(body)
{

View File

@ -106,6 +106,12 @@ not {
return {
return yy::parser::make_RETURN(this->location);
}
cast {
return yy::parser::make_CAST(this->location);
}
as {
return yy::parser::make_AS(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
%token AND OR NOT CAST AS
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
%token PLUS MINUS MULTIPLICATION DIVISION REMAINDER
%token ASSIGNMENT COLON HAT AT
@ -97,6 +97,7 @@
%type <std::pair<std::string, elna::source::type_expression *>> field_declaration;
%type <std::vector<std::pair<std::string, elna::source::type_expression *>>> field_list;
%type <std::vector<elna::source::conditional_statements *>> elsif_statement_list;
%type <elna::source::cast_expression *> cast_expression;
%%
program:
type_part constant_part procedure_part variable_part BEGIN_BLOCK optional_statements END_BLOCK DOT
@ -183,6 +184,10 @@ call_expression: IDENTIFIER actual_parameter_list
$$ = new elna::source::call_expression(elna::source::make_position(@1), $1);
std::swap($$->arguments(), $2);
}
cast_expression: CAST LEFT_PAREN expression AS type_expression RIGHT_PAREN
{
$$ = new elna::source::cast_expression(elna::source::make_position(@1), $5, $3);
}
while_statement: WHILE expression DO optional_statements END_BLOCK
{
auto body = new elna::source::conditional_statements($2);
@ -247,6 +252,7 @@ literal:
pointer:
literal { $$ = $1; }
| designator_expression { $$ = $1; }
| cast_expression { $$ = $1; }
| call_expression { $$ = $1; }
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }
summand: