Example #1
0
        private static LogicExpression SplitWordsByOperator(String[] paramArrayOfString, int position, Operator paramInt2,
            ParserSyntax paramParserSyntax)
        {
            int m;
            if (position == 0)
                throw new UnexpectedSymbolException("Operator at beginning of list of arguments");
            if (position == paramArrayOfString.Length - 1)
            {
                throw new UnexpectedSymbolException("Operator at end of list of arguments");
            }
            if ((paramInt2 == Operator.Implies) || (paramInt2 == Operator.Biimplies) || (paramInt2 == Operator.Xor))
            {
                var arrayOfLogicExpression1 = new LogicExpression[2];

                var arrayOfString1 = new String[position];
                var arrayOfString2 = new String[paramArrayOfString.Length - position - 1];
                for (m = 0; m < arrayOfString1.Length; m++)
                {
                    arrayOfString1[m] = paramArrayOfString[m];
                }
                m = position + 1;

                for (int n = 0; n < arrayOfString2.Length; n++)
                {
                    arrayOfString2[n] = paramArrayOfString[(m++)];
                }
                arrayOfLogicExpression1[0] = ParseWords(arrayOfString1, paramParserSyntax);
                arrayOfLogicExpression1[1] = ParseWords(arrayOfString2, paramParserSyntax);

                var localLogicBranch1 = new LogicBranch(paramInt2, false) {Branches = arrayOfLogicExpression1};
                return localLogicBranch1;
            }

            int i = 2;
            for (int k = position - 1; k >= 0; k--)
            {
                Operator j = GetOperatorValue(paramArrayOfString[k]);

                if (j == Operator.None)
                    continue;
                if (j != paramInt2)
                {
                    break;
                }
                paramParserSyntax.UpdateLogicSyntax(GetLogicSyntax(paramArrayOfString[k]));

                i++;
            }

            var arrayOfLogicExpression2 = new LogicExpression[i];

            m = paramArrayOfString.Length - 1;
            for (int i2 = arrayOfLogicExpression2.Length - 1; i2 >= 0; i2--)
            {
                int i1 = 0;

                int i3;
                for (i3 = m; i3 >= 0; i3--)
                {
                    var j = GetOperatorValue(paramArrayOfString[i3]);

                    if (j != paramInt2)
                        i1++;
                    else if (j == paramInt2)
                    {
                        break;
                    }
                }
                var arrayOfString3 = new String[i1];

                i3 = m - i1;
                int i4 = i1 - 1;

                for (int i5 = m; i5 > i3; i5--)
                {
                    arrayOfString3[(i4--)] = paramArrayOfString[i5];
                }

                m = m - i1 - 1;

                arrayOfLogicExpression2[i2] = ParseWords(arrayOfString3, paramParserSyntax);
            }

            var localLogicBranch2 = new LogicBranch(paramInt2, false) {Branches = arrayOfLogicExpression2};
            return localLogicBranch2;
        }
Example #2
0
        private static LogicExpression ParseWords(String[] paramArrayOfString, ParserSyntax paramParserSyntax)
        {
            if (paramArrayOfString.Length == 1)
            {
                String[] arrayOfString = BreakDownInToWords(paramArrayOfString[0]);

                if (arrayOfString.Length == 1)
                {
                    return GenerateLiteral(paramArrayOfString[0], paramParserSyntax);
                }
                paramArrayOfString = arrayOfString;
            }
            int j;
            for (j = paramArrayOfString.Length - 1; j >= 0; j--)
            {
                Operator i = GetOperatorValue(paramArrayOfString[j]);

                if (i == Operator.Implies)
                {
                    var k = GetLogicSyntax(paramArrayOfString[j]);

                    if (k != 0)
                    {
                        paramParserSyntax.UpdateLogicSyntax(k);
                    }
                    return SplitWordsByOperator(paramArrayOfString, j, Operator.Implies, paramParserSyntax);
                }

                if (i == Operator.Biimplies)
                {
                    var k = GetLogicSyntax(paramArrayOfString[j]);

                    if (k != 0)
                    {
                        paramParserSyntax.UpdateLogicSyntax(k);
                    }
                    return SplitWordsByOperator(paramArrayOfString, j, Operator.Biimplies, paramParserSyntax);
                }
            }

            for (j = paramArrayOfString.Length - 1; j >= 0; j--)
            {
                Operator i = GetOperatorValue(paramArrayOfString[j]);

                if (i == Operator.Or)
                {
                    paramParserSyntax.UpdateLogicSyntax(GetLogicSyntax(paramArrayOfString[j]));

                    return SplitWordsByOperator(paramArrayOfString, j, Operator.Or, paramParserSyntax);
                }
                if (paramArrayOfString[j].Equals("!="))
                {
                    return SplitWordsByOperator(paramArrayOfString, j, Operator.Xor, paramParserSyntax);
                }
            }

            for (j = paramArrayOfString.Length - 1; j >= 0; j--)
            {
                if (GetOperatorValue(paramArrayOfString[j]) != 0)
                    continue;
                paramParserSyntax.UpdateLogicSyntax(GetLogicSyntax(paramArrayOfString[j]));

                return SplitWordsByOperator(paramArrayOfString, j, 0, paramParserSyntax);
            }

            if (paramParserSyntax.NegationSyntax == NegationSyntax.Unknown)
            {
                if (paramArrayOfString[0].Equals("NOT", StringComparison.OrdinalIgnoreCase))
                    paramParserSyntax.UpdateNegationSyntax(NegationSyntax.Written);
                else if (paramArrayOfString[0].Equals("~"))
                    paramParserSyntax.UpdateNegationSyntax(NegationSyntax.Before);
                else if (paramArrayOfString[(paramArrayOfString.Length - 1)].Equals("'"))
                {
                    paramParserSyntax.UpdateNegationSyntax(NegationSyntax.After);
                }
                else
                {
                    throw new UnexpectedSymbolException("More than one literal found and no operators");
                }
            }

            j = 0;
            var kk = paramArrayOfString.Length - 1;
            LogicExpression localLogicExpression = null;
            int m;
            if (paramParserSyntax.NegationSyntax == NegationSyntax.Written)
            {
                for (m = 0; m < kk; m++)
                {
                    if (!paramArrayOfString[m].Equals("NOT", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new UnexpectedSymbolException("Expected 'NOT' instead of (or operator after) '" +
                                                            paramArrayOfString[m] + "'");
                    }

                    j = j == 0 ? 1 : 0;
                }

                localLogicExpression = Parse(paramArrayOfString[kk], paramParserSyntax);
            }
            else if (paramParserSyntax.NegationSyntax == NegationSyntax.Before)
            {
                for (m = 0; m < kk; m++)
                {
                    if (!paramArrayOfString[m].Equals("~"))
                    {
                        throw new UnexpectedSymbolException("Expected '~' instead of (or operator after) '" +
                                                            paramArrayOfString[m] + "'");
                    }

                    j = j == 0 ? 1 : 0;
                }

                localLogicExpression = Parse(paramArrayOfString[kk], paramParserSyntax);
            }
            else if (paramParserSyntax.NegationSyntax == NegationSyntax.After)
            {
                for (m = kk; m > 0; m--)
                {
                    if (!paramArrayOfString[m].Equals("'"))
                    {
                        throw new UnexpectedSymbolException("Expected \"'\" instead of (or operator before) '" +
                                                            paramArrayOfString[m] + "'");
                    }

                    j = j == 0 ? 1 : 0;
                }

                localLogicExpression = Parse(paramArrayOfString[0], paramParserSyntax);
            }

            localLogicExpression.Negated = j == 1;
            return localLogicExpression;
        }
Example #3
0
        private static LogicExpression GenerateLiteral(String paramString, ParserSyntax paramParserSyntax)
        {
            var i = paramParserSyntax.NegationSyntax;

            if (i == NegationSyntax.Unknown)
            {
                if (paramString[0] == '~')
                    paramParserSyntax.UpdateNegationSyntax(NegationSyntax.Before);
                else if (paramString[paramString.Length - 1] == '\'')
                {
                    paramParserSyntax.UpdateNegationSyntax(NegationSyntax.After);
                }
            }
            bool @bool = false;
            int j;
            if (i == NegationSyntax.Before)
            {
                if (paramString[paramString.Length - 1] == '\'')
                {
                    throw new DifferentSyntaxesUsedException("Incorrect negation");
                }

                for (j = 0; (j < paramString.Length) && (paramString[j] == '~'); j++)
                {
                    @bool = !@bool;
                }
                if ((j == paramString.Length - 1) && (paramString[j] == '~'))
                {
                    throw new UnexpectedSymbolException("Negator(s) without literal");
                }
            }
            else if (i == NegationSyntax.After)
            {
                if (paramString[0] == '~')
                {
                    throw new DifferentSyntaxesUsedException("Incorrect negation");
                }

                for (j = paramString.Length - 1; (j >= 0) && (paramString[j] == '\''); j--)
                {
                    @bool = !@bool;
                }
                if ((j == 0) && (paramString[0] == '\''))
                {
                    throw new UnexpectedSymbolException("Negator(s) without literal");
                }
            }

            LogicExpression localLogicExpression = GenerateLiteral(paramString);
            localLogicExpression.Negated = @bool;
            return localLogicExpression;
        }
Example #4
0
        private static LogicExpression Parse(String paramString, ParserSyntax paramParserSyntax)
        {
            if (paramString.Length == 0)
            {
                throw new EmptyLogicException("No expression entered");
            }
            paramString = StripDown(paramString);

            if (paramString.Length == 0)
            {
                throw new EmptyLogicException("Only whitespace or brackets entered");
            }

            String[] arrayOfString = BreakDownInToWords(paramString);

            return ParseWords(arrayOfString, paramParserSyntax);
        }
Example #5
0
        public static ParsedExpression ParseLogic(String paramString)
        {
            var localParserSyntax = new ParserSyntax();
            LogicExpression localLogicExpression = Parse(paramString, localParserSyntax);

            var i = localParserSyntax.LogicSyntax;
            var j = localParserSyntax.NegationSyntax;

            if (i == LogicSyntax.f0)
            {
                if (j == NegationSyntax.Unknown)
                {
                    i = LogicSyntax.DotPlus;
                    j = GetNegationSyntax(i);
                }
                else
                {
                    switch (j)
                    {
                        case NegationSyntax.Written:
                            i = LogicSyntax.Written;
                            break;
                        case NegationSyntax.Before:
                        case NegationSyntax.After:
                            i = LogicSyntax.DotPlus;
                            break;
                    }
                }
            }

            return new ParsedExpression(paramString, localLogicExpression, i, j);
        }