Example #1
0
        private void BuildCfgList(string p_sourceCode)
        {
            GNode        currentPreviousNode = null;
            GNode        actualNode          = null;
            ProcedureCfg actualCfgStructure  = null;

            string[] sourceCodeLines;
            string[] lineWords;
            int      programLineNumber    = 0;
            int      howManyStatementsEnd = 0;

            sourceCodeLines = p_sourceCode.Split('\n');

            for (int i = 0; i < sourceCodeLines.Length; i++)
            {
                lineWords = sourceCodeLines[i].Split(' ');
                switch (lineWords[0])
                {
                case "if":
                {
                    BuildIf(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);
                }
                break;

                case "while":
                {
                    BuildWhile(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);
                }
                break;

                case "procedure":
                {
                    if (actualCfgStructure != null)
                    {
                        actualCfgStructure.lastProgramLineNumber = programLineNumber;
                        CfgList.Add(actualCfgStructure);
                    }
                    actualCfgStructure  = new ProcedureCfg(pkb.GetProcIndex(lineWords[1]));
                    actualNode          = null;
                    currentPreviousNode = null;
                }
                break;

                default:
                {
                    BuildAssignCall(ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber);
                }
                break;
                }
            }

            actualCfgStructure.lastProgramLineNumber = programLineNumber;
            this.CfgList.Add(actualCfgStructure);
        }
Example #2
0
        public List <TNode> NextX(TNode p_from, string p_to)
        {
            if (p_from is null)
            {
                return(null);
            }

            List <TNodeTypeEnum> acceptableType = DetermineAcceptableTypes(p_to);


            List <TNode> resultList = new List <TNode>();
            ProcedureCfg cfg        = FindCfg((int)p_from.programLine);

            List <int>[] visitorsTable = new List <int> [cfg.GNodeList.Count()];
            for (int i = 0; i < visitorsTable.Length; i++)
            {
                visitorsTable[i] = new List <int>();
            }
            int   programLinesInNode;
            TNode tmp;

            GNode actual = cfg.GNodeList.Where(p => p.programLineList.Contains((int)p_from.programLine)).FirstOrDefault();

            programLinesInNode = actual.programLineList.Count();

            if (programLinesInNode > 1) // jesli nextS w tym samym nodzie
            {
                for (int i = 0; i < programLinesInNode; i++)
                {
                    if (actual.programLineList[i] == (int)p_from.programLine &&
                        (i + 1) < programLinesInNode)
                    {
                        for (int j = i + 1; j < programLinesInNode; j++)
                        {
                            tmp = AstManager.GetInstance().FindNode(actual.programLineList.ElementAt(j));
                            if (acceptableType.Contains(tmp.type))
                            {
                                resultList.Add(tmp);
                            }
                        }
                    }
                }
            }

            FindAndAddAllNextSInNextNodes(actual, ref resultList, ref visitorsTable, acceptableType);


            return(resultList.Count > 0 ? resultList : null);;
        }
Example #3
0
        internal List <List <int> > IsNextXSpecial(int fromInt, int toInt)
        {
            if (OutOfRange(fromInt) || OutOfRange(toInt))
            {
                return(null);
            }

            List <List <int> > flows      = new List <List <int> >();
            List <int>         actualFlow = new List <int>();

            ProcedureCfg cfg = FindCfg(fromInt);

            List <int>[] visitorsTable = new List <int> [cfg.GNodeList.Count()];
            for (int i = 0; i < visitorsTable.Length; i++)
            {
                visitorsTable[i] = new List <int>();
            }
            int programLinesInNode;

            GNode actual = cfg.GNodeList.Where(p => p.programLineList.Contains(fromInt)).FirstOrDefault();

            programLinesInNode = actual.programLineList.Count();

            if (programLinesInNode > 1) // jesli nextS w tym samym nodzie
            {
                for (int i = 0; i < programLinesInNode; i++)
                {
                    if (actual.programLineList[i] == fromInt && (i + 1) < programLinesInNode)
                    {
                        for (int j = i + 1; j < programLinesInNode; j++)
                        {
                            if (actual.programLineList.ElementAt(j) != toInt)
                            {
                                actualFlow.Add(j);
                            }
                            else
                            {
                                flows.Add(actualFlow);
                                return(flows);
                            }
                        }
                    }
                }
            }

            FindAndAddAllNextSInNextNodesSpecial(actual, ref flows, actualFlow, ref visitorsTable, toInt);

            return(flows);
        }
Example #4
0
        public List <TNode> Next(TNode p_from, string p_to)
        {
            if (p_from is null)
            {
                return(null);
            }

            List <TNodeTypeEnum> acceptableType = DetermineAcceptableTypes(p_to);
            TNode        tmp;
            List <TNode> resultList = new List <TNode>();
            ProcedureCfg cfg        = FindCfg((int)p_from.programLine);
            int          programLinesInNode;

            GNode actual = cfg.GNodeList.Where(p => p.programLineList.Contains((int)p_from.programLine)).FirstOrDefault();

            programLinesInNode = actual.programLineList.Count();
            if (programLinesInNode > 1) // jesli next w tym samym nodzie
            {
                for (int i = 0; i < programLinesInNode; i++)
                {
                    if (actual.programLineList[i] == (int)p_from.programLine &&
                        (i + 1) < programLinesInNode)
                    {
                        tmp = AstManager.GetInstance().FindNode(actual.programLineList.ElementAt(i + 1));
                        if (acceptableType.Contains(tmp.type))
                        {
                            resultList.Add(tmp);
                        }

                        return(resultList.Count > 0 ? resultList : null);
                    }
                }
            }
            if (actual.nextGNodeList != null)
            {
                foreach (var nodeNext in actual.nextGNodeList)
                {
                    FindAndAddNextResult(nodeNext, ref resultList, acceptableType);
                }
            }
            else
            {
            }

            return(resultList.Count > 0 ? resultList : null);
        }
Example #5
0
        public List <TNode> Previous(TNode p_to, string p_from)
        {
            if (p_to is null)
            {
                return(null);
            }
            List <TNodeTypeEnum> acceptableType = DetermineAcceptableTypes(p_from);

            List <TNode> resultList = new List <TNode>();
            TNode        tmp;
            ProcedureCfg cfg = FindCfg((int)p_to.programLine);
            int          programLinesInNode;

            GNode actual = cfg.GNodeList.Where(p => p.programLineList.Contains((int)p_to.programLine)).FirstOrDefault();

            programLinesInNode = actual.programLineList.Count();
            if (programLinesInNode > 1) // jesli previous w tym samym nodzie
            {
                for (int i = 0; i < programLinesInNode; i++)
                {
                    if (actual.programLineList[i] == (int)p_to.programLine &&
                        i != 0)
                    {
                        tmp = AstManager.GetInstance().FindNode(actual.programLineList.ElementAt(i - 1));
                        if (acceptableType.Contains(tmp.type))
                        {
                            resultList.Add(tmp);
                        }
                    }
                }
            }

            if (actual.previousGNodeList != null && !resultList.Any())
            {
                foreach (var nodePrevious in actual.previousGNodeList)
                {
                    FindAndAddPreviousResult(nodePrevious, ref resultList, acceptableType);
                }
            }

            return(resultList.Count > 0 ? resultList : null);
        }
Example #6
0
        public bool IsNextX(int p1, int p2)
        {
            if (OutOfRange(p1) || OutOfRange(p2))
            {
                return(false);
            }

            ProcedureCfg cfg = FindCfg(p1);
            int          programLinesInNode;
            Boolean      findedNextX = false;

            List <int>[] visitorsTable = new List <int> [cfg.GNodeList.Count()];
            for (int i = 0; i < visitorsTable.Length; i++)
            {
                visitorsTable[i] = new List <int>();
            }

            GNode actual = cfg.GNodeList.Where(p => p.programLineList.Contains(p1)).FirstOrDefault();

            programLinesInNode = actual.programLineList.Count();

            if (programLinesInNode > 1) // jesli nextS w tym samym nodzie
            {
                for (int i = 0; i < programLinesInNode; i++)
                {
                    if (actual.programLineList[i] == p1 &&
                        (i + 1) < programLinesInNode)
                    {
                        for (int j = i + 1; j < programLinesInNode; j++)
                        {
                            if (actual.programLineList.ElementAt(j) == p2)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            CheckIfNextSInNextNodes(actual, p2, ref findedNextX, ref visitorsTable);
            return(findedNextX);
        }
Example #7
0
        private void BuildAssignCall(ref GNode currentPreviousNode, ref GNode actualNode, ref ProcedureCfg actualCfgStructure, ref int programLineNumber)
        {
            if (actualNode == null) // jesli pierwsza instrukcja w procedurze
            {
                actualNode = new GNode(GNodeTypeEnum.StmtLst, ++programLineNumber, actualCfgStructure.GNodeList.Count());
                actualCfgStructure.GNodeList.Add(actualNode);
            }
            else if (actualNode.type == GNodeTypeEnum.While || actualNode.type == GNodeTypeEnum.If || actualNode.type == GNodeTypeEnum.Ghost) // jesli pierwsza instrukcja w while/if
            {
                currentPreviousNode = actualNode;
                actualNode          = new GNode(GNodeTypeEnum.StmtLst, ++programLineNumber, actualCfgStructure.GNodeList.Count());
                actualCfgStructure.GNodeList.Add(actualNode);

                currentPreviousNode.nextGNodeList.Add(actualNode);
                actualNode.previousGNodeList.Add(currentPreviousNode);
            }
            else
            {
                actualNode.programLineList.Add(++programLineNumber);
            }
        }
Example #8
0
        private void BuildWhile(string[] sourceCodeLines, ref int i, ref GNode currentPreviousNode, ref GNode actualNode, ref ProcedureCfg actualCfgStructure, ref int programLineNumber, ref int howManyStatementsEnd)
        {
            string[] lineWords;
            GNode    whileNodeMain;

            #region create node for while and remember it
            currentPreviousNode = actualNode;

            actualNode = new GNode(GNodeTypeEnum.While, ++programLineNumber, actualCfgStructure.GNodeList.Count());
            actualCfgStructure.GNodeList.Add(actualNode);

            //zapamietaj
            whileNodeMain = actualNode;

            if (currentPreviousNode != null)
            {
                currentPreviousNode.nextGNodeList.Add(actualNode);
                actualNode.previousGNodeList.Add(currentPreviousNode);
            }
            currentPreviousNode = actualNode;
            #endregion

            for (++i; i < sourceCodeLines.Length; i++)
            {
                lineWords = sourceCodeLines[i].Split(' ');

                switch (lineWords[0])
                {
                case "while":
                {
                    BuildWhile(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);
                    if (howManyStatementsEnd > 0)
                    {
                        CloseWhileLoop(ref whileNodeMain, ref actualNode, ref currentPreviousNode, ref howManyStatementsEnd);
                        return;
                    }
                }
                break;

                case "if":
                {
                    BuildIf(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);
                    if (howManyStatementsEnd > 0)
                    {
                        CloseWhileLoop(ref whileNodeMain, ref actualNode, ref currentPreviousNode, ref howManyStatementsEnd);
                        return;
                    }
                }
                break;

                default:
                {
                    BuildAssignCall(ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber);

                    if (EndOfStatement(lineWords, ref howManyStatementsEnd))
                    {
                        CloseWhileLoop(ref whileNodeMain, ref actualNode, ref currentPreviousNode, ref howManyStatementsEnd);
                        return;
                    }
                }
                break;
                }
            }
        }
Example #9
0
        private void BuildElse(string[] sourceCodeLines, ref int i, ref GNode currentPreviousNode, ref GNode actualNode, ref ProcedureCfg actualCfgStructure, ref int programLineNumber, ref int howManyStatementsEnd)
        {
            string[] lineWords;

            for (++i; i < sourceCodeLines.Length; i++)
            {
                lineWords = sourceCodeLines[i].Split(' ');

                switch (lineWords[0])
                {
                case "while":
                {
                    BuildWhile(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);
                    if (howManyStatementsEnd > 0)
                    {
                        --howManyStatementsEnd;
                        return;
                    }
                }
                break;

                case "if":
                {
                    BuildIf(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);

                    if (howManyStatementsEnd > 0)
                    {
                        --howManyStatementsEnd;
                        return;
                    }
                }
                break;

                default:
                {
                    BuildAssignCall(ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber);

                    if (EndOfStatement(lineWords, ref howManyStatementsEnd))
                    {
                        --howManyStatementsEnd;
                        return;
                    }
                }
                break;
                }
            }
        }
Example #10
0
        private void BuildIf(string[] sourceCodeLines, ref int i, ref GNode currentPreviousNode, ref GNode actualNode, ref ProcedureCfg actualCfgStructure, ref int programLineNumber, ref int howManyStatementsEnd)
        {
            string[] lineWords;
            GNode    ifNodeMain, endNodeThenSection = null, ghostNode;

            #region create node for if and remember this node and endNodeThenSection
            currentPreviousNode = actualNode;

            actualNode = new GNode(GNodeTypeEnum.If, ++programLineNumber, actualCfgStructure.GNodeList.Count());
            actualCfgStructure.GNodeList.Add(actualNode);

            // zapamietaj
            ifNodeMain = actualNode;

            if (currentPreviousNode != null)
            {
                currentPreviousNode.nextGNodeList.Add(actualNode);
                actualNode.previousGNodeList.Add(currentPreviousNode);
            }
            #endregion

            for (++i; i < sourceCodeLines.Length; i++)
            {
                lineWords = sourceCodeLines[i].Split(' ');

                switch (lineWords[0])
                {
                case "else":
                {
                    BuildElse(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);

                    ghostNode = new GNode(GNodeTypeEnum.Ghost, actualCfgStructure.GNodeList.Count());
                    actualCfgStructure.GNodeList.Add(ghostNode);

                    actualNode.nextGNodeList.Add(ghostNode);
                    endNodeThenSection.nextGNodeList.Add(ghostNode);

                    ghostNode.previousGNodeList.Add(actualNode);
                    ghostNode.previousGNodeList.Add(endNodeThenSection);

                    currentPreviousNode = null;
                    actualNode          = ghostNode;

                    return;
                }
                break;

                case "while":
                {
                    BuildWhile(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);

                    if (howManyStatementsEnd > 0)         // end of then section
                    {
                        endNodeThenSection = actualNode;
                        actualNode         = ifNodeMain;
                        --howManyStatementsEnd;
                    }
                }
                break;

                case "if":
                {
                    BuildIf(sourceCodeLines, ref i, ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber, ref howManyStatementsEnd);


                    if (howManyStatementsEnd > 0)         // end of then section
                    {
                        endNodeThenSection = actualNode;
                        actualNode         = ifNodeMain;
                        --howManyStatementsEnd;
                    }
                }
                break;

                default:
                {
                    BuildAssignCall(ref currentPreviousNode, ref actualNode, ref actualCfgStructure, ref programLineNumber);

                    if (EndOfStatement(lineWords, ref howManyStatementsEnd))         // end of then section
                    {
                        endNodeThenSection = actualNode;
                        actualNode         = ifNodeMain;
                        --howManyStatementsEnd;
                    }
                }
                break;
                }
            }
        }