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 {
|
|
|
|
|
2021-01-21 11:36:58 +01:00
|
|
|
class LogConsole {
|
2020-11-19 11:36:52 +01:00
|
|
|
public:
|
2021-01-21 11:36:58 +01:00
|
|
|
enum Level {
|
2021-01-09 23:48:42 +01:00
|
|
|
Debug,
|
|
|
|
Info,
|
|
|
|
Warning,
|
|
|
|
Error
|
|
|
|
};
|
2021-01-07 15:37:37 +01:00
|
|
|
|
2021-01-21 11:36:58 +01:00
|
|
|
const auto& getLog() { return this->m_consoleLog; }
|
|
|
|
|
|
|
|
using EvaluateError = std::string;
|
|
|
|
|
|
|
|
void log(Level level, std::string_view message) {
|
|
|
|
switch (level) {
|
|
|
|
default:
|
|
|
|
case Level::Debug: this->m_consoleLog.emplace_back(level, "[-] " + std::string(message)); break;
|
|
|
|
case Level::Info: this->m_consoleLog.emplace_back(level, "[i] " + std::string(message)); break;
|
|
|
|
case Level::Warning: this->m_consoleLog.emplace_back(level, "[*] " + std::string(message)); break;
|
|
|
|
case Level::Error: this->m_consoleLog.emplace_back(level, "[!] " + std::string(message)); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:49:30 +01:00
|
|
|
[[noreturn]] void abortEvaluation(std::string_view message) {
|
2021-01-21 11:36:58 +01:00
|
|
|
throw EvaluateError(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::pair<Level, std::string>> m_consoleLog;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Evaluator {
|
|
|
|
public:
|
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);
|
2021-01-21 11:36:58 +01:00
|
|
|
|
2021-01-21 17:49:30 +01:00
|
|
|
LogConsole& getConsole() { return this->m_console; }
|
2021-01-09 23:48:42 +01:00
|
|
|
|
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;
|
2021-01-21 11:36:58 +01:00
|
|
|
LogConsole m_console;
|
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);
|
|
|
|
|
2021-01-20 22:54:46 +01:00
|
|
|
PatternData* patternFromName(const std::vector<std::string> &name);
|
2021-01-21 17:49:30 +01:00
|
|
|
PatternData* evaluateAttributes(ASTNode *currNode, PatternData *currPattern);
|
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* 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-21 11:36:58 +01:00
|
|
|
this->m_console.abortEvaluation("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
|
|
|
|
2021-01-21 11:36:58 +01:00
|
|
|
#define BUILTIN_FUNCTION(name) ASTNodeIntegerLiteral* TOKEN_CONCAT(builtin_, name)(LogConsole &console, 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
|
|
|
};
|
|
|
|
|
|
|
|
}
|