Example #1
0
 public Expression SetValue(Compiler compiler, Expression rhs)
 {
     ParameterExpression left = Expression.Variable(rhs.Type);
     int count = this.ArrayLiteral.Elements.Count;
     List<Expression> expressions = new List<Expression> {
         Expression.Assign(left, rhs)
     };
     for (int i = 0; i < count; i++)
     {
         Expression expression2 = Expression.Call(left, CachedReflectionInfo.IList_get_Item, new Expression[] { ExpressionCache.Constant(i) });
         ExpressionAst ast = this.ArrayLiteral.Elements[i];
         ArrayLiteralAst pureExpression = ast as ArrayLiteralAst;
         if (ast is ParenExpressionAst)
         {
             pureExpression = ((ParenExpressionAst) ast).Pipeline.GetPureExpression() as ArrayLiteralAst;
         }
         if (pureExpression != null)
         {
             expression2 = Expression.Dynamic(PSArrayAssignmentRHSBinder.Get(pureExpression.Elements.Count), typeof(IList), expression2);
         }
         expressions.Add(compiler.ReduceAssignment((ISupportsAssignment) ast, TokenKind.Equals, expression2));
     }
     expressions.Add(left);
     return Expression.Block(new ParameterExpression[] { left }, expressions);
 }
Example #2
0
        public Expression SetValue(Compiler compiler, Expression rhs)
        {
            Diagnostics.Assert(rhs.Type == typeof(IList), "Code generation must ensure we have an IList to assign from.");
            var rhsTemp = Expression.Variable(rhs.Type);

            int count = ArrayLiteral.Elements.Count;
            var exprs = new List<Expression>();
            exprs.Add(Expression.Assign(rhsTemp, rhs));
            for (int i = 0; i < count; ++i)
            {
                Expression indexedRHS = Expression.Call(rhsTemp, CachedReflectionInfo.IList_get_Item, ExpressionCache.Constant(i));
                var lhsElement = ArrayLiteral.Elements[i];
                var nestedArrayLHS = lhsElement as ArrayLiteralAst;
                var parenExpressionAst = lhsElement as ParenExpressionAst;
                if (parenExpressionAst != null)
                {
                    nestedArrayLHS = parenExpressionAst.Pipeline.GetPureExpression() as ArrayLiteralAst;
                }

                if (nestedArrayLHS != null)
                {
                    // Need to turn the rhs into an IList
                    indexedRHS = DynamicExpression.Dynamic(PSArrayAssignmentRHSBinder.Get(nestedArrayLHS.Elements.Count), typeof(IList),
                                                           indexedRHS);
                }

                exprs.Add(compiler.ReduceAssignment((ISupportsAssignment)lhsElement,
                                                    TokenKind.Equals, indexedRHS));
            }
            // Add the temp as the last expression for chained assignment, i.e. $x = $y,$z = 1,2
            exprs.Add(rhsTemp);
            return Expression.Block(new[] { rhsTemp }, exprs);
        }