mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-30 13:05:25 -05:00
patterns: Huge refactor of Pattern Language runtime to use smart pointers (#458)
* patterns: Initial work to refactor pattern language to use smart pointers * patterns: Fixed remaining issues, moved patterns to unique files * sys: Added missing includes for macOS
This commit is contained in:
@@ -7,14 +7,43 @@
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
#include <hex/pattern_language/pattern_language.hpp>
|
||||
#include <hex/pattern_language/evaluator.hpp>
|
||||
#include <hex/pattern_language/ast_node.hpp>
|
||||
#include <hex/pattern_language/ast/ast_node.hpp>
|
||||
#include <hex/pattern_language/patterns/pattern.hpp>
|
||||
#include <hex/api/content_registry.hpp>
|
||||
|
||||
#include "test_provider.hpp"
|
||||
#include "test_patterns/test_pattern.hpp"
|
||||
|
||||
#include <fmt/args.h>
|
||||
|
||||
using namespace hex::test;
|
||||
|
||||
static std::string format(hex::pl::Evaluator *ctx, const auto ¶ms) {
|
||||
auto format = hex::pl::Token::literalToString(params[0], true);
|
||||
std::string message;
|
||||
|
||||
fmt::dynamic_format_arg_store<fmt::format_context> formatArgs;
|
||||
|
||||
for (u32 i = 1; i < params.size(); i++) {
|
||||
auto ¶m = params[i];
|
||||
|
||||
std::visit(hex::overloaded {
|
||||
[&](const std::shared_ptr<hex::pl::Pattern> &value) {
|
||||
formatArgs.push_back(value->toString(ctx->getProvider()));
|
||||
},
|
||||
[&](auto &&value) {
|
||||
formatArgs.push_back(value);
|
||||
} },
|
||||
param);
|
||||
}
|
||||
|
||||
try {
|
||||
return fmt::vformat(format, formatArgs);
|
||||
} catch (fmt::format_error &error) {
|
||||
hex::pl::LogConsole::abortEvaluation(hex::format("format error: {}", error.what()));
|
||||
}
|
||||
}
|
||||
|
||||
void addFunctions() {
|
||||
hex::ContentRegistry::PatternLanguage::Namespace nsStd = { "std" };
|
||||
hex::ContentRegistry::PatternLanguage::addFunction(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> Token::Literal {
|
||||
@@ -26,6 +55,12 @@ void addFunctions() {
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
hex::ContentRegistry::PatternLanguage::addFunction(nsStd, "print", hex::ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
||||
ctx->getConsole().log(LogConsole::Level::Info, format(ctx, params));
|
||||
|
||||
return std::nullopt;
|
||||
});
|
||||
}
|
||||
|
||||
int test(int argc, char **argv) {
|
||||
@@ -55,7 +90,6 @@ int test(int argc, char **argv) {
|
||||
}
|
||||
|
||||
hex::pl::PatternLanguage language;
|
||||
addFunctions();
|
||||
|
||||
// Check if compilation succeeded
|
||||
auto result = language.executeString(provider, testPatterns[testName]->getSourceCode());
|
||||
@@ -75,11 +109,6 @@ int test(int argc, char **argv) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
ON_SCOPE_EXIT {
|
||||
for (auto &pattern : language.getPatterns())
|
||||
delete pattern;
|
||||
};
|
||||
|
||||
// Check if the right number of patterns have been produced
|
||||
if (language.getPatterns().size() != currTest->getPatterns().size() && !currTest->getPatterns().empty()) {
|
||||
hex::log::fatal("Source didn't produce expected number of patterns");
|
||||
@@ -103,6 +132,8 @@ int test(int argc, char **argv) {
|
||||
int main(int argc, char **argv) {
|
||||
int result = EXIT_SUCCESS;
|
||||
|
||||
addFunctions();
|
||||
|
||||
for (u32 i = 0; i < 16; i++) {
|
||||
result = test(argc, argv);
|
||||
if (result != EXIT_SUCCESS)
|
||||
|
||||
Reference in New Issue
Block a user