summaryrefslogtreecommitdiff
path: root/src/helpers/math/Expression.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/helpers/math/Expression.cpp')
-rw-r--r--src/helpers/math/Expression.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/helpers/math/Expression.cpp b/src/helpers/math/Expression.cpp
new file mode 100644
index 00000000..fb28628d
--- /dev/null
+++ b/src/helpers/math/Expression.cpp
@@ -0,0 +1,22 @@
+#include "Expression.hpp"
+#include "muParser.h"
+#include "../../debug/Log.hpp"
+
+using namespace Math;
+
+CExpression::CExpression() : m_parser(makeUnique<mu::Parser>()) {
+ ;
+}
+
+void CExpression::addVariable(const std::string& name, double val) {
+ m_parser->DefineConst(name, val);
+}
+
+std::optional<double> CExpression::compute(const std::string& expr) {
+ try {
+ m_parser->SetExpr(expr);
+ return m_parser->Eval();
+ } catch (mu::Parser::exception_type& e) { Debug::log(ERR, "CExpression::compute: mu threw: {}", e.GetMsg()); }
+
+ return std::nullopt;
+}