Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix node2code for array indexing expressions #12805

Merged
merged 6 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/DynamoCore/Engine/NodeToCode/NodeToCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,10 @@ internal static NodeToCodeResult NodeToCode(
IEnumerable<NodeModel> nodes,
INamingProvider namingProvider)
{
// The basic worflow is:
// 1. Compile each node to get its cde in AST format
// The basic workflow is:
// 1. Compile each node to get its code in AST format
//
// 2. Variable numbering to avoid confliction. For example, two
// 2. Variable numbering to avoid conflicts. For example, two
// code block nodes both have assignment "y = x", we need to
// rename "y" to "y1" and "y2" respectively.
//
Expand All @@ -1120,7 +1120,7 @@ internal static NodeToCodeResult NodeToCode(
// 4. Generate short name for long name variables. Typically they
// are from output ports from other nodes.
//
// 5. Do constant progation to optimize the generated code.
// 5. Do constant propagation to optimize the generated code.
#region Step 1 AST compilation

AstBuilder builder = new AstBuilder(null);
Expand All @@ -1130,7 +1130,7 @@ internal static NodeToCodeResult NodeToCode(

#endregion

#region Step 2 Varialbe numbering
#region Step 2 Variable numbering

// External inputs will be in input map
// Internal non-cbn will be input map & output map
Expand Down
78 changes: 77 additions & 1 deletion src/DynamoCore/Engine/ShortestQualifiedNameReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ namespace Dynamo.Engine
/// automatically shortened to partial namespaces so that they can still be resolved uniquely.
/// E.g., given {"A.B.C.D.E", "X.Y.A.B.E.C.E", "X.Y.A.C.B.E"}, all with the same class E,
/// their shortest unique names would be: {"D.E", "E.E", "C.B.E"}.
///
/// Also replaces DesignScript.BuiltIn.Get.ValueAtIndex(exp1, exp2) calls to exp1[exp2] expressions.
/// </summary>
internal class ShortestQualifiedNameReplacer : AstReplacer
{
private readonly ClassTable classTable;
private readonly ElementResolver resolver;
private delegate void SetArrayDimensionsDelegate(ref ArrayNameNode indxExp, AssociativeNode indx);

public ShortestQualifiedNameReplacer(ClassTable classTable, ElementResolver resolver)
{
Expand Down Expand Up @@ -69,12 +72,84 @@ public override AssociativeNode VisitTypedIdentifierNode(TypedIdentifierNode nod
return node;
}

/// <summary>
/// If node is a DesignScript.BuiltIn.Get.ValueAtIndex(exp1, exp2) function call node.
/// it is transformed into a node with indexing operator e.g. exp1[exp2].
/// </summary>
/// <param name="node">input node</param>
/// <param name="indexExp">output node</param>
/// <returns>true if input expression is of the form DesignScript.BuiltIn.Get.ValueAtIndex(exp1, exp2), false otherwise</returns>
private bool TryBuildIndexingExpression(IdentifierListNode node, out ArrayNameNode indexExp)
{
// If indxExp already has a rank >= 1, we need to append another [] to it with indx.
SetArrayDimensionsDelegate setArrayDimensions = (ref ArrayNameNode indxExp, AssociativeNode indx) =>
{
if (indxExp.ArrayDimensions != null)
{
if (indxExp is IdentifierListNode iln)
{
if (iln.RightNode is ArrayNameNode arr)
{
arr.ArrayDimensions = indxExp.ArrayDimensions;
}
}
else indxExp = new IdentifierNode(indxExp.ToString());
}
indxExp.ArrayDimensions = new ArrayNode(indx, null);
};

indexExp = null;
if(node.RightNode is FunctionCallNode fcn)
{
if(fcn.Function.Name == ProtoCore.AST.Node.BuiltinValueAtIndexMethodName)
{
if(node.LeftNode.ToString() == ProtoCore.AST.Node.BuiltinGetValueAtIndexTypeName)
{
var exp1 = fcn.FormalArguments[0];
var exp2 = fcn.FormalArguments[1];

// exp1 = expression to be indexed.
// Only expressions of ArrayNameNode type can be indexed.
if (exp1 is ArrayNameNode ann)
{
if (exp1 is IdentifierListNode iln1)
{
if (!TryBuildIndexingExpression(iln1, out indexExp))
{
indexExp = iln1;
}
}
else indexExp = ann;

// exp2 = expression that evaluates to an index.
if (exp2 is IdentifierListNode iln2)
{
if (TryBuildIndexingExpression(iln2, out ArrayNameNode index))
{
setArrayDimensions(ref indexExp, index);
return true;
}
}
setArrayDimensions(ref indexExp, exp2);
return true;
}
}
}
}
return false;
}

public override AssociativeNode VisitIdentifierListNode(IdentifierListNode node)
{
if (node == null)
return null;

if (TryBuildIndexingExpression(node, out ArrayNameNode indexExp))
{
if (indexExp is IdentifierListNode iln) node = iln;
else return indexExp;
}

// First pass attempt to resolve the node class name
// and shorten it before traversing it deeper
AssociativeNode shortNameNode;
Expand All @@ -93,7 +168,8 @@ public override AssociativeNode VisitIdentifierListNode(IdentifierListNode node)
{
LeftNode = newLeftNode,
RightNode = rightNode,
Optr = Operator.dot
Optr = Operator.dot,
ArrayDimensions = node.ArrayDimensions
};
return node;
}
Expand Down
3 changes: 2 additions & 1 deletion src/DynamoCore/Graph/Nodes/CodeBlockNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ public override ProtoCore.Type GetTypeHintForOutput(int index)
{
func = targetClass.ProcTable.GetPropertyGetterByName(funcName);
}
type = func.ReturnType;
if(func != null) type = func.ReturnType;

return type;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Engine/ProtoCore/Parser/AssociativeAST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ public override int GetHashCode()

public override string ToString()
{
return LeftNode + "." + RightNode;
return LeftNode + "." + RightNode + base.ToString();
}

public override AstKind Kind
Expand Down
96 changes: 96 additions & 0 deletions test/DynamoCoreTests/NodeToCodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,102 @@ public void TestFilePathToCode()
Assert.IsTrue(cbn.Code.Contains(@"D:\\foo\\bar"));
}

[Test, Category("Failure")]
public void ImperativeArrayIndexingInCodeBlockToCode()
{
OpenModel(@"core\node2code\imperative_indexing.dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;

SelectAll(nodes);
var command = new DynamoModel.ConvertNodesToCodeCommand();
CurrentDynamoModel.ExecuteCommand(command);

var cbn = CurrentDynamoModel.CurrentWorkspace.Nodes.OfType<CodeBlockNodeModel>().FirstOrDefault();
Assert.IsNotNull(cbn);

Assert.IsTrue(cbn.Code.Contains("return = p[0].DistanceTo(p[1]);"));
}

[Test]
public void ArrayIndexingInCodeBlockToCode()
{
OpenModel(@"core\node2code\array_indexing.dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;

SelectAll(nodes);
var command = new DynamoModel.ConvertNodesToCodeCommand();
CurrentDynamoModel.ExecuteCommand(command);

var cbn = CurrentDynamoModel.CurrentWorkspace.Nodes.OfType<CodeBlockNodeModel>().FirstOrDefault();
Assert.IsNotNull(cbn);

Assert.IsTrue(cbn.Code.Contains("p[0].DistanceTo(p[1]);"));
}

[Test]
public void StringIndexingInCodeBlockToCode()
{
OpenModel(@"core\node2code\string_indexing.dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;

SelectAll(nodes);
var command = new DynamoModel.ConvertNodesToCodeCommand();
CurrentDynamoModel.ExecuteCommand(command);

var cbn = CurrentDynamoModel.CurrentWorkspace.Nodes.OfType<CodeBlockNodeModel>().FirstOrDefault();
Assert.IsNotNull(cbn);

Assert.IsTrue(cbn.Code.Contains("t1 = s[1..2];"));
}

[Test]
public void DictIndexingInCodeBlockToCode()
{
OpenModel(@"core\node2code\dict_indexing.dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;

SelectAll(nodes);
var command = new DynamoModel.ConvertNodesToCodeCommand();
CurrentDynamoModel.ExecuteCommand(command);

var cbn = CurrentDynamoModel.CurrentWorkspace.Nodes.OfType<CodeBlockNodeModel>().FirstOrDefault();
Assert.IsNotNull(cbn);

Assert.IsTrue(cbn.Code.Contains("dict[\"abc\"];"));
}

[Test]
public void NestedIndexingInCodeBlockToCode1()
{
OpenModel(@"core\node2code\func()[][].dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;

SelectAll(nodes);
var command = new DynamoModel.ConvertNodesToCodeCommand();
CurrentDynamoModel.ExecuteCommand(command);

var cbn = CurrentDynamoModel.CurrentWorkspace.Nodes.OfType<CodeBlockNodeModel>().FirstOrDefault();
Assert.IsNotNull(cbn);

Assert.IsTrue(cbn.Code.Contains("Point.ByCoordinates(x<1>, y<2>)[0][0];"));
}

[Test]
public void NestedIndexingInCodeBlockToCode2()
{
OpenModel(@"core\node2code\func()[func()[]].dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;

SelectAll(nodes);
var command = new DynamoModel.ConvertNodesToCodeCommand();
CurrentDynamoModel.ExecuteCommand(command);

var cbn = CurrentDynamoModel.CurrentWorkspace.Nodes.OfType<CodeBlockNodeModel>().FirstOrDefault();
Assert.IsNotNull(cbn);

Assert.IsTrue(cbn.Code.Contains("Math.Pow(x<1>, y<2>)[0][Math.Round(1..2)[0]];"));
}

[Test]
public void TestDirectoryToCode()
{
Expand Down
Loading