483 lines
17 KiB
Plaintext
483 lines
17 KiB
Plaintext
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
* obtain one at http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
%require "3.2"
|
|
%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
|
|
%token CONST VAR PROCEDURE ARRAY OF TYPE RECORD POINTER TO UNION
|
|
%token BEGIN_BLOCK END_BLOCK EXTERN
|
|
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
|
|
%token AND OR NOT CAST AS 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
|
|
|
|
%left OR AND
|
|
%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;
|
|
%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::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;
|
|
%type <elna::boot::procedure_definition *> procedure_definition;
|
|
%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;
|
|
%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::vector<elna::boot::conditional_statements *>> elsif_statement_list;
|
|
%type <elna::boot::cast_expression *> cast_expression;
|
|
%%
|
|
program:
|
|
type_part constant_part procedure_part variable_part BEGIN_BLOCK optional_statements END_BLOCK DOT
|
|
{
|
|
std::vector<elna::boot::definition *> definitions($1.size() + $3.size());
|
|
std::vector<elna::boot::definition *>::iterator definition = definitions.begin();
|
|
std::vector<elna::boot::definition *> value_definitions($2.size() + $4.size());
|
|
std::vector<elna::boot::definition *>::iterator value_definition = value_definitions.begin();
|
|
|
|
for (auto type : $1)
|
|
{
|
|
*definition++ = type;
|
|
}
|
|
for (auto constant : $2)
|
|
{
|
|
*value_definition++ = constant;
|
|
}
|
|
for (auto procedure : $3)
|
|
{
|
|
*definition++ = procedure;
|
|
}
|
|
for (auto variable : $4)
|
|
{
|
|
*value_definition++ = variable;
|
|
}
|
|
auto tree = new elna::boot::program(elna::boot::make_position(@5),
|
|
std::move(definitions), std::move(value_definitions), std::move($6));
|
|
|
|
driver.tree.reset(tree);
|
|
}
|
|
block: constant_part variable_part BEGIN_BLOCK optional_statements END_BLOCK
|
|
{
|
|
std::vector<elna::boot::definition *> definitions($1.size() + $2.size());
|
|
std::vector<elna::boot::definition *>::iterator definition = definitions.begin();
|
|
|
|
for (auto constant : $1)
|
|
{
|
|
*definition++ = constant;
|
|
}
|
|
for (auto variable : $2)
|
|
{
|
|
*definition++ = variable;
|
|
}
|
|
$$ = new elna::boot::block(elna::boot::make_position(@3),
|
|
std::move(definitions), std::move($4));
|
|
}
|
|
procedure_definition:
|
|
PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON block SEMICOLON
|
|
{
|
|
$$ = new elna::boot::procedure_definition(elna::boot::make_position(@1),
|
|
$2, std::move($3), nullptr, $5);
|
|
}
|
|
| PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON EXTERN SEMICOLON
|
|
{
|
|
$$ = new elna::boot::procedure_definition(elna::boot::make_position(@1),
|
|
$2, std::move($3), nullptr, nullptr);
|
|
}
|
|
| PROCEDURE IDENTIFIER formal_parameter_list COLON type_expression SEMICOLON block SEMICOLON
|
|
{
|
|
$$ = new elna::boot::procedure_definition(elna::boot::make_position(@1),
|
|
$2, std::move($3), $5, $7);
|
|
}
|
|
| PROCEDURE IDENTIFIER formal_parameter_list COLON type_expression SEMICOLON EXTERN SEMICOLON
|
|
{
|
|
$$ = new elna::boot::procedure_definition(elna::boot::make_position(@1),
|
|
$2, std::move($3), $5, nullptr);
|
|
}
|
|
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);
|
|
}
|
|
cast_expression: CAST LEFT_PAREN expression AS type_expression RIGHT_PAREN
|
|
{
|
|
$$ = 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);
|
|
}
|
|
return_statement:
|
|
RETURN expression
|
|
{
|
|
$$ = new elna::boot::return_statement(elna::boot::make_position(@1), $2);
|
|
}
|
|
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
|
|
{
|
|
$$ = new elna::boot::string_literal(elna::boot::make_position(@1), $1);
|
|
}
|
|
operand:
|
|
literal { $$ = $1; }
|
|
| designator_expression { $$ = $1; }
|
|
| SIZEOF LEFT_PAREN type_expression RIGHT_PAREN
|
|
{
|
|
$$ = new elna::boot::size_of_expression(elna::boot::make_position(@1), $3);
|
|
}
|
|
| 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);
|
|
}
|
|
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);
|
|
}
|
|
| operand { $$ = $1; }
|
|
expressions:
|
|
expression COMMA expressions
|
|
{
|
|
std::swap($$, $3);
|
|
$$.emplace($$.cbegin(), $1);
|
|
}
|
|
| expression { $$.emplace_back(std::move($1)); }
|
|
designator_expression:
|
|
designator_expression LEFT_SQUARE expression RIGHT_SQUARE
|
|
{
|
|
$$ = new elna::boot::array_access_expression(elna::boot::make_position(@1), $1, $3);
|
|
}
|
|
| designator_expression DOT IDENTIFIER
|
|
{
|
|
$$ = new elna::boot::field_access_expression(elna::boot::make_position(@2), $1, $3);
|
|
}
|
|
| designator_expression HAT
|
|
{
|
|
$$ = 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);
|
|
}
|
|
statements:
|
|
statement SEMICOLON statements
|
|
{
|
|
std::swap($$, $3);
|
|
$$.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); }
|
|
field_list:
|
|
field_declaration SEMICOLON field_list
|
|
{
|
|
std::swap($$, $3);
|
|
$$.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);
|
|
}
|
|
| POINTER TO type_expression
|
|
{
|
|
$$ = new elna::boot::pointer_type_expression(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));
|
|
}
|
|
| UNION field_list END_BLOCK
|
|
{
|
|
$$ = new elna::boot::union_type_expression(elna::boot::make_position(@1), std::move($2));
|
|
}
|
|
| IDENTIFIER
|
|
{
|
|
$$ = new elna::boot::basic_type_expression(elna::boot::make_position(@1), $1);
|
|
}
|
|
variable_declaration: IDENTIFIER COLON type_expression
|
|
{
|
|
$$ = new elna::boot::variable_declaration(elna::boot::make_position(@1), $1, $3);
|
|
}
|
|
variable_declarations:
|
|
variable_declaration COMMA variable_declarations
|
|
{
|
|
std::swap($$, $3);
|
|
$$.emplace($$.cbegin(), $1);
|
|
}
|
|
| variable_declaration { $$.emplace_back(std::move($1)); }
|
|
variable_part:
|
|
/* no variable declarations */ {}
|
|
| VAR variable_declarations SEMICOLON { std::swap($$, $2); }
|
|
constant_definition: IDENTIFIER EQUALS literal
|
|
{
|
|
$$ = new elna::boot::constant_definition(elna::boot::make_position(@1), $1, $3);
|
|
}
|
|
constant_definitions:
|
|
constant_definition COMMA constant_definitions
|
|
{
|
|
std::swap($$, $3);
|
|
$$.emplace($$.cbegin(), std::move($1));
|
|
}
|
|
| constant_definition { $$.emplace_back(std::move($1)); }
|
|
constant_part:
|
|
/* no constant definitions */ {}
|
|
| CONST constant_definitions SEMICOLON { std::swap($$, $2); }
|
|
type_definition: IDENTIFIER EQUALS type_expression
|
|
{
|
|
$$ = new elna::boot::type_definition(elna::boot::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_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); }
|
|
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);
|
|
}
|