From 94b90a2cb0b4a0e13018cd5ebbf09394abf3b96b Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Tue, 28 Apr 2026 18:10:25 +0100 Subject: config/lua: add ExpressionVec2, allow using a table for vec2 rules (#14197) --- src/helpers/math/Expression.cpp | 42 +++++++++++++++++++++++++++++++++++++++++ src/helpers/math/Expression.hpp | 14 +++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) (limited to 'src/helpers/math') diff --git a/src/helpers/math/Expression.cpp b/src/helpers/math/Expression.cpp index 3c0bee91..2289b85e 100644 --- a/src/helpers/math/Expression.cpp +++ b/src/helpers/math/Expression.cpp @@ -2,8 +2,50 @@ #include "muParser.h" #include "../../debug/log/Logger.hpp" +#include +#include + using namespace Math; +static std::string_view trimExprView(std::string_view s) { + while (!s.empty() && std::isspace(static_cast(s.front()))) + s.remove_prefix(1); + + while (!s.empty() && std::isspace(static_cast(s.back()))) + s.remove_suffix(1); + + return s; +} + +bool SExpressionVec2::empty() const { + return x.empty() || y.empty(); +} + +std::string SExpressionVec2::toString() const { + if (empty()) + return ""; + + return std::format("{} {}", x, y); +} + +std::expected Math::parseExpressionVec2(std::string_view raw) { + raw = trimExprView(raw); + if (raw.empty()) + return SExpressionVec2{}; + + const auto spacePos = raw.find_first_of(" \t\n\r\f\v"); + if (spacePos == std::string_view::npos) + return std::unexpected("expression vec2 requires two expressions separated by whitespace"); + + auto lhs = trimExprView(raw.substr(0, spacePos)); + auto rhs = trimExprView(raw.substr(spacePos + 1)); + + if (lhs.empty() || rhs.empty()) + return std::unexpected("expression vec2 requires two non-empty expressions"); + + return SExpressionVec2{std::string{lhs}, std::string{rhs}}; +} + CExpression::CExpression() : m_parser(makeUnique()) { ; } diff --git a/src/helpers/math/Expression.hpp b/src/helpers/math/Expression.hpp index 1780e3ee..631d78ad 100644 --- a/src/helpers/math/Expression.hpp +++ b/src/helpers/math/Expression.hpp @@ -1,14 +1,26 @@ #pragma once #include "../memory/Memory.hpp" +#include #include #include +#include namespace mu { class Parser; }; namespace Math { + struct SExpressionVec2 { + std::string x; + std::string y; + + bool empty() const; + std::string toString() const; + }; + + std::expected parseExpressionVec2(std::string_view raw); + class CExpression { public: CExpression(); @@ -25,4 +37,4 @@ namespace Math { private: UP m_parser; }; -}; \ No newline at end of file +}; -- cgit v1.2.3