Skip to content

Commit

Permalink
Add local_field_statement class and execute method to conjunction class
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardodebenedictis committed Feb 1, 2024
1 parent bbf2fd7 commit f4d2d60
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 21 deletions.
11 changes: 10 additions & 1 deletion include/conjunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

namespace riddle
{
class statement;

class conjunction : public scope
{
public:
conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx, const utils::rational &cst);
conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx, const utils::rational &cst, std::vector<std::unique_ptr<statement>> &&body);

/**
* @brief Get the cost of the conjunction.
Expand All @@ -18,8 +20,15 @@ namespace riddle
*/
utils::rational get_cost() const { return cst; }

/**
* @brief Execute the conjunction.
*
*/
void execute();

private:
std::shared_ptr<env> ctx;
const utils::rational cst;
const std::vector<std::unique_ptr<statement>> body;
};
} // namespace riddle
2 changes: 2 additions & 0 deletions include/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace riddle
class core;
class item;
class constructor;
class local_field_statement;

/**
* @brief The environment class.
Expand All @@ -18,6 +19,7 @@ namespace riddle
class env : public std::enable_shared_from_this<env>
{
friend class constructor;
friend class local_field_statement;

public:
env(core &c, std::shared_ptr<env> parent = nullptr);
Expand Down
29 changes: 19 additions & 10 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, env &ctx) const = 0;
virtual void execute(scope &scp, std::shared_ptr<env> &ctx) const = 0;

virtual std::string to_string() const = 0;
};
Expand All @@ -20,7 +20,8 @@ namespace riddle
public:
field_argument(id_token &&object_id, std::unique_ptr<expression> &&expr = nullptr) : object_id(std::move(object_id)), expr(std::move(expr)) {}

void execute(scope &scp, env &ctx) const;
const id_token &get_id() const { return object_id; }
const std::unique_ptr<expression> &get_expression() const { return expr; }

std::string to_string() const
{
Expand All @@ -39,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, env &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> &ctx) const override;

std::string to_string() const override
{
Expand All @@ -62,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, env &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> &ctx) const override;

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

void execute(scope &scp, 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 @@ -98,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, env &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> &ctx) const override;

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

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

std::string to_string() const override
{
Expand All @@ -140,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, env &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> &ctx) const override;

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

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

std::string to_string() const override
{
Expand All @@ -182,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, env &ctx) const override;
void execute(scope &scp, std::shared_ptr<env> &ctx) const override;

std::string to_string() const override
{
Expand Down Expand Up @@ -212,4 +213,12 @@ namespace riddle
id_token predicate_name;
std::vector<field_argument> arguments;
};

class inconsistency_exception : public std::exception
{
public:
inconsistency_exception() = default;

virtual const char *what() const noexcept override { return "Inconsistency detected"; }
};
} // namespace riddle
12 changes: 10 additions & 2 deletions include/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ namespace riddle
public:
typedef_type(core &c, const std::string &name, type &base_type, std::unique_ptr<expression> &&value);

private:
std::shared_ptr<item> new_instance() override;

private:
type &base_type;
std::unique_ptr<expression> value;
};
Expand All @@ -143,9 +143,9 @@ namespace riddle

std::vector<std::shared_ptr<item>> get_values() const;

private:
std::shared_ptr<item> new_instance() override;

private:
std::vector<std::shared_ptr<item>> values;
std::vector<std::reference_wrapper<enum_type>> enums;
};
Expand Down Expand Up @@ -174,9 +174,17 @@ namespace riddle
*/
constructor &get_constructor(const std::vector<std::reference_wrapper<const type>> &argument_types) const;

/**
* @brief Get the instances of the type.
*
* @return const std::vector<std::shared_ptr<item>>& The instances of the type.
*/
const std::vector<std::shared_ptr<item>> &get_instances() const { return instances; }

private:
std::vector<std::shared_ptr<component_type>> parents; // the base types (i.e. the types this type inherits from)..
std::vector<std::unique_ptr<constructor>> constructors; // the constructors of the type..
std::vector<std::shared_ptr<item>> instances; // the instances of the type..
};

class predicate : public type, public scope
Expand Down
9 changes: 8 additions & 1 deletion src/conjunction.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include "conjunction.hpp"
#include "statement.hpp"

namespace riddle
{
conjunction::conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx, const utils::rational &cst) : scope(parent->get_core(), parent), ctx(ctx), cst(cst) {}
conjunction::conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx, const utils::rational &cst, std::vector<std::unique_ptr<statement>> &&body) : scope(parent->get_core(), parent), ctx(ctx), cst(cst), body(std::move(body)) {}

void conjunction::execute()
{ // execute the body of the conjunction..
for (const auto &stmt : body)
stmt->execute(*this, ctx);
}
} // namespace riddle
12 changes: 6 additions & 6 deletions src/constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace riddle
// we create a new instance of the type
auto instance = std::static_pointer_cast<component>(tp.new_instance());
// we create a new environment for the constructor
env ctx(get_core(), instance);
auto ctx = std::make_shared<env>(get_core(), instance);
for (size_t i = 0; i < args.size(); i++)
ctx.items.emplace(args.at(i)->get_name(), arguments.at(i));
ctx->items.emplace(args.at(i)->get_name(), arguments.at(i));

// we initialize the instance
for (const auto &init : inits)
Expand All @@ -32,7 +32,7 @@ namespace riddle
if (f) // the field is declared in the component type
{
if (f->get().get_type().is_primitive()) // we initialize a primitive field
instance->items.emplace(init.get_name().id, init.get_args().at(0)->evaluate(*this, ctx));
instance->items.emplace(init.get_name().id, init.get_args().at(0)->evaluate(*this, *ctx));
else // we initialize a component field by invoking its constructor
{
auto tp = dynamic_cast<component_type *>(&f->get().get_type());
Expand All @@ -44,7 +44,7 @@ namespace riddle

for (const auto &arg : init.get_args())
{
auto xpr = arg->evaluate(*this, ctx);
auto xpr = arg->evaluate(*this, *ctx);
arg_types.push_back(xpr->get_type());
arguments.push_back(xpr);
}
Expand All @@ -61,7 +61,7 @@ namespace riddle

for (const auto &arg : init.get_args())
{
auto xpr = arg->evaluate(*this, ctx);
auto xpr = arg->evaluate(*this, *ctx);
arg_types.push_back(xpr->get_type());
arguments.push_back(xpr);
}
Expand All @@ -77,7 +77,7 @@ namespace riddle
if (!f->is_synthetic() && instance->items.find(f_name) == instance->items.end())
{ // the field is not initialized
if (f->get_init())
instance->items.emplace(f_name, f->get_init()->evaluate(*this, ctx));
instance->items.emplace(f_name, f->get_init()->evaluate(*this, *ctx));
else if (auto c_tp = dynamic_cast<component_type *>(&f->get_type()))
instance->items.emplace(f_name, f->get_type().new_instance());
else
Expand Down
56 changes: 55 additions & 1 deletion src/statement.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
#include "statement.hpp"
#include "statement.hpp"
#include "type.hpp"
#include "core.hpp"

namespace riddle
{
void local_field_statement::execute(scope &scp, std::shared_ptr<env> &ctx) const
{
auto tp_opt = scp.get_type(field_type.front().id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + field_type.front().id);
auto tp = &tp_opt.value().get();
if (!tp)
throw std::runtime_error("Class " + field_type.front().id + " is not a component type");
for (auto it = field_type.begin() + 1; it != field_type.end(); it++)
if (auto cmp_tp = dynamic_cast<component_type *>(tp))
{
tp_opt = cmp_tp->get_type(it->id);
if (!tp_opt)
throw std::runtime_error("Cannot find class " + it->id);
tp = &tp_opt.value().get();
}

for (const auto &field : fields)
if (tp->is_primitive())
ctx->items.emplace(field.get_id().id, field.get_expression()->evaluate(scp, *ctx));
else if (auto ct = dynamic_cast<component_type *>(tp))
switch (ct->get_instances().size())
{
case 0:
throw inconsistency_exception();
case 1:
ctx->items.emplace(field.get_id().id, ct->get_instances().front());
default:
ctx->items.emplace(field.get_id().id, scp.get_core().new_enum(*ct, ct->get_instances()));
}
else if (auto et = dynamic_cast<enum_type *>(tp))
{
auto values = et->get_values();
switch (values.size())
{
case 0:
throw inconsistency_exception();
case 1:
ctx->items.emplace(field.get_id().id, values.front());
default:
ctx->items.emplace(field.get_id().id, scp.get_core().new_enum(*et, values));
}
}
else if (auto td = dynamic_cast<typedef_type *>(tp))
ctx->items.emplace(field.get_id().id, td->new_instance());
else
throw std::runtime_error("Cannot create instance of type " + tp->get_name());
}
} // namespace riddle

0 comments on commit f4d2d60

Please sign in to comment.