2020-11-19 11:36:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
2020-11-21 20:19:33 +01:00
|
|
|
#include "providers/provider.hpp"
|
2020-11-19 11:36:52 +01:00
|
|
|
#include "lang/pattern_data.hpp"
|
|
|
|
#include "ast_node.hpp"
|
|
|
|
|
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:
|
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
|
|
|
Evaluator(prv::Provider* &provider, std::endian defaultDataEndian);
|
2020-11-19 11:36:52 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
std::optional<std::vector<PatternData*>> evaluate(const std::vector<ASTNode*>& ast);
|
2020-11-19 11:36:52 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
const std::pair<u32, std::string>& getError() { return this->m_error; }
|
|
|
|
|
2021-01-07 15:37:37 +01:00
|
|
|
|
|
|
|
struct Function {
|
|
|
|
constexpr static u32 UnlimitedParameters = 0xFFFF'FFFF;
|
|
|
|
constexpr static u32 MoreParametersThan = 0x8000'0000;
|
|
|
|
constexpr static u32 LessParametersThan = 0x4000'0000;
|
|
|
|
constexpr static u32 NoParameters = 0x0000'0000;
|
|
|
|
|
|
|
|
u32 parameterCount;
|
|
|
|
std::function<ASTNodeIntegerLiteral*(std::vector<ASTNodeIntegerLiteral*>)> func;
|
|
|
|
};
|
|
|
|
|
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-07 15:37:37 +01:00
|
|
|
std::map<std::string, Function> m_functions;
|
2020-11-22 16:22:02 +01:00
|
|
|
|
2020-11-27 21:20:23 +01:00
|
|
|
std::pair<u32, std::string> m_error;
|
2020-11-19 11:36:52 +01: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 20:27:11 +01: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 {
|
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-07 15:37:37 +01:00
|
|
|
void addFunction(std::string_view name, u32 parameterCount, std::function<ASTNodeIntegerLiteral*(std::vector<ASTNodeIntegerLiteral*>)> func) {
|
|
|
|
if (this->m_functions.contains(name.data()))
|
|
|
|
throwEvaluateError(hex::format("redefinition of function '%s'", name.data()), 1);
|
|
|
|
|
|
|
|
this->m_functions[name.data()] = { parameterCount, func };
|
|
|
|
}
|
|
|
|
|
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-07 15:37:37 +01:00
|
|
|
ASTNodeIntegerLiteral* 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-07 00:02:51 +01:00
|
|
|
std::vector<PatternData*> evaluateMember(ASTNode *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
|
|
|
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-07 15:37:37 +01:00
|
|
|
|
|
|
|
#define BUILTIN_FUNCTION(name) ASTNodeIntegerLiteral* name(std::vector<ASTNodeIntegerLiteral*> params)
|
|
|
|
|
|
|
|
BUILTIN_FUNCTION(findSequence);
|
|
|
|
BUILTIN_FUNCTION(readUnsigned);
|
|
|
|
BUILTIN_FUNCTION(readSigned);
|
|
|
|
|
|
|
|
#undef BUILTIN_FUNCTION
|
2020-11-19 11:36:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|