2020-11-19 11:36:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/lang/pattern_data.hpp>
|
|
|
|
#include <hex/lang/ast_node.hpp>
|
2020-11-19 11:36:52 +01:00
|
|
|
|
2020-11-22 16:22:02 +01:00
|
|
|
#include <bit>
|
2020-11-19 11:36:52 +01:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace hex::lang {
|
|
|
|
|
|
|
|
class Evaluator {
|
|
|
|
public:
|
2021-01-09 23:48:42 +01:00
|
|
|
enum ConsoleLogLevel {
|
|
|
|
Debug,
|
|
|
|
Info,
|
|
|
|
Warning,
|
|
|
|
Error
|
|
|
|
};
|
2021-01-07 15:37:37 +01:00
|
|
|
|
2021-01-09 23:48:42 +01:00
|
|
|
Evaluator(prv::Provider* &provider, std::endian defaultDataEndian);
|
|
|
|
|
|
|
|
std::optional<std::vector<PatternData*>> evaluate(const std::vector<ASTNode*>& ast);
|
|
|
|
const auto& getConsoleLog() { return this->m_consoleLog; }
|
|
|
|
|
2020-11-19 11:36:52 +01:00
|
|
|
private:
|
2021-01-06 16:28:41 +01:00
|
|
|
std::map<std::string, ASTNode*> m_types;
|
2020-11-21 20:19:33 +01:00
|
|
|
prv::Provider* &m_provider;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
std::endian m_defaultDataEndian;
|
|
|
|
u64 m_currOffset = 0;
|
2021-01-08 15:03:53 +01:00
|
|
|
std::vector<std::endian> m_endianStack;
|
2021-01-08 17:37:05 +01:00
|
|
|
std::vector<PatternData*> m_globalMembers;
|
2021-01-04 16:13:03 +01:00
|
|
|
std::vector<std::vector<PatternData*>*> m_currMembers;
|
2020-11-22 16:22:02 +01:00
|
|
|
|
2021-01-09 23:48:42 +01:00
|
|
|
std::vector<std::pair<ConsoleLogLevel, std::string>> m_consoleLog;
|
|
|
|
|
|
|
|
using EvaluateError = std::string;
|
|
|
|
|
|
|
|
void emmitDebugInfo(std::string_view message) {
|
|
|
|
this->m_consoleLog.emplace_back(ConsoleLogLevel::Debug, "[-] " + std::string(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void emmitInfo(std::string_view message) {
|
|
|
|
this->m_consoleLog.emplace_back(ConsoleLogLevel::Info, "[i] " + std::string(message));
|
|
|
|
}
|
2020-11-19 11:36:52 +01:00
|
|
|
|
2021-01-09 23:48:42 +01:00
|
|
|
void emmitWaring(std::string_view message) {
|
|
|
|
this->m_consoleLog.emplace_back(ConsoleLogLevel::Warning, "[*] " + std::string(message));
|
|
|
|
}
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
2021-01-09 23:48:42 +01:00
|
|
|
[[noreturn]] static void throwEvaluateError(std::string_view message) {
|
|
|
|
throw EvaluateError("[!] " + std::string(message));
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::endian getCurrentEndian() const {
|
2021-01-08 15:03:53 +01:00
|
|
|
return this->m_endianStack.back();
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 16:28:41 +01:00
|
|
|
ASTNodeIntegerLiteral* evaluateScopeResolution(ASTNodeScopeResolution *node);
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
ASTNodeIntegerLiteral* evaluateRValue(ASTNodeRValue *node);
|
2021-01-11 23:54:12 +01:00
|
|
|
ASTNode* evaluateFunctionCall(ASTNodeFunctionCall *node);
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
ASTNodeIntegerLiteral* evaluateOperator(ASTNodeIntegerLiteral *left, ASTNodeIntegerLiteral *right, Token::Operator op);
|
2021-01-07 01:19:54 +01:00
|
|
|
ASTNodeIntegerLiteral* evaluateOperand(ASTNode *node);
|
|
|
|
ASTNodeIntegerLiteral* evaluateTernaryExpression(ASTNodeTernaryExpression *node);
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
ASTNodeIntegerLiteral* evaluateMathematicalExpression(ASTNodeNumericExpression *node);
|
|
|
|
|
|
|
|
PatternData* evaluateBuiltinType(ASTNodeBuiltinType *node);
|
2021-01-10 19:40:44 +01:00
|
|
|
void evaluateMember(ASTNode *node, std::vector<PatternData*> &currMembers, bool increaseOffset);
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
PatternData* evaluateStruct(ASTNodeStruct *node);
|
|
|
|
PatternData* evaluateUnion(ASTNodeUnion *node);
|
|
|
|
PatternData* evaluateEnum(ASTNodeEnum *node);
|
|
|
|
PatternData* evaluateBitfield(ASTNodeBitfield *node);
|
|
|
|
PatternData* evaluateType(ASTNodeTypeDecl *node);
|
|
|
|
PatternData* evaluateVariable(ASTNodeVariableDecl *node);
|
|
|
|
PatternData* evaluateArray(ASTNodeArrayVariableDecl *node);
|
|
|
|
PatternData* evaluatePointer(ASTNodePointerVariableDecl *node);
|
2020-11-19 11:36:52 +01:00
|
|
|
|
2021-01-09 21:47:11 +01:00
|
|
|
template<typename T>
|
|
|
|
T* asType(ASTNode *param) {
|
|
|
|
if (auto evaluatedParam = dynamic_cast<T*>(param); evaluatedParam != nullptr)
|
|
|
|
return evaluatedParam;
|
|
|
|
else
|
2021-01-09 23:48:42 +01:00
|
|
|
throwEvaluateError("function got wrong type of parameter");
|
2021-01-09 21:47:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 23:54:12 +01:00
|
|
|
void registerBuiltinFunctions();
|
2021-01-09 21:47:11 +01:00
|
|
|
|
|
|
|
#define BUILTIN_FUNCTION(name) ASTNodeIntegerLiteral* TOKEN_CONCAT(builtin_, name)(std::vector<ASTNode*> params)
|
2021-01-07 15:37:37 +01:00
|
|
|
|
|
|
|
BUILTIN_FUNCTION(findSequence);
|
|
|
|
BUILTIN_FUNCTION(readUnsigned);
|
|
|
|
BUILTIN_FUNCTION(readSigned);
|
|
|
|
|
2021-01-09 21:47:46 +01:00
|
|
|
BUILTIN_FUNCTION(assert);
|
2021-01-09 23:48:42 +01:00
|
|
|
BUILTIN_FUNCTION(warnAssert);
|
|
|
|
BUILTIN_FUNCTION(print);
|
2021-01-09 21:47:46 +01:00
|
|
|
|
2021-01-07 15:37:37 +01:00
|
|
|
#undef BUILTIN_FUNCTION
|
2020-11-19 11:36:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|