Add menu unary expression for minus

This commit is contained in:
Jan 2021-11-14 20:04:36 +01:00
parent ebb8eb9e5b
commit 9a2e74d083
3 changed files with 20 additions and 0 deletions

View File

@ -31,10 +31,24 @@ const CommonExpressionUnaryOperationType CommonExpressionUnaryOperationType::OPE
} }
); );
const CommonExpressionUnaryOperationType CommonExpressionUnaryOperationType::OPERATION_NEGATIVE(
"-",
[](const CommonExpressionValue& operand) -> CommonExpressionValue
{
if(operand.m_type == CommonExpressionValue::Type::INT)
return CommonExpressionValue(-operand.m_int_value);
if (operand.m_type == CommonExpressionValue::Type::DOUBLE)
return CommonExpressionValue(-operand.m_double_value);
return CommonExpressionValue(0);
}
);
const CommonExpressionUnaryOperationType* const CommonExpressionUnaryOperationType::ALL_OPERATION_TYPES[static_cast<int>(UnaryOperationId::COUNT)] const CommonExpressionUnaryOperationType* const CommonExpressionUnaryOperationType::ALL_OPERATION_TYPES[static_cast<int>(UnaryOperationId::COUNT)]
{ {
&OPERATION_NOT, &OPERATION_NOT,
&OPERATION_BITWISE_NOT, &OPERATION_BITWISE_NOT,
&OPERATION_NEGATIVE,
}; };
CommonExpressionUnaryOperation::CommonExpressionUnaryOperation(const CommonExpressionUnaryOperationType* operationType, std::unique_ptr<ICommonExpression> operand) CommonExpressionUnaryOperation::CommonExpressionUnaryOperation(const CommonExpressionUnaryOperationType* operationType, std::unique_ptr<ICommonExpression> operand)

View File

@ -13,6 +13,7 @@ namespace menu
{ {
NOT, NOT,
BITWISE_NOT, BITWISE_NOT,
NEGATIVE,
COUNT COUNT
}; };
@ -31,6 +32,7 @@ namespace menu
public: public:
static const CommonExpressionUnaryOperationType OPERATION_NOT; static const CommonExpressionUnaryOperationType OPERATION_NOT;
static const CommonExpressionUnaryOperationType OPERATION_BITWISE_NOT; static const CommonExpressionUnaryOperationType OPERATION_BITWISE_NOT;
static const CommonExpressionUnaryOperationType OPERATION_NEGATIVE;
static const CommonExpressionUnaryOperationType* const ALL_OPERATION_TYPES[static_cast<int>(UnaryOperationId::COUNT)]; static const CommonExpressionUnaryOperationType* const ALL_OPERATION_TYPES[static_cast<int>(UnaryOperationId::COUNT)];
}; };

View File

@ -276,6 +276,10 @@ std::unique_ptr<MenuCommonMatchers::matcher_t> MenuCommonMatchers::ParseUnaryOpe
{ {
return SimpleParserValue::Integer(values[0].get().GetPos(), static_cast<int>(UnaryOperationId::BITWISE_NOT)); return SimpleParserValue::Integer(values[0].get().GetPos(), static_cast<int>(UnaryOperationId::BITWISE_NOT));
}), }),
create.Char('-').Transform([](const MenuMatcherFactory::token_list_t& values)
{
return SimpleParserValue::Integer(values[0].get().GetPos(), static_cast<int>(UnaryOperationId::NEGATIVE));
}),
}).Tag(TAG_EXPRESSION_UNARY_OPERATION).Capture(CAPTURE_UNARY_OPERATION_TYPE); }).Tag(TAG_EXPRESSION_UNARY_OPERATION).Capture(CAPTURE_UNARY_OPERATION_TYPE);
} }