diff --git a/NetPrints/Translator/ExecutionGraphTranslator.cs b/NetPrints/Translator/ExecutionGraphTranslator.cs index a212941..eb84082 100644 --- a/NetPrints/Translator/ExecutionGraphTranslator.cs +++ b/NetPrints/Translator/ExecutionGraphTranslator.cs @@ -119,24 +119,33 @@ private bool TryGetConstantValue(NodeInputDataPin pin, out T value) private string GetPinIncomingValue(NodeInputDataPin pin) { - if (pin.IncomingPin == null) + if(TryGetPinIncomingValue(pin, out var value)) { - return TranslatorUtil.GetUnconnectedValue(pin); + return value; } - return GetOrCreatePinName(pin.IncomingPin); + throw TranslatorUtil.NoValue(pin); } - private string GetPinIncomingValueOrDefault(NodeInputDataPin pin) + private bool TryGetPinIncomingValue(NodeInputDataPin pin, out string value) { - try + if(pin.IncomingPin is {} incomingPin) { - return GetPinIncomingValue(pin); + value = GetOrCreatePinName(incomingPin); + return true; } - catch + + return TranslatorUtil.TryGetUnconnectedValue(pin, out value); + } + + private string GetPinIncomingValueOrDefault(NodeInputDataPin pin) + { + if(TryGetPinIncomingValue(pin, out var value) == false) { - return $"default({pin.PinType.Value.FullCodeName})"; + value = $"default({pin.PinType.Value.FullCodeName})"; } + + return value; } private string[] GetOrCreatePinNames(IEnumerable pins) @@ -525,7 +534,29 @@ public void TranslateCallMethodNode(CallMethodNode node) } // Get arguments for method call - var argumentValues = GetPinIncomingValues(node.ArgumentPins); + var argumentPins = node.ArgumentPins; + var argumentValues = new string[argumentPins.Count]; + for(int i = 0; i < argumentValues.Length; i++) + { + var arg = argumentPins[i]; + if(TryGetPinIncomingValue(arg, out var argValue)) + { + argumentValues[i] = argValue; + } + else if + ( + this.graph is MethodGraph method && + (method.Modifiers.Value & MethodModifiers.Static) == 0 && + this.graph.Class.AllBaseTypes.Any(x => x == arg.PinType.Value) + ) + { + argumentValues[i] = "this"; + } + else + { + throw TranslatorUtil.NoValue(arg); + } + } // Check whether the method is an operator and we need to translate its name // into operator symbols. Otherwise just call the method normally. diff --git a/NetPrints/Translator/TranslatorUtil.cs b/NetPrints/Translator/TranslatorUtil.cs index 144ff52..1b41729 100644 --- a/NetPrints/Translator/TranslatorUtil.cs +++ b/NetPrints/Translator/TranslatorUtil.cs @@ -288,6 +288,11 @@ public static string FormatCode(string code) return formatted.ToFullString(); } + public static Exception NoValue(NodeInputDataPin pin) + { + return new($"Input data pin {pin} on {pin.Node} was unconnected without an explicit default or unconnected value."); + } + public static string GetUnconnectedValue(NodeInputDataPin pin) { if(TryGetUnconnectedValue(pin, out var value)) @@ -295,7 +300,7 @@ public static string GetUnconnectedValue(NodeInputDataPin pin) return value; } - throw new Exception($"Input data pin {pin} on {pin.Node} was unconnected without an explicit default or unconnected value."); + throw NoValue(pin); } public static bool TryGetUnconnectedValue(NodeInputDataPin pin, out string value)