Skip to content

Commit

Permalink
Add new bool expression methods to core class
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardodebenedictis committed Jan 28, 2024
1 parent d9aa57c commit 4ba0a23
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(RiDDLe VERSION 0.2.0 LANGUAGES C CXX)
include(CTest)
enable_testing()

add_library(RiDDLe src/scope.cpp src/env.cpp src/core.cpp src/type.cpp src/constructor.cpp src/method.cpp src/item.cpp)
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_include_directories(RiDDLe PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
Expand Down
16 changes: 16 additions & 0 deletions include/conjunction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

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

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

private:
std::shared_ptr<env> ctx;
};
} // namespace riddle
14 changes: 14 additions & 0 deletions include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ namespace riddle
core();
virtual ~core() = default;

/**
* @brief Create a new bool expression.
*
* @return std::shared_ptr<item> The bool expression.
*/
virtual std::shared_ptr<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<item> The bool expression.
*/
virtual std::shared_ptr<item> new_bool(bool value) = 0;

/**
* @brief Get a field by name.
*
Expand Down
29 changes: 29 additions & 0 deletions include/expression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

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

namespace riddle
{
class scope;

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

virtual std::shared_ptr<item> evaluate(scope &scp, env &ctx) = 0;
};

class bool_expression final : public expression
{
public:
bool_expression(const bool_token &l) : l(l) {}

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

private:
bool_token l;
};
} // namespace riddle
82 changes: 82 additions & 0 deletions include/lexer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma once

#include <cstddef>

namespace riddle
{
enum symbol
{
BOOL_ID, // `bool`
INT_ID, // `int`
REAL_ID, // `real`
TIME_ID, // `time`
STRING_ID, // `string`
TYPEDEF_ID, // `typedef`
ENUM_ID, // `enum`
CLASS_ID, // `class`
GOAL_ID, // `goal`
FACT_ID, // `fact`
PREDICATE_ID, // `predicate`
NEW_ID, // `new`
OR_ID, // `or`
FOR_ID, // `for`
THIS_ID, // `this`
VOID_ID, // `void`
RETURN_ID, // `return`
DOT_ID, // `.`
COMMA_ID, // `,`
COLON_ID, // `:`
SEMICOLON_ID, // `;`
LPAREN_ID, // `(`
RPAREN_ID, // `)`
LBRACKET_ID, // `[`
RBRACKET_ID, // `]`
LBRACE_ID, // `{`
RBRACE_ID, // `}`
PLUS_ID, // `+`
MINUS_ID, // `-`
STAR_ID, // `*`
SLASH_ID, // `/`
AMP_ID, // `&`
BAR_ID, // `|`
EQ_ID, // `=`
GT_ID, // `>`
LT_ID, // `<`
BANG_ID, // `!`
EQEQ_ID, // `==`
LTEQ_ID, // `<=`
GTEQ_ID, // `>=`
BANGEQ_ID, // `!=`
IMPLICATION_ID, // `->`
CARET_ID, // `^`
ID_ID, // (`a`..`z`|`A`..`Z`|`_`) (`a`..`z`|`A`..`Z`|`0`..`9`|`_`)*
BoolLiteral_ID, // `true` | `false`
IntLiteral_ID, // [0-9]+
RealLiteral_ID, // [0-9]+ `.` [0-9]+)? | `.` [0-9]+
StringLiteral_ID, // `" . . ."`
EOF_ID
};

class token
{
public:
token(const symbol &sym, const size_t &start_line, const size_t &start_pos, const size_t &end_line, const size_t &end_pos) : sym(sym), start_line(start_line), start_pos(start_pos), end_line(end_line), end_pos(end_pos) {}
virtual ~token() = default;

const symbol sym;
const size_t start_line, start_pos, end_line, end_pos;
};

class bool_token final : public token
{
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 lexer
{
};
} // namespace riddle
6 changes: 6 additions & 0 deletions src/conjunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "conjunction.hpp"

namespace riddle
{
conjunction::conjunction(std::shared_ptr<scope> parent, std::shared_ptr<env> ctx) : scope(parent->get_core(), parent), ctx(ctx) {}
} // 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(scope &scp, env &ctx) { return scp.get_core().new_bool(l.value); }
} // namespace riddle

0 comments on commit 4ba0a23

Please sign in to comment.