diff --git a/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_call.hpp b/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_call.hpp index a12019f2d..016247afb 100644 --- a/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_call.hpp +++ b/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_call.hpp @@ -76,20 +76,24 @@ namespace hex::pl { auto function = functions[this->m_functionName]; const auto &[min, max] = function.parameterCount; - if (evaluatedParams.size() >= min && evaluatedParams.size() < max && evaluatedParams.size() + function.defaultParameters.size() >= max) { + if (evaluatedParams.size() >= min && evaluatedParams.size() < max) { while (true) { auto remainingParams = max - evaluatedParams.size(); - if (remainingParams <= 0) break; + if (remainingParams <= 0) + break; auto offset = evaluatedParams.size() - min; + if (offset >= function.defaultParameters.size()) + break; + evaluatedParams.push_back(function.defaultParameters[offset]); } } if (evaluatedParams.size() < min) - LogConsole::abortEvaluation(hex::format("too many parameters for function '{0}'. Expected {1} at least", this->m_functionName, min), this); + LogConsole::abortEvaluation(hex::format("too few parameters for function '{0}'. Expected {1} at least", this->m_functionName, min), this); else if (evaluatedParams.size() > max) - LogConsole::abortEvaluation(hex::format("too few parameters for function '{0}'. Expected {1} at most", this->m_functionName, max), this); + LogConsole::abortEvaluation(hex::format("too many parameters for function '{0}'. Expected {1} at most", this->m_functionName, max), this); try { if (function.dangerous && evaluator->getDangerousFunctionPermission() != DangerousFunctionPermission::Allow) { diff --git a/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_definition.hpp b/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_definition.hpp index bc544fbc6..8c015d1dd 100644 --- a/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_definition.hpp +++ b/lib/libimhex/include/hex/pattern_language/ast/ast_node_function_definition.hpp @@ -48,7 +48,9 @@ namespace hex::pl { ParameterCount paramCount; - if (this->m_parameterPack.has_value()) + if (this->m_parameterPack.has_value() && !this->m_defaultParameters.empty()) + paramCount = ParameterCount::atLeast(this->m_params.size() - this->m_defaultParameters.size()); + else if (this->m_parameterPack.has_value()) paramCount = ParameterCount::atLeast(this->m_params.size()); else if (!this->m_defaultParameters.empty()) paramCount = ParameterCount::between(this->m_params.size() - this->m_defaultParameters.size(), this->m_params.size()); @@ -84,7 +86,7 @@ namespace hex::pl { ctx->createParameterPack(this->m_parameterPack.value(), parameterPackContent); } - for (u32 paramIndex = 0; paramIndex < this->m_params.size(); paramIndex++) { + for (u32 paramIndex = 0; paramIndex < this->m_params.size() && paramIndex < params.size(); paramIndex++) { const auto &[name, type] = this->m_params[paramIndex]; ctx->createVariable(name, type.get(), params[paramIndex]);