Skip to content

Commit

Permalink
Refactor class inheritance in env.hpp, core.hpp, compilation_unit.cpp…
Browse files Browse the repository at this point in the history
…, compilation_unit.hpp, method.hpp, method.cpp, core.cpp, and statement.cpp
  • Loading branch information
riccardodebenedictis committed Apr 10, 2024
1 parent ee0a81c commit bcfc383
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion include/compilation_unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace riddle
void declare(scope &scp) const;
void refine(scope &scp) const;
void refine_predicates(scope &scp) const;
void execute(scope &scp, std::shared_ptr<env> &ctx) const;
void execute(scope &scp, std::shared_ptr<env> ctx) const;

private:
std::vector<std::unique_ptr<type_declaration>> types; // The type declarations.
Expand Down
2 changes: 1 addition & 1 deletion include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace riddle
/**
* @brief The core of the language.
*/
class core : public scope, public env
class core : public scope, public env, public std::enable_shared_from_this<core>
{
friend class typedef_declaration;
friend class enum_declaration;
Expand Down
2 changes: 1 addition & 1 deletion include/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace riddle
*
* An environment is a collection of items.
*/
class env : public std::enable_shared_from_this<env>
class env
{
friend class constructor;
friend class method;
Expand Down
52 changes: 26 additions & 26 deletions include/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace riddle
expression() = default;
virtual ~expression() = default;

[[nodiscard]] virtual std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const = 0;
[[nodiscard]] virtual std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const = 0;

virtual std::string to_string() const = 0;
};
Expand All @@ -24,7 +24,7 @@ namespace riddle
public:
bool_expression(const bool_token &l) : l(l) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l.to_string(); }

Expand All @@ -37,7 +37,7 @@ namespace riddle
public:
int_expression(const int_token &l) : l(l) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l.to_string(); }

Expand All @@ -50,7 +50,7 @@ namespace riddle
public:
real_expression(const real_token &l) : l(l) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l.to_string(); }

Expand All @@ -63,7 +63,7 @@ namespace riddle
public:
string_expression(const string_token &l) : l(l) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l.to_string(); }

Expand All @@ -76,7 +76,7 @@ namespace riddle
public:
cast_expression(std::vector<id_token> &&tp, std::unique_ptr<expression> &&xpr) : cast_to_type(std::move(tp)), expr(std::move(xpr)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -96,7 +96,7 @@ namespace riddle
public:
plus_expression(std::unique_ptr<expression> &&xpr) : expr(std::move(xpr)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return "+" + expr->to_string(); }

Expand All @@ -109,7 +109,7 @@ namespace riddle
public:
minus_expression(std::unique_ptr<expression> &&xpr) : expr(std::move(xpr)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return "-" + expr->to_string(); }

Expand All @@ -122,7 +122,7 @@ namespace riddle
public:
not_expression(std::unique_ptr<expression> &&xpr) : expr(std::move(xpr)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return "!" + expr->to_string(); }

Expand All @@ -135,7 +135,7 @@ namespace riddle
public:
constructor_expression(std::vector<id_token> &&tp, std::vector<std::unique_ptr<expression>> &&args) : instance_type(std::move(tp)), args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -162,7 +162,7 @@ namespace riddle
public:
eq_expression(std::unique_ptr<expression> &&l, std::unique_ptr<expression> &&r) : l(std::move(l)), r(std::move(r)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l->to_string() + " == " + r->to_string(); }

Expand All @@ -176,7 +176,7 @@ namespace riddle
public:
neq_expression(std::unique_ptr<expression> &&l, std::unique_ptr<expression> &&r) : l(std::move(l)), r(std::move(r)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l->to_string() + " != " + r->to_string(); }

Expand All @@ -190,7 +190,7 @@ namespace riddle
public:
lt_expression(std::unique_ptr<expression> &&l, std::unique_ptr<expression> &&r) : l(std::move(l)), r(std::move(r)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l->to_string() + " < " + r->to_string(); }

Expand All @@ -204,7 +204,7 @@ namespace riddle
public:
leq_expression(std::unique_ptr<expression> &&l, std::unique_ptr<expression> &&r) : l(std::move(l)), r(std::move(r)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l->to_string() + " <= " + r->to_string(); }

Expand All @@ -218,7 +218,7 @@ namespace riddle
public:
geq_expression(std::unique_ptr<expression> &&l, std::unique_ptr<expression> &&r) : l(std::move(l)), r(std::move(r)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l->to_string() + " >= " + r->to_string(); }

Expand All @@ -232,7 +232,7 @@ namespace riddle
public:
gt_expression(std::unique_ptr<expression> &&l, std::unique_ptr<expression> &&r) : l(std::move(l)), r(std::move(r)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l->to_string() + " > " + r->to_string(); }

Expand All @@ -246,7 +246,7 @@ namespace riddle
public:
function_expression(std::vector<id_token> &&obj_id, id_token &&fn_name, std::vector<std::unique_ptr<expression>> &&args) : object_id(std::move(obj_id)), function_name(std::move(fn_name)), args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand Down Expand Up @@ -274,7 +274,7 @@ namespace riddle
public:
id_expression(std::vector<id_token> &&obj_id) : object_id(std::move(obj_id)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -293,7 +293,7 @@ namespace riddle
public:
implication_expression(std::unique_ptr<expression> &&l, std::unique_ptr<expression> &&r) : l(std::move(l)), r(std::move(r)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return l->to_string() + " -> " + r->to_string(); }

Expand All @@ -307,7 +307,7 @@ namespace riddle
public:
disjunction_expression(std::vector<std::unique_ptr<expression>> &&args) : args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -326,7 +326,7 @@ namespace riddle
public:
conjunction_expression(std::vector<std::unique_ptr<expression>> &&args) : args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -345,7 +345,7 @@ namespace riddle
public:
xor_expression(std::vector<std::unique_ptr<expression>> &&args) : args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -364,7 +364,7 @@ namespace riddle
public:
addition_expression(std::vector<std::unique_ptr<expression>> &&args) : args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -383,7 +383,7 @@ namespace riddle
public:
subtraction_expression(std::vector<std::unique_ptr<expression>> &&args) : args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -402,7 +402,7 @@ namespace riddle
public:
multiplication_expression(std::vector<std::unique_ptr<expression>> &&args) : args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -421,7 +421,7 @@ namespace riddle
public:
division_expression(std::vector<std::unique_ptr<expression>> &&args) : args(std::move(args)) {}

[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> &ctx) const override;
[[nodiscard]] std::shared_ptr<item> evaluate(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand Down
2 changes: 1 addition & 1 deletion include/method.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace riddle
* @param arguments The arguments.
* @return std::shared_ptr<item> The result of the method.
*/
[[nodiscard]] std::shared_ptr<item> invoke(std::shared_ptr<env> &ctx, std::vector<std::shared_ptr<item>> &&args);
[[nodiscard]] std::shared_ptr<item> invoke(std::shared_ptr<env> ctx, std::vector<std::shared_ptr<item>> &&args);

private:
std::optional<std::reference_wrapper<type>> return_type; // the return type of the method..
Expand Down
18 changes: 9 additions & 9 deletions include/statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace riddle
statement() = default;
virtual ~statement() = default;

virtual void execute(scope &scp, std::shared_ptr<env> &ctx) const = 0;
virtual void execute(scope &scp, std::shared_ptr<env> ctx) const = 0;

virtual std::string to_string() const = 0;
};
Expand Down Expand Up @@ -40,7 +40,7 @@ namespace riddle
public:
local_field_statement(std::vector<id_token> &&field_type, std::vector<field_argument> &&fields) : field_type(std::move(field_type)), fields(std::move(fields)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -63,7 +63,7 @@ namespace riddle
public:
assignment_statement(std::vector<id_token> &&object_id, id_token &&field_name, std::unique_ptr<expression> &&rhs) : object_id(std::move(object_id)), field_name(std::move(field_name)), rhs(std::move(rhs)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -86,7 +86,7 @@ namespace riddle
public:
expression_statement(std::unique_ptr<expression> &&expr) : expr(std::move(expr)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override { return expr->to_string() + ";"; }

Expand All @@ -99,7 +99,7 @@ namespace riddle
public:
conjunction_statement(std::vector<std::unique_ptr<statement>> &&statements, std::unique_ptr<expression> &&cst = nullptr) : statements(std::move(statements)), cst(std::move(cst)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -122,7 +122,7 @@ namespace riddle
public:
disjunction_statement(std::vector<std::unique_ptr<conjunction_statement>> &&blocks) : blocks(std::move(blocks)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -141,7 +141,7 @@ namespace riddle
public:
for_all_statement(std::vector<id_token> &&enum_type, id_token &&enum_id, std::vector<std::unique_ptr<statement>> &&statements) : enum_type(std::move(enum_type)), enum_id(std::move(enum_id)), statements(std::move(statements)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -165,7 +165,7 @@ namespace riddle
public:
return_statement(std::unique_ptr<expression> &&expr) : expr(std::move(expr)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand All @@ -183,7 +183,7 @@ namespace riddle
public:
formula_statement(bool is_fact, id_token &&formula_name, std::vector<id_token> &&formula_scope, id_token &&predicate_name, std::vector<field_argument> &&arguments) : is_fact(is_fact), formula_name(std::move(formula_name)), formula_scope(std::move(formula_scope)), predicate_name(std::move(predicate_name)), arguments(std::move(arguments)) {}

void execute(scope &scp, std::shared_ptr<env> &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> ctx) const override;

std::string to_string() const override
{
Expand Down
2 changes: 1 addition & 1 deletion src/compilation_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace riddle
t->refine_predicates(scp);
}

void compilation_unit::execute(scope &scp, std::shared_ptr<env> &ctx) const
void compilation_unit::execute(scope &scp, std::shared_ptr<env> ctx) const
{
// we execute the statements..
for (const auto &stmt : body)
Expand Down
4 changes: 2 additions & 2 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace riddle

void core::read(const std::string &script)
{
auto ctx = env::shared_from_this();
auto ctx = shared_from_this();
parser p(script);
auto cu = p.parse();
cu->declare(*this);
Expand All @@ -20,7 +20,7 @@ namespace riddle
}
void core::read(const std::vector<std::string> &files)
{
auto ctx = env::shared_from_this();
auto ctx = shared_from_this();
std::vector<std::unique_ptr<compilation_unit>> c_cus;
c_cus.reserve(files.size());
for (const auto &file : files)
Expand Down
Loading

0 comments on commit bcfc383

Please sign in to comment.