forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_statement.go
453 lines (386 loc) · 8.42 KB
/
node_statement.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package otto
import (
"fmt"
)
type _blockNode struct {
_nodeType
_node_
Body []_node
labelSet _labelSet
}
func newBlockNode() *_blockNode {
return &_blockNode{
_nodeType: nodeBlock,
labelSet: _labelSet{},
}
}
func (self _blockNode) String() string {
if len(self.Body) == 0 {
return "{}"
}
return fmtNodeString("{ %s }", self.Body)
}
type _breakNode struct {
_nodeType
_node_
Target string
}
func newBreakNode(target string) *_breakNode {
return &_breakNode{
_nodeType: nodeBreak,
Target: target,
}
}
func (self _breakNode) String() string {
return fmt.Sprintf("<break:%s>", self.Target)
}
type _continueNode struct {
_nodeType
_node_
Target string
}
func newContinueNode(target string) *_continueNode {
return &_continueNode{
_nodeType: nodeContinue,
Target: target,
}
}
func (self _continueNode) String() string {
return fmt.Sprintf("<continue:%s>", self.Target)
}
type _emptyNode struct {
_nodeType
_node_
}
func newEmptyNode() *_emptyNode {
return &_emptyNode{
_nodeType: nodeEmpty,
}
}
func (self _emptyNode) String() string {
return ";"
}
type _iteratorNode struct {
body []_node
}
func (self _iteratorNode) String() string {
if len(self.body) == 0 {
return "{}"
}
return fmtNodeString("{ %s }", self.body)
}
type _doWhileNode struct {
_nodeType
_node_
_iteratorNode
Test _node
labelSet _labelSet
}
func newDoWhileNode(test _node, body []_node) *_doWhileNode {
self := &_doWhileNode{
_nodeType: nodeDoWhile,
Test: test,
_iteratorNode: _iteratorNode{
body: body,
},
labelSet: _labelSet{},
}
return self
}
func (self _doWhileNode) String() string {
return fmtNodeString("{ <%s> %s %s }", self.labelSet.label("do-while"), self.Test, self._iteratorNode)
}
type _forNode struct {
_nodeType
_node_
_iteratorNode
Initial _node
Test _node
Update _node
labelSet _labelSet
}
func newForNode(initial _node, test _node, update _node, body []_node) *_forNode {
self := &_forNode{
_nodeType: nodeFor,
Initial: initial,
Test: test,
Update: update,
_iteratorNode: _iteratorNode{
body: body,
},
labelSet: _labelSet{},
}
return self
}
func (self _forNode) String() string {
return fmtNodeString("{ <%s> %s %s %s %s }", self.labelSet.label("for"),
fmtNodeString(self.Initial, ";"),
fmtNodeString(self.Test, ";"),
fmtNodeString(self.Update, ";"),
self._iteratorNode,
)
}
type _forInNode struct {
_nodeType
_node_
_iteratorNode
Into _node
Source _node
labelSet _labelSet
}
func newForInNode(into _node, source _node, body []_node) *_forInNode {
self := &_forInNode{
_nodeType: nodeForIn,
Into: into,
Source: source,
_iteratorNode: _iteratorNode{
body: body,
},
labelSet: _labelSet{},
}
return self
}
func (self _forInNode) String() string {
return fmtNodeString("{ <%s> %s in %s %s }", self.labelSet.label("for-in"),
self.Into,
self.Source,
self._iteratorNode,
)
}
type _whileNode struct {
_nodeType
_node_
_iteratorNode
Test _node
labelSet _labelSet
}
func newWhileNode(test _node, body []_node) *_whileNode {
self := &_whileNode{
_nodeType: nodeWhile,
Test: test,
_iteratorNode: _iteratorNode{
body: body,
},
labelSet: _labelSet{},
}
return self
}
func (self _whileNode) String() string {
return fmtNodeString("{ <%s> %s %s }", self.labelSet.label("while"), self.Test, self._iteratorNode)
}
type _ifNode struct {
_nodeType
_node_
Test _node
Consequent _node
Alternate _node
}
func newIfNode(test _node, consequent _node) *_ifNode {
return &_ifNode{
_nodeType: nodeIf,
Test: test,
Consequent: consequent,
}
}
func newIfElseNode(test _node, consequent _node, alternate _node) *_ifNode {
return &_ifNode{
Test: test,
Consequent: consequent,
Alternate: alternate,
}
}
func (self _ifNode) String() string {
if self.Alternate != nil {
return fmtNodeString("{ <if> %s %s %s }", self.Test, self.Consequent, self.Alternate)
}
return fmtNodeString("{ <if> %s %s }", self.Test, self.Consequent)
}
type _programNode struct {
_nodeType
_node_
Body []_node
VariableList []_declaration
FunctionList []_declaration
}
func newProgramNode() *_programNode {
return &_programNode{
_nodeType: nodeProgram,
}
}
func (self _programNode) String() string {
return fmtNodeString("{ @ %s }", self.Body)
}
func (self _programNode) toFunction(parameterList []string) *_functionNode {
node := newFunctionNode()
node.ParameterList = parameterList
node.Body = self.Body
node.VariableList = self.VariableList
node.FunctionList = self.FunctionList
return node
}
type _returnNode struct {
_nodeType
_node_
Argument _node
}
func newReturnNode() *_returnNode {
return &_returnNode{
_nodeType: nodeReturn,
}
}
func (self _returnNode) String() string {
if self.Argument != nil {
return fmt.Sprintf("{ <return> %s }", self.Argument)
}
return fmt.Sprintf("{ <return> }")
}
type _switchNode struct {
_nodeType
_node_
Discriminant _node
Default int
CaseList [](*_caseNode)
labelSet _labelSet
}
func newSwitchNode(discriminant _node) *_switchNode {
return &_switchNode{
_nodeType: nodeSwitch,
Discriminant: discriminant,
Default: -1,
labelSet: _labelSet{},
}
}
func (self *_switchNode) AddCase(_node *_caseNode) {
self.CaseList = append(self.CaseList, _node)
}
func (self _switchNode) String() string {
return fmtNodeString("{ <switch> %s %s }", self.Discriminant, self.CaseList)
}
type _caseNode struct {
_nodeType
_node_
Test _node
Body []_node
}
func newCaseNode(test _node) *_caseNode {
return &_caseNode{
_nodeType: nodeCase,
Test: test,
}
}
func newDefaultCaseNode() *_caseNode {
return &_caseNode{}
}
func (self _caseNode) String() string {
if self.Test != nil {
return fmtNodeString("{ <case> %s %s }", self.Test, self.Body)
}
return fmtNodeString("{ <default> %s }", self.Body)
}
type _throwNode struct {
_nodeType
_node_
Argument _node
}
func newThrowNode(argument _node) *_throwNode {
return &_throwNode{
_nodeType: nodeThrow,
Argument: argument,
}
}
func (self _throwNode) String() string {
return fmt.Sprintf("{ <throw> %s }", self.Argument)
}
type _tryCatchNode struct {
_nodeType
_node_
Try _node
Catch *_catchNode
Finally *_blockNode
}
func newTryCatchNode(try _node) *_tryCatchNode {
return &_tryCatchNode{
_nodeType: nodeTryCatch,
Try: try,
}
}
func (self _tryCatchNode) String() string {
return fmtNodeString("{ <try> %s %s </try> }", self.Try, func() string {
result := ""
if self.Catch != nil && self.Finally != nil {
result = fmtNodeString("%s <finally> %s", self.Catch, self.Finally)
} else if self.Catch != nil {
result = fmtNodeString("%s", self.Catch)
} else {
result = fmtNodeString("<finally> %s", self.Finally)
}
return result
}())
}
func (self *_tryCatchNode) AddCatch(identifier string, body *_blockNode) {
self.Catch = newCatchNode(identifier, body)
}
type _catchNode struct {
_nodeType
_node_
Identifier string
Body *_blockNode
}
func newCatchNode(identifier string, body *_blockNode) *_catchNode {
return &_catchNode{
_nodeType: nodeCatch,
Identifier: identifier,
Body: body,
}
}
func (self _catchNode) String() string {
return fmtNodeString("<catch> %s %s", self.Identifier, self.Body)
}
type _variableDeclarationListNode struct {
_nodeType
_node_
VariableList []*_variableDeclarationNode
}
func newVariableDeclarationListNode() *_variableDeclarationListNode {
return &_variableDeclarationListNode{
_nodeType: nodeVariableDeclarationList,
}
}
func (self _variableDeclarationListNode) String() string {
return fmtNodeString("%s", self.VariableList)
}
type _variableDeclarationNode struct {
_nodeType
_node_
Identifier string
Operator string
Initializer _node
}
func newVariableDeclarationNode(identifier string) *_variableDeclarationNode {
return &_variableDeclarationNode{
_nodeType: nodeVariableDeclaration,
Identifier: identifier,
}
}
func (self _variableDeclarationNode) String() string {
if self.Operator != "" {
return fmtNodeString("{ <var> %s %s %s }", self.Operator, self.Identifier, self.Initializer)
}
return fmtNodeString("{ <var> %s }", self.Identifier)
}
type _withNode struct {
_nodeType
_node_
Object _node
Body _node
}
func newWithNode(object _node, body _node) *_withNode {
return &_withNode{
_nodeType: nodeWith,
Object: object,
Body: body,
}
}
func (self _withNode) String() string {
return fmt.Sprintf("{ <with> %s %s }", self.Object, self.Body)
}