Skip to content

Commit

Permalink
added this[] indexer support
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinKa committed Mar 17, 2019
1 parent 6b76050 commit 4a921f0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
30 changes: 30 additions & 0 deletions NetPrints/Graph/VariableNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ public bool IsStatic
get => Variable.Modifiers.HasFlag(VariableModifiers.Static);
}

/// <summary>
/// Whether this variable node is for an indexer (eg. dict["key"]).
/// </summary>
public bool IsIndexer
{
get => Variable.Name == "this[]";
}

/// <summary>
/// Specifier for the type of the index.
/// </summary>
public BaseType IndexType
{
// TODO: Get indexer type
get => IsIndexer ? TypeSpecifier.FromType<object>() : null;
}

/// <summary>
/// Data pin for the indexer.
/// </summary>
public NodeInputDataPin IndexPin
{
get => IsIndexer ? InputDataPins[1] : null;
}

/// <summary>
/// Specifier for the underlying variable.
/// </summary>
Expand All @@ -71,6 +96,11 @@ public VariableNode(Method method, TypeSpecifier targetType, Variable variable)
AddInputDataPin("Target", targetType);
}

if (IsIndexer)
{
AddInputDataPin("Index", IndexType);
}

AddOutputDataPin(Variable.VariableType.ShortName, Variable.VariableType);
}
}
Expand Down
40 changes: 30 additions & 10 deletions NetPrints/Translator/MethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,27 +562,37 @@ public void TranslateVariableSetterNode(VariableSetterNode node)
{
if (!(node.TargetType is null))
{
builder.Append($"{node.TargetType.FullCodeName}.");
builder.Append(node.TargetType.FullCodeName);
}
else
{
builder.Append($"{node.Method.Class.Name}.");
builder.Append(node.Method.Class.Name);
}
}
if (node.TargetPin != null)
{
if (node.TargetPin.IncomingPin != null)
{
string targetName = GetOrCreatePinName(node.TargetPin.IncomingPin);
builder.Append($"{targetName}.");
builder.Append(targetName);
}
else
{
builder.Append("this.");
builder.Append("this");
}
}

builder.AppendLine($"{node.VariableName} = {valueName};");
// Add index if needed
if (node.IsIndexer)
{
builder.Append($"[{GetPinIncomingValue(node.IndexPin)}]");
}
else
{
builder.Append($".{node.VariableName}");
}

builder.AppendLine($" = {valueName};");

// Set output pin of this node to the same value
builder.AppendLine($"{GetOrCreatePinName(node.OutputDataPins[0])} = {valueName};");
Expand Down Expand Up @@ -692,28 +702,38 @@ public void PureTranslateVariableGetterNode(VariableGetterNode node)
{
if (!(node.TargetType is null))
{
builder.Append($"{node.TargetType.FullCodeName}.");
builder.Append(node.TargetType.FullCodeName);
}
else
{
builder.Append($"{node.Method.Class.Name}.");
builder.Append(node.Method.Class.Name);
}
}
else
{
if (node.TargetPin != null && node.TargetPin.IncomingPin != null)
{
string targetName = GetOrCreatePinName(node.TargetPin.IncomingPin);
builder.Append($"{targetName}.");
builder.Append(targetName);
}
else
{
// Default to this
builder.Append("this.");
builder.Append("this");
}
}

builder.AppendLine($"{node.VariableName};");
// Add index if needed
if (node.IsIndexer)
{
builder.Append($"[{GetPinIncomingValue(node.IndexPin)}]");
}
else
{
builder.Append($".{node.VariableName}");
}

builder.AppendLine(";");
}

public void PureTranslateLiteralNode(LiteralNode node)
Expand Down

0 comments on commit 4a921f0

Please sign in to comment.