mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 16:15:43 +00:00
Add equals operation to simpleexpressionvalue to check if an expression is the same as another one
This commit is contained in:
parent
cc88fb0a5a
commit
245a2ed642
@ -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
|
||||
{
|
||||
return false;
|
||||
|
@ -13,6 +13,7 @@ namespace menu
|
||||
|
||||
explicit CommonExpressionFunctionCall(std::string functionName);
|
||||
|
||||
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
_NODISCARD SimpleExpressionValue Evaluate() const override;
|
||||
};
|
||||
|
@ -14,6 +14,7 @@ public:
|
||||
ISimpleExpression& operator=(const ISimpleExpression& other) = default;
|
||||
ISimpleExpression& operator=(ISimpleExpression&& other) noexcept = default;
|
||||
|
||||
_NODISCARD virtual bool Equals(const ISimpleExpression* other) const = 0;
|
||||
_NODISCARD virtual bool IsStatic() const = 0;
|
||||
_NODISCARD virtual SimpleExpressionValue Evaluate() const = 0;
|
||||
};
|
||||
|
@ -443,6 +443,16 @@ bool SimpleExpressionBinaryOperation::Operand2NeedsParenthesis() const
|
||||
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
|
||||
{
|
||||
assert(m_operand1 && m_operand2);
|
||||
|
@ -93,6 +93,7 @@ public:
|
||||
_NODISCARD bool Operand1NeedsParenthesis() const;
|
||||
_NODISCARD bool Operand2NeedsParenthesis() const;
|
||||
|
||||
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
_NODISCARD SimpleExpressionValue Evaluate() const override;
|
||||
};
|
||||
|
@ -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
|
||||
{
|
||||
return m_condition->IsStatic() && m_true_value->IsStatic() && m_false_value->IsStatic();
|
||||
|
@ -8,6 +8,7 @@ public:
|
||||
std::unique_ptr<ISimpleExpression> m_true_value;
|
||||
std::unique_ptr<ISimpleExpression> m_false_value;
|
||||
|
||||
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
_NODISCARD SimpleExpressionValue Evaluate() const override;
|
||||
|
||||
|
@ -64,6 +64,13 @@ bool SimpleExpressionUnaryOperation::OperandNeedsParenthesis() const
|
||||
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
|
||||
{
|
||||
assert(m_operand);
|
||||
|
@ -46,6 +46,7 @@ public:
|
||||
|
||||
_NODISCARD bool OperandNeedsParenthesis() const;
|
||||
|
||||
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
_NODISCARD SimpleExpressionValue Evaluate() const override;
|
||||
};
|
||||
|
@ -24,6 +24,23 @@ SimpleExpressionValue::SimpleExpressionValue(const int 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
|
||||
{
|
||||
return true;
|
||||
|
@ -29,6 +29,7 @@ public:
|
||||
explicit SimpleExpressionValue(double doubleValue);
|
||||
explicit SimpleExpressionValue(int intValue);
|
||||
|
||||
_NODISCARD bool Equals(const ISimpleExpression* other) const override;
|
||||
_NODISCARD bool IsStatic() const override;
|
||||
_NODISCARD SimpleExpressionValue Evaluate() const override;
|
||||
_NODISCARD bool IsTruthy() const;
|
||||
|
Loading…
x
Reference in New Issue
Block a user