susumu.yata
null+****@clear*****
Tue Aug 5 11:25:13 JST 2014
susumu.yata 2014-08-05 11:25:13 +0900 (Tue, 05 Aug 2014) New Revision: 7458bb4b738a0adc139a1b3d38a5925b73c70cec https://github.com/groonga/grnxx/commit/7458bb4b738a0adc139a1b3d38a5925b73c70cec Message: Add ExpressionBuilder::push_unary/binary_operator(). Modified files: include/grnxx/expression.hpp lib/grnxx/expression.cpp Modified: include/grnxx/expression.hpp (+5 -0) =================================================================== --- include/grnxx/expression.hpp 2014-08-04 15:59:02 +0900 (856ad14) +++ include/grnxx/expression.hpp 2014-08-05 11:25:13 +0900 (2ede2b6) @@ -184,6 +184,8 @@ class ExpressionBuilder { explicit ExpressionBuilder(const Table *table); + // Push a unary operator. + bool push_unary_operator(Error *error, OperatorType operator_type); // Push a positive operator. bool push_positive_operator(Error *error); // Push a negative operator. @@ -192,6 +194,9 @@ class ExpressionBuilder { bool push_to_int_operator(Error *error); // Push a typecast operator to Float. bool push_to_float_operator(Error *error); + + // Push a binary operator. + bool push_binary_operator(Error *error, OperatorType operator_type); // Push an operator &&. bool push_logical_and_operator(Error *error); // Push an operator ||. Modified: lib/grnxx/expression.cpp (+68 -56) =================================================================== --- lib/grnxx/expression.cpp 2014-08-04 15:59:02 +0900 (3c120cc) +++ lib/grnxx/expression.cpp 2014-08-05 11:25:13 +0900 (0b844fe) @@ -895,6 +895,59 @@ bool ExpressionBuilder::push_column(Error *error, String name) { bool ExpressionBuilder::push_operator(Error *error, OperatorType operator_type) { switch (operator_type) { + case POSITIVE_OPERATOR: + case NEGATIVE_OPERATOR: + case TO_INT_OPERATOR: + case TO_FLOAT_OPERATOR: { + return push_unary_operator(error, operator_type); + } + case LOGICAL_AND_OPERATOR: + case LOGICAL_OR_OPERATOR: + case EQUAL_OPERATOR: + case NOT_EQUAL_OPERATOR: + case LESS_OPERATOR: + case LESS_EQUAL_OPERATOR: + case GREATER_OPERATOR: + case GREATER_EQUAL_OPERATOR: + case BITWISE_AND_OPERATOR: + case BITWISE_OR_OPERATOR: + case BITWISE_XOR_OPERATOR: + case PLUS_OPERATOR: + case MINUS_OPERATOR: + case MULTIPLICATION_OPERATOR: { + return push_binary_operator(error, operator_type); + } + default: { + // TODO: Not supported yet. + GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not supported yet"); + return false; + } + } +} + +void ExpressionBuilder::clear() { + stack_.clear(); +} + +unique_ptr<Expression> ExpressionBuilder::release(Error *error) { + if (stack_.size() != 1) { + GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Incomplete expression"); + return nullptr; + } + unique_ptr<ExpressionNode> root_node = std::move(stack_[0]); + stack_.clear(); + return Expression::create(error, table_, std::move(root_node)); +} + +ExpressionBuilder::ExpressionBuilder(const Table *table) : table_(table) {} + +bool ExpressionBuilder::push_unary_operator(Error *error, + OperatorType operator_type) { + if (stack_.size() < 1) { + GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); + return false; + } + switch (operator_type) { case POSITIVE_OPERATOR: { return push_positive_operator(error); } @@ -907,6 +960,21 @@ bool ExpressionBuilder::push_operator(Error *error, case TO_FLOAT_OPERATOR: { return push_to_float_operator(error); } + default: { + // TODO: Not supported yet. + GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not supported yet"); + return false; + } + } +} + +bool ExpressionBuilder::push_binary_operator(Error *error, + OperatorType operator_type) { + if (stack_.size() < 2) { + GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); + return false; + } + switch (operator_type) { case LOGICAL_AND_OPERATOR: { return push_logical_and_operator(error); } @@ -957,27 +1025,7 @@ bool ExpressionBuilder::push_operator(Error *error, } } -void ExpressionBuilder::clear() { - stack_.clear(); -} - -unique_ptr<Expression> ExpressionBuilder::release(Error *error) { - if (stack_.size() != 1) { - GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Incomplete expression"); - return nullptr; - } - unique_ptr<ExpressionNode> root_node = std::move(stack_[0]); - stack_.clear(); - return Expression::create(error, table_, std::move(root_node)); -} - -ExpressionBuilder::ExpressionBuilder(const Table *table) : table_(table) {} - bool ExpressionBuilder::push_positive_operator(Error *error) { - if (stack_.size() < 1) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } auto &arg = stack_[stack_.size() - 1]; switch (arg->data_type()) { case INT_DATA: @@ -993,10 +1041,6 @@ bool ExpressionBuilder::push_positive_operator(Error *error) { } bool ExpressionBuilder::push_negative_operator(Error *error) { - if (stack_.size() < 1) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } unique_ptr<ExpressionNode> node; auto &arg = stack_[stack_.size() - 1]; switch (arg->data_type()) { @@ -1026,10 +1070,6 @@ bool ExpressionBuilder::push_negative_operator(Error *error) { } bool ExpressionBuilder::push_to_int_operator(Error *error) { - if (stack_.size() < 1) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } unique_ptr<ExpressionNode> node; auto &arg = stack_[stack_.size() - 1]; switch (arg->data_type()) { @@ -1057,10 +1097,6 @@ bool ExpressionBuilder::push_to_int_operator(Error *error) { } bool ExpressionBuilder::push_to_float_operator(Error *error) { - if (stack_.size() < 1) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } unique_ptr<ExpressionNode> node; auto &arg = stack_[stack_.size() - 1]; switch (arg->data_type()) { @@ -1088,10 +1124,6 @@ bool ExpressionBuilder::push_to_float_operator(Error *error) { } bool ExpressionBuilder::push_logical_and_operator(Error *error) { - if (stack_.size() < 2) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } auto &lhs = stack_[stack_.size() - 2]; auto &rhs = stack_[stack_.size() - 1]; if ((lhs->data_type() != BOOL_DATA) || @@ -1111,10 +1143,6 @@ bool ExpressionBuilder::push_logical_and_operator(Error *error) { } bool ExpressionBuilder::push_logical_or_operator(Error *error) { - if (stack_.size() < 2) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } auto &lhs = stack_[stack_.size() - 2]; auto &rhs = stack_[stack_.size() - 1]; if ((lhs->data_type() != BOOL_DATA) || @@ -1135,10 +1163,6 @@ bool ExpressionBuilder::push_logical_or_operator(Error *error) { template <typename T> bool ExpressionBuilder::push_equality_operator(Error *error) { - if (stack_.size() < 2) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } auto &lhs = stack_[stack_.size() - 2]; auto &rhs = stack_[stack_.size() - 1]; if (lhs->data_type() != rhs->data_type()) { @@ -1200,10 +1224,6 @@ bool ExpressionBuilder::push_equality_operator(Error *error) { template <typename T> bool ExpressionBuilder::push_comparison_operator(Error *error) { - if (stack_.size() < 2) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } auto &lhs = stack_[stack_.size() - 2]; auto &rhs = stack_[stack_.size() - 1]; if (lhs->data_type() != rhs->data_type()) { @@ -1253,10 +1273,6 @@ bool ExpressionBuilder::push_comparison_operator(Error *error) { template <typename T> bool ExpressionBuilder::push_bitwise_operator(Error *error) { - if (stack_.size() < 2) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } auto &lhs = stack_[stack_.size() - 2]; auto &rhs = stack_[stack_.size() - 1]; if (lhs->data_type() != rhs->data_type()) { @@ -1293,10 +1309,6 @@ bool ExpressionBuilder::push_bitwise_operator(Error *error) { template <typename T> bool ExpressionBuilder::push_arithmetic_operator(Error *error) { - if (stack_.size() < 2) { - GRNXX_ERROR_SET(error, INVALID_OPERAND, "Not enough operands"); - return false; - } auto &lhs = stack_[stack_.size() - 2]; auto &rhs = stack_[stack_.size() - 1]; if (lhs->data_type() != rhs->data_type()) { -------------- next part -------------- HTML����������������������������...Download