Exemplo n.º 1
0
 private static Variable FindVariable(
     char aSymbol,
     VariableDictionary aBoundVariables,
     CollectedItems aCollectedItems)
 {
     if (aBoundVariables.ContainsVariableForSymbol(aSymbol))
     {
         return(aBoundVariables.Retrieve(aSymbol));
     }
     else
     {
         return(aCollectedItems.AddUnboundVariable(aSymbol));
     }
 }
        public VariableDictionary CreateNewSetThatRebinds(char aSymbol, Variable aVariable)
        {
            VariableDictionary lUpdatedDictionary = new VariableDictionary();

            foreach (char lSymbol in mVariables.Keys)
            {
                if (lSymbol != aSymbol)
                {
                    lUpdatedDictionary.Add(lSymbol, mVariables[lSymbol]);
                }
            }

            lUpdatedDictionary.Add(aSymbol, aVariable);

            return(lUpdatedDictionary);
        }
Exemplo n.º 3
0
        private static Matrix Parse(
            string[] aExpressions,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            string lFirst = aExpressions[0];

            if (aExpressions.Length == 1)
            {
                return(ParsePropositionalSubexpression(lFirst, aCollectedItems, aBoundVariables));
            }

            string[] lTail = aExpressions.Tail <string>();

            if (AreUnariesFollowedByAQuantifier(aExpressions))
            {
                return(ParseUnaryOperatorOnTail(lFirst, lTail, aCollectedItems, aBoundVariables));
            }

            if (lFirst.IsQuantifier())
            {
                return(ParseQuantification(lFirst, lTail, aCollectedItems, aBoundVariables));
            }

            if (aExpressions.TakeWhile(fExpression => !fExpression.IsQuantifier()).Any(fToken => fToken.IsBinaryOperator()))
            {
                return(ParseBinaryOperator(aExpressions, aCollectedItems, aBoundVariables));
            }

            if (IsUnaryOperator(lFirst))
            {
                return(ParseUnaryOperatorOnTail(lFirst, lTail, aCollectedItems, aBoundVariables));
            }

            throw new ParseError("Could not parse \"{0}\"", string.Join("", aExpressions));
        }
Exemplo n.º 4
0
        private static Matrix ParsePropositionalSubexpression(
            string aExpression,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            if (aExpression.Matches(TwoTermProposition))
            {
                return(ParseTwoTermProposition(aExpression, aCollectedItems));
            }

            if (aExpression.Matches(Is))
            {
                return(aCollectedItems.AddUnaryPredication(
                           aCollectedItems.AddUnaryPredicate(aExpression[0]),
                           FindVariable(aExpression[1], aBoundVariables, aCollectedItems)));
            }

            if (aExpression.Matches(Are))
            {
                return(aCollectedItems.AddBinaryPredication(
                           aCollectedItems.AddBinaryPredicate(aExpression[1]),
                           FindVariable(aExpression[0], aBoundVariables, aCollectedItems),
                           FindVariable(aExpression[2], aBoundVariables, aCollectedItems)));
            }

            if (aExpression.Matches(TrueThat))
            {
                return(aCollectedItems.AddNullPredicate(aExpression[0]));
            }

            if (aExpression.Matches(IsTheSameAs))
            {
                return(aCollectedItems.AddIdentification(
                           FindVariable(aExpression[0], aBoundVariables, aCollectedItems),
                           FindVariable(aExpression[2], aBoundVariables, aCollectedItems)));
            }

            if (aExpression.Matches(ThereAreThisManyOfThese))
            {
                return(Factory.ThereAreThisManyOfThese(
                           UInt32.Parse(aExpression.Substring(0, aExpression.Length - 1)),
                           aCollectedItems.AddUnaryPredicate(aExpression[aExpression.Length - 1])));
            }

            if (aExpression.Matches(ThisManyOfTheseAreTrue))
            {
                List <Matrix> lPredicates = new List <Matrix>();
                RegexMatch    lMatch      = ThisManyOfTheseAreTrue.Exec(aExpression);
                for (int i = 0; i < lMatch[2].Length; i++)
                {
                    lPredicates.Add(aCollectedItems.AddNullPredicate(lMatch[2][i]));
                }
                return(Factory.ThisManyOfTheseAreTrue(UInt32.Parse(lMatch[1]), lPredicates));
            }

            if (aExpression.Matches(ParenthesizedExpression))
            {
                return(Parse(
                           aExpression.Substring(1, aExpression.Length - 2).GetSubexpressions(),
                           aCollectedItems,
                           aBoundVariables));
            }

            throw new ParseError("Expected predication or parenthesized expression, found \"{0}\"", aExpression);
        }
Exemplo n.º 5
0
        private static Matrix ParseBinaryOperator(
            string[] aTokens,
            CollectedItems aCollectedItems,
            VariableDictionary aBoundVariables)
        {
            int lNumberOfTokensBeforeBinaryOperator = -1;
            int lHighestPrecedenceFound             = -1;

            for (int i = 0; i < aTokens.Length; i++)
            {
                if (aTokens[i].IsBinaryOperator())
                {
                    int lPrecedence = aTokens[i].PrecedenceOfBinaryOperator();
                    if (lPrecedence >= lHighestPrecedenceFound)
                    {
                        lHighestPrecedenceFound             = lPrecedence;
                        lNumberOfTokensBeforeBinaryOperator = i;
                    }
                }

                if (aTokens[i].IsQuantifier())
                {
                    break;
                }
            }

            string[] lTokensBeforeLastBinaryOperator = aTokens.Subarray(0, lNumberOfTokensBeforeBinaryOperator);
            string[] lTokensAfterLastBinaryOperator  = aTokens.Subarray(
                lNumberOfTokensBeforeBinaryOperator + 1,
                aTokens.Length - lNumberOfTokensBeforeBinaryOperator - 1);
            string lLastBinaryOperator = aTokens[lNumberOfTokensBeforeBinaryOperator];

            if (lTokensBeforeLastBinaryOperator.Length == 0 || lTokensAfterLastBinaryOperator.Length == 0)
            {
                throw new ParseError("Could not parse \"{0}\"", string.Join("", aTokens));
            }

            Matrix lLeft  = Parse(lTokensBeforeLastBinaryOperator, aCollectedItems, aBoundVariables);
            Matrix lRight = Parse(lTokensAfterLastBinaryOperator, aCollectedItems, aBoundVariables);

            if (lLastBinaryOperator.Matches(And))
            {
                return(Factory.And(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(Or))
            {
                return(Factory.Or(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(OnlyIf))
            {
                return(Factory.OnlyIf(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(IfAndOnlyIf))
            {
                return(Factory.IfAndOnlyIf(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(Nor))
            {
                return(Factory.Nor(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(Xor))
            {
                return(Factory.Xor(lLeft, lRight));
            }

            if (lLastBinaryOperator.Matches(NecessarilyOnlyIf))
            {
                return(Factory.NecessarilyOnlyIf(lLeft, lRight));
            }

            // The program should never reach this point.
            throw new Exception();
        }