diff --git a/include/core.hpp b/include/core.hpp index 61c6749..0d82104 100644 --- a/include/core.hpp +++ b/include/core.hpp @@ -14,6 +14,6 @@ namespace riddle core() noexcept; virtual ~core() = default; - [[nodiscard]] std::optional> get(std::string_view name) noexcept override; + [[nodiscard]] std::shared_ptr get(std::string_view name) noexcept override; }; } // namespace riddle diff --git a/include/env.hpp b/include/env.hpp index a9a7d1d..6c978b8 100644 --- a/include/env.hpp +++ b/include/env.hpp @@ -4,7 +4,6 @@ #include #include #include -#include namespace riddle { @@ -26,28 +25,28 @@ namespace riddle class env { public: - env(core &c, env &parent, std::map, std::less<>> &&items = {}) noexcept; + env(core &c, env &parent, std::map, 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> 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> get(std::string_view name) noexcept; + [[nodiscard]] virtual std::shared_ptr get(std::string_view name) noexcept; private: core &cr; env &parent; protected: - std::map, std::less<>> items; + std::map, std::less<>> items; }; } // namespace riddle diff --git a/include/expression.hpp b/include/expression.hpp new file mode 100644 index 0000000..f27a879 --- /dev/null +++ b/include/expression.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "item.hpp" + +namespace riddle +{ + class expression + { + public: + expression() = default; + virtual ~expression() = default; + + [[nodiscard]] virtual std::shared_ptr evaluate(env &e) const = 0; + }; +} // namespace riddle diff --git a/src/core.cpp b/src/core.cpp index 19c35d3..bc43a64 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -5,10 +5,10 @@ namespace riddle { core::core() noexcept : env(*this, *this) {} - std::optional> core::get(std::string_view name) noexcept + std::shared_ptr 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 \ No newline at end of file diff --git a/src/env.cpp b/src/env.cpp index 99ac93f..a6c630d 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -4,12 +4,12 @@ namespace riddle { - env::env(core &c, env &parent, std::map, std::less<>> &&items) noexcept : cr(c), parent(parent), items(std::move(items)) {} + env::env(core &c, env &parent, std::map, std::less<>> &&items) noexcept : cr(c), parent(parent), items(std::move(items)) {} - std::optional> env::get(std::string_view name) noexcept + std::shared_ptr 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); }