patterns: Rewrite evaluation engine (#306)

* patterns: Rewrite most of the evaluator to mainly use polymorphism instead of just RTTI

* patterns: Fixed a couple of AST memory leaks

* patterns: Parse string operations correctly

* patterns: Various fixes and cleanup

* patterns: Implement primitive function definitions

Function parameters now need to provide their type in the definition

* patterns: Added function variable definition and assignment

* patterns: Added remaining function statements

* patterns: Added unsized and while-sized arrays

* patterns: Added multi variable declarations to functions

* patterns: Added std::format built-in function

* patterns: Allow passing custom types to functions

* patterns: Added attributes and new "format" attribute

* patterns: Use libfmt for std::print instead of custom version

* patterns: Remove unnecessary string compare function

* pattern: Fix preprocessor directives

* patterns: Fix unit tests

* patterns: Added cast expression

* patterns: Handle endianess in function parameters

* patterns: Added casting to different endian

* patterns: Added 'str' type for functions
This commit is contained in:
WerWolv
2021-09-21 21:29:18 +02:00
committed by GitHub
parent ed9e463550
commit c051f5d3e7
25 changed files with 2000 additions and 1903 deletions

View File

@@ -2,6 +2,7 @@
#include <hex/helpers/file.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/pattern_language/preprocessor.hpp>
#include <hex/pattern_language/lexer.hpp>
@@ -61,7 +62,6 @@ namespace hex::pl {
delete this->m_lexer;
delete this->m_parser;
delete this->m_validator;
delete this->m_evaluator;
}
@@ -70,6 +70,10 @@ namespace hex::pl {
this->m_evaluator->getConsole().clear();
this->m_evaluator->setProvider(provider);
for (auto &node : this->m_currAST)
delete node;
this->m_currAST.clear();
auto preprocessedCode = this->m_preprocessor->preprocess(string);
if (!preprocessedCode.has_value()) {
this->m_currError = this->m_preprocessor->getError();
@@ -77,7 +81,7 @@ namespace hex::pl {
}
this->m_evaluator->setDefaultEndian(this->m_defaultEndian);
this->m_evaluator->setRecursionLimit(this->m_recursionLimit);
// this->m_evaluator->setRecursionLimit(this->m_recursionLimit);
auto tokens = this->m_lexer->lex(preprocessedCode.value());
if (!tokens.has_value()) {
@@ -91,22 +95,15 @@ namespace hex::pl {
return { };
}
ON_SCOPE_EXIT {
for(auto &node : ast.value())
delete node;
};
this->m_currAST = ast.value();
auto validatorResult = this->m_validator->validate(ast.value());
if (!validatorResult) {
this->m_currError = this->m_validator->getError();
auto patterns = this->m_evaluator->evaluate(ast.value());
if (!patterns.has_value()) {
this->m_currError = this->m_evaluator->getConsole().getLastHardError();
return { };
}
auto patternData = this->m_evaluator->evaluate(ast.value());
if (!patternData.has_value())
return { };
return patternData.value();
return patterns;
}
std::optional<std::vector<PatternData*>> PatternLanguage::executeFile(prv::Provider *provider, const std::string &path) {