Skip to content

Commit

Permalink
Refactor core and env classes to use shared_ptr for item management a…
Browse files Browse the repository at this point in the history
…nd introduce expression class for evaluation
  • Loading branch information
riccardodebenedictis committed Nov 21, 2024
1 parent 02a7abe commit b536b5a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ namespace riddle
core() noexcept;
virtual ~core() = default;

[[nodiscard]] std::optional<std::reference_wrapper<item>> get(std::string_view name) noexcept override;
[[nodiscard]] std::shared_ptr<item> get(std::string_view name) noexcept override;
};
} // namespace riddle
19 changes: 9 additions & 10 deletions include/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <map>
#include <string>
#include <string_view>
#include <optional>

namespace riddle
{
Expand All @@ -26,28 +25,28 @@ namespace riddle
class env
{
public:
env(core &c, env &parent, std::map<std::string, std::unique_ptr<item>, std::less<>> &&items = {}) noexcept;
env(core &c, env &parent, std::map<std::string, std::shared_ptr<item>, std::less<>> &&items = {}) noexcept;
virtual ~env() = default;

/**
* @brief Retrieves an item by its name.
*
* This function searches for an item with the specified name and returns it
* wrapped in a std::optional. If the item is found, it is returned as a
* std::reference_wrapper. If the item is not found, an empty std::optional
* is returned.
* This function retrieves an item by its name. The function will first
* search for the item in the environment's items. If the item is not found,
* the function will search for the item in the parent environment. This
* process continues until the item is found or there are no more parent
* environments to search.
*
* @param name The name of the item to retrieve.
* @return std::optional<std::reference_wrapper<item>> The item wrapped in
* a std::optional if found, otherwise an empty std::optional.
* @return The item with the given name, or nullptr if the item is not found.
*/
[[nodiscard]] virtual std::optional<std::reference_wrapper<item>> get(std::string_view name) noexcept;
[[nodiscard]] virtual std::shared_ptr<item> get(std::string_view name) noexcept;

private:
core &cr;
env &parent;

protected:
std::map<std::string, std::unique_ptr<item>, std::less<>> items;
std::map<std::string, std::shared_ptr<item>, std::less<>> items;
};
} // namespace riddle
15 changes: 15 additions & 0 deletions include/expression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "item.hpp"

namespace riddle
{
class expression
{
public:
expression() = default;
virtual ~expression() = default;

[[nodiscard]] virtual std::shared_ptr<item> evaluate(env &e) const = 0;
};
} // namespace riddle
6 changes: 3 additions & 3 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace riddle
{
core::core() noexcept : env(*this, *this) {}

std::optional<std::reference_wrapper<item>> core::get(std::string_view name) noexcept
std::shared_ptr<item> core::get(std::string_view name) noexcept
{
if (auto it = items.find(name); it != items.end())
return *it->second;
return std::nullopt;
return it->second;
return nullptr;
}
} // namespace riddle
6 changes: 3 additions & 3 deletions src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace riddle
{
env::env(core &c, env &parent, std::map<std::string, std::unique_ptr<item>, std::less<>> &&items) noexcept : cr(c), parent(parent), items(std::move(items)) {}
env::env(core &c, env &parent, std::map<std::string, std::shared_ptr<item>, std::less<>> &&items) noexcept : cr(c), parent(parent), items(std::move(items)) {}

std::optional<std::reference_wrapper<item>> env::get(std::string_view name) noexcept
std::shared_ptr<item> env::get(std::string_view name) noexcept
{
if (auto it = items.find(name); it != items.end())
return *it->second;
return it->second;
else
return parent.get(name);
}
Expand Down

0 comments on commit b536b5a

Please sign in to comment.