2020-11-19 10:36:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2020-11-21 19:19:33 +00:00
|
|
|
#include "providers/provider.hpp"
|
2020-11-19 10:36:52 +00:00
|
|
|
#include "lang/pattern_data.hpp"
|
|
|
|
#include "ast_node.hpp"
|
|
|
|
|
2020-11-22 15:22:02 +00:00
|
|
|
#include <bit>
|
2020-11-19 10:36:52 +00:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace hex::lang {
|
|
|
|
|
|
|
|
class Evaluator {
|
|
|
|
public:
|
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 19:27:11 +00:00
|
|
|
Evaluator(prv::Provider* &provider, std::endian defaultDataEndian);
|
2020-11-19 10:36:52 +00:00
|
|
|
|
2020-12-22 17:10:01 +00:00
|
|
|
std::optional<std::vector<PatternData*>> evaluate(const std::vector<ASTNode*>& ast);
|
2020-11-19 10:36:52 +00:00
|
|
|
|
2020-11-27 20:20:23 +00:00
|
|
|
const std::pair<u32, std::string>& getError() { return this->m_error; }
|
|
|
|
|
2020-11-19 10:36:52 +00:00
|
|
|
private:
|
|
|
|
std::unordered_map<std::string, ASTNode*> m_types;
|
2020-11-21 19:19:33 +00: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 19:27:11 +00:00
|
|
|
std::endian m_defaultDataEndian;
|
|
|
|
u64 m_currOffset = 0;
|
|
|
|
std::optional<std::endian> m_currEndian;
|
2021-01-04 15:13:03 +00:00
|
|
|
std::vector<std::vector<PatternData*>*> m_currMembers;
|
2020-11-22 15:22:02 +00:00
|
|
|
|
2020-11-27 20:20:23 +00:00
|
|
|
std::pair<u32, std::string> m_error;
|
2020-11-19 10:36:52 +00:00
|
|
|
|
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 19:27:11 +00:00
|
|
|
using EvaluateError = std::pair<u32, std::string>;
|
|
|
|
|
|
|
|
[[noreturn]] static void throwEvaluateError(std::string_view error, u32 lineNumber) {
|
|
|
|
throw EvaluateError(lineNumber, "Evaluator: " + std::string(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::endian getCurrentEndian() const {
|
|
|
|
return this->m_currEndian.value_or(this->m_defaultDataEndian);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTNodeIntegerLiteral* evaluateRValue(ASTNodeRValue *node);
|
|
|
|
ASTNodeIntegerLiteral* evaluateOperator(ASTNodeIntegerLiteral *left, ASTNodeIntegerLiteral *right, Token::Operator op);
|
|
|
|
ASTNodeIntegerLiteral* evaluateMathematicalExpression(ASTNodeNumericExpression *node);
|
|
|
|
|
|
|
|
PatternData* evaluateBuiltinType(ASTNodeBuiltinType *node);
|
|
|
|
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 10:36:52 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|