Ejemplo n.º 1
0
        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);
                }
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
        /// <summary>
        /// Create BinaryExpressionNode
        /// </summary>
        /// <param name="node"></param>
        /// <param name="outnode"></param>
        private void EmitBlockNode(Block node, out AssociativeNode outnode)
        {
            Validity.Assert(node != null);
            
            // TODO: Confirm that these children are returned in the order that they
            // appear in the code
            Dictionary<int, Node> childNodes = node.GetChildrenWithIndices();

            // Parse program statement in node.Name
            string code = node.Name + ";";

            ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlockNode = (ProtoCore.AST.AssociativeAST.CodeBlockNode)ProtoCore.Utils.ParserUtils.Parse(code);
            Validity.Assert(codeBlockNode != null);

            List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = codeBlockNode.Body;
            Validity.Assert(astList.Count == 1);

            if (astList[0] is ProtoCore.AST.AssociativeAST.IdentifierNode)
            {
                ProtoCore.AST.AssociativeAST.BinaryExpressionNode ben = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode();
                ben.LeftNode = astList[0];
                ben.Optr = ProtoCore.DSASM.Operator.assign;
                ProtoCore.AST.AssociativeAST.AssociativeNode statement = null;
                foreach (KeyValuePair<int, Node> kvp in childNodes)
                    DFSTraverse(kvp.Value, out statement);
                ben.RightNode = statement;
                astList[0] = ben;
            }

            //I don't know what I am doing
            if (astList[0] is BinaryExpressionNode)
            {
                BinaryExpressionNode tempBen = astList[0] as BinaryExpressionNode;
                if (tempBen.LeftNode is IdentifierNode)
                {
                    IdentifierNode identitiferNode = tempBen.LeftNode as IdentifierNode;
                    if (identitiferNode.ArrayDimensions != null)
                    {
                        ArrayIndexerNode arrIndex = new ArrayIndexerNode();
                        arrIndex.ArrayDimensions = identitiferNode.ArrayDimensions;
                        arrIndex.Array = identitiferNode;
                        tempBen.LeftNode = arrIndex;
                    }
                }
                if (tempBen.RightNode is IdentifierNode)
                {
                    IdentifierNode identitiferNode = tempBen.RightNode as IdentifierNode;
                    if (identitiferNode.ArrayDimensions != null)
                    {
                        ArrayIndexerNode arrIndex = new ArrayIndexerNode();
                        arrIndex.ArrayDimensions = identitiferNode.ArrayDimensions;
                        arrIndex.Array = identitiferNode;
                        tempBen.RightNode = arrIndex;
                    }
                }
                astList[0] = tempBen;
            }
            //it should be correct, if not, debug?

            ProtoCore.AST.AssociativeAST.BinaryExpressionNode bNode = astList[0] as ProtoCore.AST.AssociativeAST.BinaryExpressionNode;
            Validity.Assert(bNode != null);
            bNode.Guid = node.Guid;
            //bNode.SplitFromUID = node.splitFomUint;

            // Child nodes are arguments to expression in bNode.RightNode. 
            // Match child nodes with IdentifierNode's in bNode.RightNode - pratapa
            foreach (Node n in childNodes.Values)
            {
                AssociativeNode argNode = null;
                DFSTraverse(n, out argNode);

                // DFS traverse the bNode.RightNode and check for IdentifierNode's
                // and if their names match with the names of argNode, then replace
                // the IdentifierNode in bNode.RightNode with argNode
                BinaryExpressionNode ben = argNode as BinaryExpressionNode;
                Validity.Assert(ben != null);
                //ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(ben);
                AstCodeBlockTraverse sourceGen = new AstCodeBlockTraverse(ben);
                ProtoCore.AST.AssociativeAST.AssociativeNode right = bNode.RightNode;
                sourceGen.DFSTraverse(ref right);
                bNode.RightNode = right;
            }

            //(AstRootNode as CodeBlockNode).Body.Add(expressionNode);

            Validity.Assert(gc != null);
            gc.HandleNewNode(bNode);

            outnode = bNode;
        }
Ejemplo n.º 5
0
        /*
        proc RewriteCodeBlock(Node codeblock)
		
		    // Create new codeblocks for every line of code in the current codeblock CodeBlock[] newBlockList = new CodeBlock[node.lines.length]
            for n = 0 to node.lines.length
	            newBlockList[n].code = node.lines[n]
	            newBlockList[n].uid = generateUID(codeblock.uid)
            end
		
		    // At this point, determine which parents of the current codeblock node need to be connected to each splitted node

		    // Iterate through each parent of the current code block
		    foreach parentNode in codeblock.parents 

			    // Iterate through each child of the parent
			    for n = 0 to parentNode.children.length
			
				    // Check if the child is this codeblock
				    if parentNode.children[n] is equal to codeblock

                        // index ‘n’ is the current output slot of the codeblock		
                        newBlockList[n].parent.push(parentNode)

					    // Rewire the parent’s child to this new codeblock
					    parentNode.children[n] = newBlockList[n]

				    end
				    n++;
			    end
		    end			
        end

         */


        /*private List<Block> RewriteCodeBlock(Block codeblock)
        {
            Validity.Assert(codeblock != null);

            if (codeblock.Name == null || codeblock.Name.Length <= 0)
            {
                return null;
            }


            // Comment Jun: Im just trying to find the number of times ';' occurs
            // Make this more efficient by turning this into a function in a utils class
            string dummy = codeblock.Name.Replace(";", "");
            if ((codeblock.Name.Length - dummy.Length) <= 1)
            {
                // Single line codeblocks need not be split
                return null;
            }

            string[] token = new string[1];
            token[0] = ProtoCore.DSASM.Constants.termline;
            StringSplitOptions opt = new StringSplitOptions();
            string[] contents = codeblock.Name.Split(token, opt);
            int length = contents.Length;


            // The map of the new uid and its associated connecting slot
            Dictionary<int, uint> slotIndexToUIDMap = new Dictionary<int, uint>();

            // Create new codeblocks for every line of code in the current codeblock CodeBlock[] newBlockList = new CodeBlock[node.lines.length]
            List<Block> newBlockList = new List<Block>();
            for (int n = 0; n < length; n++)
            {
                // TODO Jun: Check with IDE why the semicolon is inconsitent
                string code = contents[n];
                if (code.Length > 0 && code[0] != ';')
                {
                    uint newGuid = GraphUtilities.GenerateUID();

                    List<AssignmentStatement> assignmentData = new List<AssignmentStatement>();
                    if (codeblock.assignmentData.Count > 0)
                    {
                        // This assignemnt data list must contain only 1 element
                        // This element is associated witht he current line in the codeblock
                        assignmentData.Add(codeblock.assignmentData[n]);
                    }
                    
                    Block newBlock = new Block(code, newGuid, assignmentData);
                    newBlock.splitFomUint = codeblock.Guid;
                    newBlockList.Add(newBlock);
                    slotIndexToUIDMap.Add(n, newGuid);
                }
            }

            // At this point, determine which parents of the current codeblock node need to be connected to each splitted node

            // Iterate through each parent of the current code block
            List<Node> parents = codeblock.GetParents();
            foreach (Node parentNode in parents)
            {
                // Iterate through each child of the parent
                for (int childIndex = 0; childIndex < parentNode.children.Count; childIndex++)
                {
                    Node child = null;
                    bool foundIndex = parentNode.children.TryGetValue(childIndex, out child);

                    if (foundIndex)
                    {
                        // Check if the child is this codeblock
                        if (child.Guid == codeblock.Guid)
                        {
                            int fromIndex = parentNode.childConnections[childIndex].from;

                            // Set the new codeblock's parent
                            newBlockList[fromIndex].AddParent(parentNode);

                            // Rewire the parent’s child to this new codeblock
                            parentNode.RemoveChild(childIndex);

                            parentNode.AddChild(newBlockList[fromIndex], childIndex, fromIndex);
                        }
                    }
                }
            }
            if (codeBlockUIDMap.ContainsKey(codeblock.Guid))
            {
                codeBlockUIDMap[codeblock.Guid] = slotIndexToUIDMap;
            }
            else
            {
                codeBlockUIDMap.Add(codeblock.Guid, slotIndexToUIDMap);
            }
            return newBlockList;
        }*/

        private List<Block> RewriteCodeBlock(Block codeblock)
        {
            Validity.Assert(codeblock != null);

            if (codeblock.Name == null || codeblock.Name.Length <= 0 )
            {
                return null;
            }

            // Comment Aparajit: This check is erroneous and should be removed
            // It's there since in some Nunit tests, comment strings are being passed in here
            string dummy = codeblock.Name.Replace(";", "");
            if ((codeblock.Name.Length - dummy.Length) <= 1)
            {
                return null;
            }

            //if (!codeblock.Name.EndsWith(";"))
              //  codeblock.Name += ";";

            List<ProtoCore.AST.Node> nodes = GraphUtilities.ParseCodeBlock(codeblock.Name);
            if (nodes.Count <= 1)
            {
                // Single line codeblocks need not be split
                return null;
            }

            int length = nodes.Count;


            // The map of the new uid and its associated connecting slot
            Dictionary<int, uint> slotIndexToUIDMap = new Dictionary<int, uint>();

            // Create new codeblocks for every line of code in the current codeblock CodeBlock[] newBlockList = new CodeBlock[node.lines.length]
            List<Block> newBlockList = new List<Block>();
            for (int n = 0; n < length; n++)
            {
                ProtoCore.AST.Node node = nodes[n];
                Validity.Assert(node is ProtoCore.AST.AssociativeAST.BinaryExpressionNode);

                string code = ProtoCore.Utils.ParserUtils.ExtractStatementFromCode(codeblock.Name, node);
                if (code.Length > 0)
                {
                    uint newGuid = GraphUtilities.GenerateUID();

                    List<AssignmentStatement> assignmentData = new List<AssignmentStatement>();
                    if (codeblock.assignmentData.Count > 0)
                    {
                        // This assignemnt data list must contain only 1 element
                        // This element is associated witht he current line in the codeblock
                        assignmentData.Add(codeblock.assignmentData[n]);
                    }

                    Block newBlock = new Block(code, newGuid, assignmentData);
                    newBlock.splitFomUint = codeblock.Guid;
                    newBlockList.Add(newBlock);
                    slotIndexToUIDMap.Add(n, newGuid);
                }
            }

            // At this point, determine which parents of the current codeblock node need to be connected to each splitted node

            // Iterate through each parent of the current code block
            List<Node> parents = codeblock.GetParents();
            foreach (Node parentNode in parents)
            {
                // Iterate through each child of the parent
                for (int childIndex = 0; childIndex < parentNode.children.Count; childIndex++)
                {
                    Node child = null;
                    bool foundIndex = parentNode.children.TryGetValue(childIndex, out child);

                    if (foundIndex)
                    {
                        // Check if the child is this codeblock
                        if (child.Guid == codeblock.Guid)
                        {
                            int fromIndex = parentNode.childConnections[childIndex].from;

                            // Set the new codeblock's parent
                            newBlockList[fromIndex].AddParent(parentNode);

                            // Rewire the parent’s child to this new codeblock
                            parentNode.RemoveChild(childIndex);

                            parentNode.AddChild(newBlockList[fromIndex], childIndex, fromIndex);
                        }
                    }
                }
            }
            if (codeBlockUIDMap.ContainsKey(codeblock.Guid))
            {
                codeBlockUIDMap[codeblock.Guid] = slotIndexToUIDMap;
            }
            else
            {
                codeBlockUIDMap.Add(codeblock.Guid, slotIndexToUIDMap);
            }
            return newBlockList;
        }