コード例 #1
0
        public bool ParentStar(int statementNumber1, int statementNumber2)
        {
            if (ParentTable.Where(x => x.ContainerStatementNumber == statementNumber1 && x.ChildsStatementNumber.Contains(statementNumber2)).Any())
            {
                return(true);
            }
            ParrentTableCell cell = ParentTable.Where(x => x.ChildsStatementNumber.Contains(statementNumber2)).FirstOrDefault();

            if (cell == null)
            {
                return(false);
            }
            return(ParentStar(statementNumber1, cell.ContainerStatementNumber));
        }
コード例 #2
0
        private ParrentTableCell GetParentCell(AstNode node, ref List <ParrentTableCell> parentList)
        {
            ParrentTableCell cell = parentList.Where(x => x.ContainerStatementNumber == node.StatementNumber).FirstOrDefault();

            if (cell == null)
            {
                cell = new ParrentTableCell()
                {
                    ContainerStatementNumber = node.StatementNumber,
                    ChildsStatementNumber    = new List <int>()
                };
                parentList.Add(cell);
            }
            return(cell);
        }
コード例 #3
0
        //private static void FindCallInStatements(AstNode procedure, ref List<ProcTableCell> resultList)
        //{
        //    //For each statement
        //    foreach (var node in ExtractStatements(procedure))
        //    {
        //        //Find call
        //        if (node.NodeType == NodeType.Call)
        //        {
        //            var parentValue = GetParentProcedureName(node);
        //            var cell = resultList.FirstOrDefault(c => c.ProcedureName == parentValue);
        //            if (cell != null)
        //            {
        //                cell.CalledProcedures.Add(GetCalledProcedureName(node));
        //            }
        //            else
        //            {
        //                resultList.Add(new ProcTableCell()
        //                {
        //                    ProcedureName = parentValue,
        //                    CalledProcedures = new List<string> { GetCalledProcedureName(node) }
        //                });
        //            }
        //        }
        //        else if (node.NodeType == NodeType.If || node.NodeType == NodeType.While)
        //        {
        //            FindCallInStatements(node, ref resultList);
        //        }
        //    }
        //}

        private StatementTableModel FindStatements(AstNode parentNode, ref List <ProcTableCell> resultList, ref List <StatementColumn> modifiesList, ref List <StatementColumn> usesList, ref List <ParrentTableCell> parentList, ref Dictionary <int, int> followsDicionary)
        {
            List <string> tmpElementsModifies = new List <string>();
            List <string> tmpElementsUses     = new List <string>();
            var           statements          = parentNode?.Children;

            if (!(parentNode.NodeType == NodeType.StatementList))
            {
                statements = ExtractStatements(parentNode);
            }
            int previousStatementNumber = 0;

            foreach (var node in statements)
            {
                if (node.NodeType == NodeType.Variable && Regex.IsMatch(node.Value, @"^[a-zA-Z_$][a-zA-Z_$0-9]*$") && !UsedVariables.Contains(node.Value))
                {
                    UsedVariables.Add(node.Value);
                }
                if (previousStatementNumber == 0)
                {
                    previousStatementNumber = node.StatementNumber;
                }
                else
                {
                    if (!followsDicionary.ContainsKey(previousStatementNumber))
                    {
                        followsDicionary.Add(previousStatementNumber, node.StatementNumber);
                    }
                    previousStatementNumber = node.StatementNumber;
                }

                //Find call
                if (node.NodeType == NodeType.Assign)
                {
                    tmpElementsModifies.Add(AddAssignModifiesColumn(node, ref modifiesList));
                    tmpElementsUses.AddRange(AddUsesAssignData(node, ref usesList).Distinct());
                }
                else if (node.NodeType == NodeType.If)
                {
                    //Parent
                    ParrentTableCell cell = GetParentCell(node, ref parentList);
                    foreach (var childNode in node.Children[1].Children)
                    {
                        if (!cell.ChildsStatementNumber.Contains(childNode.StatementNumber))
                        {
                            cell.ChildsStatementNumber.Add(childNode.StatementNumber);
                        }
                    }
                    foreach (var childNode in node.Children[2].Children)
                    {
                        if (!cell.ChildsStatementNumber.Contains(childNode.StatementNumber))
                        {
                            cell.ChildsStatementNumber.Add(childNode.StatementNumber);
                        }
                    }

                    List <string>       tmpModifiesArray = new List <string>();
                    List <string>       tmpUsesArray     = new List <string>();
                    StatementTableModel thenResult       = FindStatements(node.Children[1], ref resultList, ref modifiesList, ref usesList, ref parentList, ref followsDicionary);
                    StatementTableModel elseResult       = FindStatements(node.Children[2], ref resultList, ref modifiesList, ref usesList, ref parentList, ref followsDicionary);

                    //Modifies
                    tmpModifiesArray.AddRange(thenResult.ModifiesElements.Distinct());
                    tmpModifiesArray.AddRange(elseResult.ModifiesElements.Distinct());
                    CheckTableRow(node, ref tmpModifiesArray, ref modifiesList);
                    tmpElementsModifies.AddRange(tmpModifiesArray.Distinct());

                    //Uses
                    if (node.Children[0].NodeType == NodeType.Variable && Regex.IsMatch(node.Children[0].Value, @"^[a-zA-Z_$][a-zA-Z_$0-9]*$"))
                    {
                        if (!UsedVariables.Contains(node.Children[0].Value))
                        {
                            UsedVariables.Add(node.Children[0].Value);
                        }
                        tmpUsesArray.Add(node.Children[0].Value);
                    }
                    tmpUsesArray.AddRange(thenResult.UsesElements.Distinct());
                    tmpUsesArray.AddRange(elseResult.UsesElements.Distinct());
                    CheckTableRow(node, ref tmpUsesArray, ref usesList);
                    tmpElementsUses.AddRange(tmpUsesArray.Distinct());
                }
                else if (node.NodeType == NodeType.While)
                {
                    //Parent
                    ParrentTableCell cell = GetParentCell(node, ref parentList);
                    foreach (var childNode in node.Children[1].Children)
                    {
                        if (!cell.ChildsStatementNumber.Contains(childNode.StatementNumber))
                        {
                            cell.ChildsStatementNumber.Add(childNode.StatementNumber);
                        }
                    }

                    List <string>       tmpModifiesArray = new List <string>();
                    List <string>       tmpUsesArray     = new List <string>();
                    StatementTableModel result           = FindStatements(node.Children[1], ref resultList, ref modifiesList, ref usesList, ref parentList, ref followsDicionary);

                    //Modifies
                    tmpModifiesArray.AddRange(result.ModifiesElements.Distinct());
                    CheckTableRow(node, ref tmpModifiesArray, ref modifiesList);
                    tmpElementsModifies.AddRange(tmpModifiesArray.Distinct());

                    //Uses
                    if (node.Children[0].NodeType == NodeType.Variable && Regex.IsMatch(node.Children[0].Value, @"^[a-zA-Z_$][a-zA-Z_$0-9]*$"))
                    {
                        if (!UsedVariables.Contains(node.Children[0].Value))
                        {
                            UsedVariables.Add(node.Children[0].Value);
                        }
                        tmpUsesArray.Add(node.Children[0].Value);
                    }
                    tmpUsesArray.AddRange(result.UsesElements.Distinct());
                    CheckTableRow(node, ref tmpUsesArray, ref usesList);
                    tmpElementsUses.AddRange(tmpUsesArray.Distinct());
                }
                else if (node.NodeType == NodeType.Call)
                {
                    var    parentValue         = GetParentProcedureName(node);
                    var    cell                = resultList.FirstOrDefault(c => c.ProcedureName == parentValue);
                    string calledProcedureName = GetCalledProcedureName(node);
                    if (cell != null)
                    {
                        if (!cell.CalledProcedures.Contains(calledProcedureName))
                        {
                            cell.CalledProcedures.Add(calledProcedureName);
                        }
                    }
                    else
                    {
                        resultList.Add(new ProcTableCell()
                        {
                            ProcedureName    = parentValue,
                            CalledProcedures = new List <string> {
                                GetCalledProcedureName(node)
                            }
                        });
                    }
                    List <string>       tmpModifiesArray    = new List <string>();
                    List <string>       tmpUsesArray        = new List <string>();
                    AstNode             calledProcedureNode = PkbReference.AbstractSyntaxTree.GetAllChildren(PkbReference.AbstractSyntaxTree.AstRoot).Where(x => x.Value == calledProcedureName).FirstOrDefault();
                    StatementTableModel result = FindStatements(calledProcedureNode, ref resultList, ref modifiesList, ref usesList, ref parentList, ref followsDicionary);

                    //Modifies
                    tmpModifiesArray.AddRange(result.ModifiesElements.Distinct());
                    CheckTableRow(node, ref tmpModifiesArray, ref modifiesList);
                    tmpElementsModifies.AddRange(tmpModifiesArray.Distinct());

                    //Uses
                    tmpUsesArray.AddRange(result.UsesElements.Distinct());
                    CheckTableRow(node, ref tmpUsesArray, ref usesList);
                    tmpElementsUses.AddRange(tmpUsesArray.Distinct());
                }
            }
            return(new StatementTableModel()
            {
                ModifiesElements = tmpElementsModifies.Distinct().ToList(),
                UsesElements = tmpElementsUses.Distinct().ToList(),
            });
        }