Skip to content

Commit

Permalink
Add utils library and link it to RiDDLe
Browse files Browse the repository at this point in the history
Update conjunction class to include a cost
Update core class to include new int and real expressions
  • Loading branch information
riccardodebenedictis committed Jan 28, 2024
1 parent 312c110 commit 07311a2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ project(RiDDLe VERSION 0.2.0 LANGUAGES C CXX)
include(CTest)
enable_testing()

add_subdirectory(extern/utils)

add_library(RiDDLe src/scope.cpp src/env.cpp src/core.cpp src/type.cpp src/constructor.cpp src/method.cpp src/conjunction.cpp src/item.cpp src/expression.cpp)
target_link_libraries(RiDDLe PUBLIC utils)
target_include_directories(RiDDLe PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
Expand Down
11 changes: 10 additions & 1 deletion include/conjunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

#include "scope.hpp"
#include "env.hpp"
#include "rational.hpp"

namespace riddle
{
class conjunction : public scope
{
public:
conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx);
conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx, const utils::rational cst);

/**
* @brief Get the cost of the conjunction.
*
* @return utils::rational The cost of the conjunction.
*/
utils::rational get_cost() const { return cst; }

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

#include "scope.hpp"
#include "env.hpp"
#include "rational.hpp"

namespace riddle
{
Expand All @@ -25,6 +26,34 @@ namespace riddle
*/
virtual std::shared_ptr<item> new_bool(bool value) = 0;

/**
* @brief Create a new int expression.
*
* @return std::shared_ptr<item> The int expression.
*/
virtual std::shared_ptr<item> new_int() = 0;
/**
* @brief Create a new int expression with a value.
*
* @param value The value of the int expression.
* @return std::shared_ptr<item> The int expression.
*/
virtual std::shared_ptr<item> new_int(INTEGER_TYPE value) = 0;

/**
* @brief Create a new real expression.
*
* @return std::shared_ptr<item> The real expression.
*/
virtual std::shared_ptr<item> new_real() = 0;
/**
* @brief Create a new real expression with a value.
*
* @param value The value of the real expression.
* @return std::shared_ptr<item> The real expression.
*/
virtual std::shared_ptr<item> new_real(const utils::rational &value) = 0;

/**
* @brief Get a field by name.
*
Expand Down
22 changes: 22 additions & 0 deletions include/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,26 @@ namespace riddle
private:
bool_token l;
};

class int_expression final : public expression
{
public:
int_expression(const int_token &l) : l(l) {}

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

private:
int_token l;
};

class real_expression final : public expression
{
public:
real_expression(const real_token &l) : l(l) {}

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

private:
real_token l;
};
} // namespace riddle
18 changes: 17 additions & 1 deletion include/lexer.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstddef>
#include "rational.hpp"

namespace riddle
{
Expand Down Expand Up @@ -71,11 +72,26 @@ namespace riddle
{
public:
bool_token(const bool &value, const size_t &start_line, const size_t &start_pos, const size_t &end_line, const size_t &end_pos) : token(BOOL_ID, start_line, start_pos, end_line, end_pos), value(value) {}
virtual ~bool_token() = default;

const bool value;
};

class int_token final : public token
{
public:
int_token(const INTEGER_TYPE &value, const size_t &start_line, const size_t &start_pos, const size_t &end_line, const size_t &end_pos) : token(INT_ID, start_line, start_pos, end_line, end_pos), value(value) {}

const INTEGER_TYPE value;
};

class real_token final : public token
{
public:
real_token(const utils::rational &value, const size_t &start_line, const size_t &start_pos, const size_t &end_line, const size_t &end_pos) : token(REAL_ID, start_line, start_pos, end_line, end_pos), value(value) {}

const utils::rational value;
};

class lexer
{
};
Expand Down
2 changes: 1 addition & 1 deletion src/conjunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

namespace riddle
{
conjunction::conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx) : scope(parent->get_core(), parent), ctx(ctx) {}
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) {}
} // namespace riddle
2 changes: 2 additions & 0 deletions src/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
namespace riddle
{
std::shared_ptr<item> bool_expression::evaluate(scope &scp, env &ctx) { return scp.get_core().new_bool(l.value); }
std::shared_ptr<item> int_expression::evaluate(scope &scp, env &ctx) { return scp.get_core().new_int(l.value); }
std::shared_ptr<item> real_expression::evaluate(scope &scp, env &ctx) { return scp.get_core().new_real(l.value); }
} // namespace riddle

0 comments on commit 07311a2

Please sign in to comment.