/// <summary> /// Transforms Node to the Final ProtoAST form /// </summary> /// <returns></returns> public List<AssociativeNode> ToGraphIR(AST graph, GraphCompiler graphCompiler) { GraphNodeToASTGenerator astGen = new GraphNodeToASTGenerator(graph, graphCompiler); astGen.AddNodesToAST(); List<ProtoCore.AST.AssociativeAST.AssociativeNode> irGraph = astGen.SplitAST(); return irGraph; }
static List<Node> DFS(Node node, AST statementList) { List<Node> sotedNodes = new List<Node>(); Dictionary<Node, int> nodeStateMap = new Dictionary<Node, int>(); if (IsNotVisited(node, nodeStateMap)) { DFSVisit(node, nodeStateMap, sotedNodes, statementList); } return sotedNodes; }
public void BuildGraphFromNodes(List<SnapshotNode> snapshotNodeList) { // Instantiate SynchronizeData and GraphCompiler SynchronizeData syncData = new SynchronizeData(); syncData.AddedNodes = snapshotNodeList; GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance(); GraphBuilder gb = new GraphBuilder(syncData, gc); gb.AddNodesToAST(); gb.MakeConnectionsForAddedNodes(); graph = gb.Graph; }
static List<Node> DFS(AST graph) { List<Node> nodes = graph.GetNodes(); List<Node> sotedNodes = new List<Node>(); Dictionary<Node, int> nodeStateMap = new Dictionary<Node, int>(); IEnumerable iter = nodes; foreach (Node node in iter) { if (IsNotVisited(node, nodeStateMap)) { DFSVisit(node, nodeStateMap, sotedNodes); } } return sotedNodes; }
private static void BuildBlockToBlockStatement(Node node, Node nodeI, AST statementList) { if (node.children.Count > 1) { statementList.AddNode(node); } else { string content = string.Empty; Block block = nodeI as Block; Validity.Assert(block != null); Assignment a1 = null; Node n1 = statementList.GetNode(node.Guid); if (n1 is Assignment) { a1 = n1 as Assignment; // // Comment Jun: // This condition basically checks if the single line codeblock is either a full expression or a single ident // For now, in order to check for single ident, we check if the LHS if empty // This needs refinement bool isSingleIdent = string.IsNullOrEmpty((((Block)node).LHS)); if (isSingleIdent) { // This single line codeblock is a single identifier a1.right.Name = block.LHS != "" ? block.LHS : block.Name.Replace(";", "").Trim(); content = a1.ToScript(); } else { // This single line codeblock is a full expression content = ((Block)node).Name; } // Comment Jun: Create a new block that represents the new contents Block block2 = new Block(content, node.Guid); int index = statementList.nodeList.IndexOf(a1); statementList.RemoveNode(a1); statementList.nodeList.Insert(index, block2); statementList.nodeMap.Add(block2.Guid, block2); // Comment Jun: Reflect the new changes to the original block (node as Block).SetData(block2.LHS, content); } } }
private static int BuildIdentToCodeBlockIdentStatement(Node node, Node nodeI, List<Node> nodes, AST statementList, int j) { Block block = nodeI as Block; Validity.Assert(block != null); // Comment Jun: Check if the codeblock is a single ident string lhs = block.LHS; if (string.IsNullOrEmpty(lhs)) { lhs = block.TrimName(); } Assignment a = new Assignment((IdentNode)node, new IdentNode(lhs, block.Guid)); if (statementList.nodeMap.ContainsKey(a.Guid)) { int index = statementList.nodeList.IndexOf(statementList.GetNode(a.Guid)); statementList.RemoveNode(statementList.GetNode(a.Guid)); statementList.nodeList.Insert(index, a); statementList.nodeMap.Add(a.Guid, a); } else { statementList.AddNode(a); } return ++j; }
public static List<Node> sort(Node node, AST statementList) { return DFS(node, statementList); }
/**/ public List<uint> ConnectionToUID(List<Connection> input) { List<uint> result = new List<uint>(); foreach (Connection inputConnection in input) { result.Add(inputConnection.OtherNode); } return result; } /*Tron: Use for node to code function *For each connected component of the graph, generate a respective string of code */ public List<SnapshotNode> ToCode(AST graph, GraphCompiler originalGC, List<SnapshotNode> inputs) { List<SnapshotNode> result = new List<SnapshotNode>(); List<Node> li = TopSort.sort(graph); tguid = 20000; List<string> listIslands = new List<string>(); List<Node> islandNodes = new List<Node>(); int countIslands = 0; statementList = new AST(); ModifiedStmtGuidList.Clear(); List<string> importIslands = new List<string>(); IEnumerable iter = li; List<Node> islandNodeList = new List<Node>(); //List<List<Node>> listing = new List<List<Node>>(); List<Node> listing = new List<Node>(); foreach (Node node in iter) { if (node != null) { if (node is ImportNode) { importIslands.Add(node.ToScript() + ProtoCore.DSASM.Constants.termline); } else if (node.IsIsland) { countIslands++; if (node is ArrayNode) { BuildArrayNodeStatement(node, statementList); if (!islandNodes.Contains(node)) islandNodes.Add(node); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is LiteralNode) { BuildLiteralNodeStatement(node, statementList); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is Operator) { BuildOperatorStatement(node, statementList); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is Block) { BuildBlockStatement(node, statementList); islandNodes.Add(node); //string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; //if (!listIslands.Contains(island)) // listIslands.Add(island); } else if (node is IdentNode) { // comment Jun: // An island identifier node is handled by emitting a null as its rhs statementList.AddNode(node); string contents = statementList.GetNode(node.Guid).ToScript() + "=" + ProtoCore.DSASM.Literal.Null + ProtoCore.DSASM.Constants.termline; listIslands.Add(contents); } else { statementList.AddNode(node); string island = node.ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } islandNodeList.Add(node); listing = listing.ToList().Union<Node>(BuildStatement(node, statementList)).ToList(); HandleNewNode(node); } else if (node.IsLeaf) { if (node is ArrayNode) { BuildArrayNodeStatement(node, statementList); } else if (node is LiteralNode) { BuildLiteralNodeStatement(node, statementList); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); } else if (node is Operator) { BuildOperatorStatement(node, statementList); } else if (node is Block) { BuildBlockStatement(node, statementList); } else if (node is IdentNode) { statementList.AddNode(node); string contents = statementList.GetNode(node.Guid).ToScript() + "=" + ProtoCore.DSASM.Literal.Null + ProtoCore.DSASM.Constants.termline; listIslands.Add(contents); } HandleNewNode(node); } else if (node.IsRoot && !node.IsIsland) { if (node is Operator) { BuildOperatorStatement(node, statementList); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); } else if (node is Block) { BuildBlockStatement(node, statementList); } //liststat = BuildStatement(node, statementList); //finalScript=finalScript.Union(BuildStatement(node, statementList)).ToList(); //comment out for NodeToCode function //listing.Add(BuildStatement(node, statementList)); listing = BuildStatement(node, statementList); HandleNewNode(node); } else if (node is Operator) { BuildOperatorStatement(node, statementList); HandleNewNode(node); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); HandleNewNode(node); } else if (node is ArrayNode) { BuildArrayNodeStatement(node, statementList); HandleNewNode(node); } else if (node is LiteralNode) { BuildLiteralNodeStatement(node, statementList); HandleNewNode(node); } else if (node is Block) { BuildBlockStatement(node, statementList); listing = BuildStatement(node, statementList); HandleNewNode(node); } } } StringBuilder builder = new StringBuilder(); foreach (string island in importIslands)// Loop through all strings { builder.Append(island); // Append string to StringBuilder } foreach (string island in listIslands) // Loop through all strings { builder.Append(island); // Append string to StringBuilder } /*N2C*/ #region get connected components of the graph List<Node> nodeToCodeInputList = new List<Node>(); List<List<Node>> listingAlternate = new List<List<Node>>(); List<List<Node>> listingAlt2 = new List<List<Node>>(); if (nodeToCodeUIDs.Count != 0) { foreach (uint nodeID in nodeToCodeUIDs) { if (graph.GetNode(nodeID) != null) { nodeToCodeInputList.Add(graph.GetNode(nodeID)); } else { if (this.codeBlockUIDMap[nodeID] != null) { foreach (KeyValuePair<int, uint> pair in this.codeBlockUIDMap[nodeID]) { nodeToCodeInputList.Add(graph.GetNode(pair.Value)); } } } } //listingAlternate = GetConnectedComponents(nodeToCodeInputList, graph); listingAlt2 = GetConnectedComponents_02(nodeToCodeInputList, graph); } #endregion //foreach (List<Node> n1 in listing) //{ // foreach (Node n2 in n1) // if (!finalScript.Contains(n2)) // finalScript.Add(n2); // else // { // finalScript.Remove(n2); // finalScript.Add(n2); // } //} listing = SortCodeBlocks(listing); List<List<Node>> finalList = new List<List<Node>>(); foreach (List<Node> l1 in listingAlt2) { List<Node> temp = new List<Node>(); foreach (Node n1 in l1) foreach (Node listingNode in listing) if (listingNode.Guid == n1.Guid) temp.Add(listingNode); if (temp.Count != 0) finalList.Add(temp); } islandNodeList = islandNodeList.Union(islandNodes).ToList(); //islandNodeList = SortCodeBlocks(islandNodeList); //if (islandNodeList.Count != 0) // finalList.Add(islandNodeList); #region generate code and snapshot node uint id = 0; foreach (List<Node> nodeList in finalList) { string output = ""; List<Node> tempList = SortCodeBlocks(nodeList); //tempList.Reverse(); foreach (Node node in tempList) { if (nodeToCodeUIDs.Contains(node.Guid)) { if (node.ToCode() != null) output += node.ToCode().Replace(";", "") + ProtoCore.DSASM.Constants.termline; } else { foreach (KeyValuePair<uint, Dictionary<int, uint>> pair in this.codeBlockUIDMap) { if (pair.Value.ContainsValue(node.Guid)) { output += node.ToCode().Replace(";", "") + ProtoCore.DSASM.Constants.termline; //ensure only one semicolon at the end of a statement, by request from UI } } } } output = output.TrimEnd('\n'); SnapshotNode ssn = new SnapshotNode(); ssn.Id = id++; ssn.Content = output; ssn.Type = SnapshotNodeType.CodeBlock; ssn.InputList = new List<Connection>(); foreach (SnapshotNode inputNode in inputs) { foreach (Node subTreeNode in nodeList) { if (inputNode.Id == subTreeNode.Guid) { foreach (Connection c1 in inputNode.InputList) { if (!IsInternalConnection(c1, this)) //the connection is not internal, return it back to UI { Connection newInputConnection = new Connection(); newInputConnection.OtherNode = c1.OtherNode; newInputConnection.OtherIndex = c1.OtherIndex; newInputConnection.IsImplicit = c1.IsImplicit; string[] tokens = graph.GetNode(c1.OtherNode).Name.Split('='); newInputConnection.LocalName = tokens[0]; ssn.InputList.Add(newInputConnection); } } } else if (codeBlockUIDMap.ContainsKey(inputNode.Id)) //inputNode was split { if (codeBlockUIDMap[inputNode.Id].ContainsValue(subTreeNode.Guid)) { foreach (Connection c1 in inputNode.InputList) { if (!IsInternalConnection(c1, this)) { int indexSlot = 0; foreach (KeyValuePair<int, uint> pair in codeBlockUIDMap[inputNode.Id]) { if (pair.Value == subTreeNode.Guid) indexSlot = pair.Key; } if (c1.OtherIndex == indexSlot) { Connection newInputConnection = new Connection(); newInputConnection.OtherNode = c1.OtherNode; newInputConnection.OtherIndex = c1.OtherIndex; foreach (KeyValuePair<uint, Dictionary<int, uint>> pair in originalGC.codeBlockUIDMap) { if (pair.Value.ContainsValue(c1.OtherNode)) //this means if the other node was split, return the original Id that was sent to us by the UI { newInputConnection.OtherNode = pair.Key; newInputConnection.OtherIndex = pair.Value.First(x => x.Value == c1.OtherNode).Key; } } newInputConnection.IsImplicit = c1.IsImplicit; string[] tokens = graph.GetNode(c1.OtherNode).Name.Split('='); newInputConnection.LocalName = tokens[0]; ssn.InputList.Add(newInputConnection); } } } } } } } ssn.OutputList = new List<Connection>(); foreach (SnapshotNode inputNode in inputs) { foreach (Node subTreeNode in nodeList) { //if (subTreeNode.Name.Split('=')[0] == outputCnt.LocalName) if (inputNode.Id == subTreeNode.Guid) { foreach (Connection c1 in inputNode.OutputList) { if (!IsInternalConnection(c1, this)) { Connection newOutputConnection = new Connection(); newOutputConnection.OtherNode = c1.OtherNode; newOutputConnection.OtherIndex = c1.OtherIndex; newOutputConnection.IsImplicit = c1.IsImplicit; newOutputConnection.LocalName = c1.LocalName; ssn.OutputList.Add(newOutputConnection); } } } else if (codeBlockUIDMap.ContainsKey(inputNode.Id)) //inputNode was split { if (codeBlockUIDMap[inputNode.Id].ContainsValue(subTreeNode.Guid)) { foreach (Connection c1 in inputNode.OutputList) { if (!IsInternalConnection(c1, this)) { int indexSlot = 0; foreach (KeyValuePair<int, uint> pair in codeBlockUIDMap[inputNode.Id]) { if (pair.Value == subTreeNode.Guid) { indexSlot = pair.Key; break; } } if (c1.LocalIndex == indexSlot) { Connection newOutputConnection = new Connection(); newOutputConnection.OtherNode = c1.OtherNode; newOutputConnection.OtherIndex = c1.OtherIndex; foreach (KeyValuePair<uint, Dictionary<int, uint>> pair in originalGC.codeBlockUIDMap) { if (pair.Value.ContainsValue(c1.OtherNode)) //this means if the other node was split, return the original Id that was sent to us by the UI { newOutputConnection.OtherNode = pair.Key; newOutputConnection.OtherIndex = pair.Value.First(x => x.Value == c1.OtherNode).Key; } } newOutputConnection.IsImplicit = c1.IsImplicit; newOutputConnection.LocalName = c1.LocalName; ssn.OutputList.Add(newOutputConnection); } } } } } } } result.Add(ssn); } #endregion #region remove _temp_xxx name //Dictionary<string, string> tempReplaceValue = new Dictionary<string, string>(); //for (int i = 0; i < result.Count; i++) //{ // SnapshotNode ssn = result[i]; // string[] statements = ssn.Content.Split(';'); // for (int j = 0; j < statements.Length; j++) // { // string statement = statements[j]; // string lhsTempName = statement.Split('=')[0].Replace("\n", "").Trim(); // foreach (Node astNode in graph.nodeList) // { // if (astNode.Name.Split('=')[0].Trim() == lhsTempName && lhsTempName.StartsWith("_temp_")) // { // //this means in the ast there is some statement _temp_abc = something // //which means the _temp_xxx is generated, not typed in by users // tempReplaceValue.Add(lhsTempName, statement.Split('=')[1].Replace("\n", "").Trim()); // } // } // } //} //for (int i = 0; i < result.Count; i++) //{ // SnapshotNode ssn = result[i]; // foreach (KeyValuePair<string, string> pair in tempReplaceValue) // { // ssn.Content.Replace(pair.Key, pair.Value); // } //} #endregion #region replace _temp_ name with more elegant name //int tempId = 0; //for (int i = 0; i < result.Count; i++) //{ // SnapshotNode ssn = result[i]; // string[] statements = ssn.Content.Split(';'); // for (int j = 0; j < statements.Length; j++) // { // string statement = statements[j]; // string lhsTempName = statement.Split('=')[0].Replace("\n", "").Trim(); // foreach (Node astNode in graph.nodeList) // { // if (astNode.Name.Split('=')[0].Trim() == lhsTempName && lhsTempName.StartsWith("_temp_")) // { // string newTempName = "temp_" + tempId++; // for (int k = 0; k < result.Count; k++) // { // result[k].Content = result[k].Content.Replace(lhsTempName, newTempName); // } // } // } // if (statement.Split('=').Length > 1) // { // string rhsTempName = statement.Split('=')[1].Replace("\n", "").Trim(); // foreach (Node astNode in graph.nodeList) // { // if (astNode.Name.Split('=')[0].Trim() == rhsTempName && rhsTempName.StartsWith("_temp_")) // { // string newTempName = "temp_" + tempId++; // for (int k = 0; k < result.Count; k++) // { // result[k].Content = result[k].Content.Replace(rhsTempName, newTempName); // } // } // } // } // } //} #endregion #region return to original input connections for (int i = 0; i < result.Count; i++) { SnapshotNode ssn = result[i]; for (int j = 0; j < ssn.InputList.Count; j++) { Connection inputConnection = ssn.InputList[j]; foreach (KeyValuePair<uint, Dictionary<int, uint>> kvp in originalGC.codeBlockUIDMap) { foreach (KeyValuePair<int, uint> kvp2 in kvp.Value) { if (kvp2.Value == inputConnection.OtherNode) { Connection oldInputConnection = new Connection(); oldInputConnection.OtherNode = kvp.Key; oldInputConnection.OtherIndex = inputConnection.OtherIndex; oldInputConnection.IsImplicit = inputConnection.IsImplicit; oldInputConnection.LocalName = inputConnection.LocalName; oldInputConnection.LocalIndex = inputConnection.LocalIndex; ssn.InputList.Remove(inputConnection); ssn.InputList.Insert(j, oldInputConnection); } } } } for (int j = 0; j < ssn.OutputList.Count; j++) { Connection outputConnection = ssn.OutputList[j]; foreach (KeyValuePair<uint, Dictionary<int, uint>> kvp in originalGC.codeBlockUIDMap) { foreach (KeyValuePair<int, uint> kvp2 in kvp.Value) { if (kvp2.Value == outputConnection.OtherNode) { Connection oldInputConnection = new Connection(); oldInputConnection.OtherNode = kvp.Key; oldInputConnection.OtherIndex = outputConnection.OtherIndex; oldInputConnection.IsImplicit = outputConnection.IsImplicit; oldInputConnection.LocalName = outputConnection.LocalName; oldInputConnection.LocalIndex = outputConnection.LocalIndex; ssn.OutputList.Remove(outputConnection); ssn.OutputList.Insert(j, oldInputConnection); } } } } } #endregion /*Chirag's foreach (var value in finalScript) { if (nodeToCodeUIDs.Contains((value as Node).Guid)) if (value.ToCode() != null) liststat += value.ToCode() + ProtoCore.DSASM.Constants.termline; } liststat = builder.ToString() + liststat; //liststat += builder.ToString(); //GraphUtilities.runningUID = GraphToDSCompiler.Constants.UIDStart; //result.AddRange(liststat.Split(';')); */ UpdateAddedNodesInModifiedNameList(); return result; }
private static int BuildOperatorToOperatorStatement(Node node, Node nodeI, Dictionary<int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); Assignment a2 = (Assignment)statementList.GetNode(nodeI.Guid); List<int> keys = GetKeysFromValue(nodeDic, nodeI); if (keys.Count > 1) { if (keys[j] == 0) { ((BinExprNode)a1.right).left = a2.left; if (keys.Count == 2) ++j; } else { ((BinExprNode)a1.right).right = a2.left; if (keys.Count == 2) ++j; } } else { if (keys[0] == 0) { ((BinExprNode)a1.right).left = a2.left; if (keys.Count == 2) ++j; } else { ((BinExprNode)a1.right).right = a2.left; if (keys.Count == 2) ++j; } } return j; }
private static int BuildFuncToIdentStatement(Node node, Node nodeI, Dictionary <int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); IdentNode identNode = nodeI as IdentNode; Validity.Assert(null != identNode); List <int> keys = GetKeysFromValue(nodeDic, nodeI); FunctionCall f = ((FunctionCall)a1.right); if (keys.Count > 1) { do { f.parameters[keys[j]] = identNode; j++; } while (j < keys.Count); } else { f.parameters[keys[j]] = identNode; } j = 0; return(j); }
private static int BuildFuncToBlockStatement(Node node, Node nodeI, Dictionary <int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); Block block = nodeI as Block; List <int> keys = GetKeysFromValue(nodeDic, nodeI); FunctionCall f = ((FunctionCall)a1.right); if (keys.Count > 1) { do { f.parameters[keys[j]] = new IdentNode(block.LHS, block.Guid); j++; } while (j < keys.Count); } else { f.parameters[keys[j]] = new IdentNode(block.LHS, block.Guid); } j = 0; return(j); }
private static int BuildIdentToCodeBlockIdentStatement(Node node, Node nodeI, List <Node> nodes, AST statementList, int j) { Block block = nodeI as Block; Validity.Assert(block != null); // Comment Jun: Check if the codeblock is a single ident string lhs = block.LHS; if (string.IsNullOrEmpty(lhs)) { lhs = block.TrimName(); } Assignment a = new Assignment((IdentNode)node, new IdentNode(lhs, block.Guid)); if (statementList.nodeMap.ContainsKey(a.Guid)) { int index = statementList.nodeList.IndexOf(statementList.GetNode(a.Guid)); statementList.RemoveNode(statementList.GetNode(a.Guid)); statementList.nodeList.Insert(index, a); statementList.nodeMap.Add(a.Guid, a); } else { statementList.AddNode(a); } return(++j); }
public static List <Node> sort(Node node, AST statementList) { return(DFS(node, statementList)); }
public static List <Node> sort(AST graph) { return(DFS(graph)); }
private static int BuildFuncToIdentStatement(Node node, Node nodeI, Dictionary<int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); IdentNode identNode = nodeI as IdentNode; Validity.Assert(null != identNode); List<int> keys = GetKeysFromValue(nodeDic, nodeI); FunctionCall f = ((FunctionCall)a1.right); if (keys.Count > 1) { do { f.parameters[keys[j]] = identNode; j++; } while (j < keys.Count); } else { f.parameters[keys[j]] = identNode; } j = 0; return j; }
private static void BuildIdentToIdentStatement(Node node, Node nodeI, AST statementList) { Assignment a = new Assignment((IdentNode)node, (IdentNode)nodeI); if (statementList.nodeMap.ContainsKey(node.Guid)) { Node nodeToRemove = statementList.GetNode(a.Guid); int index = statementList.nodeList.IndexOf(nodeToRemove); statementList.RemoveNode(nodeToRemove); statementList.nodeList.Insert(index, a); statementList.nodeMap.Add(a.Guid, a); } else statementList.AddNode(a); }
private static int BuildOperatorToOperatorStatement(Node node, Node nodeI, Dictionary <int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); Assignment a2 = (Assignment)statementList.GetNode(nodeI.Guid); List <int> keys = GetKeysFromValue(nodeDic, nodeI); if (keys.Count > 1) { if (keys[j] == 0) { ((BinExprNode)a1.right).left = a2.left; if (keys.Count == 2) { ++j; } } else { ((BinExprNode)a1.right).right = a2.left; if (keys.Count == 2) { ++j; } } } else { if (keys[0] == 0) { ((BinExprNode)a1.right).left = a2.left; if (keys.Count == 2) { ++j; } } else { ((BinExprNode)a1.right).right = a2.left; if (keys.Count == 2) { ++j; } } } return(j); }
// // TODO Jun: Re-evaluate the topsort implementation // static void DFSVisit(Node node, Dictionary<Node, int> nodeStateMap, List<Node> list, AST statementList) { nodeStateMap.Add(node, VISITING); List<Node> nodes = node.GetChildren(); Dictionary<int, Node> nodeDic = node.GetChildrenWithIndices(); IEnumerable iter = nodes; int j = 0; foreach (Node nodeI in iter) { if (node is IdentNode && nodeI is LiteralNode) { BuildIdentToLiteralStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is IdentNode) { BuildIdentToIdentStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is Block) { Block blocknode = (Block)nodeI; if (GraphBuilder.AnalyzeString(blocknode.Name) == SnapshotNodeType.Literal) { LiteralNode literal = new LiteralNode(blocknode.content, nodeI.Guid); BuildIdentToLiteralStatement(node, literal, statementList); } else { j = BuildIdentToCodeBlockIdentStatement(node, nodeI, nodes, statementList, j); } } else if (node is Operator && nodeI is Block) { j = BuildOperatorToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Block) { j = BuildFuncToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is IdentNode) { j = BuildFuncToIdentStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Operator) { BuildIdentToOperatorStatement(node, statementList, nodeI); } else if (node is Operator && nodeI is Operator) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Func) { BuildIdentToFuncStatement(node, nodeI, statementList); } else if (node is Func && nodeI is Func) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Operator && nodeI is Func) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Operator) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Operator && nodeI is ArrayNode) || (node is Operator && nodeI is LiteralNode)) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Func && nodeI is ArrayNode) || (node is Func && nodeI is LiteralNode)) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Block && nodeI is Block)) { BuildBlockToBlockStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Func)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Operator)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is IdentNode)) { BuildBlockToIdentStatement(node, nodeI, statementList); } /*Block to Operator*/ else if (node is Block && nodeI is Operator) { //BuildBlockToOperatorStatement(node, nodeI, statementList); } //else if ((node is Block && nodeI is Func)) //{ // BuildBlockToBlockStatement(node, nodeI, statementList); //} else { if (node is Operator) { if (nodes.IndexOf(nodeI, j) == 0) { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).left = nodeI; ++j; } else { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).right = nodeI; } } else if (node is Func) { Assignment a = (Assignment)statementList.GetNode(node.Guid); FunctionCall f = ((FunctionCall)a.right); f.parameters[nodes.IndexOf(nodeI, j)] = nodeI; j = 0; } } if (IsNotVisited(nodeI, nodeStateMap)) { DFSVisit(nodeI, nodeStateMap, list, statementList); } } nodeStateMap[node] = VISITED; list.Add(node); }
private static int BuildFuncToFuncStatement(Node node, Node nodeI, Dictionary <int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); Assignment a2 = (Assignment)statementList.GetNode(nodeI.Guid); List <int> keys = GetKeysFromValue(nodeDic, nodeI); FunctionCall f = ((FunctionCall)a1.right); if (keys.Count > 1) { do { f.parameters[keys[j]] = a2.left; j++; } while (j < keys.Count); } else { f.parameters[keys[j]] = a2.left; } j = 0; return(j); }
List<Node> BuildStatement(Node node, AST statementList) { //statementList.RemoveNode(node); List<Node> l = TopSort.sort(node, statementList); //if (node is Block) //{ // // statementList.AddNode(node); //} List<uint> guid = new List<uint>(); IEnumerable iter = l; string liststat = ""; foreach (Node nodus in iter) { if (nodus != null) { guid.Add(nodus.Guid); } } List<Node> nodes = new List<Node>(statementList.GetNodes()); foreach (var value in nodes) { liststat += value.ToScript() + ProtoCore.DSASM.Constants.termline; } return nodes; }
// // TODO Jun: Re-evaluate the topsort implementation // static void DFSVisit(Node node, Dictionary <Node, int> nodeStateMap, List <Node> list, AST statementList) { nodeStateMap.Add(node, VISITING); List <Node> nodes = node.GetChildren(); Dictionary <int, Node> nodeDic = node.GetChildrenWithIndices(); IEnumerable iter = nodes; int j = 0; foreach (Node nodeI in iter) { if (node is IdentNode && nodeI is LiteralNode) { BuildIdentToLiteralStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is IdentNode) { BuildIdentToIdentStatement(node, nodeI, statementList); } else if (node is IdentNode && nodeI is Block) { Block blocknode = (Block)nodeI; if (GraphBuilder.AnalyzeString(blocknode.Name) == SnapshotNodeType.Literal) { LiteralNode literal = new LiteralNode(blocknode.content, nodeI.Guid); BuildIdentToLiteralStatement(node, literal, statementList); } else { j = BuildIdentToCodeBlockIdentStatement(node, nodeI, nodes, statementList, j); } } else if (node is Operator && nodeI is Block) { j = BuildOperatorToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Block) { j = BuildFuncToBlockStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is IdentNode) { j = BuildFuncToIdentStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Operator) { BuildIdentToOperatorStatement(node, statementList, nodeI); } else if (node is Operator && nodeI is Operator) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is IdentNode && nodeI is Func) { BuildIdentToFuncStatement(node, nodeI, statementList); } else if (node is Func && nodeI is Func) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Operator && nodeI is Func) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if (node is Func && nodeI is Operator) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Operator && nodeI is ArrayNode) || (node is Operator && nodeI is LiteralNode)) { j = BuildOperatorToOperatorStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Func && nodeI is ArrayNode) || (node is Func && nodeI is LiteralNode)) { j = BuildFuncToFuncStatement(node, nodeI, nodeDic, statementList, j); } else if ((node is Block && nodeI is Block)) { BuildBlockToBlockStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Func)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is Operator)) { BuildBlockToFuncStatement(node, nodeI, statementList); } else if ((node is Block && nodeI is IdentNode)) { BuildBlockToIdentStatement(node, nodeI, statementList); } /*Block to Operator*/ else if (node is Block && nodeI is Operator) { //BuildBlockToOperatorStatement(node, nodeI, statementList); } //else if ((node is Block && nodeI is Func)) //{ // BuildBlockToBlockStatement(node, nodeI, statementList); //} else { if (node is Operator) { if (nodes.IndexOf(nodeI, j) == 0) { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).left = nodeI; ++j; } else { Assignment a = (Assignment)statementList.GetNode(node.Guid); ((BinExprNode)a.right).right = nodeI; } } else if (node is Func) { Assignment a = (Assignment)statementList.GetNode(node.Guid); FunctionCall f = ((FunctionCall)a.right); f.parameters[nodes.IndexOf(nodeI, j)] = nodeI; j = 0; } } if (IsNotVisited(nodeI, nodeStateMap)) { DFSVisit(nodeI, nodeStateMap, list, statementList); } } nodeStateMap[node] = VISITED; list.Add(node); }
public GraphNodeToASTGenerator(AST graph, GraphCompiler gc) { this.graph = graph; this.gc = gc; }
private string BuildScriptFromIR(AST graph) { return ""; }
public static List<Node> sort(AST graph) { return DFS(graph); }
/* proc Rewrite(graph) foreach node in graph if node is CodeBlock RewriteCodeBlock(node) end end end */ private void RewriteCodeBlock(AST graph) { List<int> nodesToRemove = new List<int>(); List<uint> guidListOfNodesToRemove = new List<uint>(); List<Node> rewrittenNodeList = new List<Node>(); Dictionary<uint, Node> rewrittenUIDMap = new Dictionary<uint, Node>(); foreach (Node node in graph.nodeList) { if (node != null) { if (node is Block) { List<Block> listBlocks = RewriteCodeBlock(node as Block); if (listBlocks != null && listBlocks.Count > 1) { // Add the new range of split blocks //rewrittenNodeList.AddRange(listBlocks); // Add the uids of every split block foreach (Block toAdd in listBlocks) { if (rewrittenUIDMap.ContainsKey(toAdd.Guid)) { rewrittenNodeList.RemoveAll((x) => x.Guid == toAdd.Guid); rewrittenNodeList.Add(toAdd); rewrittenUIDMap[toAdd.Guid] = toAdd; } else { rewrittenNodeList.Add(toAdd); rewrittenUIDMap.Add(toAdd.Guid, toAdd); } } } else { if (((Block)node).splitFomUint ==0) ((Block)node).splitFomUint = node.Guid; if (!rewrittenNodeList.Contains(node)) rewrittenNodeList.Add(node); if (rewrittenUIDMap.ContainsKey(node.Guid)) rewrittenUIDMap[node.Guid] = node; else rewrittenUIDMap.Add(node.Guid, node); } } else { if (!rewrittenNodeList.Contains(node)) rewrittenNodeList.Add(node); if (rewrittenUIDMap.ContainsKey(node.Guid)) rewrittenUIDMap[node.Guid] = node; else rewrittenUIDMap.Add(node.Guid, node); } } } graph.nodeList = new List<Node>(rewrittenNodeList); graph.nodeMap = new Dictionary<uint, Node>(rewrittenUIDMap); }
private static void BuildBlockToIdentStatement(Node node, Node nodeIdent, AST statementList) { Validity.Assert(node is Block); if (node.children.Count > 1) { statementList.AddNode(node); } else { IdentNode identNode = nodeIdent as IdentNode; Validity.Assert(null != identNode); Block block = node as Block; string lhs = string.Empty; string content = string.Empty; bool isSingleIdent = string.IsNullOrEmpty(block.LHS); if (isSingleIdent) { lhs = block.TrimName(); } else { lhs = block.LHS; } // Create the cnontents of the new block. content = lhs + '=' + identNode.Name; // Comment Jun: Remove the current codeblock first // Codeblock removal is guid dependent Node nodeToRemove = statementList.GetNode(node.Guid); int index = statementList.nodeList.IndexOf(nodeToRemove); statementList.RemoveNode(nodeToRemove); // Comment Jun: Create a new block using the current guid // This new codeblock represents the new contents Block block2 = new Block(content, node.Guid); statementList.nodeList.Insert(index, block2); statementList.nodeMap.Add(block2.Guid, block2); statementList.AddNode(block2); // Comment Jun: Reflect the new changes to the original block (node as Block).SetData(block2.LHS, content); } }
private string BuildScriptFromGraph(AST graph) { string liststat = ""; //RewriteCodeBlock(graph); List<Node> li = TopSort.sort(graph); //li = SortCodeBlocks(li); tguid = 20000; //IEnumerable iter = li; List<string> listIslands = new List<string>(); List<Node> islandNodes = new List<Node>(); int countIslands = 0; statementList = new AST(); ModifiedStmtGuidList.Clear(); List<string> importIslands = new List<string>(); IEnumerable iter = li; List<Node> finalScript = new List<Node>(); List<List<Node>> listing = new List<List<Node>>(); foreach (Node node in iter) { if (node != null) { if (node is ImportNode) { importIslands.Add(node.ToScript() + ProtoCore.DSASM.Constants.termline); } else if (node.IsIsland) { countIslands++; if (node is ArrayNode) { BuildArrayNodeStatement(node, statementList); if (!islandNodes.Contains(node)) islandNodes.Add(node); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is LiteralNode) { BuildLiteralNodeStatement(node, statementList); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is Operator) { BuildOperatorStatement(node, statementList); string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } else if (node is Block) { BuildBlockStatement(node, statementList); if (!islandNodes.Contains(node)) islandNodes.Add(node); /*string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island);*/ } else if (node is IdentNode) { // comment Jun: // An island identifier node is handled by emitting a null as its rhs statementList.AddNode(node); string contents = statementList.GetNode(node.Guid).ToScript() + "=" + ProtoCore.DSASM.Literal.Null + ProtoCore.DSASM.Constants.termline; listIslands.Add(contents); } else { statementList.AddNode(node); string island = node.ToScript() + ProtoCore.DSASM.Constants.termline; if (!listIslands.Contains(island)) listIslands.Add(island); } HandleNewNode(node); } else if (node.IsLeaf) { if (node is ArrayNode) { BuildArrayNodeStatement(node, statementList); } else if (node is LiteralNode) { BuildLiteralNodeStatement(node, statementList); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); } else if (node is Operator) { BuildOperatorStatement(node, statementList); } else if (node is Block) { BuildBlockStatement(node, statementList); } else if (node is IdentNode) { statementList.AddNode(node); string contents = statementList.GetNode(node.Guid).ToScript() + "=" + ProtoCore.DSASM.Literal.Null + ProtoCore.DSASM.Constants.termline; listIslands.Add(contents); } HandleNewNode(node); } else if (node.IsRoot && !node.IsIsland) { if (node is Operator) { BuildOperatorStatement(node, statementList); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); } else if (node is Block) { BuildBlockStatement(node, statementList); } else if (node is IdentNode) { BuildIdentStatement(node, statementList); } //liststat = BuildStatement(node, statementList); //finalScript=finalScript.Union(BuildStatement(node, statementList)).ToList(); //comment out for new node to code listing.Add(BuildStatement(node, statementList)); HandleNewNode(node); } else if (node is Operator) { BuildOperatorStatement(node, statementList); HandleNewNode(node); } else if (node is Func) { BuildFunctionCallStatement(node, statementList); HandleNewNode(node); } else if (node is ArrayNode) { BuildArrayNodeStatement(node, statementList); HandleNewNode(node); } else if (node is LiteralNode) { BuildLiteralNodeStatement(node, statementList); HandleNewNode(node); } else if (node is Block) { BuildBlockStatement(node, statementList); HandleNewNode(node); } else if (node is IdentNode) { BuildIdentStatement(node, statementList); HandleNewNode(node); } else { HandleNewNode(node); } } } StringBuilder builder = new StringBuilder(); foreach (string island in importIslands) // Loop through all strings { builder.Append(island); // Append string to StringBuilder } // nullify deleted variables or variables that undefined in this // run. HashSet<string> nullifyVariables = new HashSet<string>(RemovedNodeNameList); nullifyVariables.UnionWith(this.UndefinedNameList); foreach (string variable in nullifyVariables) { if (!String.IsNullOrEmpty(variable)) { builder.Append(variable + " = null;\n"); } } //for_taking_care_of_nulls const string tempNullAssign = Constants.kwTempNull + "=" + ProtoCore.DSDefinitions.Keyword.Null + ";\n"; builder.Append(tempNullAssign); foreach (string island in listIslands) // Loop through all strings { builder.Append(island); // Append string to StringBuilder } foreach (List<Node> n1 in listing) { foreach (Node n2 in n1) { if (!nodesToGuid(finalScript).Contains(n2.Guid)) { finalScript.Add(n2); } else { finalScript.RemoveAll(x=>x.Guid == n2.Guid); finalScript.Add(n2); } } } finalScript = finalScript.Union(islandNodes).ToList(); finalScript = SortCodeBlocks(finalScript); foreach (var value in finalScript) { liststat += value.ToScript() + ProtoCore.DSASM.Constants.termline; } liststat = builder.ToString() + liststat; //GraphUtilities.runningUID = GraphToDSCompiler.Constants.UIDStart; UpdateAddedNodesInModifiedNameList(); return liststat; }
private static void BuildBlockToFuncStatement(Node node, Node nodeFunc, AST statementList) { Validity.Assert(node is Block); if (node.children.Count > 1) { statementList.AddNode(node); } else { Assignment funcCall = (Assignment)statementList.GetNode(nodeFunc.Guid); Block block = node as Block; string lhs = string.Empty; string content = string.Empty; bool isSingleIdent = string.IsNullOrEmpty(block.LHS); if (isSingleIdent) { lhs = block.TrimName(); // Create the cnontents of the new block. // The rhs of the block is the rhs of the function call statement content = lhs + '=' + funcCall.left.Name; } else { lhs = block.LHS; content = block.Name; } // Comment Jun: Remove the current codeblock first // Codeblock removal is guid dependent Node nodeToRemove = statementList.GetNode(node.Guid); int index = statementList.nodeList.IndexOf(nodeToRemove); statementList.RemoveNode(nodeToRemove); // Comment Jun: Create a new block using the current guid // This new codeblock represents the new contents // Comment Tron: Insert the new node into the index where the removed node was originally Block block2 = new Block(content, node.Guid); statementList.nodeList.Insert(index, block2); statementList.nodeMap.Add(block2.Guid, block2); // Comment Jun: Reflect the new changes to the original block (node as Block).SetData(block2.LHS, content); } }
private void BuildIdentStatement(Node node, AST statementList) { IdentNode identNode = node as IdentNode; Assignment a = new Assignment(identNode, identNode); statementList.AddNode(a); }
private static int BuildFuncToBlockStatement(Node node, Node nodeI, Dictionary<int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); Block block = nodeI as Block; List<int> keys = GetKeysFromValue(nodeDic, nodeI); FunctionCall f = ((FunctionCall)a1.right); if (keys.Count > 1) { do { f.parameters[keys[j]] = new IdentNode(block.LHS, block.Guid); j++; } while (j < keys.Count); } else { f.parameters[keys[j]] = new IdentNode(block.LHS, block.Guid); } j = 0; return j; }
private void BuildBlockStatement(Node node, AST statementList) { if (node.children.Count > 1 || node.IsLeaf || node.IsIsland) { statementList.AddNode(node); } else { Block n = node as Block; Node a; // TODO Jun: why is this check needed? // This checks if the current node is a full expression // If a codeblock contains LHS then it is a full expression if (!string.IsNullOrEmpty(n.LHS)) { // This is a full expression // such as: [a = 2;] a = new Assignment(new IdentNode(n.LHS, node.Guid), new IdentNode(n.LHS, node.Guid)); } else { // This is a single identifier // such as: [a;] string s = n.Name; s = s.TrimEnd(';'); a = new Assignment(new IdentNode(s, node.Guid), new IdentNode(s, node.Guid)); } statementList.AddNode(a); } }
private void BuildLiteralNodeStatement(Node node, AST statementList) { LiteralNode n = (LiteralNode)node; tguid = GetTempGUID(n.Guid); string name = GraphToDSCompiler.kw.tempPrefix + tguid; n.tempName = name; Node a = new Assignment(new IdentNode(name, tguid), n); statementList.AddNode(a); }
private void BuildArrayNodeStatement(Node node, AST statementList) { Expr e = (Expr)node; tguid = GetTempGUID(e.Guid); Assignment a = new Assignment(new IdentNode(GraphToDSCompiler.kw.tempPrefix + tguid, tguid), e); statementList.AddNode(a); }
private static void BuildIdentToOperatorStatement(Node node, AST statementList, Node nodeI) { Assignment a2 = (Assignment)statementList.GetNode(nodeI.Guid); Assignment a = new Assignment((IdentNode)node, (IdentNode)a2.left); if (statementList.nodeMap.ContainsKey(a.Guid)) { Node nodeToRemove = statementList.GetNode(a.Guid); int index = statementList.nodeList.IndexOf(nodeToRemove); statementList.RemoveNode(nodeToRemove); statementList.nodeList.Insert(index, a); statementList.nodeMap.Add(a.Guid, a); } else statementList.AddNode(a); }
private void BuildFunctionCallStatement(Node node, AST statementList) { Func n = (Func)node; tguid = GetTempGUID(n.Guid); FunctionCall f = new FunctionCall(n); Assignment a = new Assignment(new IdentNode(n.tempName, tguid), f); statementList.AddNode(a); }
private static int BuildFuncToFuncStatement(Node node, Node nodeI, Dictionary<int, Node> nodeDic, AST statementList, int j) { Assignment a1 = (Assignment)statementList.GetNode(node.Guid); Assignment a2 = (Assignment)statementList.GetNode(nodeI.Guid); List<int> keys = GetKeysFromValue(nodeDic, nodeI); FunctionCall f = ((FunctionCall)a1.right); if (keys.Count > 1) do { f.parameters[keys[j]] = a2.left; j++; } while (j < keys.Count); else f.parameters[keys[j]] = a2.left; j = 0; return j; }
private void BuildOperatorStatement(Node node, AST statementList) { Operator n = (Operator)node; Expr e1 = null; Expr e2 = null; tguid = GetTempGUID(n.Guid); BinExprNode b = new BinExprNode(e1, n, e2); Assignment a = new Assignment(new IdentNode(n.tempName, tguid), b); statementList.AddNode(a); }
public string BuildScript(AST graph) { string statements = ""; if (usePatternRewrite) {/* // Transform the graph into the graph IR GraphTransform graphTransform = new GraphTransform(graph); graph = graphTransform.ToFinalGraph(); statements = BuildScriptFromIR(graph);*/ } else { statements = BuildScriptFromGraph(graph); } return statements; }