Exemplo n.º 1
0
 public override bool Matches(Expression E, MatchContext Matched)
 {
     return(target.CallMatches(arguments, E, Matched));
 }
Exemplo n.º 2
0
        public override bool Matches(Expression E, MatchContext Matched)
        {
            // Move the constants in this pattern to E.
            IEnumerable <Expression> PTerms    = Terms;
            IEnumerable <Expression> Constants = PTerms.OfType <Constant>();

            if (Constants.Any())
            {
                E      = Binary.Subtract(E, New(Constants)).Evaluate();
                PTerms = PTerms.Except(Constants, RefComparer);
            }

            IEnumerable <Expression> ETerms = TermsOf(E);

            // Try starting the match at each term of the pattern.
            foreach (Expression p in PTerms)
            {
                // Remaining terms of the pattern.
                Expression P = New(PTerms.ExceptUnique(p, RefComparer));

                // If p is a variable, we have to handle the possibility that more than one term of E might match this term.
                if (p is Variable)
                {
                    // Check if p has already been matched. If it has, treat it as a constant and match the rest of the terms.
                    Expression matched;
                    if (Matched.TryGetValue(p, out matched))
                    {
                        // p has already been matched. Remove it out of E and match the remainder of the pattern.
                        if (P.Matches(Binary.Subtract(E, matched).Evaluate(), Matched))
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        // Try matching p to the various combinations of the terms of E.
                        for (int i = 1; i <= ETerms.Count(); ++i)
                        {
                            foreach (IEnumerable <Expression> e in ETerms.Combinations(i))
                            {
                                if (Matched.TryMatch(() =>
                                                     p.Matches(New(e), Matched) &&
                                                     P.Matches(New(ETerms.ExceptUnique(e, RefComparer)), Matched)))
                                {
                                    return(true);
                                }
                            }
                        }

                        // Try matching p to identity.
                        if (Matched.TryMatch(() => p.Matches(0, Matched) && P.Matches(E, Matched)))
                        {
                            return(true);
                        }
                    }
                }
                else
                {
                    // If p is not a variable, try matching it to any of the terms of E.
                    foreach (Expression e in ETerms)
                    {
                        if (Matched.TryMatch(() =>
                                             p.Matches(e, Matched) &&
                                             P.Matches(New(ETerms.ExceptUnique(e, RefComparer)), Matched)))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 3
0
 public override bool CallMatches(IEnumerable <Expression> Arguments, Expression E, MatchContext Matched)
 {
     return(base.CallMatches(Arguments, E, Matched) || PowHalf.Matches(E, Matched));
 }
Exemplo n.º 4
0
 protected override Expression ApplyTransform(Expression x, MatchContext Matched)
 {
     return(result.Substitute(Matched, true).Evaluate());
 }
Exemplo n.º 5
0
        /// <summary>
        /// Check if a call to this function with the given arguments matches an expression.
        /// </summary>
        /// <param name="Arguments"></param>
        /// <param name="E"></param>
        /// <param name="Matched"></param>
        /// <returns></returns>
        public virtual bool CallMatches(IEnumerable <Expression> Arguments, Expression E, MatchContext Matched)
        {
            Call EF = E as Call;

            if (!ReferenceEquals(EF, null))
            {
                if (!Equals(EF.Target))
                {
                    return(false);
                }
                if (Arguments.Count() != EF.Arguments.Count())
                {
                    return(false);
                }

                return(Matched.TryMatch(() => Arguments.Reverse().Zip(EF.Arguments.Reverse(), (p, e) => p.Matches(e, Matched)).All()));
            }

            return(false);
        }
Exemplo n.º 6
0
 public override bool Matches(Expression Expr, MatchContext Matched)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 7
0
 public override bool Matches(Expression E, MatchContext Matched)
 {
     return((ReferenceEquals(Ring, null) || Ring.Contains(E)) && Matched.Matches(this, E));
 }
Exemplo n.º 8
0
 protected abstract Expression ApplyTransform(Expression x, MatchContext Matched);
Exemplo n.º 9
0
 /// <summary>
 /// Try matching this pattern against an expression such that if Matches(Expr, Matched) is true, Substitute(Matched) is equal to Expr.
 /// </summary>
 /// <param name="Expr">Expression to match.</param>
 /// <param name="Matched">A context to store matched variables.</param>
 /// <returns>true if Equals(Expr.Substitute(Matched)) is true.</returns>
 public virtual bool Matches(Expression Expr, MatchContext Matched)
 {
     return(Equals(Expr));
 }
Exemplo n.º 10
0
 public override bool Matches(Expression E, MatchContext Matched)
 {
     return((Ring is null || Ring.Contains(E)) && Matched.Matches(this, E));
 }