Allow declaring type synonyms

This commit is contained in:
2025-01-07 14:37:30 +01:00
parent ed1bb621d6
commit 2d61828903
13 changed files with 274 additions and 97 deletions

View File

@@ -63,7 +63,7 @@
%token <std::string> STRING "string"
%token <bool> BOOLEAN
%token IF WHILE DO
%token CONST VAR PROCEDURE ARRAY OF
%token CONST VAR PROCEDURE ARRAY OF TYPE
%token BEGIN_BLOCK END_BLOCK
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
@@ -87,6 +87,7 @@
%type <elna::source::expression *> expression pointer summand factor address comparand;
%type <std::vector<elna::source::expression *>> expressions actual_parameter_list;
%type <elna::source::variable_expression *> variable_expression;
%type <elna::source::designator_expression *> designator_expression;
%type <elna::source::compound_statement *> compound_statement;
%type <elna::source::assign_statement *> assign_statement;
%type <elna::source::call_statement *> call_statement;
@@ -95,26 +96,31 @@
%type <elna::source::statement *> statement;
%type <std::vector<elna::source::statement *>> statements optional_statements;
%type <elna::source::procedure_definition *> procedure_definition;
%type <std::vector<elna::source::procedure_definition *>> procedure_definitions
procedure_definition_part;
%type <std::vector<elna::source::procedure_definition *>> procedure_definitions procedure_definition_part;
%type <elna::source::type_definition *> type_definition;
%type <std::vector<elna::source::type_definition *>> type_definitions type_definition_part;
%type <elna::source::block *> block;
%%
program: constant_definition_part procedure_definition_part variable_declaration_part statement DOT
program:
type_definition_part constant_definition_part procedure_definition_part variable_declaration_part statement DOT
{
std::vector<elna::source::definition *> definitions($1.size() + $2.size());
std::vector<elna::source::definition *> definitions($1.size() + $2.size() + $3.size());
std::vector<elna::source::definition *>::iterator definition = definitions.begin();
for (auto& constant : $1)
for (auto& type : $1)
{
*definition++ = type;
}
for (auto& constant : $2)
{
*definition++ = constant;
}
for (auto& procedure : $2)
for (auto& procedure : $3)
{
*definition++ = procedure;
}
driver.tree = std::make_unique<elna::source::program>(elna::source::position{},
std::move(definitions), std::move($3),
std::move($4));
std::move(definitions), std::move($4), std::move($5));
}
block: constant_definition_part variable_declaration_part statement
{
@@ -170,7 +176,7 @@ compound_statement: BEGIN_BLOCK optional_statements END_BLOCK
$$ = new elna::source::compound_statement(elna::source::make_position(@1));
std::swap($$->statements(), $2);
}
assign_statement: IDENTIFIER ASSIGNMENT expression
assign_statement: designator_expression ASSIGNMENT expression
{
$$ = new elna::source::assign_statement(elna::source::make_position(@1), $1, $3);
}
@@ -201,7 +207,7 @@ pointer:
| boolean_literal { $$ = $1; }
| character_literal { $$ = $1; }
| string_literal { $$ = $1; }
| variable_expression { $$ = $1; }
| designator_expression { $$ = $1; }
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }
summand:
factor { $$ = std::move($1); }
@@ -280,8 +286,14 @@ expressions:
$$.emplace($$.cbegin(), $1);
}
| expression { $$.emplace_back(std::move($1)); }
variable_expression: IDENTIFIER
{ $$ = new elna::source::variable_expression(elna::source::make_position(@1), $1); }
variable_expression:
IDENTIFIER { $$ = new elna::source::variable_expression(elna::source::make_position(@1), $1); }
designator_expression:
variable_expression LEFT_SQUARE expression RIGHT_SQUARE
{
$$ = new elna::source::array_access_expression(elna::source::make_position(@1), $1, $3);
}
| variable_expression { $$ = $1; }
statement:
compound_statement { $$ = $1; }
| assign_statement { $$ = $1; }
@@ -324,9 +336,8 @@ variable_declaration_part:
| VAR variable_declarations SEMICOLON { std::swap($$, $2); }
constant_definition: IDENTIFIER EQUALS integer_literal
{
$$ = new elna::source::constant_definition(elna::source::make_position(@1),
$1, $3);
};
$$ = new elna::source::constant_definition(elna::source::make_position(@1), $1, $3);
}
constant_definitions:
constant_definition COMMA constant_definitions
{
@@ -336,7 +347,21 @@ constant_definitions:
| constant_definition { $$.emplace_back(std::move($1)); }
constant_definition_part:
/* no constant definitions */ {}
| CONST constant_definitions SEMICOLON { std::swap($$, $2); };
| CONST constant_definitions SEMICOLON { std::swap($$, $2); }
type_definition: IDENTIFIER EQUALS type_expression
{
$$ = new elna::source::type_definition(elna::source::make_position(@1), $1, $3);
}
type_definitions:
type_definition COMMA type_definitions
{
std::swap($$, $3);
$$.emplace($$.cbegin(), std::move($1));
}
| type_definition { $$.emplace_back(std::move($1)); }
type_definition_part:
/* no type definitions */ {}
| TYPE type_definitions SEMICOLON { std::swap($$, $2); }
formal_parameter_list:
LEFT_PAREN RIGHT_PAREN {}
| LEFT_PAREN variable_declarations RIGHT_PAREN { std::swap($$, $2); }