Use colon instead of as to cast

This commit is contained in:
2025-02-14 08:05:03 +01:00
parent c564847c6b
commit ee4ebf64b9
10 changed files with 488 additions and 290 deletions

View File

@@ -61,7 +61,7 @@ namespace boot
expression->value().accept(this);
}
void empty_visitor::visit(size_of_expression *expression)
void empty_visitor::visit(type_expression *expression)
{
expression->body().accept(this);
}
@@ -152,21 +152,21 @@ namespace boot
expression->operand().accept(this);
}
void empty_visitor::visit(basic_type_expression *)
void empty_visitor::visit(basic_type *)
{
}
void empty_visitor::visit(array_type_expression *expression)
void empty_visitor::visit(array_type *expression)
{
expression->base().accept(this);
}
void empty_visitor::visit(pointer_type_expression *expression)
void empty_visitor::visit(pointer_type *expression)
{
expression->base().accept(this);
}
void empty_visitor::visit(record_type_expression *expression)
void empty_visitor::visit(record_type *expression)
{
for (auto& field : expression->fields)
{
@@ -174,7 +174,7 @@ namespace boot
}
}
void empty_visitor::visit(union_type_expression *expression)
void empty_visitor::visit(union_type *expression)
{
for (auto& field : expression->fields)
{
@@ -250,115 +250,113 @@ namespace boot
{
}
type_expression::type_expression(const struct position position)
top_type::top_type(const struct position position)
: node(position)
{
}
basic_type_expression *type_expression::is_basic()
basic_type *top_type::is_basic()
{
return nullptr;
}
array_type_expression *type_expression::is_array()
array_type *top_type::is_array()
{
return nullptr;
}
record_type_expression *type_expression::is_record()
record_type *top_type::is_record()
{
return nullptr;
}
union_type_expression *type_expression::is_union()
union_type *top_type::is_union()
{
return nullptr;
}
pointer_type_expression *type_expression::is_pointer()
pointer_type *top_type::is_pointer()
{
return nullptr;
}
basic_type_expression::basic_type_expression(
const struct position position, const std::string& name)
: type_expression(position), m_name(name)
basic_type::basic_type(const struct position position, const std::string& name)
: top_type(position), m_name(name)
{
}
void basic_type_expression::accept(parser_visitor *visitor)
void basic_type::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
const std::string& basic_type_expression::base_name()
const std::string& basic_type::base_name()
{
return m_name;
}
basic_type_expression *basic_type_expression::is_basic()
basic_type *basic_type::is_basic()
{
return this;
}
array_type_expression::array_type_expression(const struct position position, type_expression *base,
array_type::array_type(const struct position position, top_type *base,
const std::uint32_t size)
: type_expression(position), m_base(base), size(size)
: top_type(position), m_base(base), size(size)
{
}
void array_type_expression::accept(parser_visitor *visitor)
void array_type::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
type_expression& array_type_expression::base()
top_type& array_type::base()
{
return *m_base;
}
array_type_expression *array_type_expression::is_array()
array_type*array_type::is_array()
{
return this;
}
array_type_expression::~array_type_expression()
array_type::~array_type()
{
delete m_base;
}
pointer_type_expression::pointer_type_expression(const struct position position, type_expression *base)
: type_expression(position), m_base(base)
pointer_type::pointer_type(const struct position position, top_type *base)
: top_type(position), m_base(base)
{
}
void pointer_type_expression::accept(parser_visitor *visitor)
void pointer_type::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
type_expression& pointer_type_expression::base()
top_type& pointer_type::base()
{
return *m_base;
}
pointer_type_expression *pointer_type_expression::is_pointer()
pointer_type *pointer_type::is_pointer()
{
return this;
}
pointer_type_expression::~pointer_type_expression()
pointer_type::~pointer_type()
{
delete m_base;
}
composite_type_expression::composite_type_expression(const struct position position,
fields_t&& fields)
: type_expression(position), fields(std::move(fields))
composite_type::composite_type(const struct position position, fields_t&& fields)
: top_type(position), fields(std::move(fields))
{
}
composite_type_expression::~composite_type_expression()
composite_type::~composite_type()
{
for (auto& field_declaration : fields)
{
@@ -366,40 +364,38 @@ namespace boot
}
}
record_type_expression::record_type_expression(const struct position position,
fields_t&& fields)
: composite_type_expression(position, std::move(fields))
record_type::record_type(const struct position position, fields_t&& fields)
: composite_type(position, std::move(fields))
{
}
void record_type_expression::accept(parser_visitor *visitor)
void record_type::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
record_type_expression *record_type_expression::is_record()
record_type *record_type::is_record()
{
return this;
}
union_type_expression::union_type_expression(const struct position position,
fields_t&& fields)
: composite_type_expression(position, std::move(fields))
union_type::union_type(const struct position position, fields_t&& fields)
: composite_type(position, std::move(fields))
{
}
union_type_expression *union_type_expression::is_union()
union_type *union_type::is_union()
{
return this;
}
void union_type_expression::accept(parser_visitor *visitor)
void union_type::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
variable_declaration::variable_declaration(const struct position position, const std::string& identifier,
const bool exported, type_expression *type)
const bool exported, top_type *type)
: definition(position, identifier, exported), m_type(type)
{
}
@@ -414,7 +410,7 @@ namespace boot
visitor->visit(this);
}
type_expression& variable_declaration::type()
top_type& variable_declaration::variable_type()
{
return *m_type;
}
@@ -446,7 +442,7 @@ namespace boot
}
procedure_definition::procedure_definition(const struct position position, const std::string& identifier,
const bool exported, type_expression *return_type)
const bool exported, top_type *return_type)
: definition(position, identifier, exported), m_return_type(return_type)
{
}
@@ -467,7 +463,7 @@ namespace boot
return this;
}
type_expression *procedure_definition::return_type()
top_type *procedure_definition::return_type()
{
return m_return_type;
}
@@ -485,7 +481,7 @@ namespace boot
}
type_definition::type_definition(const struct position position, const std::string& identifier,
const bool exported, type_expression *body)
const bool exported, top_type *body)
: definition(position, identifier, exported), m_body(body)
{
}
@@ -495,7 +491,7 @@ namespace boot
visitor->visit(this);
}
type_expression& type_definition::body()
top_type& type_definition::body()
{
return *m_body;
}
@@ -776,7 +772,7 @@ namespace boot
}
}
cast_expression::cast_expression(const struct position position, type_expression *target, expression *value)
cast_expression::cast_expression(const struct position position, top_type *target, expression *value)
: expression(position), m_target(target), m_value(value)
{
}
@@ -786,7 +782,7 @@ namespace boot
visitor->visit(this);
}
type_expression& cast_expression::target()
top_type& cast_expression::target()
{
return *m_target;
}
@@ -802,22 +798,22 @@ namespace boot
delete m_value;
}
size_of_expression::size_of_expression(const struct position position, type_expression *body)
type_expression::type_expression(const struct position position, top_type *body)
: expression(position), m_body(body)
{
}
void size_of_expression::accept(parser_visitor *visitor)
void type_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
type_expression& size_of_expression::body()
top_type& type_expression::body()
{
return *m_body;
}
size_of_expression::~size_of_expression()
type_expression::~type_expression()
{
delete m_body;
}
@@ -1014,6 +1010,8 @@ namespace boot
return "and";
case binary_operator::disjunction:
return "or";
case binary_operator::exclusive_disjunction:
return "xor";
}
__builtin_unreachable();
};

View File

@@ -122,6 +122,9 @@ nil {
and {
return yy::parser::make_AND(this->location);
}
xor {
return yy::parser::make_XOR(this->location);
}
or {
return yy::parser::make_OR(this->location);
}
@@ -134,9 +137,6 @@ return {
cast {
return yy::parser::make_CAST(this->location);
}
as {
return yy::parser::make_AS(this->location);
}
sizeof {
return yy::parser::make_SIZEOF(this->location);
}

View File

@@ -85,12 +85,12 @@ along with GCC; see the file COPYING3. If not see
%token CONST VAR PROCEDURE ARRAY OF TYPE RECORD POINTER TO UNION
%token BEGIN_BLOCK END_BLOCK EXTERN DEFER
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
%token AND OR NOT CAST AS SIZEOF
%token AND OR NOT CAST SIZEOF
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
%token PLUS MINUS MULTIPLICATION DIVISION REMAINDER
%token ASSIGNMENT COLON HAT AT NIL ARROW
%left OR AND
%left OR AND XOR
%left EQUALS NOT_EQUAL LESS_THAN GREATER_THAN LESS_EQUAL GREATER_EQUAL
%left PLUS MINUS
%left MULTIPLICATION DIVISION REMAINDER
@@ -101,7 +101,7 @@ along with GCC; see the file COPYING3. If not see
%type <elna::boot::variable_declaration *> variable_declaration;
%type <std::vector<elna::boot::variable_declaration *>> variable_declarations variable_part
formal_parameter_list;
%type <elna::boot::type_expression *> type_expression;
%type <elna::boot::top_type *> type_expression;
%type <elna::boot::expression *> expression operand unary;
%type <std::vector<elna::boot::expression *>> expressions actual_parameter_list;
%type <elna::boot::designator_expression *> designator_expression;
@@ -117,8 +117,8 @@ along with GCC; see the file COPYING3. If not see
%type <elna::boot::type_definition *> type_definition;
%type <std::vector<elna::boot::type_definition *>> type_definitions type_part;
%type <elna::boot::block *> block;
%type <std::pair<std::string, elna::boot::type_expression *>> field_declaration;
%type <std::vector<std::pair<std::string, elna::boot::type_expression *>>> field_list;
%type <std::pair<std::string, elna::boot::top_type *>> field_declaration;
%type <std::vector<std::pair<std::string, elna::boot::top_type *>>> field_list;
%type <std::vector<elna::boot::conditional_statements *>> elsif_statement_list;
%type <elna::boot::cast_expression *> cast_expression;
%type <elna::boot::defer_statement *> defer_statement;
@@ -189,7 +189,7 @@ call_expression: IDENTIFIER actual_parameter_list
$$ = new elna::boot::call_expression(elna::boot::make_position(@1), $1);
std::swap($$->arguments(), $2);
}
cast_expression: CAST LEFT_PAREN expression AS type_expression RIGHT_PAREN
cast_expression: CAST LEFT_PAREN expression COLON type_expression RIGHT_PAREN
{
$$ = new elna::boot::cast_expression(elna::boot::make_position(@1), $5, $3);
}
@@ -265,9 +265,9 @@ literal:
operand:
literal { $$ = $1; }
| designator_expression { $$ = $1; }
| SIZEOF LEFT_PAREN type_expression RIGHT_PAREN
| LEFT_PAREN type_expression RIGHT_PAREN
{
$$ = new elna::boot::size_of_expression(elna::boot::make_position(@1), $3);
$$ = new elna::boot::type_expression(elna::boot::make_position(@1), $2);
}
| cast_expression { $$ = $1; }
| call_expression { $$ = $1; }
@@ -339,6 +339,11 @@ expression:
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
elna::boot::binary_operator::disjunction);
}
| expression XOR expression
{
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
elna::boot::binary_operator::exclusive_disjunction);
}
unary:
AT operand
{
@@ -403,32 +408,32 @@ optional_statements:
field_declaration:
IDENTIFIER COLON type_expression { $$ = std::make_pair($1, $3); }
field_list:
field_declaration SEMICOLON field_list
field_declaration field_list
{
std::swap($$, $3);
std::swap($$, $2);
$$.emplace($$.cbegin(), $1);
}
| field_declaration { $$.emplace_back($1); }
type_expression:
ARRAY INTEGER OF type_expression
{
$$ = new elna::boot::array_type_expression(elna::boot::make_position(@1), $4, $2);
$$ = new elna::boot::array_type(elna::boot::make_position(@1), $4, $2);
}
| POINTER TO type_expression
{
$$ = new elna::boot::pointer_type_expression(elna::boot::make_position(@1), $3);
$$ = new elna::boot::pointer_type(elna::boot::make_position(@1), $3);
}
| RECORD field_list END_BLOCK
{
$$ = new elna::boot::record_type_expression(elna::boot::make_position(@1), std::move($2));
$$ = new elna::boot::record_type(elna::boot::make_position(@1), std::move($2));
}
| UNION field_list END_BLOCK
{
$$ = new elna::boot::union_type_expression(elna::boot::make_position(@1), std::move($2));
$$ = new elna::boot::union_type(elna::boot::make_position(@1), std::move($2));
}
| IDENTIFIER
{
$$ = new elna::boot::basic_type_expression(elna::boot::make_position(@1), $1);
$$ = new elna::boot::basic_type(elna::boot::make_position(@1), $1);
}
variable_declaration: identifier_definition COLON type_expression
{