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:
WerWolv
2022-02-27 23:25:39 +01:00
committed by GitHub
parent b28eaf2dbf
commit 66d1b3fd2f
85 changed files with 4990 additions and 5146 deletions

View File

@@ -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 &params) {
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 &param = 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)