private static bool IsEqual(ConstantExpression left, ConstantExpression right) { if (left.ConstantType != right.ConstantType) { return(false); } if (left.ConstantType == ConstantType.Boolean) { BooleanConstantExpression leftBoolean = (BooleanConstantExpression)left; BooleanConstantExpression rightBoolean = (BooleanConstantExpression)right; return(leftBoolean.Value == rightBoolean.Value); } else if (left.ConstantType == ConstantType.Integer) { IntegerConstantExpression leftInteger = (IntegerConstantExpression)left; IntegerConstantExpression rightInteger = (IntegerConstantExpression)right; return(leftInteger.Value == rightInteger.Value); } else if (left.ConstantType == ConstantType.String) { StringConstantExpression leftString = (StringConstantExpression)left; StringConstantExpression rightString = (StringConstantExpression)right; return(string.Equals(leftString.Value, rightString.Value, StringComparison.OrdinalIgnoreCase)); } else { throw new EnableInfoException(string.Format(CultureInfo.InvariantCulture, "Comparing {0} constants is not supported.", left.ConstantType)); } }
Expression IExpressionVisitor.VisitLogical(LogicalExpression node) { BooleanConstantExpression left = Visit(node.Left) as BooleanConstantExpression; if (left == null) { return(BooleanConstantExpression.False); } switch (node.NodeType) { case ExpressionType.AndAlso: if (!left.Value) { return(left); } break; case ExpressionType.OrElse: if (left.Value) { return(left); } break; default: throw new EnableInfoException(string.Format(CultureInfo.InvariantCulture, "{0} is not a supported logical expression.", node.NodeType)); } BooleanConstantExpression right = Visit(node.Right) as BooleanConstantExpression; return(right ?? BooleanConstantExpression.False); }
Expression IExpressionVisitor.VisitUnary(UnaryExpression node) { ConstantExpression expression = Visit(node.Expression) as ConstantExpression; if (expression == null) { throw new EnableInfoException(string.Format(CultureInfo.InvariantCulture, "The {0} operand must be a ConstantExpression, actual type: {1}", node.NodeType, node.Expression?.GetType().ToString() ?? "null")); } if (expression.ConstantType == ConstantType.UndefinedVariable) { return(expression); } if (node.NodeType == ExpressionType.Not) { if (expression.ConstantType != ConstantType.Boolean) { throw new EnableInfoException(string.Format(CultureInfo.InvariantCulture, "The {0} operand must be a Boolean constant, actual value: {1}", expression.NodeType, expression.ConstantType)); } BooleanConstantExpression booleanConstant = (BooleanConstantExpression)expression; return(new BooleanConstantExpression(!booleanConstant.Value)); } else if (node.NodeType == ExpressionType.Negate || node.NodeType == ExpressionType.UnaryPlus) { if (expression.ConstantType != ConstantType.Integer) { throw new EnableInfoException(string.Format(CultureInfo.InvariantCulture, "The {0} operand must be an Integer constant, actual value: {1}", expression.NodeType, expression.ConstantType)); } IntegerConstantExpression integerConstant = (IntegerConstantExpression)expression; switch (node.NodeType) { case ExpressionType.Negate: return(new IntegerConstantExpression(-integerConstant.Value)); case ExpressionType.UnaryPlus: return(new IntegerConstantExpression(+integerConstant.Value)); default: throw new EnableInfoException(string.Format(CultureInfo.InvariantCulture, "{0} is not a supported unary operator.", expression.NodeType)); } } else { throw new EnableInfoException(string.Format(CultureInfo.InvariantCulture, "{0} is not a supported unary operator.", expression.NodeType)); } }
private BooleanConstantExpression VisitInFunction(ReadOnlyCollection <Expression> args) { if (args != null && args.Count >= 2) { // The EnableInfo documentation states that the 'in' function returns true // if the first parameter is equal to at least one of the following parameters. ConstantExpression first = Visit(args[0]) as ConstantExpression; if (first != null) { if (first.ConstantType == ConstantType.String) { string firstParameter = ((StringConstantExpression)first).Value; for (int i = 1; i < args.Count; i++) { StringConstantExpression parameter = Visit(args[i]) as StringConstantExpression; if (parameter != null && string.Equals(firstParameter, parameter.Value, StringComparison.OrdinalIgnoreCase)) { return(BooleanConstantExpression.True); } } } else if (first.ConstantType == ConstantType.Integer) { int firstParameter = ((IntegerConstantExpression)first).Value; for (int i = 1; i < args.Count; i++) { IntegerConstantExpression parameter = Visit(args[i]) as IntegerConstantExpression; if (parameter != null && firstParameter == parameter.Value) { return(BooleanConstantExpression.True); } } } else if (first.ConstantType == ConstantType.Boolean) { bool firstParameter = ((BooleanConstantExpression)first).Value; for (int i = 1; i < args.Count; i++) { BooleanConstantExpression parameter = Visit(args[i]) as BooleanConstantExpression; if (parameter != null && firstParameter == parameter.Value) { return(BooleanConstantExpression.True); } } } } } return(BooleanConstantExpression.False); }
/// <summary> /// Evaluates the specified enable info expression. /// </summary> /// <param name="node">The expression to evaluate.</param> /// <returns>A <see cref="bool"/> indicating the result of the enable info expression.</returns> /// <exception cref="ArgumentNullException"><paramref name="node"/> is null.</exception> /// <exception cref="EnableInfoException">The specified expression is not valid.</exception> public bool Evaluate(Expression node) { if (node is null) { throw new ArgumentNullException(nameof(node)); } BooleanConstantExpression result = Visit(node) as BooleanConstantExpression; if (result is null) { return(false); } return(result.Value); }
private BooleanConstantExpression VisitDimFunction(ReadOnlyCollection <Expression> args) { BooleanConstantExpression result = BooleanConstantExpression.False; if (args != null && args.Count == 2) { // The EnableInfo documentation does not provide any information on how the 'dim' function is used. // After using a filter to test the 'dim' function behavior in Photoshop 5.0 it appears that the // function returns true if the first parameter evaluates to true and the second parameter evaluates to false. BooleanConstantExpression first = Visit(args[0]) as BooleanConstantExpression; BooleanConstantExpression second = Visit(args[1]) as BooleanConstantExpression; if (first != null && second != null) { result = new BooleanConstantExpression(first.Value && !second.Value); } } return(result); }