patterns: Make default parameters work properly with parameter packs

This commit is contained in:
WerWolv 2022-03-25 09:53:58 +01:00
parent 368c943040
commit e72a30ca59
2 changed files with 12 additions and 6 deletions

View File

@ -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) {

View File

@ -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]);