Utils: Break out expression parsing and evaluation
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I16101a37220be56722cedd273d5788393aa860fa
diff --git a/src/Utils.cpp b/src/Utils.cpp
index dd13113..b73e469 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -17,6 +17,7 @@
#include "Utils.hpp"
+#include "Expression.hpp"
#include "VariantVisitors.hpp"
#include <boost/algorithm/string/classification.hpp>
@@ -297,7 +298,8 @@
int number = std::visit(VariantToIntVisitor(), propValue);
bool isOperator = true;
- TemplateOperation next = TemplateOperation::addition;
+ std::optional<expression::Operation> next =
+ expression::Operation::addition;
auto it = split.begin();
@@ -305,37 +307,18 @@
{
if (isOperator)
{
- if (*it == "+")
- {
- next = TemplateOperation::addition;
- }
- else if (*it == "-")
- {
- next = TemplateOperation::subtraction;
- }
- else if (*it == "*")
- {
- next = TemplateOperation::multiplication;
- }
- else if (*it == R"(%)")
- {
- next = TemplateOperation::modulo;
- }
- else if (*it == R"(/)")
- {
- next = TemplateOperation::division;
- }
- else
+ next = expression::parseOperation(*it);
+ if (!next)
{
break;
}
}
else
{
- int constant = 0;
try
{
- constant = std::stoi(*it);
+ int constant = std::stoi(*it);
+ number = expression::evaluate(number, *next, constant);
}
catch (const std::invalid_argument&)
{
@@ -343,37 +326,6 @@
<< "\n";
continue;
}
- switch (next)
- {
- case TemplateOperation::addition:
- {
- number += constant;
- break;
- }
- case TemplateOperation::subtraction:
- {
- number -= constant;
- break;
- }
- case TemplateOperation::multiplication:
- {
- number *= constant;
- break;
- }
- case TemplateOperation::division:
- {
- number /= constant;
- break;
- }
- case TemplateOperation::modulo:
- {
- number = number % constant;
- break;
- }
-
- default:
- break;
- }
}
isOperator = !isOperator;
}