public override Expression Reduce(Expression root, ExpressionReductor reductor) { if (!(root is FunctionCallExpression)) { return(root); } FunctionCallExpression func = root as FunctionCallExpression; if (func.MethodName == "pow") { if (func.Parameters[1].IsConstant(1)) { return(func.Parameters[0]); } else if (func.Parameters[1].IsConstant(0)) { return(ConstantExpression.create(1, 0, 0)); } } return(root); }
public override Expression Reduce(Expression root, ExpressionReductor reductor) { if (root.IsConstantExpression()) { return(root.GetConstant(reductor.LanguageService.CreateEvaluator().Evaluate(root, new VariableContext()))); } else { return(root); } }
public override Expression Reduce(Expression root, ExpressionReductor reductor) { if (root is BinaryExpression) { Expression left = reductor.Reduce((root as BinaryExpression).Left); Expression right = reductor.Reduce((root as BinaryExpression).Right); if (left is BinaryExpression && ExpressionSnippets.GetOperatorLevel((left as BinaryExpression).Operator) == ExpressionSnippets.GetOperatorLevel((root as BinaryExpression).Operator)) { Expression ll = reductor.Reduce((left as BinaryExpression).Left); Expression lr = reductor.Reduce((left as BinaryExpression).Right); if (lr.IsConstantExpression()) { return(BinaryExpression.Create((left as BinaryExpression).Operator, ll, BinaryExpression.Create((root as BinaryExpression).Operator, lr, right))); } } } return(root); }
public abstract Expression Reduce(Expression root, ExpressionReductor reductor);
public override Expression Reduce(Expression root, ExpressionReductor reductor) { if (!(root is BinaryExpression)) { return(root); } Expression left = reductor.Reduce((root as BinaryExpression).Left); Expression right = reductor.Reduce((root as BinaryExpression).Right); BinaryOperator op = (root as BinaryExpression).Operator; if (op == BinaryOperator.Add) { if (left.IsConstant(0)) { return(right); } else if (right.IsConstant(0)) { return(left); } } else if (op == BinaryOperator.Subtract) { if (left.IsConstant(0)) { return(new UnaryExpression(UnaryOperator.Negation, right, 0, 0)); } else if (right.IsConstant(0)) { return(left); } } else if (op == BinaryOperator.Multiply) { if (left.IsConstant(0)) { return(ConstantExpression.create(0, 0, 0)); } else if (right.IsConstant(0)) { return(ConstantExpression.create(0, 0, 0)); } else if (left.IsConstant(1)) { return(right); } else if (right.IsConstant(1)) { return(left); } } else if (op == BinaryOperator.Divide) { if (left.IsConstant(0)) { return(ConstantExpression.create(0, 0, 0)); } else if (right.IsConstant(0)) { return(ConstantExpression.create(double.NaN, 0, 0)); } else if (left.IsConstant(1)) { } else if (right.IsConstant(1)) { return(left); } } return(root); }