Пример #1
0
 protected void OutputForLoop(MethodCallExpression expr)
 {
     var indexVar = expr.Method.IsSelect (_linqType) ?
         expr.GetSelectLambda ().Parameters[0] :
         expr.Arguments[2].ExpectLambda ().Parameters[1];
     var range = expr.Arguments[0].Expect<MethodCallExpression> (ExpressionType.Call);
     var start = Expr (range.Arguments[0]);
     if (range.Method.DeclaringType == typeof (Enumerable))
     {
         var len = Expr (range.Arguments[1]);
         CodeOut ("for (int {0} = {1}; {0} < {2}; {0}++)", indexVar, start, len);
     }
     else
     {
         var end = Expr (range.Arguments[1]);
         var step = Expr (range.Arguments[2]);
         CodeOut ("for (int {0} = {1}; {0} != {2}; {0} += {3})", indexVar, start, end, step);
     }
     CodeOut ("{");
     _tabLevel++;
 }
Пример #2
0
 protected void IterateArray(MethodCallExpression expr)
 {
     var array = expr.Arguments[0];
     var member  = array.SkipUnary (ExpressionType.Not)
         .Expect<MemberExpression> (ExpressionType.MemberAccess).Member;
     var len = 0;
     var field = member as FieldInfo;
     if (field != null)
         len = field.ExpectFixedArrayAttribute ().Length;
     else if (_constants.ContainsKey (member.Name))
     {
         var constant = _constants[member.Name];
         if (!constant.Type.IsArray)
             throw new ParseException ("Invalid array expression. Referenced constant is not an array.");
         var nai = constant.Value.Expect<NewArrayExpression> (ExpressionType.NewArrayInit);
         len = nai.Expressions.Count;
     }
     else
         throw new ParseException ("Invalid array expression. " +
             "Expected uniform field reference or constant array. Encountered: " + array);
     var indexVar = NewIndexVar ("ind");
     var item = expr.Method.IsSelect (_linqType) ?
         expr.GetSelectLambda ().Parameters[0] :
         expr.Arguments[2].ExpectLambda ().Parameters[1];
     CodeOut ("for (int {0} = 0; {0} < {1}; {0}++)", indexVar, len);
     CodeOut ("{");
     _tabLevel++;
     CodeOut ("{0} {1} = {2}[{3}];", MapType (item.Type), item.Name,
         Expr (array), indexVar);
 }