Allow expressions in designators

This commit is contained in:
Eugen Wissner 2025-02-07 00:56:54 +01:00
parent 5e9b4259ca
commit ef46796d7c
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
5 changed files with 31 additions and 21 deletions

View File

@ -576,7 +576,7 @@ namespace boot
} }
array_access_expression::array_access_expression(const struct position position, array_access_expression::array_access_expression(const struct position position,
designator_expression *base, expression *index) expression *base, expression *index)
: designator_expression(position), m_base(base), m_index(index) : designator_expression(position), m_base(base), m_index(index)
{ {
} }
@ -591,7 +591,7 @@ namespace boot
return *m_index; return *m_index;
} }
designator_expression& array_access_expression::base() expression& array_access_expression::base()
{ {
return *m_base; return *m_base;
} }
@ -603,6 +603,7 @@ namespace boot
array_access_expression::~array_access_expression() array_access_expression::~array_access_expression()
{ {
delete m_index;
delete m_base; delete m_base;
} }
@ -638,7 +639,7 @@ namespace boot
} }
dereference_expression::dereference_expression(const struct position position, dereference_expression::dereference_expression(const struct position position,
designator_expression *base) expression *base)
: designator_expression(position), m_base(base) : designator_expression(position), m_base(base)
{ {
} }
@ -648,7 +649,7 @@ namespace boot
visitor->visit(this); visitor->visit(this);
} }
designator_expression& dereference_expression::base() expression& dereference_expression::base()
{ {
return *m_base; return *m_base;
} }

View File

@ -328,6 +328,11 @@ unary:
$$ = new elna::boot::unary_expression(elna::boot::make_position(@1), $2, $$ = new elna::boot::unary_expression(elna::boot::make_position(@1), $2,
elna::boot::unary_operator::negation); elna::boot::unary_operator::negation);
} }
| MINUS operand
{
$$ = new elna::boot::unary_expression(elna::boot::make_position(@1), $2,
elna::boot::unary_operator::minus);
}
| operand { $$ = $1; } | operand { $$ = $1; }
expressions: expressions:
expression COMMA expressions expression COMMA expressions
@ -337,7 +342,7 @@ expressions:
} }
| expression { $$.emplace_back(std::move($1)); } | expression { $$.emplace_back(std::move($1)); }
designator_expression: designator_expression:
designator_expression LEFT_SQUARE expression RIGHT_SQUARE operand LEFT_SQUARE expression RIGHT_SQUARE
{ {
$$ = new elna::boot::array_access_expression(elna::boot::make_position(@1), $1, $3); $$ = new elna::boot::array_access_expression(elna::boot::make_position(@1), $1, $3);
} }
@ -345,7 +350,7 @@ designator_expression:
{ {
$$ = new elna::boot::field_access_expression(elna::boot::make_position(@2), $1, $3); $$ = new elna::boot::field_access_expression(elna::boot::make_position(@2), $1, $3);
} }
| designator_expression HAT | operand HAT
{ {
$$ = new elna::boot::dereference_expression(elna::boot::make_position(@1), $1); $$ = new elna::boot::dereference_expression(elna::boot::make_position(@1), $1);
} }

View File

@ -412,6 +412,9 @@ namespace gcc
this->current_expression = build1_loc(get_location(&expression->position()), TRUTH_NOT_EXPR, this->current_expression = build1_loc(get_location(&expression->position()), TRUTH_NOT_EXPR,
boolean_type_node, this->current_expression); boolean_type_node, this->current_expression);
break; break;
case boot::unary_operator::minus:
this->current_expression = fold_build1(NEGATE_EXPR, TREE_TYPE(this->current_expression),
this->current_expression);
} }
} }

View File

@ -33,7 +33,8 @@ namespace boot
enum class unary_operator enum class unary_operator
{ {
reference, reference,
negation negation,
minus
}; };
class variable_declaration; class variable_declaration;
@ -528,14 +529,14 @@ namespace boot
class array_access_expression : public designator_expression class array_access_expression : public designator_expression
{ {
designator_expression *m_base; expression *m_base;
expression *m_index; expression *m_index;
public: public:
array_access_expression(const struct position position, designator_expression *base, expression *index); array_access_expression(const struct position position, expression *base, expression *index);
virtual void accept(parser_visitor *visitor) override; virtual void accept(parser_visitor *visitor) override;
designator_expression& base(); expression& base();
expression& index(); expression& index();
array_access_expression *is_array_access() override; array_access_expression *is_array_access() override;
@ -563,13 +564,13 @@ namespace boot
class dereference_expression : public designator_expression class dereference_expression : public designator_expression
{ {
designator_expression *m_base; expression *m_base;
public: public:
dereference_expression(const struct position position, designator_expression *base); dereference_expression(const struct position position, expression *base);
virtual void accept(parser_visitor *visitor) override; virtual void accept(parser_visitor *visitor) override;
designator_expression& base(); expression& base();
dereference_expression *is_dereference() override; dereference_expression *is_dereference() override;

View File

@ -73,7 +73,6 @@ proc malloc(size: Word): pointer to Byte; extern;
proc free(ptr: pointer to Byte); extern; proc free(ptr: pointer to Byte); extern;
proc calloc(nmemb: Word, size: Word): pointer to Byte; extern; proc calloc(nmemb: Word, size: Word): pointer to Byte; extern;
proc realloc(ptr: pointer to Byte, size: Word): pointer to Byte; extern; proc realloc(ptr: pointer to Byte, size: Word): pointer to Byte; extern;
proc reallocarray(ptr: pointer to Byte, n: Word, size: Word): pointer to Byte; extern;
proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern; proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern;
@ -91,6 +90,11 @@ proc exit(code: Int); extern;
(* (*
Standard procedures. Standard procedures.
*) *)
proc reallocarray(ptr: pointer to Byte, n: Word, size: Word): pointer to Byte;
begin
return realloc(ptr, n * size)
end;
proc write_s(value: String); proc write_s(value: String);
begin begin
write(0, value, strlen(value)) write(0, value, strlen(value))
@ -128,7 +132,7 @@ begin
buffer[n] := cast(cast('0' as Int) + digit as Char); buffer[n] := cast(cast('0' as Int) + digit as Char);
n := n - 1 n := n - 1
end; end;
while n < 10 do while n < 9 do
n := n + 1; n := n + 1;
write_c(buffer[n]) write_c(buffer[n])
end end
@ -255,7 +259,6 @@ end;
proc lex_comment(input: pointer to Char): pointer to Char; proc lex_comment(input: pointer to Char): pointer to Char;
var var
current: pointer to Char,
next: pointer to Char; next: pointer to Char;
begin begin
while input^ <> '\0' do while input^ <> '\0' do
@ -264,7 +267,7 @@ begin
if input^ = '*' and next^ = ')' then if input^ = '*' and next^ = ')' then
return next + 1 return next + 1
end; end;
input := input + 1 input := next
end; end;
return nil return nil
end; end;
@ -286,16 +289,13 @@ end;
proc lex_string(input: pointer to Char, current_token: pointer to Token): pointer to Char; proc lex_string(input: pointer to Char, current_token: pointer to Token): pointer to Char;
var var
token_end: pointer to Char, token_end: pointer to Char,
previous: pointer to Char,
constructed_string: pointer to Char, constructed_string: pointer to Char,
token_length: Word, token_length: Word,
is_valid: Bool; is_valid: Bool;
begin begin
token_end := input; token_end := input;
previous := input - 1;
while token_end^ <> '\0' and not (previous^ <> '\\' and token_end^ = '"') do while token_end^ <> '\0' and not ((token_end - 1)^ <> '\\' and token_end^ = '"') do
previous := token_end;
token_end := token_end + 1 token_end := token_end + 1
end; end;
if token_end^ <> '\"' then if token_end^ <> '\"' then