Exemplo n.º 1
0
        private void BuildPath(List <IStatementList> paths, IStatementList path, Statement lastElement)
        {
            Statement element = path.GetSize() > 0 ? path.GetLast() : lastElement;

            if (path.GetSize() == 0 || element != lastElement)
            {
                IStatementList nexting = GetNext(element);
                if (nexting.GetSize() > 0)
                {
                    if (nexting.GetLast() != nexting.GetFirst())
                    {
                        IStatementList newPath = path.Copy();
                        newPath.AddStatement(nexting.GetLast());
                        BuildPath(paths, newPath, element);
                    }
                    path.AddStatement(nexting.GetFirst());
                    BuildPath(paths, path, element);
                }
                else
                {
                    paths.Add(path);
                }
            }
            else
            {
                paths.Add(path);
            }
        }
Exemplo n.º 2
0
        private void ExtractAffects()
        {
            IStatementList assignStatements = Statements.Copy().FilterByType(typeof(Assign)) as IStatementList;

            foreach (Statement assignStatement in assignStatements)
            {
                Assign   assignment = assignStatement as Assign;
                Variable variable   = ModifiesTable.GetModifiedBy(assignment).GetVariableByIndex(0);

                List <IStatementList> paths = NextTable.GetPathsFrom(assignStatement);
                foreach (IStatementList path in paths)
                {
                    foreach (Statement nextStatement in path)
                    {
                        if (nextStatement is Assign && UsesTable.IsUses(nextStatement, variable))
                        {
                            AffectsTable.SetAffects(assignment, nextStatement as Assign);
                        }
                        if (nextStatement != assignStatement && (nextStatement is Assign || nextStatement is Call) && ModifiesTable.IsModifies(nextStatement, variable))
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 public Assign(Variable left, Token operation, Factor right, int programLine) : base(programLine)
 {
     Left       = left;
     Operation  = operation;
     Right      = right;
     AffectedBy = ImplementationFactory.CreateStatementList();
     Affecting  = ImplementationFactory.CreateStatementList();
 }
Exemplo n.º 4
0
        public List <IStatementList> GetPathsFrom(Statement statement)
        {
            List <IStatementList> paths   = new List <IStatementList>();
            IStatementList        newPath = ImplementationFactory.CreateStatementList();

            BuildPath(paths, newPath, statement);
            return(paths);
        }
Exemplo n.º 5
0
        protected override IEntityList ProcessRightSide(IProgramKnowledgeBase pkb, IEntity arg)
        {
            Statement      statement = arg as Statement;
            Statement      parent    = pkb.ParentTable.GetParent(statement);
            IStatementList result    = ImplementationFactory.CreateStatementList();

            result.AddStatement(parent);
            return(result);
        }
Exemplo n.º 6
0
        protected override IEntityList ProcessRightSide(IProgramKnowledgeBase pkb, IEntity arg)
        {
            Statement      statement = arg as Statement;
            Statement      followed  = pkb.FollowsTable.GetFollowedBy(statement);
            IStatementList result    = ImplementationFactory.CreateStatementList();

            result.AddStatement(followed);
            return(result);
        }
Exemplo n.º 7
0
 public Variable(Token token)
 {
     Name                 = token.Value.ToString();
     Attribute            = new Attribute("varName", Name);
     ModifiedByProcedures = ImplementationFactory.CreateProcedureList();
     ModifiedByStatements = ImplementationFactory.CreateStatementList();
     UsedByProcedures     = ImplementationFactory.CreateProcedureList();
     UsedByStatements     = ImplementationFactory.CreateStatementList();
 }
Exemplo n.º 8
0
 public Variable(string name)
 {
     Name                 = name;
     Attribute            = new Attribute("varName", Name);
     ModifiedByProcedures = ImplementationFactory.CreateProcedureList();
     ModifiedByStatements = ImplementationFactory.CreateStatementList();
     UsedByProcedures     = ImplementationFactory.CreateProcedureList();
     UsedByStatements     = ImplementationFactory.CreateStatementList();
 }
Exemplo n.º 9
0
        public IStatementList GetNextT(Statement statement)
        {
            IStatementList nexting = GetNext(statement);

            for (int i = 0; i < nexting.GetSize(); i++)
            {
                nexting.Sum(GetNext(nexting[i]));
            }
            return(nexting);
        }
Exemplo n.º 10
0
        public IStatementList GetNextedByT(Statement statement)
        {
            IStatementList nexted = GetNextedBy(statement);

            for (int i = 0; i < nexted.GetSize(); i++)
            {
                nexted.Sum(GetNextedBy(nexted[i]));
            }
            return(nexted);
        }
Exemplo n.º 11
0
 public Procedure(string name, IStatementList body)
 {
     Name      = name;
     Body      = body;
     Attribute = new Attribute("procName", Name);
     Modifying = ImplementationFactory.CreateVariableList();
     Using     = ImplementationFactory.CreateVariableList();
     CalledBy  = ImplementationFactory.CreateProcedureList();
     Calling   = ImplementationFactory.CreateProcedureList();
 }
Exemplo n.º 12
0
        public IStatementList GetParentedByT(Statement statement)
        {
            IStatementList parented = GetParentedBy(statement);

            for (int i = 0; i < parented.GetSize(); i++)
            {
                parented.Sum(GetParentedBy(parented[i]));
            }
            return(parented);
        }
Exemplo n.º 13
0
 public Statement(int programLine)
 {
     ProgramLine = programLine;
     Attribute   = new Attribute("progLine", ProgramLine.ToString());
     Modifying   = ImplementationFactory.CreateVariableList();
     Using       = ImplementationFactory.CreateVariableList();
     NextedBy    = ImplementationFactory.CreateStatementList();
     Nexting     = ImplementationFactory.CreateStatementList();
     Children    = ImplementationFactory.CreateStatementList();
 }
Exemplo n.º 14
0
        private While WhileStatement()
        {
            int programLine = ++statementCounter;

            Eat(TokenType.WHILE);
            Variable var = Var();

            IStatementList body = StmtLst();

            return(new While(var, body, programLine));
        }
Exemplo n.º 15
0
        public IStatementList GetAffectedByT(Assign assignment)
        {
            IStatementList affected = GetAffectedBy(assignment);

            for (int i = 0; i < affected.GetSize(); i++)
            {
                Assign affectedAssignment = affected[i] as Assign;
                affected.Sum(GetAffectedBy(affectedAssignment));
            }
            return(affected);
        }
Exemplo n.º 16
0
        public IStatementList GetAffectsT(Assign assignment)
        {
            IStatementList affecting = GetAffects(assignment);

            for (int i = 0; i < affecting.GetSize(); i++)
            {
                Assign affectingAssignment = affecting[i] as Assign;
                affecting.Sum(GetAffects(affectingAssignment));
            }
            return(affecting);
        }
Exemplo n.º 17
0
        public IStatementList GetFollowedByT(Statement statement)
        {
            IStatementList followed          = ImplementationFactory.CreateStatementList();
            Statement      followedStatement = GetFollowedBy(statement);

            followed.AddStatement(followedStatement);
            for (int i = 0; i < followed.GetSize(); i++)
            {
                followed.AddStatement(GetFollowedBy(followed[i]));
            }
            return(followed);
        }
Exemplo n.º 18
0
        public IStatementList GetFollowsT(Statement statement)
        {
            IStatementList following          = ImplementationFactory.CreateStatementList();
            Statement      followingStatement = GetFollows(statement);

            following.AddStatement(followingStatement);
            for (int i = 0; i < following.GetSize(); i++)
            {
                following.AddStatement(GetFollows(following[i]));
            }
            return(following);
        }
Exemplo n.º 19
0
        public IStatementList GetParentT(Statement statement)
        {
            IStatementList parents         = ImplementationFactory.CreateStatementList();
            Statement      parentStatement = GetParent(statement);

            parents.AddStatement(parentStatement);
            for (int i = 0; i < parents.GetSize(); i++)
            {
                parents.AddEntity(GetParent(parents[i]));
            }
            return(parents);
        }
Exemplo n.º 20
0
        private IStatementList Statements()
        {
            Statement      statement  = SingleStatement();
            IStatementList statements = ImplementationFactory.CreateStatementList();

            statements.AddStatement(statement);

            while (currentToken.Type != TokenType.RBRACE)
            {
                statements.AddStatement(SingleStatement());
            }
            return(statements);
        }
Exemplo n.º 21
0
        private IStatementList StmtLst()
        {
            Eat(TokenType.LBRACE);
            IStatementList statements = Statements();

            Eat(TokenType.RBRACE);

            IStatementList root = ImplementationFactory.CreateStatementList();

            foreach (Statement statement in statements)
            {
                root.AddStatement(statement);
            }

            return(root);
        }
Exemplo n.º 22
0
        private If IfStatement()
        {
            int programLine = ++statementCounter;

            Eat(TokenType.IF);
            Variable var = Var();

            Eat(TokenType.THEN);

            IStatementList body     = StmtLst();
            IStatementList elseBody = null;

            if (currentToken.Type == TokenType.ELSE)
            {
                Eat(TokenType.ELSE);
                elseBody = StmtLst();
            }
            return(new If(var, body, elseBody, programLine));
        }
Exemplo n.º 23
0
        private void ExtractBody(Container container, IStatementList body, Procedure procedureContext)
        {
            for (int i = 0; i < body.GetSize(); i++)
            {
                Statement child = body[i];
                ExtractStatement(child, procedureContext);

                ParentTable.SetParent(container, child);
                if (i > 0)
                {
                    Statement previousChild = body[i - 1];
                    FollowsTable.SetFollows(previousChild, child);

                    if (!(previousChild is If))
                    {
                        NextTable.SetNext(previousChild, child);
                    }
                    else
                    {
                        ExtractIfNext(previousChild as If, child);
                    }
                }

                IVariableList modifiedVariables = ModifiesTable.GetModifiedBy(child);
                IVariableList usedVariables     = UsesTable.GetUsedBy(child);

                foreach (Variable variable in modifiedVariables)
                {
                    ModifiesTable.SetModifies(container, variable);
                }
                foreach (Variable variable in usedVariables)
                {
                    UsesTable.SetUses(container, variable);
                }
            }
        }
Exemplo n.º 24
0
        private void ExtractProcedureCalls(Procedure procedure)
        {
            IProcedureList callingProcedures = CallsTable.GetCalling(procedure);

            foreach (Procedure callingProcedure in callingProcedures)
            {
                List <Call> procedureCalls = calls[callingProcedure].Where(x => x.Procedure == procedure).ToList();
                foreach (Call call in procedureCalls)
                {
                    IStatementList callParents = ParentTable.GetParentT(call);

                    IVariableList modifiedVariables = ModifiesTable.GetModifiedBy(procedure);
                    IVariableList usedVariables     = UsesTable.GetUsedBy(procedure);

                    foreach (Variable variable in modifiedVariables)
                    {
                        ModifiesTable.SetModifies(call, variable);
                        ModifiesTable.SetModifies(callingProcedure, variable);
                        foreach (Statement parent in callParents)
                        {
                            ModifiesTable.SetModifies(parent, variable);
                        }
                    }
                    foreach (Variable variable in usedVariables)
                    {
                        UsesTable.SetUses(call, variable);
                        UsesTable.SetUses(callingProcedure, variable);
                        foreach (Statement parent in callParents)
                        {
                            UsesTable.SetUses(parent, variable);
                        }
                    }
                    ExtractProcedureCalls(callingProcedure);
                }
            }
        }
Exemplo n.º 25
0
        private void ExtractProcedure(Procedure procedure)
        {
            IStatementList children = procedure.Body;

            for (int i = 0; i < children.GetSize(); i++)
            {
                Statement child = children[i];
                ExtractStatement(child, procedure);
                if (i > 0)
                {
                    Statement previousChild = children[i - 1];
                    FollowsTable.SetFollows(previousChild, child);

                    if (!(previousChild is If))
                    {
                        NextTable.SetNext(previousChild, child);
                    }
                    else
                    {
                        ExtractIfNext(previousChild as If, child);
                    }
                }

                IVariableList modifiedVariables = ModifiesTable.GetModifiedBy(child);
                IVariableList usedVariables     = UsesTable.GetUsedBy(child);

                foreach (Variable variable in modifiedVariables)
                {
                    ModifiesTable.SetModifies(procedure, variable);
                }
                foreach (Variable variable in usedVariables)
                {
                    UsesTable.SetUses(procedure, variable);
                }
            }
        }
Exemplo n.º 26
0
        public bool IsAffectsT(Assign assignment1, Assign assignment2)
        {
            IStatementList affectingAssignments = GetAffectsT(assignment2);

            return(affectingAssignments.Contains(assignment1));
        }
Exemplo n.º 27
0
 public If(Variable condition, IStatementList ifBody, IStatementList elseBody, int programLine) : base(programLine)
 {
     Condition = condition;
     IfBody    = ifBody;
     ElseBody  = elseBody;
 }
Exemplo n.º 28
0
        public bool IsFollowsT(Statement firstStatement, Statement secondStatement)
        {
            IStatementList statementList = GetFollowedByT(secondStatement);

            return(statementList.Contains(firstStatement));
        }
Exemplo n.º 29
0
 public While(Variable condition, IStatementList body, int programLine) : base(programLine)
 {
     Condition = condition;
     Body      = body;
 }
Exemplo n.º 30
0
 public While(int programLine) : base(programLine)
 {
     Body = ImplementationFactory.CreateStatementList();
 }