private DynamicMetaObject IncrDecr(DynamicMetaObject target, int valueToAdd, DynamicMetaObject errorSuggestion)
 {
     if (!target.HasValue)
     {
         return(base.Defer(target, new DynamicMetaObject[0]));
     }
     if ((target.Value is PSObject) && (PSObject.Base(target.Value) != target.Value))
     {
         return(this.DeferForPSObject(new DynamicMetaObject[] { target }));
     }
     if (target.Value == null)
     {
         return(new DynamicMetaObject(ExpressionCache.Constant(valueToAdd).Cast(typeof(object)), target.PSGetTypeRestriction()));
     }
     if (target.LimitType.IsNumeric())
     {
         DynamicMetaObject arg = new DynamicMetaObject(ExpressionCache.Constant(valueToAdd), BindingRestrictions.Empty, valueToAdd);
         return(new DynamicMetaObject(PSBinaryOperationBinder.Get(ExpressionType.Add, true, false).FallbackBinaryOperation(target, arg, errorSuggestion).Expression, target.PSGetTypeRestriction()));
     }
     return(errorSuggestion ?? target.ThrowRuntimeError(new DynamicMetaObject[0], BindingRestrictions.Empty, "OperatorRequiresNumber", ParserStrings.OperatorRequiresNumber, new Expression[] { Expression.Constant(((base.Operation == ExpressionType.Increment) ? TokenKind.PlusPlus : TokenKind.MinusMinus).Text()), Expression.Constant(target.LimitType) }));
 }
        private DynamicMetaObject UnaryPlus(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            if (!target.HasValue)
            {
                return(base.Defer(target, new DynamicMetaObject[0]));
            }
            if ((target.Value is PSObject) && (PSObject.Base(target.Value) != target.Value))
            {
                return(this.DeferForPSObject(new DynamicMetaObject[] { target }));
            }
            if (!target.LimitType.IsNumeric())
            {
                return(new DynamicMetaObject(Expression.Dynamic(PSBinaryOperationBinder.Get(ExpressionType.Add, true, false), typeof(object), ExpressionCache.Constant(0), target.Expression), target.PSGetTypeRestriction()));
            }
            Expression expr = target.Expression.Cast(target.LimitType);

            if (target.LimitType.Equals(typeof(byte)) || target.LimitType.Equals(typeof(sbyte)))
            {
                expr = expr.Cast(typeof(int));
            }
            return(new DynamicMetaObject(Expression.UnaryPlus(expr).Cast(typeof(object)), target.PSGetTypeRestriction()));
        }
        internal DynamicMetaObject BNot(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            if (!target.HasValue)
            {
                return(base.Defer(target, new DynamicMetaObject[0]));
            }
            if ((target.Value is PSObject) && (PSObject.Base(target.Value) != target.Value))
            {
                return(this.DeferForPSObject(new DynamicMetaObject[] { target }));
            }
            if (target.Value == null)
            {
                return(new DynamicMetaObject(ExpressionCache.Constant(-1).Cast(typeof(object)), target.PSGetTypeRestriction()));
            }
            MethodInfo method = target.LimitType.GetMethod("op_OnesComplement", BindingFlags.Public | BindingFlags.Static, null, new Type[] { target.LimitType }, null);

            if (method != null)
            {
                return(new DynamicMetaObject(Expression.OnesComplement(target.Expression.Cast(target.LimitType), method).Cast(typeof(object)), target.PSGetTypeRestriction()));
            }
            if (target.LimitType.Equals(typeof(string)))
            {
                return(new DynamicMetaObject(Expression.Dynamic(this, this.ReturnType, PSBinaryOperationBinder.ConvertStringToNumber(target.Expression, typeof(int))), target.PSGetTypeRestriction()));
            }
            Expression expression = null;

            if (!target.LimitType.IsNumeric())
            {
                bool flag;
                Type resultType = typeof(int);
                LanguagePrimitives.ConversionData conversion = LanguagePrimitives.FigureConversion(target.Value, resultType, out flag);
                if (conversion.Rank != ConversionRank.None)
                {
                    expression = PSConvertBinder.InvokeConverter(conversion, target.Expression, resultType, flag, ExpressionCache.InvariantCulture);
                }
                else
                {
                    resultType = typeof(long);
                    conversion = LanguagePrimitives.FigureConversion(target.Value, resultType, out flag);
                    if (conversion.Rank != ConversionRank.None)
                    {
                        expression = PSConvertBinder.InvokeConverter(conversion, target.Expression, resultType, flag, ExpressionCache.InvariantCulture);
                    }
                }
            }
            else
            {
                TypeCode typeCode = LanguagePrimitives.GetTypeCode(target.LimitType);
                if (typeCode < TypeCode.Int32)
                {
                    expression = target.Expression.Cast(typeof(int));
                }
                else if (typeCode <= TypeCode.UInt64)
                {
                    expression = target.Expression.Cast(target.LimitType);
                }
                else
                {
                    Type type2 = (typeCode == TypeCode.Decimal) ? typeof(DecimalOps) : typeof(DoubleOps);
                    Type type  = (typeCode == TypeCode.Decimal) ? typeof(decimal) : typeof(double);
                    return(new DynamicMetaObject(Expression.Call(type2.GetMethod("BNot", BindingFlags.NonPublic | BindingFlags.Static), target.Expression.Convert(type)), target.PSGetTypeRestriction()));
                }
            }
            if ((expression == null) && (errorSuggestion == null))
            {
                return(PSConvertBinder.ThrowNoConversion(target, typeof(int), this, -1, new DynamicMetaObject[0]));
            }
            return(new DynamicMetaObject(Expression.OnesComplement(expression).Cast(typeof(object)), target.PSGetTypeRestriction()));
        }
示例#4
0
        private static Tuple <CallSite <Func <CallSite, object, IEnumerator> >, CallSite <Func <CallSite, object, object, object> > > GetContainsCallSites(bool ignoreCase)
        {
            CallSite <Func <CallSite, object, IEnumerator> > site = CallSite <Func <CallSite, object, IEnumerator> > .Create(PSEnumerableBinder.Get());

            CallSite <Func <CallSite, object, object, object> > site2 = CallSite <Func <CallSite, object, object, object> > .Create(PSBinaryOperationBinder.Get(ExpressionType.Equal, ignoreCase, true));

            return(Tuple.Create <CallSite <Func <CallSite, object, IEnumerator> >, CallSite <Func <CallSite, object, object, object> > >(site, site2));
        }
示例#5
0
        private static Func <object, object, object> GetCallSiteDelegate(ExpressionType expressionType, bool ignoreCase)
        {
            CallSite <Func <CallSite, object, object, object> > site = CallSite <Func <CallSite, object, object, object> > .Create(PSBinaryOperationBinder.Get(expressionType, ignoreCase, false));

            return((x, y) => site.Target(site, x, y));
        }
示例#6
0
        protected override void BeginProcessing()
        {
            Func <object, object, object> func9  = null;
            Func <object, object, object> func10 = null;
            Func <object, object, object> func11 = null;
            Func <object, object, object> func12 = null;
            Func <object, object, object> func13 = null;
            Func <object, object, object> func14 = null;
            Func <object, object, object> func15 = null;
            Func <object, object, object> func16 = null;
            Func <object, object, object> func17 = null;
            Func <object, object, object> func18 = null;

            if (this.script == null)
            {
                switch (this._binaryOperator)
                {
                case TokenKind.Ieq:
                    if (this._forceBooleanEvaluation)
                    {
                        CallSite <Func <CallSite, object, object, object> > site = CallSite <Func <CallSite, object, object, object> > .Create(PSBinaryOperationBinder.Get(ExpressionType.Equal, true, false));

                        this.operationDelegate = (x, y) => site.Target(site, y, x);
                        return;
                    }
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.Equal, true);
                    return;

                case TokenKind.Ine:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.NotEqual, true);
                    return;

                case TokenKind.Ige:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.GreaterThanOrEqual, true);
                    return;

                case TokenKind.Igt:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.GreaterThan, true);
                    return;

                case TokenKind.Ilt:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.LessThan, true);
                    return;

                case TokenKind.Ile:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.LessThanOrEqual, true);
                    return;

                case TokenKind.Ilike:
                    if (func9 == null)
                    {
                        func9 = (lval, rval) => ParserOps.LikeOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, false, true);
                    }
                    this.operationDelegate = func9;
                    return;

                case TokenKind.Inotlike:
                    if (func11 == null)
                    {
                        func11 = (lval, rval) => ParserOps.LikeOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, true, true);
                    }
                    this.operationDelegate = func11;
                    return;

                case TokenKind.Imatch:
                    this.CheckLanguageMode();
                    if (func13 == null)
                    {
                        func13 = (lval, rval) => ParserOps.MatchOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, false, true);
                    }
                    this.operationDelegate = func13;
                    return;

                case TokenKind.Inotmatch:
                    this.CheckLanguageMode();
                    if (func15 == null)
                    {
                        func15 = (lval, rval) => ParserOps.MatchOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, true, true);
                    }
                    this.operationDelegate = func15;
                    return;

                case TokenKind.Ireplace:
                case TokenKind.Iin:
                case TokenKind.Isplit:
                case TokenKind.Creplace:
                case TokenKind.Csplit:
                    return;

                case TokenKind.Icontains:
                case TokenKind.Inotcontains:
                case TokenKind.Inotin:
                case TokenKind.In:
                {
                    Func <object, object, object> func  = null;
                    Func <object, object, object> func2 = null;
                    Func <object, object, object> func3 = null;
                    Func <object, object, object> func4 = null;
                    Tuple <CallSite <Func <CallSite, object, IEnumerator> >, CallSite <Func <CallSite, object, object, object> > > sites = GetContainsCallSites(true);
                    switch (this._binaryOperator)
                    {
                    case TokenKind.Icontains:
                        if (func == null)
                        {
                            func = (lval, rval) => ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, lval, rval);
                        }
                        this.operationDelegate = func;
                        return;

                    case TokenKind.Inotcontains:
                        if (func2 == null)
                        {
                            func2 = (lval, rval) => !ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, lval, rval);
                        }
                        this.operationDelegate = func2;
                        return;

                    case TokenKind.Iin:
                        return;

                    case TokenKind.Inotin:
                        if (func4 == null)
                        {
                            func4 = (lval, rval) => !ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, rval, lval);
                        }
                        this.operationDelegate = func4;
                        return;

                    case TokenKind.In:
                        if (func3 == null)
                        {
                            func3 = (lval, rval) => ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, rval, lval);
                        }
                        this.operationDelegate = func3;
                        return;
                    }
                    return;
                }

                case TokenKind.Ceq:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.Equal, false);
                    return;

                case TokenKind.Cne:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.NotEqual, false);
                    return;

                case TokenKind.Cge:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.GreaterThanOrEqual, false);
                    return;

                case TokenKind.Cgt:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.GreaterThan, false);
                    return;

                case TokenKind.Clt:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.LessThan, false);
                    return;

                case TokenKind.Cle:
                    this.operationDelegate = GetCallSiteDelegate(ExpressionType.LessThanOrEqual, false);
                    return;

                case TokenKind.Clike:
                    if (func10 == null)
                    {
                        func10 = (lval, rval) => ParserOps.LikeOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, false, false);
                    }
                    this.operationDelegate = func10;
                    return;

                case TokenKind.Cnotlike:
                    if (func12 == null)
                    {
                        func12 = (lval, rval) => ParserOps.LikeOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, true, false);
                    }
                    this.operationDelegate = func12;
                    return;

                case TokenKind.Cmatch:
                    this.CheckLanguageMode();
                    if (func14 == null)
                    {
                        func14 = (lval, rval) => ParserOps.MatchOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, false, false);
                    }
                    this.operationDelegate = func14;
                    return;

                case TokenKind.Cnotmatch:
                    this.CheckLanguageMode();
                    if (func16 == null)
                    {
                        func16 = (lval, rval) => ParserOps.MatchOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval, true, false);
                    }
                    this.operationDelegate = func16;
                    return;

                case TokenKind.Ccontains:
                case TokenKind.Cnotcontains:
                case TokenKind.Cin:
                case TokenKind.Cnotin:
                {
                    Func <object, object, object> func5 = null;
                    Func <object, object, object> func6 = null;
                    Func <object, object, object> func7 = null;
                    Func <object, object, object> func8 = null;
                    Tuple <CallSite <Func <CallSite, object, IEnumerator> >, CallSite <Func <CallSite, object, object, object> > > sites = GetContainsCallSites(false);
                    switch (this._binaryOperator)
                    {
                    case TokenKind.Ccontains:
                        if (func5 == null)
                        {
                            func5 = (lval, rval) => ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, lval, rval);
                        }
                        this.operationDelegate = func5;
                        return;

                    case TokenKind.Cnotcontains:
                        if (func6 == null)
                        {
                            func6 = (lval, rval) => !ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, lval, rval);
                        }
                        this.operationDelegate = func6;
                        return;

                    case TokenKind.Cin:
                        if (func7 == null)
                        {
                            func7 = (lval, rval) => ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, rval, lval);
                        }
                        this.operationDelegate = func7;
                        return;

                    case TokenKind.Cnotin:
                        if (func8 == null)
                        {
                            func8 = (lval, rval) => !ParserOps.ContainsOperatorCompiled(this.Context, sites.Item1, sites.Item2, rval, lval);
                        }
                        this.operationDelegate = func8;
                        return;
                    }
                    return;
                }

                case TokenKind.Is:
                    if (func17 == null)
                    {
                        func17 = (lval, rval) => ParserOps.IsOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval);
                    }
                    this.operationDelegate = func17;
                    return;

                case TokenKind.IsNot:
                    if (func18 == null)
                    {
                        func18 = (lval, rval) => ParserOps.IsNotOperator(base.Context, PositionUtilities.EmptyExtent, lval, rval);
                    }
                    this.operationDelegate = func18;
                    return;
                }
            }
        }