From 1955515f22ac5a246f3527da7c9503addf8286f4 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 20 Jan 2019 23:34:46 +0100 Subject: [PATCH] Override function if function with exact same parameters was already defined --- .../ScriptFunction/ScriptRuntimeFunction.cs | 30 +++++++++++++++++++ Upsilon/Evaluator/Evaluator.cs | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs b/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs index 1132449..bd18d2b 100644 --- a/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs +++ b/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs @@ -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 { diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index efa004a..1fbb9fd 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -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 {