Override function if function with exact same parameters was already defined

This commit is contained in:
Deukhoofd 2019-01-20 23:34:46 +01:00
parent 9aea17c445
commit 1955515f22
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 32 additions and 2 deletions

View File

@ -93,6 +93,36 @@ namespace Upsilon.BaseTypes.ScriptFunction
return null;
}
public void AddOption(ScriptRuntimeFunctionOption newOption)
{
var variables = newOption.Parameters;
for (var i = 0; i < Options.Count; i++)
{
var option = Options[i];
if (option.Parameters.Length != variables.Length)
continue;
bool isCompatible = true;
for (var index = 0; index < variables.Length; index++)
{
var callingVariable = variables[index];
var optionVariable = option.Parameters[index];
if (callingVariable.Type == BaseTypes.Type.Unknown || callingVariable.Type == BaseTypes.Type.Nil ||
optionVariable.Type == BaseTypes.Type.Unknown)
continue;
if (callingVariable.Type != optionVariable.Type)
{
isCompatible = false;
break;
}
}
if (!isCompatible)
continue;
Options[i] = newOption;
break;
}
Options.Add(newOption);
}
public class ScriptRuntimeFunctionOption : IScopeOwner
{

View File

@ -501,7 +501,7 @@ namespace Upsilon.Evaluator
{
if (Scope.Variables.TryGetValue(e.Variable.Name, out var f) && f is ScriptRuntimeFunction scriptRuntimeFunction)
{
scriptRuntimeFunction.Options.AddRange(func.Options);
scriptRuntimeFunction.AddOption(func.Options[0]);
}
else
{
@ -512,7 +512,7 @@ namespace Upsilon.Evaluator
{
if (Scope.TryGet(e.Variable, out var f) && f is ScriptRuntimeFunction scriptRuntimeFunction)
{
scriptRuntimeFunction.Options.AddRange(func.Options);
scriptRuntimeFunction.AddOption(func.Options[0]);
}
else
{