Ejemplo n.º 1
0
        private bool IsTypeCorrect(ProcRef procRef)
        {
            Type procRefType = procRef.EntryTypeReference.GetType();

            if (procRefType == typeof(Ident))
            {
                if (Declarations.GetDeclarationByName(procRef.entry).GetType() != typeof(Procedure))
                {
                    return(false);
                }
                return(true);
            }

            else if (procRefType == typeof(BracesIdent))
            {
                return(true);
            }

            else if (procRefType == typeof(Placeholder))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private static bool ValidateCallsAsterisk(string param1, string param2)
        {
            ProcRef procRef1 = new ProcRef(param1);
            ProcRef procRef2 = new ProcRef(param2);

            return(procRef1.IsGrammarCorrect() && procRef2.IsGrammarCorrect());
        }
Ejemplo n.º 3
0
        private static bool ValidateUses(string param1, string param2)
        {
            VarRef varRef = new VarRef(param2);

            if (varRef.IsGrammarCorrect())
            {
                ProcRef procRef = new ProcRef(param1);
                StmtRef stmtRef = new StmtRef(param1);

                return(procRef.IsGrammarCorrect() || stmtRef.IsGrammarCorrect());
            }
            return(false);
        }
Ejemplo n.º 4
0
        public MethodResultList NotImplemented()
        {
            ProcRef pr = new ProcRef("komarch");

            pr.IsGrammarCorrect();
            VarRef vr = new VarRef("_");

            vr.IsGrammarCorrect();
            MethodResultList mrl = new MethodResultList(typeof(ProcRef), typeof(VarRef));

            mrl.List1.Add(pr);
            mrl.List2.Add(vr);
            mrl.ListType1   = typeof(Ident);
            mrl.ListType2   = typeof(Placeholder);
            mrl.QueryParam1 = "komarch";
            mrl.QueryParam2 = "_";
            return(mrl);
        }
Ejemplo n.º 5
0
        public MethodResultList Calls(ProcRef procRef1, ProcRef procRef2)
        {
            Type             procRef1_Type = procRef1.EntryTypeReference.GetType();
            Type             procRef2_Type = procRef2.EntryTypeReference.GetType();
            MethodResultList callsReturn   = new MethodResultList(procRef1, procRef2);

            if (procRef1_Type == typeof(Ident) || procRef1_Type == typeof(Placeholder))
            {
                if (procRef2_Type == typeof(Ident) || procRef2_Type == typeof(Placeholder))
                {
                    List <string> allProcedures = Pkb.UsedProcedures;

                    foreach (string p in allProcedures)
                    {
                        List <string> calledProcedures = Pkb.CalledByProcedure(p);

                        foreach (string cp in calledProcedures)
                        {
                            callsReturn.List1.Add(new ProcRef(p));
                            callsReturn.List2.Add(new ProcRef(cp));
                        }
                    }
                }
                else if (procRef2_Type == typeof(BracesIdent))
                {
                    string procNameWithoutBraces = procRef2.entry.Substring(1, procRef2.entry.Length - 2);

                    List <string> proceduresThatCall = Pkb.ProceduresThatCall(procNameWithoutBraces);

                    foreach (string ptc in proceduresThatCall)
                    {
                        callsReturn.List1.Add(new ProcRef(ptc));
                        callsReturn.List2.Add(new ProcRef(procNameWithoutBraces));
                    }
                }
                else
                {
                    throw new SuchThatException($"#Invalid procRef type: expected 'Ident', 'Placeholder' or 'BracesIdent' for {procRef2.entry}");
                }
            }
            else if (procRef1_Type == typeof(BracesIdent))
            {
                string procName1_WithoutBraces = procRef1.entry.Substring(1, procRef1.entry.Length - 2);

                if (procRef2_Type == typeof(Ident) || procRef2_Type == typeof(Placeholder))
                {
                    List <string> calledProcedures = Pkb.CalledByProcedure(procName1_WithoutBraces);

                    foreach (string cp in calledProcedures)
                    {
                        callsReturn.List1.Add(new ProcRef(procName1_WithoutBraces));
                        callsReturn.List2.Add(new ProcRef(cp));
                    }
                }
                else if (procRef2_Type == typeof(BracesIdent))
                {
                    string procName2_WithoutBraces = procRef2.entry.Substring(1, procRef2.entry.Length - 2);

                    if (Pkb.Calls(procName1_WithoutBraces, procName2_WithoutBraces))
                    {
                        callsReturn.List1.Add(new ProcRef(procName1_WithoutBraces));
                        callsReturn.List2.Add(new ProcRef(procName2_WithoutBraces));
                    }
                }
                else
                {
                    throw new SuchThatException($"#Invalid procRef type: expected 'Ident', 'Placeholder' or 'BracesIdent' for {procRef2.entry}");
                }
            }
            else
            {
                throw new SuchThatException($"#Invalid procRef type: expected 'Ident', 'Placeholder' or 'BracesIdent' for {procRef1.entry}");
            }

            return(callsReturn);
        }
Ejemplo n.º 6
0
        public MethodResultList Uses(ProcRef procRef, VarRef varRef)
        {
            Type             procRefType   = procRef.EntryTypeReference.GetType();
            Type             varRefType    = varRef.EntryTypeReference.GetType();
            MethodResultList usesReturn    = new MethodResultList(procRef, varRef);
            List <string>    allProcedures = Pkb.UsedProcedures;

            if (procRefType == typeof(Ident))
            {
                if (varRefType == typeof(Ident) || varRefType == typeof(Placeholder))
                {
                    foreach (string ptc in allProcedures)
                    {
                        List <string> variables = Pkb.UsesAllProcModVar(ptc);
                        foreach (string v in variables)
                        {
                            usesReturn.List1.Add(new ProcRef(ptc));
                            usesReturn.List2.Add(new VarRef(v));
                        }
                    }
                }
                else if (varRefType == typeof(BracesIdent))
                {
                    string varNameWithoutBraces = varRef.entry.Substring(1, varRef.entry.Length - 2);

                    List <string> procedures = Pkb.UsesAllVarModProc(varNameWithoutBraces);
                    foreach (string p in procedures)
                    {
                        usesReturn.List1.Add(new ProcRef(p));
                        usesReturn.List2.Add(new VarRef(varNameWithoutBraces));
                    }
                }
                else
                {
                    throw new SuchThatException($"#Invalid VarRef type: expected 'Ident', 'Placeholder' or 'BracesIdent' for {varRef.entry}");
                }
            }
            else if (procRefType == typeof(BracesIdent))
            {
                if (varRefType == typeof(Ident) || varRefType == typeof(Placeholder))
                {
                    string procNameWithoutBraces = procRef.entry.Substring(1, procRef.entry.Length - 2);

                    List <string> variables = Pkb.UsesAllProcModVar(procNameWithoutBraces);
                    foreach (string v in variables)
                    {
                        usesReturn.List1.Add(new ProcRef(procNameWithoutBraces));
                        usesReturn.List2.Add(new VarRef(v));
                    }
                }
                else if (varRefType == typeof(BracesIdent))
                {
                    string procNameWithoutBraces = procRef.entry.Substring(1, procRef.entry.Length - 2);
                    string varNameWithoutBraces  = varRef.entry.Substring(1, varRef.entry.Length - 2);

                    if (Pkb.UsesProcedure(procNameWithoutBraces, varNameWithoutBraces))
                    {
                        usesReturn.List1.Add(new ProcRef(procNameWithoutBraces));
                        usesReturn.List2.Add(new VarRef(varNameWithoutBraces));
                    }
                }
                else
                {
                    throw new SuchThatException($"#Invalid VarRef type: expected 'Ident', 'Placeholder' or 'BracesIdent' for {varRef.entry}");
                }
            }
            else if (procRefType == typeof(Placeholder))
            {
                throw new SuchThatException($"#Invalid ProcRef type: expected 'Ident' or 'BracesIdent' for {procRef.entry}");
            }
            else
            {
                throw new SuchThatException($"#Invalid ProcRef type: expected 'Ident' or 'BracesIdent' for {procRef.entry}");
            }

            return(usesReturn);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Executes correct method
        /// </summary>
        /// <param name="methodName">method which should be executed</param>
        /// <param name="param1">first param from query</param>
        /// <param name="param2">second param from query</param>
        /// <returns></returns>
        /// <returns></returns>
        public MethodResultList DoSuchThatMethod(string methodName, string param1, string param2)
        {
            switch (methodName)
            {
            case "MODIFIES":
            {
                //helper objects needed to check the type, param1 can be either PROCEDURE or STATEMENT
                VarRef varRef = new VarRef(param2);
                varRef.IsGrammarCorrect();
                ProcRef procRef = new ProcRef(param1);
                procRef.IsGrammarCorrect();
                StmtRef stmtRef = new StmtRef(param1);
                stmtRef.IsGrammarCorrect();

                //present on DeclarationsArray and has required type
                if (!IsTypeCorrect(varRef))
                {
                    throw new SuchThatException($"#{varRef.entry} isn't declared as VARIABLE type");
                }

                if (IsTypeCorrect(procRef))
                {
                    return(Modifies(procRef, varRef));
                }
                else
                if (IsTypeCorrect(stmtRef))
                {
                    return(Modifies(stmtRef, varRef));
                }
                else
                {
                    throw new SuchThatException($"#{methodName} first param is neither 'PROCEDURE' or 'STATEMENT'");
                }
            }

            case "USES":
            {
                //helper objects needed to check the type, param1 can be either PROCEDURE or STATEMENT
                VarRef varRef = new VarRef(param2);
                varRef.IsGrammarCorrect();
                ProcRef procRef = new ProcRef(param1);
                procRef.IsGrammarCorrect();
                StmtRef stmtRef = new StmtRef(param1);
                stmtRef.IsGrammarCorrect();

                //present on DeclarationsArray and has required type
                if (!IsTypeCorrect(varRef))
                {
                    throw new SuchThatException($"#{varRef.entry} isn't declared as VARIABLE type");
                }

                bool stmtFlag = IsTypeCorrect(stmtRef);
                bool procFlag = IsTypeCorrect(procRef);

                if (procFlag)
                {
                    return(Uses(procRef, varRef));
                }
                else
                if (stmtFlag)
                {
                    return(Uses(stmtRef, varRef));
                }
                else
                {
                    throw new SuchThatException($"#{methodName} first param is neither 'PROCEDURE' or 'STATEMENT'");
                }
            }

            case "CALLS":
            {
                ProcRef procRef1 = new ProcRef(param1);
                procRef1.IsGrammarCorrect();
                ProcRef procRef2 = new ProcRef(param2);
                procRef2.IsGrammarCorrect();

                if (IsTypeCorrect(procRef1) && IsTypeCorrect(procRef2))
                {
                    return(Calls(procRef1, procRef2));
                }
                else
                {
                    throw new SuchThatException($"#{methodName} first or second param isn't 'PROCEDURE'");
                }
            }

            //case "CALLS*":
            //    {
            //        ProcRef procRef1 = new ProcRef(param1);
            //        procRef1.IsGrammarCorrect();
            //        ProcRef procRef2 = new ProcRef(param2);
            //        procRef2.IsGrammarCorrect();

            //        if (IsTypeCorrect(procRef1) && IsTypeCorrect(procRef2))
            //            return CallsAsterisk(procRef1, procRef2);
            //        else
            //            throw new SuchThatException($"#{methodName} first or second param isn't 'PROCEDURE'");
            //    }

            case "PARENT":
            {
                StmtRef stmtRef1 = new StmtRef(param1);
                stmtRef1.IsGrammarCorrect();
                StmtRef stmtRef2 = new StmtRef(param2);
                stmtRef2.IsGrammarCorrect();

                if (IsTypeCorrect(stmtRef1) && IsTypeCorrect(stmtRef2))
                {
                    return(Parent(stmtRef1, stmtRef2));
                }
                else
                {
                    throw new SuchThatException($"#{methodName} first or second param isn't 'STATEMENT'");
                }
            }

            case "PARENT*":
            {
                StmtRef stmtRef1 = new StmtRef(param1);
                stmtRef1.IsGrammarCorrect();
                StmtRef stmtRef2 = new StmtRef(param2);
                stmtRef2.IsGrammarCorrect();

                if (IsTypeCorrect(stmtRef1) && IsTypeCorrect(stmtRef2))
                {
                    return(ParentAsterisk(stmtRef1, stmtRef2));
                }
                else
                {
                    throw new SuchThatException($"#{methodName} first or second param isn't 'STATEMENT'");
                }
            }

            case "FOLLOWS":
            {
                StmtRef stmtRef1 = new StmtRef(param1);
                stmtRef1.IsGrammarCorrect();
                StmtRef stmtRef2 = new StmtRef(param2);
                stmtRef2.IsGrammarCorrect();

                if (IsTypeCorrect(stmtRef1) && IsTypeCorrect(stmtRef2))
                {
                    return(Follows(stmtRef1, stmtRef2));
                }
                else
                {
                    throw new SuchThatException($"#{methodName} first or second param isn't 'STATEMENT'");
                }
            }

            case "FOLLOWS*":
            {
                StmtRef stmtRef1 = new StmtRef(param1);
                stmtRef1.IsGrammarCorrect();
                StmtRef stmtRef2 = new StmtRef(param2);
                stmtRef2.IsGrammarCorrect();

                if (IsTypeCorrect(stmtRef1) && IsTypeCorrect(stmtRef2))
                {
                    return(FollowsAsterisk(stmtRef1, stmtRef2));
                }
                else
                {
                    throw new SuchThatException($"#{methodName} first or second param isn't 'STATEMENT'");
                }
            }

            case "NEXT":
                return(NotImplemented());

            case "NEXT*":
                return(NotImplemented());

            case "AFFECTS":
                return(NotImplemented());

            case "AFFECTS*":
                return(NotImplemented());

            default:
                throw new NotImplementedException($"#Invalid methodName for Such That. Got: {methodName}");
            }
        }