ExpressionParser: Support unary plus operator.

This commit is contained in:
Jordan Woyak 2024-03-20 21:59:08 -05:00
parent 2011c7a448
commit 5039072ae9
2 changed files with 24 additions and 0 deletions

View File

@ -830,6 +830,12 @@ private:
// Interpret it as a unary minus function.
return ParseFunctionArguments("minus", MakeFunctionExpression("minus"), tok);
}
case TOK_ADD:
{
// An atom was expected but we got an addition symbol.
// Interpret it as a unary plus.
return ParseFunctionArguments("plus", MakeFunctionExpression("plus"), tok);
}
default:
{
return ParseResult::MakeErrorResult(tok, _trans("Expected start of expression."));

View File

@ -375,6 +375,22 @@ private:
}
};
// usage: plus(expression)
class UnaryPlusExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return GetArg(0).GetValue(); }
};
// usage: deadzone(input, amount)
class DeadzoneExpression : public FunctionExpression
{
@ -689,6 +705,8 @@ std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string_view name
return std::make_unique<ToggleExpression>();
if (name == "minus")
return std::make_unique<UnaryMinusExpression>();
if (name == "plus")
return std::make_unique<UnaryPlusExpression>();
if (name == "deadzone")
return std::make_unique<DeadzoneExpression>();
if (name == "smooth")