Exemplo n.º 1
0
        protected object AssignIndexer(AssignmentExpression node, object value)
        {
            var exp = (IndexExpression)node.Left;
            var obj = exp.Target.Accept(this);

            if (obj == null)
            {
                ExecutionException.ThrowNullError(exp.Target, node);
            }
            var args = exp.Arguments.Map(arg => arg.Accept(this)).AddLast(value);

            if (exp.Setter is null)
            {
                var indexer = exp.Target.Type
                              .FindSetIndexer(args, out ArgumentConversions conversions);
                if (indexer is null)
                {
                    ExecutionException.ThrowMissingIndexer(exp.Target.Type, "set", exp.Target, node);
                }

                exp.Conversions = conversions;
                exp.Setter      = indexer;
                // ok to be node.Right.Type instead of indexer.GetParameters().Last().ParameterType
                var valueBind = conversions[args.Length - 1];
                node.Type = (valueBind == null) ? node.Right.Type : valueBind.Type;
            }
            exp.Conversions.Invoke(ref args);
            SafeInvoke(exp, exp.Setter, obj, args);
            return(value);
        }
Exemplo n.º 2
0
        static object Invoke(BinaryExpression node, string opName, object left, object right)
        {
            if (left is null)
            {
                ExecutionException.ThrowNullError(node.Left, node);
            }
            if (right is null)
            {
                ExecutionException.ThrowNullError(node.Right, node);
            }

            if (node.Method is null)
            {
                var leftType  = left.GetType();
                var rightType = right.GetType();
                var types     = new Type[2] {
                    leftType, rightType
                };
                ArgumentConversions          conversions = new ArgumentConversions(2);
                System.Reflection.MethodInfo method;
                if (leftType.IsPrimitive || rightType.IsPrimitive)
                {
                    var initial = (ReflectionUtils.FromSystemType(ref types));
                    method = ReflectionUtils.
                             GetOperatorOverload(opName, conversions, types);
                    conversions.SetInitial(initial);
                }
                else
                {
                    method = ReflectionUtils.
                             GetOperatorOverload(opName, conversions, types);
                }
                if (method is null)
                {
                    ExecutionException.ThrowInvalidOp(node);
                }
                node.Method      = method;
                node.Type        = method.ReturnType;
                node.Conversions = conversions;
            }
            object[] args = new object[2] {
                left, right
            };
            node.Conversions.Invoke(ref args);
            // operator overload invoke
            return(node.Method.Invoke(null, System.Reflection.BindingFlags.Default, null, args, null));
        }
Exemplo n.º 3
0
        object IExpressionVisitor <object> .VisitIndex(IndexExpression node)
        {
            var obj = node.Target.Accept(this);

            if (obj is null)
            {
                ExecutionException.ThrowNullError(node);
            }
            var args = node.Arguments.Map(arg => arg.Accept(this));

            if (node.Getter is null)
            {
                ResolveIndexer(node, args);
            }
            node.Conversions.Invoke(ref args);
            return(SafeInvoke(node, node.Getter, obj, args));
        }
Exemplo n.º 4
0
        private object VisitExponentiation(BinaryExpression node)
        {
            var left  = node.Left.Accept(this);
            var right = node.Right.Accept(this);

            if (left is null)
            {
                ExecutionException.ThrowNullError(node, node.Left);
            }
            if (right is null)
            {
                ExecutionException.ThrowNullError(node, node.Right);
            }
            object[] args = new object[2] {
                left, right
            };
            if (node.Method is null)
            {
                Type leftType    = left.GetType();
                Type rightType   = right.GetType();
                var  types       = new Type[] { leftType, rightType };
                var  conversions = new ArgumentConversions(2);
                if (leftType.IsPrimitive || rightType.IsPrimitive)
                {
                    conversions.SetInitial(ReflectionUtils.FromSystemType(ref types));
                }
                if (!ReflectionHelpers.MathPow.MatchesArgumentTypes(types, conversions))
                {
                    ExecutionException.ThrowArgumentMisMatch(node);
                }
                node.Conversions = conversions;
                node.Method      = ReflectionHelpers.MathPow;
                node.Type        = TypeProvider.DoubleType;
            }
            node.Conversions.Invoke(ref args);
            return(node.Method.Invoke(null, args));
        }
Exemplo n.º 5
0
        public object VisitUnary(UnaryExpression node)
        {
            var value = node.Operand.Accept(this);

            if (node.NodeType == ExpressionType.Parenthesized)
            {
                node.Type = node.Operand.Type;
                return(value);
            }
            //modified a++; updated new value
            bool modified = false, updated = true;

            switch (node.NodeType)
            {
            case ExpressionType.PostfixPlusPlus:
                modified = true;
                updated  = false;
                break;

            case ExpressionType.PrefixPlusPlus:
                modified = true;
                break;

            case ExpressionType.PostfixMinusMinus:
                modified = true;
                updated  = false;
                break;

            case ExpressionType.PrefixMinusMinus:
                modified = true;
                break;

            case ExpressionType.Bang:
                // here value is null it is as not defined
                if (value is null)
                {
                    return(Boolean.True);
                }
                break;
            }
            if (value is null)
            {
                ExecutionException.ThrowNullError(node.Operand, node);
            }
            // no primitive supported it should be wrapped

            //resolve call
            if (node.Method is null)
            {
                Type type = node.Operand.Type;
                ArgumentConversions          conversions = new ArgumentConversions(1);
                System.Reflection.MethodInfo method;
                if (type.IsPrimitive)
                {
                    type   = TypeProvider.Find(Type.GetTypeCode(type));
                    method = ReflectionUtils.GetOperatorOverload(node.MethodName, conversions, type);
                    conversions.AddFirst(new ParamConversion(0, ReflectionHelpers.ToAny));
                }
                else
                {
                    method = ReflectionUtils.GetOperatorOverload(node.MethodName, conversions, type);
                }
                if (method is null)
                {
                    ExecutionException.ThrowInvalidOp(node);
                }
                node.Conversions = conversions;
                node.Method      = method;
                node.Type        = method.ReturnType;
            }
            object[] args = new object[1] {
                value
            };
            node.Conversions.Invoke(ref args);
            object obj = node.Method.Invoke(null, args);

            if (modified)
            {
                if (node.Operand.NodeType == ExpressionType.Literal)
                {
                    ExecutionException.ThrowNotSupported(node);
                }
                var exp = new AssignmentExpression(node.Operand, new LiteralExpression(obj));
                exp.Accept(this);
            }
            return(updated ? obj : value);
        }