Support the remainder operator

This commit is contained in:
2025-01-25 19:50:36 +01:00
parent 005e9dcc52
commit 1f7c1b4cb8
8 changed files with 172 additions and 416 deletions

View File

@ -46,14 +46,6 @@ namespace source
statement->body().accept(this);
}
void empty_visitor::visit(compound_statement *statement)
{
for (const auto nested_statement : statement->statements)
{
nested_statement->accept(this);
}
}
void empty_visitor::visit(assign_statement *statement)
{
statement->rvalue().accept(this);
@ -61,14 +53,20 @@ namespace source
void empty_visitor::visit(if_statement *statement)
{
statement->prerequisite().accept(this);
statement->body().accept(this);
statement->body().prerequisite().accept(this);
for (const auto body_statement : statement->body().statements)
{
body_statement->accept(this);
}
}
void empty_visitor::visit(while_statement *statement)
{
statement->prerequisite().accept(this);
statement->body().accept(this);
statement->body().prerequisite().accept(this);
for (const auto body_statement : statement->body().statements)
{
body_statement->accept(this);
}
}
void empty_visitor::visit(return_statement *statement)
@ -683,50 +681,9 @@ namespace source
}
binary_expression::binary_expression(const struct position position, expression *lhs,
expression *rhs, const unsigned char operation)
: expression(position), m_lhs(std::move(lhs)), m_rhs(std::move(rhs))
expression *rhs, const binary_operator operation)
: expression(position), m_lhs(lhs), m_rhs(rhs), m_operator(operation)
{
switch (operation)
{
case '+':
this->m_operator = binary_operator::sum;
break;
case '-':
this->m_operator = binary_operator::subtraction;
break;
case '*':
this->m_operator = binary_operator::multiplication;
break;
case '/':
this->m_operator = binary_operator::division;
break;
case '=':
this->m_operator = binary_operator::equals;
break;
case 'n':
this->m_operator = binary_operator::not_equals;
break;
case '<':
this->m_operator = binary_operator::less;
break;
case 'l':
this->m_operator = binary_operator::less_equal;
break;
case '>':
this->m_operator = binary_operator::greater;
break;
case 'g':
this->m_operator = binary_operator::greater_equal;
break;
case 'o':
this->m_operator = binary_operator::disjunction;
break;
case 'a':
this->m_operator = binary_operator::conjunction;
break;
default:
__builtin_unreachable();
}
}
void binary_expression::accept(parser_visitor *visitor)
@ -829,18 +786,19 @@ namespace source
delete m_body;
}
compound_statement::compound_statement(const struct position position, std::vector<statement *>&& statements)
: node(position), statements(std::move(statements))
conditional_statements::conditional_statements(expression *prerequisite)
: m_prerequisite(prerequisite)
{
}
void compound_statement::accept(parser_visitor *visitor)
expression& conditional_statements::prerequisite()
{
visitor->visit(this);
return *m_prerequisite;
}
compound_statement::~compound_statement()
conditional_statements::~conditional_statements()
{
delete m_prerequisite;
for (auto statement : statements)
{
delete statement;
@ -916,10 +874,9 @@ namespace source
delete m_rvalue;
}
if_statement::if_statement(const struct position position, expression *prerequisite,
compound_statement *body, compound_statement *alternative)
: statement(position), m_prerequisite(prerequisite), m_body(body),
m_alternative(alternative)
if_statement::if_statement(const struct position position, conditional_statements *body,
std::vector<statement *> *alternative)
: statement(position), m_body(body), m_alternative(alternative)
{
}
@ -928,24 +885,18 @@ namespace source
visitor->visit(this);
}
expression& if_statement::prerequisite()
{
return *m_prerequisite;
}
compound_statement& if_statement::body()
conditional_statements& if_statement::body()
{
return *m_body;
}
compound_statement *if_statement::alternative()
std::vector<statement *> *if_statement::alternative()
{
return m_alternative;
}
if_statement::~if_statement()
{
delete m_prerequisite;
delete m_body;
if (m_alternative != nullptr)
@ -954,9 +905,8 @@ namespace source
}
}
while_statement::while_statement(const struct position position, expression *prerequisite,
compound_statement *body)
: statement(position), m_prerequisite(prerequisite), m_body(body)
while_statement::while_statement(const struct position position, conditional_statements *body)
: statement(position), m_body(body)
{
}
@ -965,19 +915,13 @@ namespace source
visitor->visit(this);
}
expression& while_statement::prerequisite()
{
return *m_prerequisite;
}
compound_statement& while_statement::body()
conditional_statements& while_statement::body()
{
return *m_body;
}
while_statement::~while_statement()
{
delete m_prerequisite;
delete m_body;
}
@ -993,6 +937,8 @@ namespace source
return "*";
case binary_operator::division:
return "/";
case binary_operator::remainder:
return "%";
case binary_operator::equals:
return "=";
case binary_operator::not_equals:

View File

@ -40,6 +40,9 @@ then {
else {
return yy::parser::make_ELSE(this->location);
}
elsif {
return yy::parser::make_ELSIF(this->location);
}
while {
return yy::parser::make_WHILE(this->location);
}
@ -237,6 +240,9 @@ return {
\/ {
return yy::parser::make_DIVISION(this->location);
}
% {
return yy::parser::make_REMAINDER(this->location);
}
:= {
return yy::parser::make_ASSIGNMENT(this->location);
}

View File

@ -63,13 +63,13 @@
%token <std::string> CHARACTER "character"
%token <std::string> STRING "string"
%token <bool> BOOLEAN
%token IF WHILE DO THEN ELSE RETURN
%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
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
%token PLUS MINUS MULTIPLICATION DIVISION
%token PLUS MINUS MULTIPLICATION DIVISION REMAINDER
%token ASSIGNMENT COLON HAT AT
%type <elna::source::literal *> literal;
@ -184,20 +184,23 @@ call_expression: IDENTIFIER actual_parameter_list
}
while_statement: WHILE expression DO optional_statements END_BLOCK
{
auto body = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
$$ = new elna::source::while_statement(elna::source::make_position(@1), $2, body);
auto body = new elna::source::conditional_statements($2);
std::swap($4, body->statements);
$$ = new elna::source::while_statement(elna::source::make_position(@1), body);
}
if_statement:
IF expression THEN optional_statements END_BLOCK
{
auto then = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
$$ = new elna::source::if_statement(elna::source::make_position(@1), $2, then);
auto then = new elna::source::conditional_statements($2);
std::swap($4, then->statements);
$$ = new elna::source::if_statement(elna::source::make_position(@1), then);
}
| IF expression THEN optional_statements ELSE optional_statements END_BLOCK
{
auto then = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
auto _else = new elna::source::compound_statement(elna::source::make_position(@5), std::move($6));
$$ = new elna::source::if_statement(elna::source::make_position(@1), $2, then, _else);
auto then = new elna::source::conditional_statements($2);
std::swap($4, then->statements);
auto _else = new std::vector<elna::source::statement *>(std::move($6));
$$ = new elna::source::if_statement(elna::source::make_position(@1), then, _else);
}
return_statement:
RETURN expression
@ -238,13 +241,18 @@ summand:
factor { $$ = std::move($1); }
| factor MULTIPLICATION factor
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1),
$1, $3, '*');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::multiplication);
}
| factor DIVISION factor
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1),
$1, $3, '/');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::division);
}
| factor REMAINDER factor
{
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::remainder);
}
factor:
AT pointer
@ -261,47 +269,57 @@ factor:
comparand:
summand PLUS summand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '+');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::sum);
}
| summand MINUS summand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '-');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::subtraction);
}
| summand { $$ = std::move($1); }
logical_operand:
comparand EQUALS comparand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '=');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::equals);
}
| comparand NOT_EQUAL comparand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'n');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::not_equals);
}
| comparand LESS_THAN comparand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '<');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::less);
}
| comparand GREATER_THAN comparand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '>');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::greater);
}
| comparand LESS_EQUAL comparand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '<');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::less_equal);
}
| comparand GREATER_EQUAL comparand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '>');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::greater_equal);
}
| comparand { $$ = $1; }
expression:
logical_operand AND logical_operand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'a');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::conjunction);
}
| logical_operand OR logical_operand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'o');
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
elna::source::binary_operator::disjunction);
}
| logical_operand { $$ = $1; }
expressions:

View File

@ -1,83 +0,0 @@
// 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/.
#include <elna/source/types.h>
namespace elna
{
namespace source
{
type::type(const std::size_t byte_size)
: byte_size(byte_size)
{
}
std::size_t type::size() const noexcept
{
return this->byte_size;
}
const pointer_type *type::is_pointer_type() const
{
return nullptr;
}
primitive_type::primitive_type(const std::string& type_name, const std::size_t byte_size)
: type(byte_size), m_type_name(type_name)
{
}
std::string primitive_type::type_name() const
{
return m_type_name;
}
pointer_type::pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size)
: type(byte_size), base_type(base_type)
{
}
const pointer_type *pointer_type::is_pointer_type() const
{
return this;
}
std::string pointer_type::type_name() const
{
return '^' + base_type->type_name();
}
procedure_type::procedure_type(std::vector<std::shared_ptr<const type>> arguments, const std::size_t byte_size)
:type(byte_size), arguments(std::move(arguments))
{
}
std::string procedure_type::type_name() const
{
std::string result{ "proc(" };
for (const auto& argument : arguments)
{
result += argument->type_name() + ',';
}
result.at(result.size() - 1) = ')';
return result;
}
bool operator==(const type& lhs, const type& rhs) noexcept
{
auto lhs_type = lhs.type_name();
auto rhs_type = rhs.type_name();
return lhs_type == rhs_type;
}
bool operator!=(const type& lhs, const type& rhs) noexcept
{
return !(lhs == rhs);
}
const primitive_type boolean_type{ "Boolean", 1 };
const primitive_type int_type{ "Int", 4 };
}
}