diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index 6d240af97..1eb022878 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -503,6 +503,7 @@ macro(addBundledLibraries) set_property(TARGET libwolv-hash PROPERTY POSITION_INDEPENDENT_CODE ON) set_property(TARGET libwolv-containers PROPERTY POSITION_INDEPENDENT_CODE ON) set_property(TARGET libwolv-net PROPERTY POSITION_INDEPENDENT_CODE ON) + set_property(TARGET libwolv-math_eval PROPERTY POSITION_INDEPENDENT_CODE ON) set(XDGPP_INCLUDE_DIRS "${THIRD_PARTY_LIBS_FOLDER}/xdgpp") set(FPHSA_NAME_MISMATCHED ON CACHE BOOL "") diff --git a/lib/external/libwolv b/lib/external/libwolv index 25430e1ad..71cddb8d4 160000 --- a/lib/external/libwolv +++ b/lib/external/libwolv @@ -1 +1 @@ -Subproject commit 25430e1ad1b6061cd64c5afa8bb7ba71c234a22a +Subproject commit 71cddb8d45763168b701dfcd671e9f47f27ac897 diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 772187646..602114899 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 7721876463431f9790c8e2ad0b87a37b6ab40889 +Subproject commit 6021148991238aad9cb5deb465e19f0c82202a36 diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index 5e77865e9..13b8f834e 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -94,7 +94,7 @@ elseif (APPLE) endif () target_link_libraries(libimhex PRIVATE ${FMT_LIBRARIES}) -target_link_libraries(libimhex PUBLIC dl ${IMGUI_LIBRARIES} ${NFD_LIBRARIES} magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${MBEDTLS_LIBRARIES} ${LIBBACKTRACE_LIBRARIES} plcli libpl libpl-gen ${MINIAUDIO_LIBRARIES} ${JTHREAD_LIBRARIES} libwolv-utils libwolv-io libwolv-hash libwolv-net libwolv-containers) +target_link_libraries(libimhex PUBLIC dl ${IMGUI_LIBRARIES} ${NFD_LIBRARIES} magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${MBEDTLS_LIBRARIES} ${LIBBACKTRACE_LIBRARIES} plcli libpl libpl-gen ${MINIAUDIO_LIBRARIES} ${JTHREAD_LIBRARIES} wolv::utils wolv::io wolv::hash wolv::net wolv::containers wolv::math_eval) set_property(TARGET libimhex PROPERTY INTERPROCEDURAL_OPTIMIZATION FALSE) diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index be38e355a..979e08b4b 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -42,11 +42,13 @@ namespace hex { this->m_functions.setImGuiContextFunction = getPluginFunction("setImGuiContext"); this->m_functions.isBuiltinPluginFunction = getPluginFunction("isBuiltinPlugin"); this->m_functions.getSubCommandsFunction = getPluginFunction("getSubCommands"); + + log::info("Loaded plugin '{}'", wolv::util::toUTF8String(path.filename())); } Plugin::Plugin(hex::PluginFunctions functions) { this->m_handle = 0; - this->m_functions = std::move(functions); + this->m_functions = functions; } @@ -97,6 +99,8 @@ namespace hex { return false; } + log::info("Plugin '{}' initialized successfully", pluginName); + this->m_initialized = true; return true; } diff --git a/main/forwarder/CMakeLists.txt b/main/forwarder/CMakeLists.txt index 629b8990a..f99376380 100644 --- a/main/forwarder/CMakeLists.txt +++ b/main/forwarder/CMakeLists.txt @@ -5,7 +5,7 @@ add_executable(main-forwarder ${IMHEX_ICON} ) -target_link_libraries(main-forwarder PRIVATE libwolv-io ${FMT_LIBRARIES}) +target_link_libraries(main-forwarder PRIVATE wolv::io ${FMT_LIBRARIES}) add_dependencies(imhex_all main-forwarder) set_target_properties(main-forwarder PROPERTIES OUTPUT_NAME "imhex" diff --git a/main/updater/CMakeLists.txt b/main/updater/CMakeLists.txt index ef9ba01f6..17c06847d 100644 --- a/main/updater/CMakeLists.txt +++ b/main/updater/CMakeLists.txt @@ -5,7 +5,7 @@ add_executable(updater ${APPLICATION_TYPE} ) add_compile_definitions(IMHEX_PROJECT_NAME="${PROJECT_NAME}") -target_link_libraries(updater PRIVATE libimhex libwolv-io ${FMT_LIBRARIES}) +target_link_libraries(updater PRIVATE libimhex wolv::io ${FMT_LIBRARIES}) add_dependencies(imhex_all updater) set_target_properties(updater PROPERTIES OUTPUT_NAME "imhex-updater" diff --git a/plugins/builtin/CMakeLists.txt b/plugins/builtin/CMakeLists.txt index e25259829..5d8fc34b3 100644 --- a/plugins/builtin/CMakeLists.txt +++ b/plugins/builtin/CMakeLists.txt @@ -104,7 +104,6 @@ add_imhex_plugin( source/content/views/view_achievements.cpp source/content/views/view_yara.cpp - source/content/helpers/math_evaluator.cpp source/content/helpers/notification.cpp source/ui/hex_editor.cpp diff --git a/plugins/builtin/include/content/helpers/math_evaluator.hpp b/plugins/builtin/include/content/helpers/math_evaluator.hpp deleted file mode 100644 index 4a58dc464..000000000 --- a/plugins/builtin/include/content/helpers/math_evaluator.hpp +++ /dev/null @@ -1,127 +0,0 @@ -#pragma once - -#include - -#include -#include -#include -#include -#include -#include - -namespace hex { - - template - class MathEvaluator { - public: - MathEvaluator() = default; - - struct Variable { - T value; - bool constant; - }; - - std::optional evaluate(const std::string &input); - - void registerStandardVariables(); - void registerStandardFunctions(); - - void setVariable(const std::string &name, T value, bool constant = false); - void setFunction(const std::string &name, const std::function(std::vector)> &function, size_t minNumArgs, size_t maxNumArgs); - - std::unordered_map &getVariables() { return this->m_variables; } - - [[nodiscard]] bool hasError() const { - return this->m_lastError.has_value(); - } - - [[nodiscard]] std::optional getLastError() const { - return this->m_lastError; - } - - private: - void setError(const std::string &error) { - this->m_lastError = error; - } - - private: - enum class TokenType - { - Number, - Variable, - Function, - Operator, - Bracket - }; - - enum class Operator : u16 - { - Invalid = 0x000, - Assign = 0x010, - Or = 0x020, - Xor = 0x030, - And = 0x040, - BitwiseOr = 0x050, - BitwiseXor = 0x060, - BitwiseAnd = 0x070, - Equals = 0x080, - NotEquals = 0x081, - GreaterThan = 0x090, - LessThan = 0x091, - GreaterThanOrEquals = 0x092, - LessThanOrEquals = 0x093, - ShiftLeft = 0x0A0, - ShiftRight = 0x0A1, - Addition = 0x0B0, - Subtraction = 0x0B1, - Multiplication = 0x0C0, - Division = 0x0C1, - Modulus = 0x0C2, - Exponentiation = 0x1D0, - Combine = 0x0E0, - BitwiseNot = 0x2F0, - Not = 0x2F1, - Plus = 0x2F2, - Minus = 0x2F3 - }; - - enum class BracketType : std::uint8_t - { - Left, - Right - }; - - struct Token { - TokenType type; - - union { - T number; - Operator op; - BracketType bracketType; - }; - - std::string name; - std::vector arguments; - }; - - static i16 comparePrecedence(const Operator &a, const Operator &b); - static bool isLeftAssociative(const Operator &op); - static bool isUnary(const Operator &op); - static std::pair toOperator(const std::string &input); - - private: - std::optional> parseInput(std::string input); - std::optional> toPostfix(std::queue inputQueue); - std::optional evaluate(std::queue postfixTokens); - - std::unordered_map m_variables; - std::unordered_map(std::vector)>> m_functions; - - std::optional m_lastError; - }; - - extern template class MathEvaluator; - extern template class MathEvaluator; - extern template class MathEvaluator; - -} \ No newline at end of file diff --git a/plugins/builtin/source/content/command_line_interface.cpp b/plugins/builtin/source/content/command_line_interface.cpp index 51e4b544e..41800f4db 100644 --- a/plugins/builtin/source/content/command_line_interface.cpp +++ b/plugins/builtin/source/content/command_line_interface.cpp @@ -14,8 +14,7 @@ #include #include - -#include "content/helpers/math_evaluator.hpp" +#include #include @@ -94,7 +93,7 @@ namespace hex::plugin::builtin { std::exit(EXIT_FAILURE); } - MathEvaluator evaluator; + wolv::math_eval::MathEvaluator evaluator; auto input = hex::format("{}", fmt::join(args, " ")); auto result = evaluator.evaluate(input); diff --git a/plugins/builtin/source/content/command_palette_commands.cpp b/plugins/builtin/source/content/command_palette_commands.cpp index 2db920ac7..7e8cf941a 100644 --- a/plugins/builtin/source/content/command_palette_commands.cpp +++ b/plugins/builtin/source/content/command_palette_commands.cpp @@ -6,8 +6,7 @@ #include #include -#include - +#include #include namespace hex::plugin::builtin { @@ -19,7 +18,7 @@ namespace hex::plugin::builtin { "#", "hex.builtin.command.calc.desc", [](auto input) { - hex::MathEvaluator evaluator; + wolv::math_eval::MathEvaluator evaluator; evaluator.registerStandardVariables(); evaluator.registerStandardFunctions(); diff --git a/plugins/builtin/source/content/helpers/math_evaluator.cpp b/plugins/builtin/source/content/helpers/math_evaluator.cpp deleted file mode 100644 index 86841c17d..000000000 --- a/plugins/builtin/source/content/helpers/math_evaluator.cpp +++ /dev/null @@ -1,501 +0,0 @@ -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace hex { - - template - i16 MathEvaluator::comparePrecedence(const Operator &a, const Operator &b) { - return (static_cast(a) & 0x0F0) - (static_cast(b) & 0x0F0); - } - - template - bool MathEvaluator::isLeftAssociative(const Operator &op) { - return (static_cast(op) & 0x100) == 0; - } - - template - bool MathEvaluator::isUnary(const Operator &op) { - return (static_cast(op) & 0x200) != 0; - } - - template - std::pair::Operator, size_t> MathEvaluator::toOperator(const std::string &input) { - if (input.starts_with("##")) return { Operator::Combine, 2 }; - if (input.starts_with("==")) return { Operator::Equals, 2 }; - if (input.starts_with("!=")) return { Operator::NotEquals, 2 }; - if (input.starts_with(">=")) return { Operator::GreaterThanOrEquals, 2 }; - if (input.starts_with("<=")) return { Operator::LessThanOrEquals, 2 }; - if (input.starts_with(">>")) return { Operator::ShiftRight, 2 }; - if (input.starts_with("<<")) return { Operator::ShiftLeft, 2 }; - if (input.starts_with("||")) return { Operator::Or, 2 }; - if (input.starts_with("^^")) return { Operator::Xor, 2 }; - if (input.starts_with("&&")) return { Operator::And, 2 }; - if (input.starts_with("**")) return { Operator::Exponentiation, 2 }; - if (input.starts_with(">")) return { Operator::GreaterThan, 1 }; - if (input.starts_with("<")) return { Operator::LessThan, 1 }; - if (input.starts_with("!")) return { Operator::Not, 1 }; - if (input.starts_with("|")) return { Operator::BitwiseOr, 1 }; - if (input.starts_with("^")) return { Operator::BitwiseXor, 1 }; - if (input.starts_with("&")) return { Operator::BitwiseAnd, 1 }; - if (input.starts_with("~")) return { Operator::BitwiseNot, 1 }; - if (input.starts_with("+")) return { Operator::Addition, 1 }; - if (input.starts_with("-")) return { Operator::Subtraction, 1 }; - if (input.starts_with("*")) return { Operator::Multiplication, 1 }; - if (input.starts_with("/")) return { Operator::Division, 1 }; - if (input.starts_with("%")) return { Operator::Modulus, 1 }; - if (input.starts_with("=")) return { Operator::Assign, 1 }; - - return { Operator::Invalid, 0 }; - } - - template - std::optional::Token>> MathEvaluator::toPostfix(std::queue inputQueue) { - std::queue outputQueue; - std::stack operatorStack; - - while (!inputQueue.empty()) { - Token currToken = inputQueue.front(); - inputQueue.pop(); - - if (currToken.type == TokenType::Number || currToken.type == TokenType::Variable || currToken.type == TokenType::Function) - outputQueue.push(currToken); - else if (currToken.type == TokenType::Operator) { - while ((!operatorStack.empty()) && ((operatorStack.top().type == TokenType::Operator && currToken.type == TokenType::Operator && (comparePrecedence(operatorStack.top().op, currToken.op) > 0)) || (comparePrecedence(operatorStack.top().op, currToken.op) == 0 && isLeftAssociative(currToken.op))) && operatorStack.top().type != TokenType::Bracket) { - outputQueue.push(operatorStack.top()); - operatorStack.pop(); - } - operatorStack.push(currToken); - } else if (currToken.type == TokenType::Bracket) { - if (currToken.bracketType == BracketType::Left) - operatorStack.push(currToken); - else { - if (operatorStack.empty()) { - this->setError("Mismatching parenthesis!"); - return std::nullopt; - } - - while (operatorStack.top().type != TokenType::Bracket || (operatorStack.top().type == TokenType::Bracket && operatorStack.top().bracketType != BracketType::Left)) { - if (operatorStack.empty()) { - this->setError("Mismatching parenthesis!"); - return std::nullopt; - } - - outputQueue.push(operatorStack.top()); - operatorStack.pop(); - } - - operatorStack.pop(); - } - } - } - - while (!operatorStack.empty()) { - auto top = operatorStack.top(); - - if (top.type == TokenType::Bracket) { - this->setError("Mismatching parenthesis!"); - return std::nullopt; - } - - outputQueue.push(top); - operatorStack.pop(); - } - - return outputQueue; - } - - template - std::optional::Token>> MathEvaluator::parseInput(std::string input) { - std::queue inputQueue; - - char *prevPos = input.data(); - for (char *pos = prevPos; *pos != 0x00;) { - if (std::isdigit(*pos) || *pos == '.') { - auto number = [&] { - if constexpr (std::floating_point) - return std::strtold(pos, &pos); - else if constexpr (std::signed_integral) - return std::strtoll(pos, &pos, 10); - else if constexpr (std::unsigned_integral) - return std::strtoull(pos, &pos, 10); - else - static_assert(hex::always_false::value, "Can't parse literal of this type"); - }(); - - if (*pos == 'x') { - pos--; - number = std::strtoull(pos, &pos, 0); - } - - inputQueue.push(Token { .type = TokenType::Number, .number = number, .name = "", .arguments = { } }); - } else if (*pos == '(') { - inputQueue.push(Token { .type = TokenType::Bracket, .bracketType = BracketType::Left, .name = "", .arguments = { } }); - pos++; - } else if (*pos == ')') { - inputQueue.push(Token { .type = TokenType::Bracket, .bracketType = BracketType::Right, .name = "", .arguments = { } }); - pos++; - } else if (std::isspace(*pos)) { - pos++; - } else { - auto [op, width] = toOperator(pos); - - if (!inputQueue.empty()) { - auto token = inputQueue.back(); - - if (token.type == TokenType::Operator || (token.type == TokenType::Bracket && token.bracketType == BracketType::Left)) { - if (op == Operator::Addition) - op = Operator::Plus; - else if (op == Operator::Subtraction) - op = Operator::Minus; - } - } - - if (op != Operator::Invalid) { - inputQueue.push(Token { .type = TokenType::Operator, .op = op, .name = "", .arguments = { } }); - pos += width; - } else { - Token token; - - while (std::isalpha(*pos) || *pos == '_') { - token.name += *pos; - pos++; - } - - if (*pos == '(') { - pos++; - - u32 depth = 1; - std::vector expressions; - expressions.emplace_back(); - - while (*pos != 0x00) { - if (*pos == '(') depth++; - else if (*pos == ')') depth--; - - if (depth == 0) - break; - - if (depth == 1 && *pos == ',') { - expressions.emplace_back(); - pos++; - } - - expressions.back() += *pos; - - pos++; - } - - pos++; - - for (const auto &expression : expressions) { - if (expression.empty() && expressions.size() > 1) { - this->setError("Invalid function call syntax!"); - return std::nullopt; - } - else if (expression.empty()) - break; - - auto newInputQueue = parseInput(expression); - if (!newInputQueue.has_value()) - return std::nullopt; - - auto postfixTokens = toPostfix(*newInputQueue); - if (!postfixTokens.has_value()) - return std::nullopt; - - auto result = evaluate(*postfixTokens); - if (!result.has_value()) { - this->setError("Invalid argument for function!"); - return std::nullopt; - } - - token.arguments.push_back(result.value()); - } - - token.type = TokenType::Function; - inputQueue.push(token); - - } else { - token.type = TokenType::Variable; - inputQueue.push(token); - } - } - } - - if (prevPos == pos) { - this->setError("Invalid syntax!"); - return std::nullopt; - } - - prevPos = pos; - } - - return inputQueue; - } - - template - std::optional MathEvaluator::evaluate(std::queue postfixTokens) { - std::stack evaluationStack; - - while (!postfixTokens.empty()) { - auto front = postfixTokens.front(); - postfixTokens.pop(); - - if (front.type == TokenType::Number) - evaluationStack.push(front.number); - else if (front.type == TokenType::Operator) { - T rightOperand, leftOperand; - if (isUnary(front.op)) { - if (evaluationStack.size() < 1) { - this->setError("Not enough operands for operator!"); - return std::nullopt; - } else { - rightOperand = evaluationStack.top(); - evaluationStack.pop(); - leftOperand = 0; - } - } else { - if (evaluationStack.size() < 2) { - this->setError("Not enough operands for operator!"); - return std::nullopt; - } else { - rightOperand = evaluationStack.top(); - evaluationStack.pop(); - leftOperand = evaluationStack.top(); - evaluationStack.pop(); - } - } - - T result = [] { - if constexpr (std::numeric_limits::has_quiet_NaN) - return std::numeric_limits::quiet_NaN(); - else - return 0; - }(); - switch (front.op) { - default: - case Operator::Invalid: - this->setError("Invalid operator!"); - return std::nullopt; - case Operator::And: - result = static_cast(leftOperand) && static_cast(rightOperand); - break; - case Operator::Or: - result = static_cast(leftOperand) || static_cast(rightOperand); - break; - case Operator::Xor: - result = (static_cast(leftOperand) ^ static_cast(rightOperand)) > 0; - break; - case Operator::GreaterThan: - result = leftOperand > rightOperand; - break; - case Operator::LessThan: - result = leftOperand < rightOperand; - break; - case Operator::GreaterThanOrEquals: - result = leftOperand >= rightOperand; - break; - case Operator::LessThanOrEquals: - result = leftOperand <= rightOperand; - break; - case Operator::Equals: - result = leftOperand == rightOperand; - break; - case Operator::NotEquals: - result = leftOperand != rightOperand; - break; - case Operator::Not: - result = !static_cast(rightOperand); - break; - case Operator::BitwiseOr: - result = static_cast(leftOperand) | static_cast(rightOperand); - break; - case Operator::BitwiseXor: - result = static_cast(leftOperand) ^ static_cast(rightOperand); - break; - case Operator::BitwiseAnd: - result = static_cast(leftOperand) & static_cast(rightOperand); - break; - case Operator::BitwiseNot: - result = ~static_cast(rightOperand); - break; - case Operator::ShiftLeft: - result = static_cast(leftOperand) << static_cast(rightOperand); - break; - case Operator::ShiftRight: - result = static_cast(leftOperand) >> static_cast(rightOperand); - break; - case Operator::Addition: - result = leftOperand + rightOperand; - break; - case Operator::Subtraction: - result = leftOperand - rightOperand; - break; - case Operator::Multiplication: - result = leftOperand * rightOperand; - break; - case Operator::Division: - result = leftOperand / rightOperand; - break; - case Operator::Modulus: - if constexpr (std::floating_point) - result = std::fmod(leftOperand, rightOperand); - else - result = leftOperand % rightOperand; - break; - case Operator::Exponentiation: - if constexpr (std::floating_point) - result = std::pow(leftOperand, rightOperand); - else - result = hex::powi(leftOperand, rightOperand); - break; - case Operator::Combine: - result = (static_cast(leftOperand) << (64 - __builtin_clzll(static_cast(rightOperand)))) | static_cast(rightOperand); - break; - case Operator::Plus: - result = +rightOperand; - break; - case Operator::Minus: - result = -rightOperand; - break; - } - - evaluationStack.push(result); - } else if (front.type == TokenType::Variable) { - if (this->m_variables.contains(front.name)) - evaluationStack.push(this->m_variables.at(front.name).value); - else { - this->setError("Unknown variable!"); - return std::nullopt; - } - } else if (front.type == TokenType::Function) { - if (!this->m_functions[front.name]) { - this->setError("Unknown function called!"); - return std::nullopt; - } - - auto result = this->m_functions[front.name](front.arguments); - - if (result.has_value()) - evaluationStack.push(result.value()); - } else { - this->setError("Parenthesis in postfix expression!"); - return std::nullopt; - } - } - - if (evaluationStack.empty()) { - return std::nullopt; - } - else if (evaluationStack.size() > 1) { - this->setError("Undigested input left!"); - return std::nullopt; - } - else { - return evaluationStack.top(); - } - } - - - template - std::optional MathEvaluator::evaluate(const std::string &input) { - auto inputQueue = parseInput(input); - if (!inputQueue.has_value() || inputQueue->empty()) - return std::nullopt; - - std::string resultVariable = "ans"; - - { - auto queueCopy = *inputQueue; - if (queueCopy.front().type == TokenType::Variable && queueCopy.size() > 2) { - resultVariable = queueCopy.front().name; - queueCopy.pop(); - if (queueCopy.front().type != TokenType::Operator || queueCopy.front().op != Operator::Assign) - resultVariable = "ans"; - else { - inputQueue->pop(); - inputQueue->pop(); - } - } - } - - auto postfixTokens = toPostfix(*inputQueue); - if (!postfixTokens.has_value()) - return std::nullopt; - - auto result = evaluate(*postfixTokens); - - if (result.has_value() && !this->getVariables()[resultVariable].constant) - this->setVariable(resultVariable, result.value()); - - return result; - } - - template - void MathEvaluator::setVariable(const std::string &name, T value, bool constant) { - this->m_variables[name] = { value, constant }; - } - - template - void MathEvaluator::setFunction(const std::string &name, const std::function(std::vector)> &function, size_t minNumArgs, size_t maxNumArgs) { - this->m_functions[name] = [this, minNumArgs, maxNumArgs, function](auto args) -> std::optional { - if (args.size() < minNumArgs || args.size() > maxNumArgs) { - this->setError("Invalid number of function arguments!"); - return std::nullopt; - } - - return function(args); - }; - } - - - template - void MathEvaluator::registerStandardVariables() { - this->setVariable("ans", 0); - this->setVariable("pi", std::numbers::pi, true); - this->setVariable("e", std::numbers::e, true); - this->setVariable("phi", std::numbers::phi, true); - } - - template - void MathEvaluator::registerStandardFunctions() { - if constexpr (std::floating_point) { - this->setFunction( - "sin", [](auto args) { return std::sin(args[0]); }, 1, 1); - this->setFunction( - "cos", [](auto args) { return std::cos(args[0]); }, 1, 1); - this->setFunction( - "tan", [](auto args) { return std::tan(args[0]); }, 1, 1); - this->setFunction( - "sqrt", [](auto args) { return std::sqrt(args[0]); }, 1, 1); - this->setFunction( - "ceil", [](auto args) { return std::ceil(args[0]); }, 1, 1); - this->setFunction( - "floor", [](auto args) { return std::floor(args[0]); }, 1, 1); - this->setFunction( - "sign", [](auto args) { return (args[0] > 0) ? 1 : (args[0] == 0) ? 0 : -1; }, 1, 1); - this->setFunction( - "abs", [](auto args) { return std::abs(args[0]); }, 1, 1); - this->setFunction( - "ln", [](auto args) { return std::log(args[0]); }, 1, 1); - this->setFunction( - "lb", [](auto args) { return std::log2(args[0]); }, 1, 1); - this->setFunction( - "log", [](auto args) { return args.size() == 1 ? std::log10(args[0]) : std::log(args[1]) / std::log(args[0]); }, 1, 2); - } - } - - template class MathEvaluator; - template class MathEvaluator; - template class MathEvaluator; - -} diff --git a/plugins/builtin/source/content/tools/color_picker.cpp b/plugins/builtin/source/content/tools/color_picker.cpp index df63ab241..986f5dc64 100644 --- a/plugins/builtin/source/content/tools/color_picker.cpp +++ b/plugins/builtin/source/content/tools/color_picker.cpp @@ -3,7 +3,7 @@ #include -#include +#include #include diff --git a/plugins/builtin/source/content/tools/graphing_calc.cpp b/plugins/builtin/source/content/tools/graphing_calc.cpp index 21668b89c..82e786670 100644 --- a/plugins/builtin/source/content/tools/graphing_calc.cpp +++ b/plugins/builtin/source/content/tools/graphing_calc.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include @@ -31,7 +31,7 @@ namespace hex::plugin::builtin { ImGui::PopItemWidth(); if ((prevPos != limits.X.Min && (ImGui::IsMouseReleased(ImGuiMouseButton_Left) || ImGui::GetIO().MouseWheel != 0)) || (ImGui::IsItemFocused() && ImGui::IsKeyPressed(ImGuiKey_Enter))) { - MathEvaluator evaluator; + wolv::math_eval::MathEvaluator evaluator; y = {}; diff --git a/plugins/builtin/source/content/tools/math_eval.cpp b/plugins/builtin/source/content/tools/math_eval.cpp index d58bb9ff7..40614e6ab 100644 --- a/plugins/builtin/source/content/tools/math_eval.cpp +++ b/plugins/builtin/source/content/tools/math_eval.cpp @@ -2,7 +2,8 @@ #include #include #include -#include + +#include #include #include @@ -20,8 +21,8 @@ namespace hex::plugin::builtin { static std::string mathInput; bool evaluate = false; - static MathEvaluator mathEvaluator = [&] { - MathEvaluator evaluator; + static wolv::math_eval::MathEvaluator mathEvaluator = [&] { + wolv::math_eval::MathEvaluator evaluator; evaluator.registerStandardVariables(); evaluator.registerStandardFunctions(); diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index c422610be..90b08ac63 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -10,8 +10,9 @@ #include +#include + #include -#include #include #include @@ -101,7 +102,7 @@ namespace hex::plugin::builtin { bool m_requestFocus = true; std::string m_input; - MathEvaluator m_evaluator; + wolv::math_eval::MathEvaluator m_evaluator; }; class PopupSelect : public ViewHexEditor::Popup { diff --git a/plugins/builtin/source/ui/pattern_drawer.cpp b/plugins/builtin/source/ui/pattern_drawer.cpp index 124a2f698..23c7c0ef3 100644 --- a/plugins/builtin/source/ui/pattern_drawer.cpp +++ b/plugins/builtin/source/ui/pattern_drawer.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include @@ -450,7 +450,7 @@ namespace hex::plugin::builtin::ui { } } else if (std::holds_alternative(value)) { if (ImGui::InputText("##Value", valueString, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) { - MathEvaluator mathEvaluator; + wolv::math_eval::MathEvaluator mathEvaluator; if (auto result = mathEvaluator.evaluate(valueString); result.has_value()) pattern.setValue(result.value()); @@ -459,7 +459,7 @@ namespace hex::plugin::builtin::ui { } } else if (std::holds_alternative(value)) { if (ImGui::InputText("##Value", valueString, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) { - MathEvaluator mathEvaluator; + wolv::math_eval::MathEvaluator mathEvaluator; if (auto result = mathEvaluator.evaluate(valueString); result.has_value()) pattern.setValue(result.value()); @@ -614,7 +614,7 @@ namespace hex::plugin::builtin::ui { auto value = pattern.toString(); if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) { - MathEvaluator mathEvaluator; + wolv::math_eval::MathEvaluator mathEvaluator; if (auto result = mathEvaluator.evaluate(value); result.has_value()) pattern.setValue(double(result.value())); @@ -670,7 +670,7 @@ namespace hex::plugin::builtin::ui { auto value = pattern.getFormattedValue(); if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) { - MathEvaluator mathEvaluator; + wolv::math_eval::MathEvaluator mathEvaluator; if (auto result = mathEvaluator.evaluate(value); result.has_value()) pattern.setValue(result.value()); @@ -817,7 +817,7 @@ namespace hex::plugin::builtin::ui { ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); auto value = pattern.toString(); if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) { - MathEvaluator mathEvaluator; + wolv::math_eval::MathEvaluator mathEvaluator; if (auto result = mathEvaluator.evaluate(value); result.has_value()) pattern.setValue(result.value());