18 using System.Collections.Generic;
19 using System.Linq.Expressions;
21 namespace Deveel.Data.Linq {
29 public static Expression
PartialEval(Expression expression, Func<Expression, bool> fnCanBeEvaluated) {
39 return PartialEval(expression, CanBeEvaluatedLocally);
43 return expression.NodeType != ExpressionType.Parameter;
53 this.candidates = candidates;
56 internal Expression
Eval(Expression exp) {
57 return this.Visit(exp);
60 public override Expression
Visit(Expression exp) {
64 if (candidates.Contains(exp)) {
67 return base.Visit(exp);
71 if (e.NodeType == ExpressionType.Constant) {
74 LambdaExpression lambda = Expression.Lambda(e);
75 Delegate fn = lambda.Compile();
76 return Expression.Constant(fn.DynamicInvoke(null), e.Type);
89 internal Nominator(Func<Expression, bool> fnCanBeEvaluated) {
90 this.fnCanBeEvaluated = fnCanBeEvaluated;
93 internal HashSet<Expression>
Nominate(Expression expression) {
94 this.candidates =
new HashSet<Expression>();
95 this.Visit(expression);
96 return this.candidates;
99 public override Expression
Visit(Expression expression) {
100 if (expression != null) {
101 bool saveCannotBeEvaluated = this.cannotBeEvaluated;
102 this.cannotBeEvaluated =
false;
103 base.Visit(expression);
104 if (!this.cannotBeEvaluated) {
105 if (this.fnCanBeEvaluated(expression)) {
106 this.candidates.Add(expression);
108 this.cannotBeEvaluated =
true;
111 this.cannotBeEvaluated |= saveCannotBeEvaluated;
override Expression Visit(Expression expression)
Performs bottom-up analysis to determine which nodes can possibly be part of an evaluated sub-tree...
HashSet< Expression > Nominate(Expression expression)
Evaluates & replaces sub-trees when first candidate is reached (top-down)
SubtreeEvaluator(HashSet< Expression > candidates)
static bool CanBeEvaluatedLocally(Expression expression)
Expression Eval(Expression exp)
Func< Expression, bool > fnCanBeEvaluated
Expression Evaluate(Expression e)
override Expression Visit(Expression exp)
static Expression PartialEval(Expression expression, Func< Expression, bool > fnCanBeEvaluated)
Performs evaluation & replacement of independent sub-trees
Nominator(Func< Expression, bool > fnCanBeEvaluated)
static Expression PartialEval(Expression expression)
Performs evaluation & replacement of independent sub-trees
HashSet< Expression > candidates
HashSet< Expression > candidates