/
statements.go
378 lines (308 loc) · 10.8 KB
/
statements.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2015 The Serulian Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package codedom
import (
"fmt"
"github.com/serulian/compiler/compilergraph"
"github.com/serulian/compiler/graphs/scopegraph"
"github.com/serulian/compiler/graphs/typegraph"
)
var _ = fmt.Printf
// YieldNode represents a yield of some sort under a generator function.
type YieldNode struct {
nextStatementBase
Value Expression // The value yielded, if any.
StreamValue Expression // The stream yielded, if any.
StreamType *typegraph.TypeReference // The type of the stream being yielded from, if any.
}
func YieldValue(value Expression, basis compilergraph.GraphNode) Statement {
return &YieldNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
value,
nil,
nil,
}
}
func YieldStream(streamValue Expression, streamType typegraph.TypeReference, basis compilergraph.GraphNode) Statement {
return &YieldNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
nil,
streamValue,
&streamType,
}
}
func YieldBreak(basis compilergraph.GraphNode) Statement {
return &YieldNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
nil,
nil,
nil,
}
}
func (yn *YieldNode) ReleasesFlow() bool { return true }
func (s *YieldNode) WalkStatements(walker statementWalker) bool {
return walker(s) && s.nextStatementBase.WalkNextStatements(walker)
}
func (s *YieldNode) WalkExpressions(walker expressionWalker) bool {
if s.Value != nil {
return walker(s.Value)
}
if s.StreamValue != nil {
return walker(s.StreamValue)
}
return true
}
func (s *YieldNode) IsLocallyAsynchronous(sg *scopegraph.ScopeGraph) bool {
// A yield statement is locally asynchronous if the Next() call of its stream *may*
// be asynchronous.
if s.StreamValue != nil {
referencedMember, _ := s.StreamType.ResolveMember("Next", typegraph.MemberResolutionInstance)
return sg.IsPromisingMember(referencedMember, scopegraph.PromisingAccessFunctionCall)
}
return false
}
// ResolutionNode represents the resolution of a function.
type ResolutionNode struct {
statementBase
Value Expression // The value resolved, if any.
}
func Resolution(value Expression, basis compilergraph.GraphNode) Statement {
return &ResolutionNode{
statementBase{domBase{basis}, false},
value,
}
}
func (s *ResolutionNode) WalkExpressions(walker expressionWalker) bool {
if s.Value != nil {
return walker(s.Value)
}
return true
}
func (s *ResolutionNode) WalkStatements(walker statementWalker) bool {
return walker(s)
}
// RejectionNode represents the rejection of a function.
type RejectionNode struct {
statementBase
Value Expression // The value rejected, if any.
}
func Rejection(value Expression, basis compilergraph.GraphNode) Statement {
return &RejectionNode{
statementBase{domBase{basis}, false},
value,
}
}
func (s *RejectionNode) WalkStatements(walker statementWalker) bool {
return walker(s)
}
func (s *RejectionNode) WalkExpressions(walker expressionWalker) bool {
return walker(s.Value)
}
// ExpressionStatementNode represents a statement of a single expression being executed.
type ExpressionStatementNode struct {
nextStatementBase
Expression Expression // The expression being executed.
}
func ExpressionStatement(expression Expression, basis compilergraph.GraphNode) Statement {
return &ExpressionStatementNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
expression,
}
}
func (s *ExpressionStatementNode) WalkStatements(walker statementWalker) bool {
return walker(s) && s.nextStatementBase.WalkNextStatements(walker)
}
func (s *ExpressionStatementNode) WalkExpressions(walker expressionWalker) bool {
return walker(s.Expression)
}
// EmptyStatementNode represents an empty statement. Typically used as a the target of jumps.
type EmptyStatementNode struct {
nextStatementBase
}
func EmptyStatement(basis compilergraph.GraphNode) Statement {
return &EmptyStatementNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
}
}
func (s *EmptyStatementNode) WalkStatements(walker statementWalker) bool {
return walker(s) && s.nextStatementBase.WalkNextStatements(walker)
}
// VarDefinitionNode represents a variable defined in the scope.
type VarDefinitionNode struct {
nextStatementBase
Name string // The name of the variable.
Initializer Expression // The initializer expression, if any.
}
func VarDefinition(name string, basis compilergraph.GraphNode) Statement {
return &VarDefinitionNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
name,
nil,
}
}
func VarDefinitionWithInit(name string, initializer Expression, basis compilergraph.GraphNode) Statement {
return &VarDefinitionNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
name,
initializer,
}
}
func (s *VarDefinitionNode) WalkStatements(walker statementWalker) bool {
return walker(s) && s.nextStatementBase.WalkNextStatements(walker)
}
func (s *VarDefinitionNode) WalkExpressions(walker expressionWalker) bool {
if s.Initializer != nil {
return walker(s.Initializer)
}
return true
}
// ResourceBlockNode represents a resource placed on the resource stack for the duration of
// a statement call.
type ResourceBlockNode struct {
nextStatementBase
ResourceName string // The name for the resource.
Resource Expression // The resource itself.
Statement Statement // The statement to execute with the resource.
ReleaseMethod typegraph.TGMember // The Release() for the resource.
}
func ResourceBlock(resourceName string, resource Expression, statement Statement, releaseMethod typegraph.TGMember, basis compilergraph.GraphNode) Statement {
return &ResourceBlockNode{
nextStatementBase{statementBase{domBase{basis}, false}, nil},
resourceName,
resource,
statement,
releaseMethod,
}
}
// HasAsyncRelease returns true if the Release() call on the resource managed by this block is async.
func (s *ResourceBlockNode) HasAsyncRelease(sg *scopegraph.ScopeGraph) bool {
return sg.IsPromisingMember(s.ReleaseMethod, scopegraph.PromisingAccessFunctionCall)
}
func (s *ResourceBlockNode) IsLocallyAsynchronous(scopegraph *scopegraph.ScopeGraph) bool {
return s.HasAsyncRelease(scopegraph)
}
func (s *ResourceBlockNode) WalkExpressions(walker expressionWalker) bool {
return walker(s.Resource)
}
func (s *ResourceBlockNode) WalkStatements(walker statementWalker) bool {
return walker(s) && walker(s.Statement) && s.nextStatementBase.WalkNextStatements(walker)
}
// ConditionalJumpNode represents a jump to a true statement if an expression is true, and
// otherwise to a false statement.
type ConditionalJumpNode struct {
statementBase
True Statement // The statement executed if the branch is true.
False Statement // The statement executed if the branch is false.
BranchExpression Expression // The expression for branching. Must be a Boolean expression.
}
func (j *ConditionalJumpNode) IsJump() bool { return true }
func BranchOn(branchExpression Expression, basis compilergraph.GraphNode) *ConditionalJumpNode {
return &ConditionalJumpNode{
statementBase{domBase{basis}, false},
nil,
nil,
branchExpression,
}
}
func ConditionalJump(branchExpression Expression, trueTarget Statement, falseTarget Statement, basis compilergraph.GraphNode) Statement {
return &ConditionalJumpNode{
statementBase{domBase{basis}, false},
trueTarget,
falseTarget,
branchExpression,
}
}
func (s *ConditionalJumpNode) WalkExpressions(walker expressionWalker) bool {
return walker(s.BranchExpression)
}
func (s *ConditionalJumpNode) WalkStatements(walker statementWalker) bool {
if !walker(s) {
return false
}
if s.True != nil && !walker(s.True) {
return false
}
if s.False != nil && !walker(s.False) {
return false
}
return true
}
// UnconditionalJumpNode represents a jump to another statement.
type UnconditionalJumpNode struct {
statementBase
Target Statement // The target statement.
}
func (j *UnconditionalJumpNode) IsJump() bool { return true }
func UnconditionalJump(target Statement, basis compilergraph.GraphNode) Statement {
return &UnconditionalJumpNode{
statementBase{domBase{basis}, false},
target,
}
}
func (s *UnconditionalJumpNode) WalkStatements(walker statementWalker) bool {
return walker(s) && walker(s.Target)
}
// ArrowPromiseNode represents a wait on a promise expression and assignment to a
// resolution expression and/or rejection expression once the promise returns.
type ArrowPromiseNode struct {
statementBase
ChildExpression Expression // The child expression containing the promise.
ResolutionAssignment Expression // Expression assigning the resolution value, if any.
RejectionAssignment Expression // Expression assigning the rejection value, if any.
Target Statement // The statement to which the await will jump.
}
func ArrowPromise(childExpr Expression, resolution Expression, rejection Expression, targetState Statement, basis compilergraph.GraphNode) Statement {
return &ArrowPromiseNode{
statementBase{domBase{basis}, false},
childExpr,
resolution,
rejection,
targetState,
}
}
func (s *ArrowPromiseNode) IsLocallyAsynchronous(scopegraph *scopegraph.ScopeGraph) bool {
// Always async.
return true
}
func (j *ArrowPromiseNode) IsJump() bool { return true }
func (s *ArrowPromiseNode) WalkExpressions(walker expressionWalker) bool {
if !walker(s.ChildExpression) {
return false
}
if s.ResolutionAssignment != nil && !walker(s.ResolutionAssignment) {
return false
}
if s.RejectionAssignment != nil && !walker(s.RejectionAssignment) {
return false
}
return true
}
func (s *ArrowPromiseNode) WalkStatements(walker statementWalker) bool {
return walker(s) && walker(s.Target)
}
// ResolveExpressionNode represents a resolution of an arbitrary expression, with assignment
// to either a resolved value or a rejected value.
type ResolveExpressionNode struct {
statementBase
ChildExpression Expression // The child expression to be executed.
ResolutionName string // Variable to which the resolution value is assigned, if any.
RejectionName string // Variable to which the rejection value is assigned, if any.
Target Statement // The statement to which the resolve will jump.
}
func ResolveExpression(childExpr Expression, resolutionName string, rejectionName string, targetState Statement, basis compilergraph.GraphNode) Statement {
return &ResolveExpressionNode{
statementBase{domBase{basis}, false},
childExpr,
resolutionName,
rejectionName,
targetState,
}
}
func (j *ResolveExpressionNode) IsJump() bool { return true }
func (s *ResolveExpressionNode) WalkExpressions(walker expressionWalker) bool {
return walker(s.ChildExpression)
}
func (s *ResolveExpressionNode) WalkStatements(walker statementWalker) bool {
return walker(s) && walker(s.Target)
}