Example #1
0
 /// <summary>
 /// Represents simple binary expressions which involve identifier on left and literal on the right
 /// </summary>
 /// <param name="i1"></param>
 /// <param name="o"></param>
 /// <param name="i2"></param>
 internal BinExprNode(IdentNode i1, Operator o, LiteralNode i2)
     : base(i1.Name + o.Name + i2.Name, o.Guid)
 {
     op = o;
     left = i1;
     right = i2;
 }
Example #2
0
 /// <summary>
 /// Represents compound binary expressions which involve other expressions on the left and right side
 /// </summary>
 /// <param name="e1"></param>
 /// <param name="o"></param>
 /// <param name="e2"></param>
 internal BinExprNode(Expr e1, Operator o, Expr e2)
     : base(o.Name, o.Guid)
 {
     op = o;
     left = e1;
     right = e2;
 }
Example #3
0
 public BinExprNode(ProtoCore.DSASM.Operator optr, Node leftNode, Node rightNode)
     : base(leftNode.Name, leftNode.Guid)
 {
     this.Optr = optr;
     this.left = leftNode;
     this.right = rightNode;
 }
        // TODO: Deprecate
        private static IdentifierNode CreateTempIdentifierNode(Node node)
        {
            uint tguid = GraphCompiler.GetTempGUID(node.Guid);
            string tempName = GraphToDSCompiler.kw.tempPrefix + tguid;

            return new IdentifierNode(tempName);
        }
Example #5
0
 //
 // TODO Jun: Deprecate the overloaded constructors.
 // All assignment nodes must be instantiated with this constructor
 public Assignment(Node left, Node right)
     : base("", left.Guid)
 {
     Optr = ProtoCore.DSASM.Operator.assign;
     base.Name = ProtoCore.Utils.CoreUtils.GetOperatorString(Optr);
     this.left = left;
     this.right = right;
 }
Example #6
0
 private static Boolean IsNotVisited(Node node, Dictionary<Node, int> nodeStateMap)
 {
     if (!nodeStateMap.ContainsKey(node))
     {
         return true;
     }
     int state = nodeStateMap[node];
     return NOT_VISTITED == state;
 }
Example #7
0
 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;
 }
Example #8
0
 public static List<int> GetKeysFromValue(Dictionary<int, Node> dict, Node node)
 {
     List<int> ks = new List<int>();
     foreach (int k in dict.Keys)
     {
         if (dict[k].Equals(node))
         {
             ks.Add(k);
         }
     }
     return ks;
 }
Example #9
0
 static void DFSVisit(Node node, Dictionary<Node, int> nodeStateMap, List<Node> list)
 {
     nodeStateMap.Add(node, VISITING);
     List<Node> nodes = node.GetChildren();
     IEnumerable iter = nodes;
     foreach (Node nodeI in iter)
     {
         if (IsNotVisited(nodeI, nodeStateMap))
             DFSVisit(nodeI, nodeStateMap, list);
     }
     nodeStateMap[node] = VISITED;
     list.Add(node);
 }
Example #10
0
 public void AddEdge(Node from, Node to, int inputIndex, int fromIndex)
 {
     from.AddChild(to, inputIndex, fromIndex);
     to.AddParent(from);
     List<Node> cycle = CheckForCycle.CreatesCycle(to);
     if (cycle != null)
     {
         // remove edge which introduced cycle
         //    RemoveEdge( from, to );
         String msg = "Edge between '" + from.Name + "' and '" + to.Name + "' introduces a cycle in the graph";
         GraphCompilationStatus.HandleError(new HasCycleException(msg, cycle)) ;
     }
 }
Example #11
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);
                }
            }
        }
Example #12
0
        /*public HashSet<uint> GetNames()
        {
            HashSet<uint> nameSet = new HashSet<uint>(nodeMap.Keys);
            return nameSet;
        }
        */
        /// <summary>
        /// Adds a new node to the nodemap given its name and guid if it is not present in the graph.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public Node AddNode(string name, uint guid)
        {
            Node newNode = null;

            if (nodeMap.ContainsKey(guid))
            {
                newNode = (Node)nodeMap[guid];
            }
            else
            {
                newNode = new Node(name, guid);
                nodeMap.Add(guid, newNode);
                nodeList.Add(newNode);
            }
            return newNode;
        }
Example #13
0
        /// <summary>
        /// Adds the node passed as parameter to the nodemap if it is not present in the graph.
        /// Returns the added newNode.
        /// </summary>
        /// <param name="nodus"></param>
        /// <returns></returns>
        public Node AddNode(Node nodus)
        {
            Node newNode = null;

            if (nodeMap.ContainsKey(nodus.Guid))
            {
                newNode = (Node)nodeMap[nodus.Guid];
            }
            else
            {
                newNode = nodus;
                nodeMap.Add(nodus.Guid, newNode);
                if (newNode is ImportNode) nodeList.Insert(0, newNode);
                else
                    nodeList.Add(newNode);
            }
            return newNode;
        }
Example #14
0
 public static List<Node> CreatesCycle(Node node, Dictionary<Node, int> nodeStateMap)
 {
     List<Node> cycleStack = new List<Node>();
     Boolean hasCycle = DFSVisit( node, nodeStateMap,cycleStack);
     if ( hasCycle )
     {
         // we have a situation like: [c, a, b, c, f, g, h].
         // Node which introduced  the cycle is at the first position in the list
         // We have to find second occurence of this node and use its position in the list
         // for getting the sublist of vertex labels of cycle paricipants
         // So in our case we are seraching for [c, a, b, c]
         //string name = cycleStack[0].Name;
         int pos = cycleStack.IndexOf(cycleStack[0],0);
         List<Node> cycle = new List<Node>(pos + 1);
         for(int i=0;i<pos+1;i++)
             cycle.Add(cycleStack[i]);
         cycle.Reverse();
         return cycle;
     }
     return null;
 }
Example #15
0
 /// <summary>
 /// Returns false if a depth-first search of Graph yields no back edges.
 /// If back edges found then return true and update cycleStack.
 /// </summary>
 /// <param name="node"></param>
 /// <param name="nodeStateMap"></param>
 /// <param name="cycleStack"></param>
 /// <returns></returns>
 public static Boolean DFSVisit(Node node, Dictionary<Node, int> nodeStateMap, List<Node> cycleStack)
 {
     cycleStack.Add(node);
     nodeStateMap.Add(node, visiting);
     List<Node> nodes = node.GetChildren();
     IEnumerable iter = nodes;
     foreach (Node nodeI in iter)
     {
         if (IsNotVisited(nodeI, nodeStateMap))
         {
             if (DFSVisit(nodeI, nodeStateMap, cycleStack)) return true;
         }
         else if (IsVisiting(nodeI, nodeStateMap))
         {
             cycleStack.Insert(0,node);
             return true;
         }
     }
     nodeStateMap[node] = visited;
     cycleStack.RemoveAt(0);
     return false;
 }
Example #16
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);
            }
        }
Example #17
0
        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);
        }
Example #18
0
        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);
        }
Example #19
0
        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;
        }
Example #20
0
        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;
        }
Example #21
0
 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;
 }
Example #22
0
 /// <summary>
 /// Remove the edge connecting the input of this note to output of the specified node
 /// Also call RemoveEdgeTo on node
 /// </summary>
 public void RemoveParent(Node node)
 {
     parents.Remove(node);
 }
Example #23
0
 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;
 }
Example #24
0
 public static List<Node> sort(Node node, AST statementList)
 {
     return DFS(node, statementList);
 }
Example #25
0
 /// <summary>
 /// Connects the output of this node to the specified input of the node passed
 /// Also call 'AddEdgeFrom' on the other node
 /// </summary>
 /// <param name="node"></param>
 /// <param name="pos"></param>
 public void AddChild(Node node, int toIndex, int fromIndex)
 {
     children.Add(toIndex, node);
     childConnections.Add(toIndex, new NodeConnection(toIndex, fromIndex));
 }
        private void DFSTraverse(Node rootNode, out AssociativeNode outnode)
        {
            AssociativeNode node = null;
            if (rootNode is LiteralNode)
            {
                EmitLiteralNode(rootNode as LiteralNode, out node);
            }
            else if (rootNode is IdentNode)
            {
                EmitIdentifierNode(rootNode as IdentNode, out node);
            }
            else if (rootNode is Operator)
            {
                EmitBinaryExpNode(rootNode as Operator, out node);
            }
            else if (rootNode is Block)
            {
                // Create BinaryExpressionNode
                EmitBlockNode(rootNode as Block, out node);
            }
            else if (rootNode is Func)
            {
                EmitFunctionNode(rootNode as Func, out node);
            }
            

            outnode = node;
        }
Example #27
0
 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;
 }
Example #28
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);
            }
        }
Example #29
0
        //
        // 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);
        }
        // TODO: Deprecate
        private static BinaryExpressionNode CreateBinaryExpNode(Node node, AssociativeNode expressionNode)
        {
            BinaryExpressionNode assignmentNode = new BinaryExpressionNode();
            assignmentNode.LeftNode = CreateTempIdentifierNode(node);
            assignmentNode.LeftNode = new IdentifierNode();
            assignmentNode.Optr = ProtoCore.DSASM.Operator.assign;
            assignmentNode.RightNode = expressionNode;
            assignmentNode.Guid = node.Guid;

            return assignmentNode;
        }