public static string ConvertNodesToCode(DynamoModel dynamoModel, IEnumerable<NodeModel> nodeList)
{
var astBuilder = new AstBuilder(dynamoModel, null);
var astNodes = astBuilder.CompileToAstNodes(nodeList, false);
var codeGen = new ProtoCore.CodeGenDS(astNodes);
return codeGen.GenerateCode();
}
/// <summary>
/// Emits the DS code given the list of ast nodes
/// </summary>
/// <param name="astList"></param>
/// <returns></returns>
public string Emit(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList)
{
// Emit the DS code from the AST tree using ProtoCore code generator
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList);
string code = codegenDS.GenerateCode();
UpdateAddedNodesInModifiedNameList();
return code;
}
public void TestRoundTrip_ClassDecl_PropertyAccess_01()
{
int result1 = 10;
ExecutionMirror mirror = null;
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
// Create an exact copy of the AST list to pass to the source conversion
// This needs to be done because the astlist to be run will be SSA'd on the AST execution run
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astListcopy= new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
// 1. Build AST
// class bar
// {
// f : var;
// }
//
// p = bar.bar();
// p.f = 10;
// a = p.f;
// Create the class node AST
ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode();
classDefNode.ClassName = "bar";
// Create the property AST
ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode();
varDeclNode.Name = "f";
varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f");
varDeclNode.ArgumentType = new ProtoCore.Type()
{
Name = "int",
rank = 0,
UID = (int)ProtoCore.PrimitiveType.Integer
};
classDefNode.Variables.Add(varDeclNode);
astList.Add(classDefNode);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.ClassDeclNode(classDefNode));
// p = bar.bar();
ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode();
constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar");
ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode();
identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar");
identListConstrcctorCall.RightNode = constructorCall;
ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("p"),
identListConstrcctorCall,
ProtoCore.DSASM.Operator.assign);
astList.Add(stmtInitClass);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtInitClass));
// p.f = 10;
ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertySet = new ProtoCore.AST.AssociativeAST.IdentifierListNode();
identListPropertySet.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p");
identListPropertySet.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f");
ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertySet = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
identListPropertySet,
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.assign);
astList.Add(stmtPropertySet);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertySet));
// a = p.f;
ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertyAccess = new ProtoCore.AST.AssociativeAST.IdentifierListNode();
identListPropertyAccess.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p");
identListPropertyAccess.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f");
ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
identListPropertyAccess,
ProtoCore.DSASM.Operator.assign);
astList.Add(stmtPropertyAccess);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertyAccess));
// 2. Execute AST and verify
mirror = thisTest.RunASTSource(astList);
Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1);
// 3. Convert AST to source
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astListcopy);
string code = codegenDS.GenerateCode();
// 4. Execute source and verify
mirror = thisTest.RunScriptSource(code);
Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1);
//.........这里部分代码省略.........
internal void ConvertNodesToCodeInternal(EngineController engineController, INamingProvider namingProvider)
{
var selectedNodes = DynamoSelection.Instance
.Selection
.OfType<NodeModel>()
.Where(n => n.IsConvertible);
if (!selectedNodes.Any())
return;
var cliques = NodeToCodeCompiler.GetCliques(selectedNodes).Where(c => !(c.Count == 1 && c.First() is CodeBlockNodeModel));
var codeBlockNodes = new List<CodeBlockNodeModel>();
//UndoRedo Action Group----------------------------------------------
NodeToCodeUndoHelper undoHelper = new NodeToCodeUndoHelper();
// using (UndoRecorder.BeginActionGroup())
{
foreach (var nodeList in cliques)
{
//Create two dictionarys to store the details of the external connections that have to
//be recreated after the conversion
var externalInputConnections = new Dictionary<ConnectorModel, string>();
var externalOutputConnections = new Dictionary<ConnectorModel, string>();
//Also collect the average X and Y co-ordinates of the different nodes
int nodeCount = nodeList.Count;
var nodeToCodeResult = engineController.ConvertNodesToCode(this.nodes, nodeList, namingProvider);
#region Step I. Delete all nodes and their connections
double totalX = 0, totalY = 0;
foreach (var node in nodeList)
{
#region Step I.A. Delete the connections for the node
foreach (var connector in node.AllConnectors.ToList())
{
if (!IsInternalNodeToCodeConnection(nodeList, connector))
{
//If the connector is an external connector, the save its details
//for recreation later
var startNode = connector.Start.Owner;
int index = startNode.OutPorts.IndexOf(connector.Start);
//We use the varibleName as the connection between the port of the old Node
//to the port of the new node.
var variableName = startNode.GetAstIdentifierForOutputIndex(index).Value;
//Store the data in the corresponding dictionary
if (startNode == node)
{
if (nodeToCodeResult.OutputMap.ContainsKey(variableName))
variableName = nodeToCodeResult.OutputMap[variableName];
externalOutputConnections.Add(connector, variableName);
}
else
{
if (nodeToCodeResult.InputMap.ContainsKey(variableName))
variableName = nodeToCodeResult.InputMap[variableName];
externalInputConnections.Add(connector, variableName);
}
}
//Delete the connector
undoHelper.RecordDeletion(connector);
connector.Delete();
}
#endregion
#region Step I.B. Delete the node
totalX += node.X;
totalY += node.Y;
undoHelper.RecordDeletion(node);
RemoveNode(node);
#endregion
}
#endregion
#region Step II. Create the new code block node
var outputVariables = externalOutputConnections.Values;
var newResult = NodeToCodeCompiler.ConstantPropagationForTemp(nodeToCodeResult, outputVariables);
// Rewrite the AST using the shortest unique name in case of namespace conflicts
NodeToCodeCompiler.ReplaceWithShortestQualifiedName(
engineController.LibraryServices.LibraryManagementCore.ClassTable, newResult.AstNodes, ElementResolver);
var codegen = new ProtoCore.CodeGenDS(newResult.AstNodes);
var code = codegen.GenerateCode();
var codeBlockNode = new CodeBlockNodeModel(
code,
System.Guid.NewGuid(),
totalX / nodeCount,
totalY / nodeCount, engineController.LibraryServices, ElementResolver);
undoHelper.RecordCreation(codeBlockNode);
AddAndRegisterNode(codeBlockNode, false);
codeBlockNodes.Add(codeBlockNode);
#endregion
//.........这里部分代码省略.........
public void TestCodeGenDS_Assign04()
{
GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance();
/*b = 30*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(30),
ProtoCore.DSASM.Operator.assign);
/*a = (b - 10) * 20 + (b + 10) * (b - 20) */
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment =
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.sub),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.mul),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.add),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.sub),
ProtoCore.DSASM.Operator.mul),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
/*c = a*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeC = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
ProtoCore.DSASM.Operator.assign);
/*a = a + 1000*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.IntNode(1000),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(nodeB);
astList.Add(assignment);
astList.Add(nodeC);
astList.Add(assignment2);
ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList);
string code = codegen.GenerateCode();
ExecutionMirror mirror = thisTest.RunScriptSource(code);
//a = 1800, c = a = 1800
Obj o = mirror.GetValue("a");
Assert.IsTrue((Int64)o.Payload == 1800);
o = mirror.GetValue("c");
Assert.IsTrue((Int64)o.Payload == 1800);
}
public void TestCodeGenDS_Assign02()
{
// Build the AST tree
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IntNode(10),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(assign);
// emit the DS code from the AST tree
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList);
string code = codegenDS.GenerateCode();
// Verify the results
ExecutionMirror mirror = thisTest.RunScriptSource(code);
Obj o = mirror.GetValue("a");
Assert.IsTrue((Int64)o.Payload == 30);
}
public void TestCodeGenDS_Assign01()
{
// Build the AST trees
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(assign);
// emit the DS code from the AST tree
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList);
string code = codegenDS.GenerateCode();
// Verify the results
ExecutionMirror mirror = thisTest.RunScriptSource(code);
thisTest.Verify("a", 10);
}
public void TestCodeGenDS_Assign05()
{
/*b = 30*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(30),
ProtoCore.DSASM.Operator.assign);
/*c = b + 30*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeC = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(30),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
/*a = (b + 20) - (c - 10) + c*5 */
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.add),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.sub),
ProtoCore.DSASM.Operator.sub),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
new ProtoCore.AST.AssociativeAST.IntNode(5),
ProtoCore.DSASM.Operator.mul),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(nodeB);
astList.Add(nodeC);
astList.Add(assignment);
ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList);
string code = codegen.GenerateCode();
/*a = 300, b = 30, c= 60 */
ExecutionMirror mirror = thisTest.RunScriptSource(code);
Obj o = mirror.GetValue("a");
Assert.IsTrue((Int64)o.Payload == 300);
}
请发表评论