Allow expressions in designators

This commit is contained in:
Eugen Wissner 2025-02-07 00:56:54 +01:00
parent 5e9b4259ca
commit 077de53c74
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
5 changed files with 51 additions and 40 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);
} }
@ -416,20 +421,21 @@ variable_declarations:
variable_part: variable_part:
/* no variable declarations */ {} /* no variable declarations */ {}
| VAR variable_declarations SEMICOLON { std::swap($$, $2); } | VAR variable_declarations SEMICOLON { std::swap($$, $2); }
constant_definition: IDENTIFIER EQUALS literal constant_definition: IDENTIFIER EQUALS literal SEMICOLON
{ {
$$ = new elna::boot::constant_definition(elna::boot::make_position(@1), $1, $3); $$ = new elna::boot::constant_definition(elna::boot::make_position(@1), $1, $3);
} }
constant_definitions: constant_definitions:
constant_definition COMMA constant_definitions constant_definition constant_definitions
{ {
std::swap($$, $3); std::swap($$, $2);
$$.emplace($$.cbegin(), std::move($1)); $$.emplace($$.cbegin(), std::move($1));
} }
| constant_definition { $$.emplace_back(std::move($1)); } | constant_definition { $$.emplace_back(std::move($1)); }
constant_part: constant_part:
/* no constant definitions */ {} /* no constant definitions */ {}
| CONST constant_definitions SEMICOLON { std::swap($$, $2); } | CONST {}
| CONST constant_definitions { std::swap($$, $2); }
type_definition: IDENTIFIER EQUALS type_expression type_definition: IDENTIFIER EQUALS type_expression
{ {
$$ = new elna::boot::type_definition(elna::boot::make_position(@1), $1, $3); $$ = new elna::boot::type_definition(elna::boot::make_position(@1), $1, $3);

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

@ -1,20 +1,20 @@
const const
SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2, SEEK_SET = 0; SEEK_CUR = 1; SEEK_END = 2;
TOKEN_IDENTIFIER = 1, TOKEN_IF = 2, TOKEN_THEN = 3, TOKEN_ELSE = 4, TOKEN_ELSIF = 5, TOKEN_IDENTIFIER = 1; TOKEN_IF = 2; TOKEN_THEN = 3; TOKEN_ELSE = 4; TOKEN_ELSIF = 5;
TOKEN_WHILE = 6, TOKEN_DO = 7, TOKEN_PROC = 8, TOKEN_BEGIN = 9, TOKEN_END = 10, TOKEN_WHILE = 6; TOKEN_DO = 7; TOKEN_PROC = 8; TOKEN_BEGIN = 9; TOKEN_END = 10;
TOKEN_EXTERN = 11, TOKEN_CONST = 12, TOKEN_VAR = 13, TOKEN_ARRAY = 14, TOKEN_OF = 15, TOKEN_EXTERN = 11; TOKEN_CONST = 12; TOKEN_VAR = 13; TOKEN_ARRAY = 14; TOKEN_OF = 15;
TOKEN_TYPE = 16, TOKEN_RECORD = 17, TOKEN_UNION = 18, TOKEN_POINTER = 19, TOKEN_TO = 20, TOKEN_TYPE = 16; TOKEN_RECORD = 17; TOKEN_UNION = 18; TOKEN_POINTER = 19; TOKEN_TO = 20;
TOKEN_BOOLEAN = 21, TOKEN_NIL = 22, TOKEN_AND = 23, TOKEN_OR = 24, TOKEN_NOT = 25, TOKEN_BOOLEAN = 21; TOKEN_NIL = 22; TOKEN_AND = 23; TOKEN_OR = 24; TOKEN_NOT = 25;
TOKEN_RETURN = 26, TOKEN_CAST = 27, TOKEN_AS = 28, TOKEN_SIZEOF = 29, TOKEN_RETURN = 26; TOKEN_CAST = 27; TOKEN_AS = 28; TOKEN_SIZEOF = 29;
TOKEN_LEFT_PAREN = 30, TOKEN_RIGHT_PAREN = 31, TOKEN_LEFT_SQUARE = 32, TOKEN_LEFT_PAREN = 30; TOKEN_RIGHT_PAREN = 31; TOKEN_LEFT_SQUARE = 32;
TOKEN_RIGHT_SQUARE = 33, TOKEN_GREATER_EQUAL = 34, TOKEN_LESS_EQUAL = 35, TOKEN_RIGHT_SQUARE = 33; TOKEN_GREATER_EQUAL = 34; TOKEN_LESS_EQUAL = 35;
TOKEN_GREATER_THAN = 36, TOKEN_LESS_THAN = 37, TOKEN_NOT_EQUAL = 38, TOKEN_EQUAL = 39, TOKEN_GREATER_THAN = 36; TOKEN_LESS_THAN = 37; TOKEN_NOT_EQUAL = 38; TOKEN_EQUAL = 39;
TOKEN_SEMICOLON = 40, TOKEN_DOT = 41, TOKEN_COMMA = 42, TOKEN_SEMICOLON = 40; TOKEN_DOT = 41; TOKEN_COMMA = 42;
TOKEN_PLUS = 43, TOKEN_MINUS = 44, TOKEN_MULTIPLICATION = 45, TOKEN_DIVISION = 46, TOKEN_PLUS = 43; TOKEN_MINUS = 44; TOKEN_MULTIPLICATION = 45; TOKEN_DIVISION = 46;
TOKEN_REMAINDER = 47, TOKEN_ASSIGNMENT = 48, TOKEN_COLON = 49, TOKEN_HAT = 50, TOKEN_REMAINDER = 47; TOKEN_ASSIGNMENT = 48; TOKEN_COLON = 49; TOKEN_HAT = 50;
TOKEN_AT = 51, TOKEN_COMMENT = 52, TOKEN_INTEGER = 53, TOKEN_WORD = 54, TOKEN_AT = 51; TOKEN_COMMENT = 52; TOKEN_INTEGER = 53; TOKEN_WORD = 54;
TOKEN_CHARACTER = 55, TOKEN_STRING = 56; TOKEN_CHARACTER = 55; TOKEN_STRING = 56;
type type
Position = record Position = record
@ -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