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

@ -19,6 +19,7 @@ namespace source
subtraction,
multiplication,
division,
remainder,
equals,
not_equals,
less,
@ -40,7 +41,6 @@ namespace source
class procedure_definition;
class type_definition;
class call_expression;
class compound_statement;
class assign_statement;
class if_statement;
class while_statement;
@ -75,7 +75,6 @@ namespace source
virtual void visit(type_definition *) = 0;
virtual void visit(call_expression *) = 0;
virtual void visit(expression_statement *) = 0;
virtual void visit(compound_statement *) = 0;
virtual void visit(assign_statement *) = 0;
virtual void visit(if_statement *) = 0;
virtual void visit(while_statement *) = 0;
@ -112,7 +111,6 @@ namespace source
virtual void visit(type_definition *definition) override;
virtual void visit(call_expression *statement) override;
virtual void visit(expression_statement *statement) override;
virtual void visit(compound_statement *statement) override;
virtual void visit(assign_statement *statement) override;
virtual void visit(if_statement *) override;
virtual void visit(while_statement *) override;
@ -484,15 +482,21 @@ namespace source
virtual ~expression_statement() override;
};
class compound_statement : public node
/**
* List of statements paired with a condition.
*/
class conditional_statements
{
expression *m_prerequisite;
public:
std::vector<statement *> statements;
compound_statement(const struct position position, std::vector<statement *>&& statements);
virtual void accept(parser_visitor *visitor) override;
conditional_statements(expression *prerequisite);
virtual ~compound_statement() override;
expression& prerequisite();
virtual ~conditional_statements();
};
class return_statement : public statement
@ -609,24 +613,16 @@ namespace source
*/
class if_statement : public statement
{
expression *m_prerequisite;
compound_statement *m_body;
compound_statement *m_alternative;
conditional_statements *m_body;
std::vector<statement *> *m_alternative;
public:
/**
* \param position Source code position.
* \param prerequisite Condition.
* \param body Statement executed if the condition is met.
* \param alternative Statement executed if the condition is not met.
*/
if_statement(const struct position position, expression *prerequisite,
compound_statement *body, compound_statement *alternative = nullptr);
if_statement(const struct position position, conditional_statements *body,
std::vector<statement *> *alternative = nullptr);
virtual void accept(parser_visitor *visitor) override;
expression& prerequisite();
compound_statement& body();
compound_statement *alternative();
conditional_statements& body();
std::vector<statement *> *alternative();
virtual ~if_statement() override;
};
@ -636,21 +632,13 @@ namespace source
*/
class while_statement : public statement
{
expression *m_prerequisite;
compound_statement *m_body;
conditional_statements *m_body;
public:
/**
* \param position Source code position.
* \param prerequisite Condition.
* \param body Statement executed while the condition is met.
*/
while_statement(const struct position position, expression *prerequisite,
compound_statement *body);
while_statement(const struct position position, conditional_statements *body);
virtual void accept(parser_visitor *visitor) override;
expression& prerequisite();
compound_statement& body();
conditional_statements& body();
virtual ~while_statement() override;
};
@ -721,7 +709,7 @@ namespace source
public:
binary_expression(const struct position position, expression *lhs,
expression *rhs, const unsigned char operation);
expression *rhs, const binary_operator operation);
virtual void accept(parser_visitor *visitor) override;
expression& lhs();

View File

@ -1,116 +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/.
#pragma once
#include <memory>
#include <string>
#include <vector>
namespace elna
{
namespace source
{
class primitive_type;
class pointer_type;
class procedure_type;
/**
* Type representation.
*/
class type
{
const std::size_t byte_size;
protected:
/**
* Constructor.
*
* \param byte_size The type size in bytes.
*/
explicit type(const std::size_t byte_size);
public:
/**
* \return The type size in bytes.
*/
virtual std::size_t size() const noexcept;
/**
* \return Unique type representation.
*/
virtual std::string type_name() const = 0;
virtual const pointer_type *is_pointer_type() const;
friend bool operator==(const type& lhs, const type& rhs) noexcept;
friend bool operator!=(const type& lhs, const type& rhs) noexcept;
};
/**
* Built-in type representation.
*/
class primitive_type final : public type
{
/// Type name.
const std::string m_type_name;
public:
/**
* Constructor.
*
* \param type_name Type name.
* \param byte_size The type size in bytes.
*/
primitive_type(const std::string& type_name, const std::size_t byte_size);
virtual std::string type_name() const override;
};
/**
* Typed pointer.
*/
struct pointer_type final : public type
{
/// Pointer target type.
std::shared_ptr<const type> base_type;
/**
* Constructor.
*
* \param base_type Pointer target type.
* \param byte_size The type size in bytes.
*/
pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size);
virtual std::string type_name() const override;
virtual const pointer_type *is_pointer_type() const override;
};
/**
* Type of a procedure.
*/
struct procedure_type final : public type
{
/// Argument types.
std::vector<std::shared_ptr<const type>> arguments;
/**
* Constructor.
*
* \param arguments Argument types.
* \param byte_size Function pointer size.
*/
procedure_type(std::vector<std::shared_ptr<const type>> arguments, const std::size_t byte_size);
virtual std::string type_name() const override;
};
bool operator==(const type& lhs, const type& rhs) noexcept;
bool operator!=(const type& lhs, const type& rhs) noexcept;
extern const primitive_type boolean_type;
extern const primitive_type int_type;
}
}