Skip to content

Commit

Permalink
Merge pull request #17 from m-peko/expression-tree-class
Browse files Browse the repository at this point in the history
Expression tree class
  • Loading branch information
m-peko authored Mar 23, 2020
2 parents ce82eea + 5fd17ab commit 567e2dd
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 201 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ int main() {
booleval::evaluator evaluator;
auto success = evaluator.build_expression_tree("field_a foo and field_b 123");
if (!success) {
std::cerr << "Error while building expression tree!" << std::endl;
auto valid = evaluator.expression("field_a foo and field_b 123");
if (!valid) {
std::cerr << "Expression not valid!" << std::endl;
}
if (evaluator.is_activated()) {
Expand Down
55 changes: 8 additions & 47 deletions include/booleval/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
#define BOOLEVAL_EVALUATOR_H

#include <map>
#include <memory>
#include <string_view>
#include <booleval/node/tree_node.h>
#include <booleval/utils/any_value.h>
#include <booleval/token/tokenizer.h>
#include <booleval/node/result_visitor.h>
#include <booleval/tree/result_visitor.h>
#include <booleval/tree/expression_tree.h>

namespace booleval {

Expand Down Expand Up @@ -68,13 +66,13 @@ class evaluator {
bool is_activated() const noexcept;

/**
* Checks whether the expression tree is successfully built.
* Sets the expression to be used for evaluation.
*
* @param expression Expression used for building the tree
* @param expression Expression to be used for evaluation
*
* @return True if the tree is successfully built, otherwise false
* @return True if the expression is valid, otherwise false
*/
bool build_expression_tree(std::string_view expression);
bool expression(std::string_view expression);

/**
* Evaluates expression tree based on the key value map passed in.
Expand All @@ -85,47 +83,10 @@ class evaluator {
*/
bool evaluate(field_map const& fields);

private:
/**
* Parses logical operation OR.
*
* @return Root tree node for the current part of the expression
*/
std::shared_ptr<node::tree_node> parse_expression();

/**
* Parses logical operation AND.
*
* @return Root tree node for the current part of the expression
*/
std::shared_ptr<node::tree_node> parse_and_operation();

/**
* Parses new expression within parentheses.
*
* @return Root tree node for the parsed expression
*/
std::shared_ptr<node::tree_node> parse_parentheses();

/**
* Parses relational operation (EQ, NEQ, GT, LT, ...).
*
* @return Root tree node for the parsed operation
*/
std::shared_ptr<node::tree_node> parse_relational_operation();

/**
* Parses terminal, i.e. field token.
*
* @return Leaf node
*/
std::shared_ptr<node::tree_node> parse_terminal();

private:
bool is_activated_;
token::tokenizer tokenizer_;
std::shared_ptr<node::tree_node> root_;
node::result_visitor result_visitor_;
tree::result_visitor result_visitor_;
tree::expression_tree expression_tree_;
};

} // booleval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
#ifndef BOOLEVAL_BASE_VISITOR_H
#define BOOLEVAL_BASE_VISITOR_H

#include <booleval/node/tree_node.h>
#include <booleval/tree/tree_node.h>
#include <booleval/token/token_type.h>

namespace booleval {

namespace node {
namespace tree {

/**
* class base_visitor
Expand Down Expand Up @@ -174,7 +174,7 @@ ReturnType base_visitor<ReturnType>::visit(tree_node const& node) {
}
}

} // node
} // tree

} // booleval

Expand Down
120 changes: 120 additions & 0 deletions include/booleval/tree/expression_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2020, Marin Peko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#ifndef BOOLEVAL_EXPRESSION_TREE_H
#define BOOLEVAL_EXPRESSION_TREE_H

#include <memory>
#include <string_view>
#include <booleval/tree/tree_node.h>
#include <booleval/token/tokenizer.h>

namespace booleval {

namespace tree {

/**
* class expression_tree
*
* Represents a class for building an expression tree by using a recursive
* descent parser method.
*/
class expression_tree {
public:
expression_tree();
expression_tree(expression_tree&& rhs) = default;
expression_tree(expression_tree const& rhs) = default;

expression_tree& operator=(expression_tree&& rhs) = default;
expression_tree& operator=(expression_tree const& rhs) = default;

~expression_tree() = default;

/**
* Gets the root tree node.
*
* @return Root tree node
*/
std::shared_ptr<tree::tree_node> root() noexcept;

/**
* Builds the expression tree.
*
* @param expression Expression to build the tree for
*
* @return True if the expression tree is built successfully, otherwise false
*/
bool build(std::string_view expression);

private:
/**
* Parses root expression by trying first to parse logical operation OR.
*
* @return Root tree node for the current part of the expression
*/
std::shared_ptr<tree::tree_node> parse_expression();

/**
* Parses logical operation AND.
*
* @return Root tree node for the parsed logical operation
*/
std::shared_ptr<tree::tree_node> parse_and_operation();

/**
* Parses new expression within parentheses.
*
* @return Root tree node for the parsed expression within parentheses
*/
std::shared_ptr<tree::tree_node> parse_parentheses();

/**
* Parses relational operation (EQ, NEQ, GT, LT, GEQ and LEQ).
*
* @return Root tree node for the parsed relational operation
*/
std::shared_ptr<tree::tree_node> parse_relational_operation();

/**
* Parses terminal.
*
* @return Leaf node
*/
std::shared_ptr<tree::tree_node> parse_terminal();

private:
token::tokenizer tokenizer_;
std::shared_ptr<tree::tree_node> root_;
};

} // tree

} // booleval

#endif // BOOLEVAL_EXPRESSION_TREE_H
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@

#include <map>
#include <string_view>
#include <booleval/node/tree_node.h>
#include <booleval/tree/tree_node.h>
#include <booleval/utils/any_value.h>
#include <booleval/node/base_visitor.h>
#include <booleval/tree/base_visitor.h>

namespace booleval {

namespace node {
namespace tree {

/**
* class result_visitor
Expand Down Expand Up @@ -149,7 +149,7 @@ class result_visitor : public base_visitor<bool> {
field_map fields_;
};

} // node
} // tree

} // booleval

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

namespace booleval {

namespace node {
namespace tree {

/**
* struct tree_node
Expand All @@ -61,7 +61,7 @@ struct tree_node {
~tree_node() = default;
};

} // node
} // tree

} // booleval

Expand Down
16 changes: 9 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ include_directories (

set (
SOURCE_FILES
node/result_visitor.cpp
node/tree_node.cpp

token/token.cpp
token/token_type.cpp
token/tokenizer.cpp

tree/expression_tree.cpp
tree/result_visitor.cpp
tree/tree_node.cpp

utils/any_value.cpp
utils/string_utils.cpp

Expand All @@ -22,14 +23,15 @@ set (

set (
INCLUDE_FILES
${BOOLEVAL_INCLUDE_DIR}/booleval/node/base_visitor.h
${BOOLEVAL_INCLUDE_DIR}/booleval/node/result_visitor.h
${BOOLEVAL_INCLUDE_DIR}/booleval/node/tree_node.h

${BOOLEVAL_INCLUDE_DIR}/booleval/token/token.h
${BOOLEVAL_INCLUDE_DIR}/booleval/token/token_type.h
${BOOLEVAL_INCLUDE_DIR}/booleval/token/tokenizer.h

${BOOLEVAL_INCLUDE_DIR}/booleval/tree/base_visitor.h
${BOOLEVAL_INCLUDE_DIR}/booleval/tree/expression_tree.h
${BOOLEVAL_INCLUDE_DIR}/booleval/tree/result_visitor.h
${BOOLEVAL_INCLUDE_DIR}/booleval/tree/tree_node.h

${BOOLEVAL_INCLUDE_DIR}/booleval/utils/any_value.h
${BOOLEVAL_INCLUDE_DIR}/booleval/utils/string_utils.h

Expand Down
Loading

0 comments on commit 567e2dd

Please sign in to comment.