Add equals operation to simpleexpressionvalue to check if an expression is the same as another one

This commit is contained in:
Jan 2021-12-27 14:15:44 +01:00
parent cc88fb0a5a
commit 245a2ed642
11 changed files with 73 additions and 0 deletions

View File

@ -7,6 +7,29 @@ CommonExpressionFunctionCall::CommonExpressionFunctionCall(std::string functionN
{ {
} }
bool CommonExpressionFunctionCall::Equals(const ISimpleExpression* other) const
{
const auto otherFunctionCall = dynamic_cast<const CommonExpressionFunctionCall*>(other);
if (!otherFunctionCall
|| m_function_name != otherFunctionCall->m_function_name
|| m_args.size() != otherFunctionCall->m_args.size())
{
return false;
}
for(auto i = 0u; i < m_args.size(); i++)
{
const auto* arg = m_args[i].get();
const auto* otherArg = otherFunctionCall->m_args[i].get();
if (!arg->Equals(otherArg))
return false;
}
return true;
}
bool CommonExpressionFunctionCall::IsStatic() const bool CommonExpressionFunctionCall::IsStatic() const
{ {
return false; return false;

View File

@ -13,6 +13,7 @@ namespace menu
explicit CommonExpressionFunctionCall(std::string functionName); explicit CommonExpressionFunctionCall(std::string functionName);
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
_NODISCARD bool IsStatic() const override; _NODISCARD bool IsStatic() const override;
_NODISCARD SimpleExpressionValue Evaluate() const override; _NODISCARD SimpleExpressionValue Evaluate() const override;
}; };

View File

@ -14,6 +14,7 @@ public:
ISimpleExpression& operator=(const ISimpleExpression& other) = default; ISimpleExpression& operator=(const ISimpleExpression& other) = default;
ISimpleExpression& operator=(ISimpleExpression&& other) noexcept = default; ISimpleExpression& operator=(ISimpleExpression&& other) noexcept = default;
_NODISCARD virtual bool Equals(const ISimpleExpression* other) const = 0;
_NODISCARD virtual bool IsStatic() const = 0; _NODISCARD virtual bool IsStatic() const = 0;
_NODISCARD virtual SimpleExpressionValue Evaluate() const = 0; _NODISCARD virtual SimpleExpressionValue Evaluate() const = 0;
}; };

View File

@ -443,6 +443,16 @@ bool SimpleExpressionBinaryOperation::Operand2NeedsParenthesis() const
return operation && operation->m_operation_type->m_precedence > m_operation_type->m_precedence; return operation && operation->m_operation_type->m_precedence > m_operation_type->m_precedence;
} }
bool SimpleExpressionBinaryOperation::Equals(const ISimpleExpression* other) const
{
const auto* otherBinaryOperation = dynamic_cast<const SimpleExpressionBinaryOperation*>(other);
return otherBinaryOperation
&& m_operation_type->m_id == otherBinaryOperation->m_operation_type->m_id
&& m_operand1->Equals(otherBinaryOperation->m_operand1.get())
&& m_operand2->Equals(otherBinaryOperation->m_operand2.get());
}
bool SimpleExpressionBinaryOperation::IsStatic() const bool SimpleExpressionBinaryOperation::IsStatic() const
{ {
assert(m_operand1 && m_operand2); assert(m_operand1 && m_operand2);

View File

@ -93,6 +93,7 @@ public:
_NODISCARD bool Operand1NeedsParenthesis() const; _NODISCARD bool Operand1NeedsParenthesis() const;
_NODISCARD bool Operand2NeedsParenthesis() const; _NODISCARD bool Operand2NeedsParenthesis() const;
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
_NODISCARD bool IsStatic() const override; _NODISCARD bool IsStatic() const override;
_NODISCARD SimpleExpressionValue Evaluate() const override; _NODISCARD SimpleExpressionValue Evaluate() const override;
}; };

View File

@ -11,6 +11,16 @@ SimpleExpressionConditionalOperator::SimpleExpressionConditionalOperator(std::un
{ {
} }
bool SimpleExpressionConditionalOperator::Equals(const ISimpleExpression* other) const
{
const auto* otherConditionalOperator = dynamic_cast<const SimpleExpressionConditionalOperator*>(other);
return otherConditionalOperator
&& m_condition->Equals(otherConditionalOperator->m_condition.get())
&& m_true_value->Equals(otherConditionalOperator->m_true_value.get())
&& m_false_value->Equals(otherConditionalOperator->m_false_value.get());
}
bool SimpleExpressionConditionalOperator::IsStatic() const bool SimpleExpressionConditionalOperator::IsStatic() const
{ {
return m_condition->IsStatic() && m_true_value->IsStatic() && m_false_value->IsStatic(); return m_condition->IsStatic() && m_true_value->IsStatic() && m_false_value->IsStatic();

View File

@ -8,6 +8,7 @@ public:
std::unique_ptr<ISimpleExpression> m_true_value; std::unique_ptr<ISimpleExpression> m_true_value;
std::unique_ptr<ISimpleExpression> m_false_value; std::unique_ptr<ISimpleExpression> m_false_value;
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
_NODISCARD bool IsStatic() const override; _NODISCARD bool IsStatic() const override;
_NODISCARD SimpleExpressionValue Evaluate() const override; _NODISCARD SimpleExpressionValue Evaluate() const override;

View File

@ -64,6 +64,13 @@ bool SimpleExpressionUnaryOperation::OperandNeedsParenthesis() const
return dynamic_cast<const SimpleExpressionBinaryOperation*>(m_operand.get()) != nullptr; return dynamic_cast<const SimpleExpressionBinaryOperation*>(m_operand.get()) != nullptr;
} }
bool SimpleExpressionUnaryOperation::Equals(const ISimpleExpression* other) const
{
const auto* otherUnaryOperation = dynamic_cast<const SimpleExpressionUnaryOperation*>(other);
return otherUnaryOperation && m_operation_type->m_id == otherUnaryOperation->m_operation_type->m_id && m_operand->Equals(otherUnaryOperation->m_operand.get());
}
bool SimpleExpressionUnaryOperation::IsStatic() const bool SimpleExpressionUnaryOperation::IsStatic() const
{ {
assert(m_operand); assert(m_operand);

View File

@ -46,6 +46,7 @@ public:
_NODISCARD bool OperandNeedsParenthesis() const; _NODISCARD bool OperandNeedsParenthesis() const;
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
_NODISCARD bool IsStatic() const override; _NODISCARD bool IsStatic() const override;
_NODISCARD SimpleExpressionValue Evaluate() const override; _NODISCARD SimpleExpressionValue Evaluate() const override;
}; };

View File

@ -24,6 +24,23 @@ SimpleExpressionValue::SimpleExpressionValue(const int intValue)
m_int_value = intValue; m_int_value = intValue;
} }
bool SimpleExpressionValue::Equals(const ISimpleExpression* other) const
{
const auto* otherExpressionValue = dynamic_cast<const SimpleExpressionValue*>(other);
if (!otherExpressionValue || m_type != otherExpressionValue->m_type)
return false;
if (m_type == Type::INT)
return m_int_value == otherExpressionValue->m_int_value;
if (m_type == Type::DOUBLE)
return std::fabs(m_double_value - otherExpressionValue->m_double_value) < std::numeric_limits<double>::epsilon();
if (m_type == Type::STRING)
return *m_string_value == *otherExpressionValue->m_string_value;
return true;
}
bool SimpleExpressionValue::IsStatic() const bool SimpleExpressionValue::IsStatic() const
{ {
return true; return true;

View File

@ -29,6 +29,7 @@ public:
explicit SimpleExpressionValue(double doubleValue); explicit SimpleExpressionValue(double doubleValue);
explicit SimpleExpressionValue(int intValue); explicit SimpleExpressionValue(int intValue);
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
_NODISCARD bool IsStatic() const override; _NODISCARD bool IsStatic() const override;
_NODISCARD SimpleExpressionValue Evaluate() const override; _NODISCARD SimpleExpressionValue Evaluate() const override;
_NODISCARD bool IsTruthy() const; _NODISCARD bool IsTruthy() const;