Example #1
0
        private IEnumerable <Expression> ReplaceVar(Dictionary <Expression, List <Expression> > vars, ExpressionTree expression)
        {
            if (expression == null)
            {
                yield break;
            }
            if (expression.Root == null)
            {
                expression.SetRoot();
            }

            if (expression.IsLeaf())
            {
                foreach (var kvp in vars)
                {
                    if (SingletonEquality(expression.Data, kvp.Key))
                    {
                        foreach (var var in kvp.Value)
                        {
                            // safeguard against infinite loop
                            if (SingletonEquality(kvp.Key, var))
                            {
                                continue;
                            }
                            ExpressionTree newVal = RewriteExpression(expression, kvp.Key, var);
                            if (newVal == null)
                            {
                                break;
                            }
                            var copy    = expression.Root.Copy();
                            var newTree = ExpressionTree.FindAndReplaceNode(copy, newVal, expression.Copy());
                            yield return(newTree.Root.TreeToExpression());

                            foreach (var item in ReplaceVar(vars, newTree.Root))
                            {
                                yield return(item);
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (var item in ReplaceVar(vars, expression.LChild))
                {
                    yield return(item);
                }
                foreach (var item in ReplaceVar(vars, expression.RChild))
                {
                    yield return(item);
                }
            }
        }
Example #2
0
        private IEnumerable <Expression> ReplaceConstants(ExpressionTree expression, List <Expression> constants, List <IVariable> vars)
        {
            if (expression == null)
            {
                yield break;
            }
            if (expression.Root == null)
            {
                expression.SetRoot();
            }

            if (expression.IsLeaf())
            {
                if (HasConstant(expression.Data, constants))
                {
                    foreach (var var in vars)
                    {
                        if (!ValidateType(var, expression.Parent.TreeToExpression() as BinaryExpr))
                        {
                            continue;
                        }
                        var newVal  = ExpressionTree.ExpressionToTree(VariableToExpression(var));
                        var copy    = expression.Root.Copy();
                        var newTree = ExpressionTree.FindAndReplaceNode(copy, newVal, expression.Copy());
                        yield return(newTree.Root.TreeToExpression());

                        foreach (var item in ReplaceConstants(newTree.Root, constants, vars))
                        {
                            yield return(item);
                        }
                    }
                }
            }
            else
            {
                foreach (var item in ReplaceConstants(expression.LChild, constants, vars))
                {
                    yield return(item);
                }
                foreach (var item in ReplaceConstants(expression.RChild, constants, vars))
                {
                    yield return(item);
                }
            }
        }