static Expression Equal(Expression left, Expression right)
        {
            List <Expression> exps = new List <Expression>();
            // Variable
            var l         = Expression.Variable(left.Type);
            var r         = Expression.Variable(right.Type);
            var succeeded = Expression.Variable(typeof(bool));

            exps.Add(Expression.Assign(l, left));
            exps.Add(Expression.Assign(r, right));
            // Left -> Right
            var leftToRight = TjsConvertBinder.TryConvertExpression(l, right.Type, succeeded);

            if (leftToRight != null)
            {
                var res = Expression.Variable(right.Type);
                // ((res = Convert(right, leftType, out succeeded)), succeeded && res == r)
                leftToRight = Expression.Block(new[] { res },
                                               Expression.Assign(res, leftToRight),
                                               Expression.AndAlso(succeeded, Expression.Equal(res, r))
                                               );
            }
            // Right -> Left
            var rightToLeft = TjsConvertBinder.TryConvertExpression(r, left.Type, succeeded);

            if (rightToLeft != null)
            {
                var res = Expression.Variable(left.Type);
                // ((res = Convert(left, rightType, out succeeded)), succeeded && l == res)
                rightToLeft = Expression.Block(new[] { res },
                                               Expression.Assign(res, rightToLeft),
                                               Expression.AndAlso(succeeded, Expression.Equal(l, res))
                                               );
            }
            if (leftToRight == null && rightToLeft == null)
            {
                return(Expression.Constant(false));
            }
            else if (leftToRight == null)
            {
                exps.Add(rightToLeft);
            }
            else if (rightToLeft == null)
            {
                exps.Add(leftToRight);
            }
            else
            {
                exps.Add(Expression.OrElse(leftToRight, rightToLeft));
            }
            return(Expression.Block(new[] { l, r, succeeded }, exps));
        }
Exemple #2
0
 public override Expression ConvertExpression(Expression expr, Type toType, ConversionResultKind kind, OverloadResolverFactory resolverFactory)
 {
     return(TjsConvertBinder.TryConvertExpression(expr, toType, null) ?? base.ConvertExpression(expr, toType, kind, resolverFactory));
 }