2025-02-09 12:30:31 +01:00
|
|
|
/* Syntax analyzer.
|
|
|
|
Copyright (C) 2025 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
GCC is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
GCC is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GCC; see the file COPYING3. If not see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
%require "3.4"
|
2025-01-31 09:46:17 +01:00
|
|
|
%language "c++"
|
|
|
|
|
|
|
|
%code requires {
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include "elna/boot/driver.h"
|
|
|
|
|
|
|
|
#if !defined(yyFlexLexerOnce)
|
|
|
|
#include <FlexLexer.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace elna
|
|
|
|
{
|
|
|
|
namespace boot
|
|
|
|
{
|
|
|
|
class lexer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
%code provides {
|
|
|
|
namespace elna
|
|
|
|
{
|
|
|
|
namespace boot
|
|
|
|
{
|
|
|
|
|
|
|
|
class lexer: public yyFlexLexer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
yy::location location;
|
|
|
|
|
|
|
|
lexer(std::istream& arg_yyin)
|
|
|
|
: yyFlexLexer(&arg_yyin)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
yy::parser::symbol_type lex(elna::boot::driver& driver);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
%define api.token.raw
|
|
|
|
%define api.token.constructor
|
|
|
|
%define api.value.type variant
|
|
|
|
|
|
|
|
%parse-param {elna::boot::lexer& lexer}
|
|
|
|
%param {elna::boot::driver& driver}
|
|
|
|
%locations
|
|
|
|
|
|
|
|
%header
|
|
|
|
|
|
|
|
%code {
|
|
|
|
#define yylex lexer.lex
|
|
|
|
}
|
|
|
|
%start program;
|
|
|
|
|
|
|
|
%token <std::string> IDENTIFIER "identifier"
|
|
|
|
%token <std::int32_t> INTEGER "integer"
|
|
|
|
%token <std::uint32_t> WORD "word"
|
|
|
|
%token <float> FLOAT "float"
|
|
|
|
%token <std::string> CHARACTER "character"
|
|
|
|
%token <std::string> STRING "string"
|
|
|
|
%token <bool> BOOLEAN
|
|
|
|
%token IF WHILE DO THEN ELSE ELSIF RETURN
|
2025-02-16 07:53:31 +01:00
|
|
|
%token CONST VAR PROCEDURE TYPE RECORD UNION
|
2025-02-07 22:12:59 +01:00
|
|
|
%token BEGIN_BLOCK END_BLOCK EXTERN DEFER
|
2025-01-31 09:46:17 +01:00
|
|
|
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
|
2025-02-15 00:38:46 +01:00
|
|
|
%token AND OR NOT CAST SHIFT_LEFT SHIFT_RIGHT
|
2025-01-31 09:46:17 +01:00
|
|
|
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
|
|
|
|
%token PLUS MINUS MULTIPLICATION DIVISION REMAINDER
|
2025-02-12 13:32:59 +01:00
|
|
|
%token ASSIGNMENT COLON HAT AT NIL ARROW
|
2025-01-31 09:46:17 +01:00
|
|
|
|
2025-02-14 08:05:03 +01:00
|
|
|
%left OR AND XOR
|
2025-01-31 09:46:17 +01:00
|
|
|
%left EQUALS NOT_EQUAL LESS_THAN GREATER_THAN LESS_EQUAL GREATER_EQUAL
|
|
|
|
%left PLUS MINUS
|
|
|
|
%left MULTIPLICATION DIVISION REMAINDER
|
|
|
|
|
|
|
|
%type <elna::boot::literal *> literal;
|
|
|
|
%type <elna::boot::constant_definition *> constant_definition;
|
|
|
|
%type <std::vector<elna::boot::constant_definition *>> constant_part constant_definitions;
|
2025-02-15 10:26:04 +01:00
|
|
|
%type <std::vector<elna::boot::variable_declaration *>> variable_declarations variable_part variable_declaration
|
|
|
|
formal_parameters formal_parameter_list;
|
|
|
|
%type <elna::boot::variable_declaration *> formal_parameter
|
|
|
|
%type <std::shared_ptr<elna::boot::top_type>> type_expression;
|
2025-01-31 09:46:17 +01:00
|
|
|
%type <elna::boot::expression *> expression operand unary;
|
|
|
|
%type <std::vector<elna::boot::expression *>> expressions actual_parameter_list;
|
|
|
|
%type <elna::boot::designator_expression *> designator_expression;
|
|
|
|
%type <elna::boot::assign_statement *> assign_statement;
|
|
|
|
%type <elna::boot::call_expression *> call_expression;
|
|
|
|
%type <elna::boot::while_statement *> while_statement;
|
|
|
|
%type <elna::boot::if_statement *> if_statement;
|
|
|
|
%type <elna::boot::return_statement *> return_statement;
|
|
|
|
%type <elna::boot::statement *> statement;
|
|
|
|
%type <std::vector<elna::boot::statement *>> statements optional_statements;
|
2025-02-07 22:12:59 +01:00
|
|
|
%type <elna::boot::procedure_definition *> procedure_definition procedure_heading;
|
2025-01-31 09:46:17 +01:00
|
|
|
%type <std::vector<elna::boot::procedure_definition *>> procedure_definitions procedure_part;
|
|
|
|
%type <elna::boot::type_definition *> type_definition;
|
|
|
|
%type <std::vector<elna::boot::type_definition *>> type_definitions type_part;
|
|
|
|
%type <elna::boot::block *> block;
|
2025-02-15 10:26:04 +01:00
|
|
|
%type <elna::boot::field_t> field_declaration;
|
2025-02-16 07:53:31 +01:00
|
|
|
%type <std::vector<std::pair<std::string, std::shared_ptr<elna::boot::top_type>>>> optional_fields fields;
|
2025-01-31 09:46:17 +01:00
|
|
|
%type <std::vector<elna::boot::conditional_statements *>> elsif_statement_list;
|
|
|
|
%type <elna::boot::cast_expression *> cast_expression;
|
2025-02-07 22:12:59 +01:00
|
|
|
%type <elna::boot::defer_statement *> defer_statement;
|
|
|
|
%type <std::pair<std::string, bool>> identifier_definition;
|
2025-02-15 10:26:04 +01:00
|
|
|
%type <std::vector<std::pair<std::string, bool>>> identifier_definitions;
|
2025-01-31 09:46:17 +01:00
|
|
|
%%
|
|
|
|
program:
|
2025-02-03 23:04:00 +01:00
|
|
|
constant_part type_part variable_part procedure_part BEGIN_BLOCK optional_statements END_BLOCK DOT
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-03 23:04:00 +01:00
|
|
|
auto tree = new elna::boot::program(elna::boot::make_position(@5));
|
|
|
|
|
|
|
|
std::swap(tree->constants, $1);
|
|
|
|
std::swap(tree->types , $2);
|
2025-02-02 08:22:40 +01:00
|
|
|
std::swap(tree->variables, $3);
|
2025-02-03 23:04:00 +01:00
|
|
|
std::swap(tree->procedures, $4);
|
2025-02-02 08:22:40 +01:00
|
|
|
std::swap(tree->body, $6);
|
2025-01-31 09:46:17 +01:00
|
|
|
|
|
|
|
driver.tree.reset(tree);
|
|
|
|
}
|
|
|
|
block: constant_part variable_part BEGIN_BLOCK optional_statements END_BLOCK
|
|
|
|
{
|
2025-02-02 08:22:40 +01:00
|
|
|
$$ = new elna::boot::block(elna::boot::make_position(@3));
|
2025-01-31 09:46:17 +01:00
|
|
|
|
2025-02-02 08:22:40 +01:00
|
|
|
std::swap($$->constants, $1);
|
|
|
|
std::swap($$->variables, $2);
|
|
|
|
std::swap($$->body, $4);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-07 22:12:59 +01:00
|
|
|
identifier_definition:
|
|
|
|
IDENTIFIER MULTIPLICATION
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-07 22:12:59 +01:00
|
|
|
$$ = std::make_pair($1, true);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-07 22:12:59 +01:00
|
|
|
| IDENTIFIER
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-07 22:12:59 +01:00
|
|
|
$$ = std::make_pair($1, false);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-15 10:26:04 +01:00
|
|
|
identifier_definitions:
|
|
|
|
identifier_definition COMMA identifier_definitions
|
|
|
|
{
|
|
|
|
std::swap($$, $3);
|
|
|
|
$$.emplace($$.cbegin(), $1);
|
|
|
|
}
|
|
|
|
| identifier_definition { $$.emplace_back(std::move($1)); }
|
2025-02-07 22:12:59 +01:00
|
|
|
procedure_heading:
|
|
|
|
PROCEDURE identifier_definition formal_parameter_list SEMICOLON
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
|
|
|
$$ = new elna::boot::procedure_definition(elna::boot::make_position(@1),
|
2025-02-07 22:12:59 +01:00
|
|
|
$2.first, $2.second);
|
|
|
|
std::swap($3, $$->parameters);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-12 13:32:59 +01:00
|
|
|
| PROCEDURE identifier_definition formal_parameter_list ARROW type_expression SEMICOLON
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
|
|
|
$$ = new elna::boot::procedure_definition(elna::boot::make_position(@1),
|
2025-02-07 22:12:59 +01:00
|
|
|
$2.first, $2.second, $5);
|
|
|
|
std::swap($3, $$->parameters);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-07 22:12:59 +01:00
|
|
|
procedure_definition:
|
|
|
|
procedure_heading block { $$ = $1->add_body($2); }
|
|
|
|
| procedure_heading EXTERN { $$ = $1; }
|
2025-01-31 09:46:17 +01:00
|
|
|
procedure_definitions:
|
|
|
|
procedure_definition procedure_definitions
|
|
|
|
{
|
|
|
|
std::swap($$, $2);
|
|
|
|
$$.emplace($$.cbegin(), std::move($1));
|
|
|
|
}
|
|
|
|
| procedure_definition { $$.emplace_back(std::move($1)); }
|
|
|
|
procedure_part:
|
|
|
|
/* no procedure definitions */ {}
|
|
|
|
| procedure_definitions { std::swap($$, $1); }
|
|
|
|
assign_statement: designator_expression ASSIGNMENT expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::assign_statement(elna::boot::make_position(@1), $1, $3);
|
|
|
|
}
|
|
|
|
call_expression: IDENTIFIER actual_parameter_list
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::call_expression(elna::boot::make_position(@1), $1);
|
|
|
|
std::swap($$->arguments(), $2);
|
|
|
|
}
|
2025-02-14 08:05:03 +01:00
|
|
|
cast_expression: CAST LEFT_PAREN expression COLON type_expression RIGHT_PAREN
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
|
|
|
$$ = new elna::boot::cast_expression(elna::boot::make_position(@1), $5, $3);
|
|
|
|
}
|
|
|
|
while_statement: WHILE expression DO optional_statements END_BLOCK
|
|
|
|
{
|
|
|
|
auto body = new elna::boot::conditional_statements($2);
|
|
|
|
std::swap($4, body->statements);
|
|
|
|
$$ = new elna::boot::while_statement(elna::boot::make_position(@1), body);
|
|
|
|
}
|
|
|
|
elsif_statement_list:
|
|
|
|
ELSIF expression THEN optional_statements elsif_statement_list
|
|
|
|
{
|
|
|
|
elna::boot::conditional_statements *branch = new elna::boot::conditional_statements($2);
|
|
|
|
std::swap(branch->statements, $4);
|
|
|
|
std::swap($5, $$);
|
|
|
|
$$.emplace($$.begin(), branch);
|
|
|
|
}
|
|
|
|
| {}
|
|
|
|
if_statement:
|
|
|
|
IF expression THEN optional_statements elsif_statement_list END_BLOCK
|
|
|
|
{
|
|
|
|
auto then = new elna::boot::conditional_statements($2);
|
|
|
|
std::swap($4, then->statements);
|
|
|
|
$$ = new elna::boot::if_statement(elna::boot::make_position(@1), then);
|
|
|
|
std::swap($5, $$->branches);
|
|
|
|
}
|
|
|
|
| IF expression THEN optional_statements elsif_statement_list ELSE optional_statements END_BLOCK
|
|
|
|
{
|
|
|
|
auto then = new elna::boot::conditional_statements($2);
|
|
|
|
std::swap($4, then->statements);
|
|
|
|
auto _else = new std::vector<elna::boot::statement *>(std::move($7));
|
|
|
|
$$ = new elna::boot::if_statement(elna::boot::make_position(@1), then, _else);
|
|
|
|
std::swap($5, $$->branches);
|
|
|
|
}
|
2025-02-07 22:12:59 +01:00
|
|
|
return_statement: RETURN expression
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
|
|
|
$$ = new elna::boot::return_statement(elna::boot::make_position(@1), $2);
|
|
|
|
}
|
2025-02-07 22:12:59 +01:00
|
|
|
defer_statement: DEFER optional_statements END_BLOCK
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::defer_statement(elna::boot::make_position(@1));
|
|
|
|
std::swap($2, $$->statements);
|
|
|
|
}
|
2025-01-31 09:46:17 +01:00
|
|
|
literal:
|
|
|
|
INTEGER
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::number_literal<std::int32_t>(elna::boot::make_position(@1), $1);
|
|
|
|
}
|
|
|
|
| WORD
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::number_literal<std::uint32_t>(elna::boot::make_position(@1), $1);
|
|
|
|
}
|
|
|
|
| FLOAT
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::number_literal<double>(elna::boot::make_position(@1), $1);
|
|
|
|
}
|
|
|
|
| BOOLEAN
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::number_literal<bool>(elna::boot::make_position(@1), $1);
|
|
|
|
}
|
|
|
|
| CHARACTER
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::number_literal<unsigned char>(elna::boot::make_position(@1), $1.at(0));
|
|
|
|
}
|
|
|
|
| NIL
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::number_literal<std::nullptr_t>(elna::boot::make_position(@1), nullptr);
|
|
|
|
}
|
|
|
|
| STRING
|
|
|
|
{
|
2025-02-07 22:12:59 +01:00
|
|
|
$$ = new elna::boot::number_literal<std::string>(elna::boot::make_position(@1), $1);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
|
|
|
operand:
|
|
|
|
literal { $$ = $1; }
|
|
|
|
| designator_expression { $$ = $1; }
|
2025-02-14 08:05:03 +01:00
|
|
|
| LEFT_PAREN type_expression RIGHT_PAREN
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-14 08:05:03 +01:00
|
|
|
$$ = new elna::boot::type_expression(elna::boot::make_position(@1), $2);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
|
|
|
| cast_expression { $$ = $1; }
|
|
|
|
| call_expression { $$ = $1; }
|
|
|
|
| LEFT_PAREN expression RIGHT_PAREN { $$ = $2; }
|
|
|
|
expression:
|
|
|
|
unary { $$ = $1; }
|
|
|
|
| expression MULTIPLICATION expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::multiplication);
|
|
|
|
}
|
|
|
|
| expression DIVISION expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::division);
|
|
|
|
}
|
|
|
|
| expression REMAINDER expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::remainder);
|
|
|
|
}
|
|
|
|
| expression PLUS expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::sum);
|
|
|
|
}
|
|
|
|
| expression MINUS expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::subtraction);
|
|
|
|
}
|
|
|
|
| expression EQUALS expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::equals);
|
|
|
|
}
|
|
|
|
| expression NOT_EQUAL expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::not_equals);
|
|
|
|
}
|
|
|
|
| expression LESS_THAN expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::less);
|
|
|
|
}
|
|
|
|
| expression GREATER_THAN expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::greater);
|
|
|
|
}
|
|
|
|
| expression LESS_EQUAL expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::less_equal);
|
|
|
|
}
|
|
|
|
| expression GREATER_EQUAL expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::greater_equal);
|
|
|
|
}
|
|
|
|
| expression AND expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::conjunction);
|
|
|
|
}
|
|
|
|
| expression OR expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::disjunction);
|
|
|
|
}
|
2025-02-14 08:05:03 +01:00
|
|
|
| expression XOR expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::exclusive_disjunction);
|
|
|
|
}
|
2025-02-15 00:38:46 +01:00
|
|
|
| expression SHIFT_LEFT expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::shift_left);
|
|
|
|
}
|
|
|
|
| expression SHIFT_RIGHT expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::binary_expression(elna::boot::make_position(@2), $1, $3,
|
|
|
|
elna::boot::binary_operator::shift_right);
|
|
|
|
}
|
2025-01-31 09:46:17 +01:00
|
|
|
unary:
|
|
|
|
AT operand
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::unary_expression(elna::boot::make_position(@1), $2,
|
|
|
|
elna::boot::unary_operator::reference);
|
|
|
|
}
|
|
|
|
| NOT operand
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::unary_expression(elna::boot::make_position(@1), $2,
|
|
|
|
elna::boot::unary_operator::negation);
|
|
|
|
}
|
2025-02-07 00:56:54 +01:00
|
|
|
| MINUS operand
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::unary_expression(elna::boot::make_position(@1), $2,
|
|
|
|
elna::boot::unary_operator::minus);
|
|
|
|
}
|
2025-01-31 09:46:17 +01:00
|
|
|
| operand { $$ = $1; }
|
|
|
|
expressions:
|
|
|
|
expression COMMA expressions
|
|
|
|
{
|
|
|
|
std::swap($$, $3);
|
|
|
|
$$.emplace($$.cbegin(), $1);
|
|
|
|
}
|
|
|
|
| expression { $$.emplace_back(std::move($1)); }
|
|
|
|
designator_expression:
|
2025-02-07 00:56:54 +01:00
|
|
|
operand LEFT_SQUARE expression RIGHT_SQUARE
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-12 20:47:47 +01:00
|
|
|
$$ = new elna::boot::array_access_expression(elna::boot::make_position(@2), $1, $3);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-07 22:12:59 +01:00
|
|
|
| operand DOT IDENTIFIER
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
|
|
|
$$ = new elna::boot::field_access_expression(elna::boot::make_position(@2), $1, $3);
|
|
|
|
}
|
2025-02-07 00:56:54 +01:00
|
|
|
| operand HAT
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
|
|
|
$$ = new elna::boot::dereference_expression(elna::boot::make_position(@1), $1);
|
|
|
|
}
|
|
|
|
| IDENTIFIER
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::variable_expression(elna::boot::make_position(@1), $1);
|
|
|
|
}
|
|
|
|
statement:
|
|
|
|
assign_statement { $$ = $1; }
|
|
|
|
| while_statement { $$ = $1; }
|
|
|
|
| if_statement { $$ = $1; }
|
|
|
|
| return_statement { $$ = $1; }
|
|
|
|
| call_expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::call_statement(elna::boot::make_position(@1), $1);
|
|
|
|
}
|
2025-02-07 22:12:59 +01:00
|
|
|
| defer_statement { $$ = $1; }
|
2025-01-31 09:46:17 +01:00
|
|
|
statements:
|
2025-02-16 07:53:31 +01:00
|
|
|
statement statements
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-16 07:53:31 +01:00
|
|
|
std::swap($$, $2);
|
2025-01-31 09:46:17 +01:00
|
|
|
$$.emplace($$.cbegin(), $1);
|
|
|
|
}
|
|
|
|
| statement { $$.push_back($1); }
|
|
|
|
optional_statements:
|
|
|
|
statements { std::swap($$, $1); }
|
|
|
|
| /* no statements */ {}
|
|
|
|
field_declaration:
|
|
|
|
IDENTIFIER COLON type_expression { $$ = std::make_pair($1, $3); }
|
2025-02-16 07:53:31 +01:00
|
|
|
fields:
|
|
|
|
field_declaration fields
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-14 08:05:03 +01:00
|
|
|
std::swap($$, $2);
|
2025-01-31 09:46:17 +01:00
|
|
|
$$.emplace($$.cbegin(), $1);
|
|
|
|
}
|
|
|
|
| field_declaration { $$.emplace_back($1); }
|
2025-02-16 07:53:31 +01:00
|
|
|
optional_fields:
|
|
|
|
fields { std::swap($$, $1); }
|
|
|
|
| /* no fields */ {}
|
2025-01-31 09:46:17 +01:00
|
|
|
type_expression:
|
2025-02-16 07:53:31 +01:00
|
|
|
LEFT_SQUARE INTEGER RIGHT_SQUARE type_expression
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-15 10:26:04 +01:00
|
|
|
$$ = std::make_shared<elna::boot::array_type>(elna::boot::make_position(@1), $4, $2);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-16 07:53:31 +01:00
|
|
|
| HAT type_expression
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-16 07:53:31 +01:00
|
|
|
$$ = std::make_shared<elna::boot::pointer_type>(elna::boot::make_position(@1), $2);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-16 07:53:31 +01:00
|
|
|
| RECORD optional_fields END_BLOCK
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-15 10:26:04 +01:00
|
|
|
$$ = std::make_shared<elna::boot::record_type>(elna::boot::make_position(@1), std::move($2));
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-16 07:53:31 +01:00
|
|
|
| UNION fields END_BLOCK
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-15 10:26:04 +01:00
|
|
|
$$ = std::make_shared<elna::boot::union_type>(elna::boot::make_position(@1), std::move($2));
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
|
|
|
| IDENTIFIER
|
|
|
|
{
|
2025-02-15 10:26:04 +01:00
|
|
|
$$ = std::make_shared<elna::boot::basic_type>(elna::boot::make_position(@1), $1);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-15 10:26:04 +01:00
|
|
|
variable_declaration: identifier_definitions COLON type_expression
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-15 10:26:04 +01:00
|
|
|
for (const std::pair<std::string, bool>& identifier : $1)
|
|
|
|
{
|
|
|
|
elna::boot::variable_declaration *declaration = new elna::boot::variable_declaration(
|
|
|
|
elna::boot::make_position(@2), identifier.first, $3, identifier.second);
|
|
|
|
$$.push_back(declaration);
|
|
|
|
}
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
|
|
|
variable_declarations:
|
2025-02-15 10:26:04 +01:00
|
|
|
variable_declaration variable_declarations
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-15 10:26:04 +01:00
|
|
|
std::swap($$, $1);
|
|
|
|
$$.reserve($$.size() + $2.size());
|
|
|
|
$$.insert(std::end($$), std::begin($2), std::end($2));
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
2025-02-15 10:26:04 +01:00
|
|
|
| variable_declaration { std::swap($$, $1); }
|
2025-01-31 09:46:17 +01:00
|
|
|
variable_part:
|
|
|
|
/* no variable declarations */ {}
|
2025-02-15 00:38:46 +01:00
|
|
|
| VAR variable_declarations { std::swap($$, $2); }
|
2025-02-13 22:54:47 +01:00
|
|
|
constant_definition: identifier_definition EQUALS literal
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-07 22:12:59 +01:00
|
|
|
$$ = new elna::boot::constant_definition(elna::boot::make_position(@1), $1.first, $1.second, $3);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
|
|
|
constant_definitions:
|
2025-02-07 00:56:54 +01:00
|
|
|
constant_definition constant_definitions
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-07 00:56:54 +01:00
|
|
|
std::swap($$, $2);
|
2025-01-31 09:46:17 +01:00
|
|
|
$$.emplace($$.cbegin(), std::move($1));
|
|
|
|
}
|
|
|
|
| constant_definition { $$.emplace_back(std::move($1)); }
|
|
|
|
constant_part:
|
|
|
|
/* no constant definitions */ {}
|
2025-02-07 00:56:54 +01:00
|
|
|
| CONST {}
|
|
|
|
| CONST constant_definitions { std::swap($$, $2); }
|
2025-02-07 22:12:59 +01:00
|
|
|
type_definition: identifier_definition EQUALS type_expression
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-07 22:12:59 +01:00
|
|
|
$$ = new elna::boot::type_definition(elna::boot::make_position(@1), $1.first, $1.second, $3);
|
2025-01-31 09:46:17 +01:00
|
|
|
}
|
|
|
|
type_definitions:
|
2025-02-13 22:54:47 +01:00
|
|
|
type_definition type_definitions
|
2025-01-31 09:46:17 +01:00
|
|
|
{
|
2025-02-13 22:54:47 +01:00
|
|
|
std::swap($$, $2);
|
2025-01-31 09:46:17 +01:00
|
|
|
$$.emplace($$.cbegin(), std::move($1));
|
|
|
|
}
|
|
|
|
| type_definition { $$.emplace_back(std::move($1)); }
|
|
|
|
type_part:
|
|
|
|
/* no type definitions */ {}
|
2025-02-13 22:54:47 +01:00
|
|
|
| TYPE {}
|
|
|
|
| TYPE type_definitions { std::swap($$, $2); }
|
2025-02-15 10:26:04 +01:00
|
|
|
formal_parameter: IDENTIFIER COLON type_expression
|
|
|
|
{
|
|
|
|
$$ = new elna::boot::variable_declaration(elna::boot::make_position(@2), $1, $3);
|
|
|
|
}
|
|
|
|
formal_parameters:
|
|
|
|
formal_parameter COMMA formal_parameters
|
|
|
|
{
|
|
|
|
std::swap($$, $3);
|
|
|
|
$$.emplace($$.cbegin(), $1);
|
|
|
|
}
|
|
|
|
| formal_parameter { $$.emplace_back(std::move($1)); }
|
2025-01-31 09:46:17 +01:00
|
|
|
formal_parameter_list:
|
|
|
|
LEFT_PAREN RIGHT_PAREN {}
|
2025-02-15 10:26:04 +01:00
|
|
|
| LEFT_PAREN formal_parameters RIGHT_PAREN { std::swap($$, $2); }
|
2025-01-31 09:46:17 +01:00
|
|
|
actual_parameter_list:
|
|
|
|
LEFT_PAREN RIGHT_PAREN {}
|
|
|
|
| LEFT_PAREN expressions RIGHT_PAREN { std::swap($$, $2); }
|
|
|
|
%%
|
|
|
|
|
|
|
|
void yy::parser::error(const location_type& loc, const std::string& message)
|
|
|
|
{
|
|
|
|
driver.error(loc, message);
|
|
|
|
}
|