Exemplo n.º 1
0
        public void CreateProcTable()
        {
            var resultList       = new List <ProcTableCell>();
            var modifiesList     = new List <StatementColumn>();
            var usesList         = new List <StatementColumn>();
            var ast              = PkbReference.AbstractSyntaxTree;
            var modifiesProc     = new Dictionary <string, List <string> >();
            var usesProc         = new Dictionary <string, List <string> >();
            var parentList       = new List <ParrentTableCell>();
            var followsDicionary = new  Dictionary <int, int>();

            UsedVariables = new List <string>();

            //For each procedure
            foreach (var procedure in ast.GetAllChildren(ast.AstRoot))
            {
                StatementTableModel result = FindStatements(procedure, ref resultList, ref modifiesList, ref usesList, ref parentList, ref followsDicionary);
                modifiesProc.Add(procedure.Value, result.ModifiesElements);
                usesProc.Add(procedure.Value, result.UsesElements);
            }
            PkbReference.ProcTable = resultList;
            PkbReference.StatementModifiesTable  = modifiesList.OrderBy(x => x.StatementNumber).ToList();
            PkbReference.ProceduresModifiesTable = modifiesProc;
            PkbReference.StatementUsesTable      = usesList.OrderBy(x => x.StatementNumber).ToList();
            PkbReference.ProceduresUsesTable     = usesProc;
            PkbReference.UsedVariables           = UsedVariables;
            PkbReference.ParentTable             = parentList.OrderBy(x => x.ContainerStatementNumber).ToList();
            PkbReference.FollowsTable            = followsDicionary;
        }
Exemplo n.º 2
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(),
            });
        }