Skip to content

Commit

Permalink
Automatically inject this to omitted function args that type-match …
Browse files Browse the repository at this point in the history
…current instance
  • Loading branch information
InflexCZE committed May 15, 2021
1 parent d46b23f commit 305a5f0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
49 changes: 40 additions & 9 deletions NetPrints/Translator/ExecutionGraphTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,33 @@ private bool TryGetConstantValue<T>(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<NodeOutputDataPin> pins)
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion NetPrints/Translator/TranslatorUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,19 @@ 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))
{
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)
Expand Down

0 comments on commit 305a5f0

Please sign in to comment.