#include "SimpleExpressionUnaryOperation.h" #include #include "SimpleExpressionBinaryOperation.h" SimpleExpressionUnaryOperationType::SimpleExpressionUnaryOperationType(const SimpleUnaryOperationId id, std::string syntax, evaluation_function_t evaluationFunction) : m_id(id), m_syntax(std::move(syntax)), m_evaluation_function(std::move(evaluationFunction)) { } const SimpleExpressionUnaryOperationType SimpleExpressionUnaryOperationType::OPERATION_NOT( SimpleUnaryOperationId::NOT, "!", [](const SimpleExpressionValue& operand) -> SimpleExpressionValue { return SimpleExpressionValue(!operand.IsTruthy()); } ); const SimpleExpressionUnaryOperationType SimpleExpressionUnaryOperationType::OPERATION_BITWISE_NOT( SimpleUnaryOperationId::BITWISE_NOT, "~", [](const SimpleExpressionValue& operand) -> SimpleExpressionValue { if (operand.m_type == SimpleExpressionValue::Type::INT) return SimpleExpressionValue(~operand.m_int_value); return SimpleExpressionValue(0); } ); const SimpleExpressionUnaryOperationType SimpleExpressionUnaryOperationType::OPERATION_NEGATIVE( SimpleUnaryOperationId::NEGATIVE, "-", [](const SimpleExpressionValue& operand) -> SimpleExpressionValue { if (operand.m_type == SimpleExpressionValue::Type::INT) return SimpleExpressionValue(-operand.m_int_value); if (operand.m_type == SimpleExpressionValue::Type::DOUBLE) return SimpleExpressionValue(-operand.m_double_value); return SimpleExpressionValue(0); } ); const SimpleExpressionUnaryOperationType* const SimpleExpressionUnaryOperationType::ALL_OPERATION_TYPES[static_cast(SimpleUnaryOperationId::COUNT)] { &OPERATION_NOT, &OPERATION_BITWISE_NOT, &OPERATION_NEGATIVE, }; SimpleExpressionUnaryOperation::SimpleExpressionUnaryOperation(const SimpleExpressionUnaryOperationType* operationType, std::unique_ptr operand) : m_operation_type(operationType), m_operand(std::move(operand)) { } bool SimpleExpressionUnaryOperation::OperandNeedsParenthesis() const { return dynamic_cast(m_operand.get()) != nullptr; } bool SimpleExpressionUnaryOperation::IsStatic() const { assert(m_operand); return m_operand->IsStatic(); } SimpleExpressionValue SimpleExpressionUnaryOperation::Evaluate() const { return m_operation_type->m_evaluation_function(m_operand->Evaluate()); }