Added function calling as well as a few builtin functions

This commit is contained in:
WerWolv
2021-01-07 15:37:37 +01:00
parent b47736b595
commit bef20f7808
8 changed files with 240 additions and 12 deletions

View File

@@ -442,4 +442,39 @@ namespace hex::lang {
std::vector<ASTNode*> m_trueBody, m_falseBody;
};
class ASTNodeFunctionCall : public ASTNode {
public:
explicit ASTNodeFunctionCall(std::string_view functionName, std::vector<ASTNode*> params)
: ASTNode(), m_functionName(functionName), m_params(std::move(params)) { }
~ASTNodeFunctionCall() override {
for (auto &param : this->m_params)
delete param;
}
ASTNodeFunctionCall(const ASTNodeFunctionCall &other) : ASTNode(other) {
this->m_functionName = other.m_functionName;
for (auto &param : other.m_params)
this->m_params.push_back(param->clone());
}
ASTNode* clone() const override {
return new ASTNodeFunctionCall(*this);
}
[[nodiscard]] std::string_view getFunctionName() {
return this->m_functionName;
}
[[nodiscard]] const std::vector<ASTNode*>& getParams() const {
return this->m_params;
}
private:
std::string m_functionName;
std::vector<ASTNode*> m_params;
};
}

View File

@@ -21,6 +21,17 @@ namespace hex::lang {
const std::pair<u32, std::string>& getError() { return this->m_error; }
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;
};
private:
std::map<std::string, ASTNode*> m_types;
prv::Provider* &m_provider;
@@ -28,6 +39,7 @@ namespace hex::lang {
u64 m_currOffset = 0;
std::optional<std::endian> m_currEndian;
std::vector<std::vector<PatternData*>*> m_currMembers;
std::map<std::string, Function> m_functions;
std::pair<u32, std::string> m_error;
@@ -41,8 +53,16 @@ namespace hex::lang {
return this->m_currEndian.value_or(this->m_defaultDataEndian);
}
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 };
}
ASTNodeIntegerLiteral* evaluateScopeResolution(ASTNodeScopeResolution *node);
ASTNodeIntegerLiteral* evaluateRValue(ASTNodeRValue *node);
ASTNodeIntegerLiteral* evaluateFunctionCall(ASTNodeFunctionCall *node);
ASTNodeIntegerLiteral* evaluateOperator(ASTNodeIntegerLiteral *left, ASTNodeIntegerLiteral *right, Token::Operator op);
ASTNodeIntegerLiteral* evaluateOperand(ASTNode *node);
ASTNodeIntegerLiteral* evaluateTernaryExpression(ASTNodeTernaryExpression *node);
@@ -59,6 +79,14 @@ namespace hex::lang {
PatternData* evaluateArray(ASTNodeArrayVariableDecl *node);
PatternData* evaluatePointer(ASTNodePointerVariableDecl *node);
#define BUILTIN_FUNCTION(name) ASTNodeIntegerLiteral* name(std::vector<ASTNodeIntegerLiteral*> params)
BUILTIN_FUNCTION(findSequence);
BUILTIN_FUNCTION(readUnsigned);
BUILTIN_FUNCTION(readSigned);
#undef BUILTIN_FUNCTION
};
}

View File

@@ -53,6 +53,7 @@ namespace hex::lang {
return this->m_curr[index].type;
}
ASTNode* parseFunctionCall();
ASTNode* parseScopeResolution(std::vector<std::string> &path);
ASTNode* parseRValue(std::vector<std::string> &path);
ASTNode* parseFactor();