forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncForStatement.cs
More file actions
130 lines (102 loc) · 4.82 KB
/
AsyncForStatement.cs
File metadata and controls
130 lines (102 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Threading;
using IronPython.Runtime.Binding;
using IronPython.Runtime.Exceptions;
using MSAst = System.Linq.Expressions;
namespace IronPython.Compiler.Ast {
/// <summary>
/// Represents an async for statement.
/// Desugared to Python AST that uses __aiter__ and await __anext__().
/// </summary>
public class AsyncForStatement : Statement, ILoopStatement {
private static int _counter;
private Statement? _desugared;
public AsyncForStatement(Expression left, Expression list, Statement body, Statement? @else) {
Left = left;
List = list;
Body = body;
Else = @else;
}
public int HeaderIndex { private get; set; }
public Expression Left { get; }
public Expression List { get; set; }
public Statement Body { get; set; }
public Statement? Else { get; }
MSAst.LabelTarget ILoopStatement.BreakLabel { get; set; } = null!;
MSAst.LabelTarget ILoopStatement.ContinueLabel { get; set; } = null!;
/// <summary>
/// Build the desugared tree. Called during Walk when Parent and IndexSpan are available.
/// </summary>
private Statement BuildDesugared() {
var parent = Parent;
var span = IndexSpan;
var id = Interlocked.Increment(ref _counter);
// async for TARGET in ITER:
// BLOCK
// else:
// ELSE_BLOCK
//
// desugars to:
//
// __aiter = ITER.__aiter__()
// __running = True
// while __running:
// try:
// TARGET = await __aiter.__anext__()
// except StopAsyncIteration:
// __running = False
// else:
// BLOCK
// else:
// ELSE_BLOCK
var iterName = $"__asyncfor_iter{id}";
var runningName = $"__asyncfor_running{id}";
// Helper to assign proper parent and span to nodes
T SetScope<T>(T node) where T : Node {
node.Parent = parent;
node.IndexSpan = span;
return node;
}
// _iter = ITER.__aiter__()
var aiterCall = SetScope(new UnaryExpression(PythonOperationKind.AIter, List));
var assignIter = SetScope(new AssignmentStatement([SetScope(new NameExpression(iterName))], aiterCall));
// running = True
var trueConst = SetScope(new ConstantExpression(true));
var assignRunning = SetScope(new AssignmentStatement([SetScope(new NameExpression(runningName))], trueConst));
// TARGET = await __aiter.__anext__()
var anextCall = SetScope(new UnaryExpression(PythonOperationKind.ANext, SetScope(new NameExpression(iterName))));
var awaitNext = new AwaitExpression(anextCall);
var assignTarget = SetScope(new AssignmentStatement([Left], awaitNext));
// except StopAsyncIteration: __running = False
var falseConst = SetScope(new ConstantExpression(false));
var stopRunning = SetScope(new AssignmentStatement([SetScope(new NameExpression(runningName))], falseConst));
var handler = SetScope(new TryStatementHandler(SetScope(new NameExpression(nameof(PythonExceptions.StopAsyncIteration))), null!, SetScope(new SuiteStatement([stopRunning]))));
handler.HeaderIndex = span.End;
// try/except/else block
var tryExcept = SetScope(new TryStatement(assignTarget, [handler], SetScope(new SuiteStatement([Body])), null));
tryExcept.HeaderIndex = span.End;
// while __running: try/except/else
var whileStmt = new WhileStatement(SetScope(new NameExpression(runningName)), tryExcept, Else);
whileStmt.SetLoc(GlobalParent, span.Start, span.End, span.End);
whileStmt.Parent = parent;
return SetScope(new SuiteStatement([assignIter, assignRunning, whileStmt]));
}
public override MSAst.Expression Reduce() {
return _desugared!.Reduce();
}
public override void Walk(PythonWalker walker) {
if (walker.Walk(this)) {
// Build the desugared tree on first walk (when Parent and IndexSpan are set)
if (_desugared == null) {
_desugared = BuildDesugared();
}
_desugared.Walk(walker);
}
walker.PostWalk(this);
}
internal override bool CanThrow => true;
}
}