mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
Added functions with string literals as parameter
This commit is contained in:
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace hex::lang {
|
||||
|
||||
#define BUILTIN_FUNCTION(name) ASTNodeIntegerLiteral* Evaluator::name(std::vector<ASTNodeIntegerLiteral*> params)
|
||||
#define BUILTIN_FUNCTION(name) ASTNodeIntegerLiteral* Evaluator::TOKEN_CONCAT(builtin_, name)(std::vector<ASTNode*> params)
|
||||
|
||||
#define LITERAL_COMPARE(literal, cond) std::visit([&, this](auto &&literal) { return (cond) != 0; }, literal)
|
||||
|
||||
BUILTIN_FUNCTION(findSequence) {
|
||||
auto& occurrenceIndex = params[0]->getValue();
|
||||
auto& occurrenceIndex = asType<ASTNodeIntegerLiteral>(params[0])->getValue();
|
||||
std::vector<u8> sequence;
|
||||
for (u32 i = 1; i < params.size(); i++) {
|
||||
sequence.push_back(std::visit([](auto &&value) -> u8 {
|
||||
@@ -15,7 +15,7 @@ namespace hex::lang {
|
||||
return value;
|
||||
else
|
||||
throwEvaluateError("sequence bytes need to fit into 1 byte", 1);
|
||||
}, params[i]->getValue()));
|
||||
}, asType<ASTNodeIntegerLiteral>(params[i])->getValue()));
|
||||
}
|
||||
|
||||
std::vector<u8> bytes(sequence.size(), 0x00);
|
||||
@@ -37,8 +37,8 @@ namespace hex::lang {
|
||||
}
|
||||
|
||||
BUILTIN_FUNCTION(readUnsigned) {
|
||||
auto address = params[0]->getValue();
|
||||
auto size = params[1]->getValue();
|
||||
auto address = asType<ASTNodeIntegerLiteral>(params[0])->getValue();
|
||||
auto size = asType<ASTNodeIntegerLiteral>(params[1])->getValue();
|
||||
|
||||
if (LITERAL_COMPARE(address, address >= this->m_provider->getActualSize()))
|
||||
throwEvaluateError("address out of range", 1);
|
||||
@@ -62,8 +62,8 @@ namespace hex::lang {
|
||||
}
|
||||
|
||||
BUILTIN_FUNCTION(readSigned) {
|
||||
auto address = params[0]->getValue();
|
||||
auto size = params[1]->getValue();
|
||||
auto address = asType<ASTNodeIntegerLiteral>(params[0])->getValue();
|
||||
auto size = asType<ASTNodeIntegerLiteral>(params[1])->getValue();
|
||||
|
||||
if (LITERAL_COMPARE(address, address >= this->m_provider->getActualSize()))
|
||||
throwEvaluateError("address out of range", 1);
|
||||
@@ -76,13 +76,13 @@ namespace hex::lang {
|
||||
this->m_provider->read(address, value, size);
|
||||
|
||||
switch ((u8)size) {
|
||||
case 1: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed8Bit, hex::changeEndianess(*reinterpret_cast<s8*>(value), 1, this->getCurrentEndian()) });
|
||||
case 2: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed16Bit, hex::changeEndianess(*reinterpret_cast<s16*>(value), 2, this->getCurrentEndian()) });
|
||||
case 4: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed32Bit, hex::changeEndianess(*reinterpret_cast<s32*>(value), 4, this->getCurrentEndian()) });
|
||||
case 8: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed64Bit, hex::changeEndianess(*reinterpret_cast<s64*>(value), 8, this->getCurrentEndian()) });
|
||||
case 16: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed128Bit, hex::changeEndianess(*reinterpret_cast<s128*>(value), 16, this->getCurrentEndian()) });
|
||||
default: throwEvaluateError("invalid read size", 1);
|
||||
}
|
||||
case 1: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed8Bit, hex::changeEndianess(*reinterpret_cast<s8*>(value), 1, this->getCurrentEndian()) });
|
||||
case 2: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed16Bit, hex::changeEndianess(*reinterpret_cast<s16*>(value), 2, this->getCurrentEndian()) });
|
||||
case 4: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed32Bit, hex::changeEndianess(*reinterpret_cast<s32*>(value), 4, this->getCurrentEndian()) });
|
||||
case 8: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed64Bit, hex::changeEndianess(*reinterpret_cast<s64*>(value), 8, this->getCurrentEndian()) });
|
||||
case 16: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed128Bit, hex::changeEndianess(*reinterpret_cast<s128*>(value), 16, this->getCurrentEndian()) });
|
||||
default: throwEvaluateError("invalid read size", 1);
|
||||
}
|
||||
}, address, size);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,15 +14,15 @@ namespace hex::lang {
|
||||
: m_provider(provider), m_defaultDataEndian(defaultDataEndian) {
|
||||
|
||||
this->addFunction("findSequence", Function::MoreParametersThan | 1, [this](auto params) {
|
||||
return this->findSequence(params);
|
||||
return this->builtin_findSequence(params);
|
||||
});
|
||||
|
||||
this->addFunction("readUnsigned", 2, [this](auto params) {
|
||||
return this->readUnsigned(params);
|
||||
return this->builtin_readUnsigned(params);
|
||||
});
|
||||
|
||||
this->addFunction("readSigned", 2, [this](auto params) {
|
||||
return this->readSigned(params);
|
||||
return this->builtin_readSigned(params);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -126,14 +126,18 @@ namespace hex::lang {
|
||||
}
|
||||
|
||||
ASTNodeIntegerLiteral* Evaluator::evaluateFunctionCall(ASTNodeFunctionCall *node) {
|
||||
std::vector<ASTNodeIntegerLiteral*> evaluatedParams;
|
||||
std::vector<ASTNode*> evaluatedParams;
|
||||
ScopeExit paramCleanup([&] {
|
||||
for (auto ¶m : evaluatedParams)
|
||||
delete param;
|
||||
});
|
||||
|
||||
for (auto ¶m : node->getParams())
|
||||
evaluatedParams.push_back(this->evaluateMathematicalExpression(static_cast<ASTNodeNumericExpression*>(param)));
|
||||
for (auto ¶m : node->getParams()) {
|
||||
if (auto numericExpression = dynamic_cast<ASTNodeNumericExpression*>(param); numericExpression != nullptr)
|
||||
evaluatedParams.push_back(this->evaluateMathematicalExpression(numericExpression));
|
||||
else if (auto stringLiteral = dynamic_cast<ASTNodeStringLiteral*>(param); stringLiteral != nullptr)
|
||||
evaluatedParams.push_back(stringLiteral->clone());
|
||||
}
|
||||
|
||||
if (!this->m_functions.contains(node->getFunctionName().data()))
|
||||
throwEvaluateError(hex::format("no function named '%s' found", node->getFunctionName().data()), node->getLineNumber());
|
||||
@@ -278,8 +282,14 @@ namespace hex::lang {
|
||||
return evaluateScopeResolution(exprScopeResolution);
|
||||
else if (auto exprTernary = dynamic_cast<ASTNodeTernaryExpression*>(node); exprTernary != nullptr)
|
||||
return evaluateTernaryExpression(exprTernary);
|
||||
else if (auto exprFunctionCall = dynamic_cast<ASTNodeFunctionCall*>(node); exprFunctionCall != nullptr)
|
||||
return evaluateFunctionCall(exprFunctionCall);
|
||||
else if (auto exprFunctionCall = dynamic_cast<ASTNodeFunctionCall*>(node); exprFunctionCall != nullptr) {
|
||||
auto returnValue = evaluateFunctionCall(exprFunctionCall);
|
||||
|
||||
if (returnValue == nullptr)
|
||||
throwEvaluateError("function returning void used in expression", node->getLineNumber());
|
||||
else
|
||||
return returnValue;
|
||||
}
|
||||
else
|
||||
throwEvaluateError("invalid operand", node->getLineNumber());
|
||||
}
|
||||
@@ -675,6 +685,9 @@ namespace hex::lang {
|
||||
this->m_globalMembers.push_back(this->evaluatePointer(pointerDeclNode));
|
||||
} else if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(node); typeDeclNode != nullptr) {
|
||||
this->m_types[typeDeclNode->getName().data()] = typeDeclNode->getType();
|
||||
} else if (auto functionCallNode = dynamic_cast<ASTNodeFunctionCall*>(node); functionCallNode != nullptr) {
|
||||
auto result = this->evaluateFunctionCall(functionCallNode);
|
||||
delete result;
|
||||
}
|
||||
|
||||
this->m_endianStack.pop_back();
|
||||
|
||||
@@ -28,7 +28,10 @@ namespace hex::lang {
|
||||
});
|
||||
|
||||
while (!MATCHES(sequence(SEPARATOR_ROUNDBRACKETCLOSE))) {
|
||||
params.push_back(parseMathematicalExpression());
|
||||
if (MATCHES(sequence(STRING)))
|
||||
params.push_back(parseStringLiteral());
|
||||
else
|
||||
params.push_back(parseMathematicalExpression());
|
||||
|
||||
if (MATCHES(sequence(SEPARATOR_COMMA, SEPARATOR_ROUNDBRACKETCLOSE)))
|
||||
throwParseError("unexpected ',' at end of function parameter list", -1);
|
||||
@@ -41,7 +44,11 @@ namespace hex::lang {
|
||||
|
||||
paramCleanup.release();
|
||||
|
||||
return TO_NUMERIC_EXPRESSION(new ASTNodeFunctionCall(functionName, params));
|
||||
return new ASTNodeFunctionCall(functionName, params);
|
||||
}
|
||||
|
||||
ASTNode* Parser::parseStringLiteral() {
|
||||
return new ASTNodeStringLiteral(getValue<std::string>(-1));
|
||||
}
|
||||
|
||||
// Identifier::<Identifier[::]...>
|
||||
@@ -86,7 +93,7 @@ namespace hex::lang {
|
||||
this->m_curr--;
|
||||
return this->parseScopeResolution(path);
|
||||
} else if (MATCHES(sequence(IDENTIFIER, SEPARATOR_ROUNDBRACKETOPEN))) {
|
||||
return this->parseFunctionCall();
|
||||
return TO_NUMERIC_EXPRESSION(this->parseFunctionCall());
|
||||
} else if (MATCHES(sequence(IDENTIFIER))) {
|
||||
std::vector<std::string> path;
|
||||
return this->parseRValue(path);
|
||||
@@ -591,6 +598,8 @@ namespace hex::lang {
|
||||
statement = parseEnum();
|
||||
else if (MATCHES(sequence(KEYWORD_BITFIELD, IDENTIFIER, SEPARATOR_CURLYBRACKETOPEN)))
|
||||
statement = parseBitfield();
|
||||
else if (MATCHES(sequence(IDENTIFIER, SEPARATOR_ROUNDBRACKETOPEN)))
|
||||
statement = parseFunctionCall();
|
||||
else throwParseError("invalid sequence", 0);
|
||||
|
||||
if (!MATCHES(sequence(SEPARATOR_ENDOFEXPRESSION)))
|
||||
|
||||
Reference in New Issue
Block a user