Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
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;
        }