예제 #1
0
        private bool IsTypeCorrect(VarRef varRef)
        {
            Type varRefType = varRef.EntryTypeReference.GetType();

            if (varRefType == typeof(Ident))
            {
                if (Declarations.GetDeclarationByName(varRef.entry).GetType() != typeof(Variable))
                {
                    return(false);
                }
                return(true);
            }
            else if (varRefType == typeof(BracesIdent))
            {
                return(true);
            }
            else if (varRefType == typeof(Placeholder))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #2
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);
        }
예제 #3
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);
        }
예제 #4
0
        public MethodResultList Uses(StmtRef stmtRef, VarRef varRef)
        {
            Type             stmtRefType = stmtRef.EntryTypeReference.GetType();
            Type             varRefType  = varRef.EntryTypeReference.GetType();
            MethodResultList usesReturn  = new MethodResultList(stmtRef, varRef);

            if (stmtRefType == typeof(Ident))
            {
                NodeType statementNodeType = ConvertStatementToPkbType(stmtRef.Type);

                if (varRefType == typeof(Ident) || varRefType == typeof(Placeholder))
                {
                    List <string> allVariables = Pkb.UsedVariables;

                    foreach (string v in allVariables)
                    {
                        List <int> statementNumbers = Pkb.UsesStmtThatModVar(v, statementNodeType);

                        foreach (int sn in statementNumbers)
                        {
                            usesReturn.List1.Add(new StmtRef("" + sn));
                            usesReturn.List2.Add(new VarRef(v));
                        }
                    }
                }
                else if (varRefType == typeof(BracesIdent))
                {
                    string     varNameWithoutBraces = varRef.entry.Substring(1, varRef.entry.Length - 2);
                    List <int> statementNumbers     = Pkb.UsesStmtThatModVar(varNameWithoutBraces, statementNodeType);

                    foreach (int sn in statementNumbers)
                    {
                        usesReturn.List1.Add(new StmtRef("" + sn));
                        usesReturn.List2.Add(new VarRef(varNameWithoutBraces));
                    }
                }
                else
                {
                    throw new SuchThatException($"#Invalid VarRef type: expected 'Ident', 'Placeholder' or 'BracesIdent' for {varRef.entry}");
                }
            }
            else if (stmtRefType == typeof(IntegerRule))
            {
                if (varRefType == typeof(Ident) || varRefType == typeof(Placeholder))
                {
                    List <string> variables = Pkb.UsesVarModInStmt(int.Parse(stmtRef.entry));

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

                    foreach (string v in variables)
                    {
                        if (varNameWithoutBraces == v)
                        {
                            usesReturn.List1.Add(new StmtRef(stmtRef.entry));
                            usesReturn.List2.Add(new VarRef(v));
                        }
                    }
                }
                else
                {
                    throw new SuchThatException($"#Invalid VarRef type: expected 'Ident', 'Placeholder' or 'BracesIdent' for {varRef.entry}");
                }
            }
            else if (stmtRefType == typeof(Placeholder))
            {
                throw new SuchThatException($"#Invalid StmtRef type: expected 'Ident' or 'Integer' for {stmtRef.entry}");
            }
            else
            {
                throw new SuchThatException($"#Invalid StmtRef type: expected 'Ident' or 'BracesIdent' for {stmtRef.entry}");
            }

            return(usesReturn);
        }
예제 #5
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);
        }
예제 #6
0
        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}");
            }
        }