Example #1
0
        public BinaryExpression(TokenKind type, Expression left, Expression right)
        {
            ContractUtils.RequiresNotNull(left, "left");
            ContractUtils.RequiresNotNull(right, "right");

            switch (type)
            {
                case TokenKind.Add: _op = MSAst.ExpressionType.Add; break;
                case TokenKind.Subtract: _op = MSAst.ExpressionType.Subtract; break;
                case TokenKind.Mod: _op = MSAst.ExpressionType.Modulo; break;
                case TokenKind.Multiply: _op = MSAst.ExpressionType.Multiply; break;
                case TokenKind.Divide: _op = MSAst.ExpressionType.Divide; break;
                case TokenKind.LessThan: _op = MSAst.ExpressionType.LessThan; break;
                case TokenKind.LessThanOrEqual: _op = MSAst.ExpressionType.LessThanOrEqual; break;
                case TokenKind.GreaterThan: _op = MSAst.ExpressionType.GreaterThan; break;
                case TokenKind.GreaterThanOrEqual: _op = MSAst.ExpressionType.GreaterThanOrEqual; break;
                case TokenKind.Equals: _op = MSAst.ExpressionType.Equal; break;
                case TokenKind.NotEquals: _op = MSAst.ExpressionType.NotEqual; break;
                case TokenKind.LogicalAnd: _op = MSAst.ExpressionType.And; break;
                case TokenKind.LogicalOr: _op = MSAst.ExpressionType.Or; break;
                default: throw Assert.Unreachable;
            }

            _left = left;
            _right = right;
            StartIndex = left.StartIndex;
            EndIndex = right.EndIndex;
        }
Example #2
0
        public void Regular(Linq.ExpressionType binaryType, Type leftType = null, Type rightType = null)
        {
            leftType ??= typeof(int);
            rightType ??= leftType;

            var expected = LinqExpression.MakeBinary(
                binaryType,
                LinqExpression.Parameter(
                    leftType),
                LinqExpression.Parameter(
                    rightType));

            var actual = $@"
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

:s
    :binaryExpressionType xt:{binaryType} ;
    :binaryLeft [
        :parameterType [
            :typeName ""{leftType}"" ;
        ]
    ] ;
    :binaryRight [
        :parameterType [
            :typeName ""{rightType}"" ;
        ] ;
    ] ;
.
";

            ShouldBe(actual, expected);
        }
Example #3
0
        public void Typed(Linq.ExpressionType expression, Type type = null)
        {
            var operandType = typeof(object);

            type ??= operandType;

            var expected = LinqExpression.MakeUnary(expression, LinqExpression.Parameter(operandType), type);

            var actual = $@"
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

:s
    :unaryExpressionType xt:{expression} ;
    :unaryOperand [
        :parameterType [
            :typeName ""{operandType}"" ;
        ] ;
    ] ;
    :unaryType [
        :typeName ""{type}"" ;
    ] ;
.
";

            ShouldBe(actual, expected);
        }
 public GridSearchOperationNodeTypeAttribute(System.Linq.Expressions.ExpressionType nodeType,
                                             bool isBinary,
                                             String stringComparisonMethod   = null,
                                             String stringManipulationMethod = null)
 {
     NodeType = nodeType;
     IsBinary = isBinary;
     StringComparisonMethod   = stringComparisonMethod;
     StringManipulationMethod = stringManipulationMethod;
 }
Example #5
0
        public AssignExpression(TokenKind type, Expression left, Expression right)
        {
            switch (type)
            {
                case TokenKind.Assign: _op = MSAst.ExpressionType.Assign; break;
                case TokenKind.AddEqual: _op = MSAst.ExpressionType.AddAssign; break;
                case TokenKind.SubtractEqual: _op = MSAst.ExpressionType.SubtractAssign; break;
                default: throw Assert.Unreachable;
            }

            _left = left;
            _right = right;
        }
Example #6
0
        /// <summary>
        /// 表达式类型计算
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        string ExpressionTypeCast(System.Linq.Expressions.ExpressionType type)
        {
            switch (type)
            {
            case System.Linq.Expressions.ExpressionType.And:
            case System.Linq.Expressions.ExpressionType.AndAlso:
                return(" AND ");

            case System.Linq.Expressions.ExpressionType.Equal:
                return(" =");

            case System.Linq.Expressions.ExpressionType.GreaterThan:
                return(" >");

            case System.Linq.Expressions.ExpressionType.GreaterThanOrEqual:
                return(">=");

            case System.Linq.Expressions.ExpressionType.LessThan:
                return("<");

            case System.Linq.Expressions.ExpressionType.LessThanOrEqual:
                return("<=");

            case System.Linq.Expressions.ExpressionType.NotEqual:
                return("<>");

            case System.Linq.Expressions.ExpressionType.Or:
            case System.Linq.Expressions.ExpressionType.OrElse:
                return(" Or ");

            case System.Linq.Expressions.ExpressionType.Add:
            case System.Linq.Expressions.ExpressionType.AddChecked:
                return("+");

            case System.Linq.Expressions.ExpressionType.Subtract:
            case System.Linq.Expressions.ExpressionType.SubtractChecked:
                return("-");

            case System.Linq.Expressions.ExpressionType.Divide:
                return("/");

            case System.Linq.Expressions.ExpressionType.Multiply:
            case System.Linq.Expressions.ExpressionType.MultiplyChecked:
                return("*");

            default:
                return(null);
            }
        }
        public static string GetExceptionMessage <TLeft, TRight, TResult>(exp.ExpressionType operatorType)
        {
            ParameterExpression leftParameter  = Expression.Parameter(typeof(TLeft), "left");
            ParameterExpression rightParameter = Expression.Parameter(typeof(TRight), "right");

            try
            {
                Expression.MakeBinary(operatorType, leftParameter, rightParameter);
                throw new Exception();
            }
            catch (InvalidOperationException e)
            {
                return(e.Message);
            }
        }
Example #8
0
        internal MSAst Operator(ExpressionType op, MSAst left, MSAst right)
        {
            var returnType = ((op == ExpressionType.IsTrue) || (op == ExpressionType.IsFalse)) ? typeof(bool) : typeof(object);

            if (right == null)
            {
                return(MSAst.Dynamic(
                           _binder.UnaryOperation(op),
                           returnType,
                           left));
            }
            return(MSAst.Dynamic(
                       _binder.BinaryOperation(op),
                       returnType,
                       left,
                       right));
        }
        public static string GetExceptionMessage <TOperand, TResult>(exp.ExpressionType operatorType)
        {
            ParameterExpression operand = Expression.Parameter(typeof(TOperand), "operand");

            try
            {
                Expression.MakeUnary(operatorType, operand, typeof(TResult));
                throw new Exception(
                          string.Format("Expression.MakeUnary was expected to throw exception for: TOperand of type {0} and TResult of type {1} for ExpressionType {2}",
                                        typeof(TOperand).Name,
                                        typeof(TResult).Name,
                                        operatorType));
            }
            catch (InvalidOperationException e)
            {
                return(e.Message);
            }
        }
Example #10
0
        private static string GetOperatorToken(MSAst.ExpressionType @operator)
        {
            switch (@operator)
            {
            case MSAst.ExpressionType.OnesComplement:
                return("~");

            case MSAst.ExpressionType.Not:
                return("!");

            case MSAst.ExpressionType.Negate:
                return("-");

            case MSAst.ExpressionType.UnaryPlus:
                return("+");
            }

            return(OperatorInfo.GetOperatorInfo(@operator).Name);
        }
Example #11
0
        static Expression DoNumericPromotion(ParseContext ec, MSAst.ExpressionType op, Expression expr)
        {
            var exprType = expr.Type;

            if ((op == MSAst.ExpressionType.UnaryPlus || op == MSAst.ExpressionType.Negate || op == MSAst.ExpressionType.OnesComplement) &&
                exprType == TypeManager.CoreTypes.Byte || exprType == TypeManager.CoreTypes.SByte ||
                exprType == TypeManager.CoreTypes.Int16 || exprType == TypeManager.CoreTypes.UInt16 ||
                exprType == TypeManager.CoreTypes.Char)
            {
                return(ConvertExpression.MakeImplicitNumericConversion(ec, expr, expr.Type, TypeManager.CoreTypes.Int32));
            }

            if (op == MSAst.ExpressionType.Negate && exprType == TypeManager.CoreTypes.UInt32)
            {
                return(ConvertExpression.MakeImplicitNumericConversion(ec, expr, expr.Type, TypeManager.CoreTypes.Int64));
            }

            return(expr);
        }
Example #12
0
        Expression SetUserDefinedOperationInformations(ResolveContext rc, OverloadResolution r)
        {//TODO:Is It OK
            if (r.BestCandidateErrors != OverloadResolutionErrors.None)
            {
                rc.Report.Error(0, loc, "Operator `{0}' is ambiguous on an operand of type `{1}'",
                                OperName(Oper), Expr.Type.ToString());
                return(r.CreateInvocation(null));
            }


            IMethod method   = (IMethod)r.BestCandidate;
            var     operands = r.GetArgumentsWithConversions();

            this.operatortype              = ResolveContext.GetLinqNodeType(this.Oper, rc.checkForOverflow);
            this.isLiftedOperator          = method is OverloadResolution.ILiftedOperator;
            this.ResolvedType              = method.ReturnType;
            this.Expr                      = operands[0];
            this.userDefinedOperatorMethod = method;
            _resolved                      = true;
            return(this);
        }
Example #13
0
        private Binary getOperator(Linq.ExpressionType type)
        {
            switch (type)
            {
            case Linq.ExpressionType.Add:
                return(Binary.Add);

            case Linq.ExpressionType.Subtract:
                return(Binary.Subtract);

            case Linq.ExpressionType.Multiply:
                return(Binary.Multiply);

            case Linq.ExpressionType.Divide:
                return(Binary.Divide);

            case Linq.ExpressionType.Power:
                return(Binary.Power);
            }

            throw new Exception("Can't parse a non-binary operator");
        }
Example #14
0
 public UnaryExpression(bool postfix, TokenKind kind, Expression expression)
 {
     _expression = expression;
     if (postfix)
     {
         switch (kind)
         {
             case TokenKind.Increment: _op = MSAst.ExpressionType.PostIncrementAssign; break;
             case TokenKind.Decrement: _op = MSAst.ExpressionType.PostDecrementAssign; break;
             default: throw Assert.Unreachable;
         }
     }
     else
     {
         switch (kind)
         {
             case TokenKind.Increment: _op = MSAst.ExpressionType.PreIncrementAssign; break;
             case TokenKind.Decrement: _op = MSAst.ExpressionType.PreDecrementAssign; break;
             case TokenKind.LogicalNot: _op = MSAst.ExpressionType.Not; break;
             default: throw Assert.Unreachable;
         }
     }
 }
Example #15
0
        public override object BinaryOperation(Func <object> proceed, object self, System.Linq.Expressions.ExpressionType operation, object value)
        {
            var supertype = ((dynamic)self).Supertype();
            var isObject  = (supertype != "Value" && supertype != "Null");

            switch (operation)
            {
            case ExpressionType.Equal:
                return((value == null)
                        ? (!isObject && _localValue == null)
                        : (isObject ? false : (_localValue != null && value.Equals(_localValue))));

            case ExpressionType.NotEqual:
                return((value == null)
                        ? (isObject || _localValue != null)
                        : (isObject ? false : (_localValue == null || !value.Equals(_localValue))));

            case ExpressionType.GreaterThan:
                return((value == null) ? false :
                       (isObject ? false : (_localValue != null && (Compare(_localValue, value) > 0))));

            case ExpressionType.GreaterThanOrEqual:
                return((value == null) ? false :
                       (isObject ? false : (_localValue != null && (Compare(_localValue, value) >= 0))));

            case ExpressionType.LessThan:
                return((value == null) ? false :
                       (isObject ? false : (_localValue != null && (Compare(_localValue, value) < 0))));

            case ExpressionType.LessThanOrEqual:
                return((value == null) ? false :
                       (isObject ? false : (_localValue != null && (Compare(_localValue, value) <= 0))));
            }

            return(proceed());
        }
Example #16
0
 internal MSAst Operator(ExpressionType op, MSAst left)
 {
     return(Operator(op, left, null));
 }
Example #17
0
 public override System.Dynamic.UnaryOperationBinder CreateUnaryOperationBinder(System.Linq.Expressions.ExpressionType operation)
 {
     return(base.CreateUnaryOperationBinder(operation));
 }
Example #18
0
 private static RemoteLinq.UnaryOperator ToUnaryOperator(this SystemLinq.ExpressionType expressionType)
 => (RemoteLinq.UnaryOperator)(int) expressionType;
Example #19
0
 public LiftedUnaryOperator(MSAst.ExpressionType @operator, Expression operand)
 {
     Operator = @operator;
     Operand  = operand;
 }
Example #20
0
 private static RemoteLinq.NewArrayType ToNewArrayType(this SystemLinq.ExpressionType expressionType)
 => (RemoteLinq.NewArrayType)(int) expressionType;
Example #21
0
        private Expression GetComparisonLinqExpression([NotNull] ParameterExpression shapes, NodeType type)
        {
            List <BinaryExpression>    comparisons = new List <BinaryExpression>();
            List <Expression>          body        = new List <Expression>();
            List <ParameterExpression> vars        = new List <ParameterExpression>();

            Expression last      = null;
            float      lastConst = 0;

            foreach (Expression exp in Expressions.Select(e => e.GetLinqExpression(shapes)))
            {
                Expression expression = exp;
                Debug.Assert(expression != null, "expression != null");

                ConstantExpression constExp = expression as ConstantExpression;
                if (constExp != null)
                {
                    Debug.Assert(constExp.Value is float);

                    float curr = (float)constExp.Value;
                    if (last != null)
                    {
                        switch (type)
                        {
                        // TODO tolerance
                        case NodeType.GreaterThan:
                            if (lastConst <= curr)
                            {
                                return(Expression.Constant(false));
                            }
                            break;

                        case NodeType.LessThan:
                            if (lastConst >= curr)
                            {
                                return(Expression.Constant(false));
                            }
                            break;

                        case NodeType.GreaterThanOrEqual:
                            if (lastConst < curr)
                            {
                                return(Expression.Constant(false));
                            }
                            break;

                        case NodeType.LessThanOrEqual:
                            if (lastConst > curr)
                            {
                                return(Expression.Constant(false));
                            }
                            break;
                        }
                    }

                    lastConst = curr;
                }
                else
                {
                    ParameterExpression varExp = Expression.Variable(typeof(float));
                    vars.Add(varExp);
                    body.Add(Expression.Assign(varExp, expression));
                    expression = varExp;
                }

                if (last != null)
                {
                    comparisons.Add(Expression.MakeBinary(type, last, expression));
                }

                last = expression;
            }

            Expression result = null;

            foreach (BinaryExpression comparison in comparisons)
            {
                Debug.Assert(comparison != null, "comparison != null");

                ConstantExpression leftConst, rightConst;

                if ((leftConst = comparison.Left as ConstantExpression) != null &&
                    (rightConst = comparison.Right as ConstantExpression) != null)
                {
#if DEBUG
                    Debug.Assert(leftConst.Value is float);
                    Debug.Assert(rightConst.Value is float);

                    float leftVal  = (float)leftConst.Value;
                    float rightVal = (float)rightConst.Value;

                    switch (type)
                    {
                    case NodeType.GreaterThan:
                        Debug.Assert(leftVal > rightVal);
                        break;

                    case NodeType.LessThan:
                        Debug.Assert(leftVal > rightVal);
                        break;

                    case NodeType.GreaterThanOrEqual:
                        Debug.Assert(leftVal > rightVal);
                        break;

                    case NodeType.LessThanOrEqual:
                        Debug.Assert(leftVal > rightVal);
                        break;

                    default:
                        Debug.Assert(false);
                        throw new ArgumentOutOfRangeException();
                    }
#endif

                    continue;
                }

                result = result == null
                    ? comparison
                    : Expression.AndAlso(result, comparison);
            }

            body.Add(result);

            return(result == null
                ? (Expression)Expression.Constant(true)
                : Expression.Block(typeof(bool), vars, body));
        }
Example #22
0
 public override BinaryOperationBinder CreateBinaryOperationBinder(System.Linq.Expressions.ExpressionType operation)
 {
     return(new TjsBinaryOperationBinder(this, operation));
 }
Example #23
0
 public Rule(string comparisonPredicate, System.Linq.Expressions.ExpressionType comparisonOperator, string comparisonValue)
 {
     ComparisonPredicate = comparisonPredicate;
     ComparisonOperator  = comparisonOperator;
     ComparisonValue     = comparisonValue;
 }
Example #24
0
        /// <summary>
        /// 二元运算符表达式
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        string BinarExpressionProvider(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Linq.Expressions.ExpressionType type)
        {
            string where = "(";
            //先处理左边
            string leftStr = ExpressionRouter(left);
            //获取实体列的特性
            List <EntityPropColumnAttributes> columnAttrList = AttributeHelper.GetEntityColumnAtrributes <TEntity>();
            var list = columnAttrList.Where(w => w.propName == leftStr);

            if (list.Count() > 0)
            {
                EntityPropColumnAttributes columnAttribute = list.First();
                leftStr = columnAttribute.fieldName;
            }
            //节点类型
            string typeStr = ExpressionTypeCast(type);

            //再处理右边
            string rightStr = RightExpressionRouter(right).ToStr();

            where += leftStr;
            where += typeStr;

            if (rightStr == "null")
            {
                if (where.EndsWith(" ="))
                {
                    where = where.Substring(0, where.Length - 2) + " is null";
                }
                else if (where.EndsWith("<>"))
                {
                    where = where.Substring(0, where.Length - 2) + " is not null";
                }
            }
            else
            {
                //如果左侧包含(则代表左侧非字段
                if (leftStr.Contains("("))
                {
                    where += rightStr;
                }
                else
                {
                    int num = random.Next(100, 999);
                    where += "@" + leftStr + num;
                    listPara.Add(new SqlParameter("@" + leftStr + num, rightStr));
                }
            }
            return(where += ")");
        }