Skip to content

Commit

Permalink
Add expression and bool_item classes for enhanced expression evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardodebenedictis committed Nov 21, 2024
1 parent 19fafa0 commit 83dc361
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enable_testing()

option(COMPUTE_NAMES "Compute RiDDLe names" OFF)

add_library(RiDDLe src/core.cpp src/scope.cpp src/env.cpp src/type.cpp src/item.cpp src/lexer.cpp)
add_library(RiDDLe src/core.cpp src/scope.cpp src/env.cpp src/type.cpp src/item.cpp src/expression.cpp src/lexer.cpp)
target_compile_features(RiDDLe PUBLIC cxx_std_17)
target_include_directories(RiDDLe PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
if(NOT TARGET utils)
Expand Down
16 changes: 15 additions & 1 deletion include/core.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "env.hpp"
#include "expression.hpp"

namespace riddle
{
Expand All @@ -14,6 +14,20 @@ namespace riddle
core() noexcept;
virtual ~core() = default;

/**
* @brief Create a new bool expression.
*
* @return std::shared_ptr<bool_item> The bool expression.
*/
[[nodiscard]] virtual std::shared_ptr<bool_item> new_bool() = 0;
/**
* @brief Create a new bool expression with a value.
*
* @param value The value of the bool expression.
* @return std::shared_ptr<bool_item> The bool expression.
*/
[[nodiscard]] std::shared_ptr<bool_item> new_bool(bool value);

[[nodiscard]] std::shared_ptr<item> get(std::string_view name) noexcept override;
};
} // namespace riddle
18 changes: 18 additions & 0 deletions include/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ namespace riddle
env(core &c, env &parent, std::map<std::string, std::shared_ptr<item>, std::less<>> &&items = {}) noexcept;
virtual ~env() = default;

/**
* @brief Retrieves the core object.
*
* This function returns a reference to the core object.
*
* @return A reference to the core object.
*/
[[nodiscard]] core &get_core() const noexcept { return cr; }

/**
* @brief Retrieves the parent environment.
*
* This function returns a reference to the parent environment.
*
* @return A reference to the parent environment.
*/
[[nodiscard]] env &get_parent() const noexcept { return parent; }

/**
* @brief Retrieves an item by its name.
*
Expand Down
23 changes: 22 additions & 1 deletion include/expression.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
#pragma once

#include "item.hpp"
#include "lexer.hpp"

namespace riddle
{
class scope;

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

[[nodiscard]] virtual std::shared_ptr<item> evaluate(env &e) const = 0;
/**
* @brief Evaluates the expression within the given scope and environment context.
*
* @param scp The scope in which the expression is evaluated.
* @param ctx The environment context used during evaluation.
* @return std::shared_ptr<item> A shared pointer to the evaluated item.
*/
[[nodiscard]] virtual std::shared_ptr<item> evaluate(const scope &scp, env &ctx) const = 0;
};

class bool_expression : public expression
{
public:
bool_expression(std::unique_ptr<bool_token> l) noexcept : l(std::move(l)) {}

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

private:
std::unique_ptr<bool_token> l;
};
} // namespace riddle
13 changes: 13 additions & 0 deletions include/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace riddle
{
class type;
class bool_type;

/**
* @class item item.hpp "include/item.hpp"
Expand All @@ -34,4 +35,16 @@ namespace riddle
private:
type &tp;
};

class bool_item final : public item
{
public:
bool_item(bool_type &tp, const utils::lit &l);

[[nodiscard]] utils::lit &get_value() { return value; }
[[nodiscard]] const utils::lit &get_value() const { return value; }

private:
utils::lit value;
};
} // namespace riddle
12 changes: 12 additions & 0 deletions include/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ namespace riddle
std::string name;
bool primitive;
};

/**
* @class bool_type type.hpp "include/type.hpp"
* @brief The bool type class.
*
* The bool type class is used to represent the boolean type.
*/
class bool_type final : public type
{
public:
bool_type() noexcept : type("bool", true) {}
};
} // namespace riddle
7 changes: 7 additions & 0 deletions src/expression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "expression.hpp"
#include "core.hpp"

namespace riddle
{
std::shared_ptr<item> bool_expression::evaluate(const scope &, env &ctx) const { return ctx.get_core().new_bool(l->value); }
} // namespace riddle
8 changes: 7 additions & 1 deletion src/item.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
#include "item.hpp"
#include "item.hpp"
#include "type.hpp"

namespace riddle
{
bool_item::bool_item(bool_type &tp, const utils::lit &l) : item(tp), value(l) {}
} // namespace riddle

0 comments on commit 83dc361

Please sign in to comment.