IronPython.Compiler.Ast.ForStatement.TransformFor C# (CSharp) Метод

TransformFor() статический приватный Метод

static private TransformFor ( ScopeStatement parent, System.Linq.Expressions enumerator, Expression list, Expression left, System.Linq.Expressions body, Statement else_, Microsoft.Scripting.SourceSpan span, Microsoft.Scripting.SourceLocation header, System.Linq.Expressions breakLabel, System.Linq.Expressions continueLabel, bool isStatement ) : Expression
parent ScopeStatement
enumerator System.Linq.Expressions
list Expression
left Expression
body System.Linq.Expressions
else_ Statement
span Microsoft.Scripting.SourceSpan
header Microsoft.Scripting.SourceLocation
breakLabel System.Linq.Expressions
continueLabel System.Linq.Expressions
isStatement bool
Результат System.Linq.Expressions.Expression
        internal static MSAst.Expression TransformFor(ScopeStatement parent, MSAst.ParameterExpression enumerator,
                                                    Expression list, Expression left, MSAst.Expression body,
                                                    Statement else_, SourceSpan span, SourceLocation header,
                                                    MSAst.LabelTarget breakLabel, MSAst.LabelTarget continueLabel, bool isStatement) {
            // enumerator, isDisposable = Dynamic(GetEnumeratorBinder, list)
            MSAst.Expression init = Ast.Assign(
                    enumerator,
                    new PythonDynamicExpression1<KeyValuePair<IEnumerator, IDisposable>>(
                        Binders.UnaryOperationBinder(
                            parent.GlobalParent.PyContext,
                            PythonOperationKind.GetEnumeratorForIteration
                        ), 
                        parent.GlobalParent.CompilationMode, 
                        AstUtils.Convert(list, typeof(object))
                    )
                );

            // while enumerator.MoveNext():
            //    left = enumerator.Current
            //    body
            // else:
            //    else
            MSAst.Expression ls = AstUtils.Loop(
                    parent.GlobalParent.AddDebugInfo(
                        Ast.Call(
                            Ast.Property(
                                enumerator,
                                typeof(KeyValuePair<IEnumerator, IDisposable>).GetProperty("Key")
                            ),
                            typeof(IEnumerator).GetMethod("MoveNext")
                        ),
                        left.Span
                    ),
                    null,
                    Ast.Block(
                        left.TransformSet(
                            SourceSpan.None,
                            Ast.Call(
                                Ast.Property(
                                    enumerator,
                                    typeof(KeyValuePair<IEnumerator, IDisposable>).GetProperty("Key")
                                ),
                                typeof(IEnumerator).GetProperty("Current").GetGetMethod()
                            ),
                            PythonOperationKind.None
                        ),
                        body,
                        isStatement ? UpdateLineNumber(parent.GlobalParent.IndexToLocation(list.StartIndex).Line) : AstUtils.Empty(),
                        AstUtils.Empty()
                    ),
                    else_,
                    breakLabel,
                    continueLabel
            );

            return Ast.Block(
                init,
                Ast.TryFinally(
                    ls,
                    Ast.Call(AstMethods.ForLoopDispose, enumerator)
                )
            );
        }

Usage Example

Пример #1
0
        internal override MSAst.Expression Transform(MSAst.Expression body)
        {
            MSAst.ParameterExpression temp = Ast.Parameter(typeof(KeyValuePair <IEnumerator, IDisposable>), "list_comprehension_for");

            return(Ast.Block(
                       new[] { temp },
                       ForStatement.TransformFor(Parent, temp, _list, _lhs, body, null, Span, GlobalParent.IndexToLocation(_lhs.EndIndex), null, null, false)
                       ));
        }