summaryrefslogtreecommitdiffstats
path: root/Lib/compiler/ast.py
blob: 9c214d453d102507773f0a5453cad76dbc5f3eb2 (plain)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
import types

nodes = {}

def flatten(list):
  l = []
  for elt in list:
    if type(elt) is types.TupleType:
      for elt2 in flatten(elt):
        l.append(elt2)
    elif type(elt) is types.ListType:
      for elt2 in flatten(elt):
        l.append(elt2)
    else:
      l.append(elt)
  return l

def asList(nodes):
  l = []
  for item in nodes:
    if hasattr(item, "asList"):
      l.append(item.asList())
    else:
      if type(item) is types.TupleType:
        l.append(tuple(asList(item)))
      elif type(item) is types.ListType:
        l.append(asList(item))
      else:
        l.append(item)
  return l

class Node:
  def __init__(self, *args):
    self._children = args
    self.lineno = None
  def __getitem__(self, index):
    return self._children[index]
  def __repr__(self):
    return "<Node %s>" % self._children[0]
  def __len__(self):
    return len(self._children)
  def __getslice__(self, low, high):
    return self._children[low:high]
  def getChildren(self):
    return tuple(flatten(self._children[1:]))
  def getType(self):
    return self._children[0]
  def asList(self):
    return tuple(asList(self._children))

class EmptyNode(Node):
  def __init__(self):
    self.lineno = None

class Module(Node):
  nodes['module'] = 'Module'

  def __init__(self, doc, node):
    self.doc = doc
    self.node = node
    self._children = ('module', doc, node)

  def __repr__(self):
    return "Module(%s,%s)" % self._children[1:]

class Stmt(Node):
  nodes['stmt'] = 'Stmt'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('stmt', nodes)

  def __repr__(self):
    return "Stmt(%s)" % self._children[1:]

class Function(Node):
  nodes['function'] = 'Function'

  def __init__(self, name, argnames, defaults, flags, doc, code):
    self.name = name
    self.argnames = argnames
    self.defaults = defaults
    self.flags = flags
    self.doc = doc
    self.code = code
    self._children = ('function',
                       name, argnames, defaults, flags, doc, code)

  def __repr__(self):
    return "Function(%s,%s,%s,%s,%s,%s)" % self._children[1:]

class Lambda(Node):
  nodes['lambda'] = 'Lambda'

  def __init__(self, argnames, defaults, flags, code):
    self.argnames = argnames
    self.defaults = defaults
    self.flags = flags
    self.code = code
    self._children = ('lambda', argnames, defaults, flags, code)

  def __repr__(self):
    return "Lambda(%s,%s,%s,%s)" % self._children[1:]

class Classdef(Node):
  nodes['classdef'] = 'Classdef'

  def __init__(self, name, bases, doc, code):
    self.name = name
    self.bases = bases
    self.doc = doc
    self.code = code
    self._children = ('classdef', name, bases, doc, code)

  def __repr__(self):
    return "Classdef(%s,%s,%s,%s)" % self._children[1:]

class Pass(EmptyNode):
  nodes['pass'] = 'Pass'
  _children = ('pass',)
  def __repr__(self):
    return "Pass()"

class Break(EmptyNode):
  nodes['break'] = 'Break'
  _children = ('break',)
  def __repr__(self):
    return "Break()"

class Continue(EmptyNode):
  nodes['continue'] = 'Continue'
  _children = ('continue',)
  def __repr__(self):
    return "Continue()"

class For(Node):
  nodes['for'] = 'For'

  def __init__(self, assign, list, body, else_):
    self.assign = assign
    self.list = list
    self.body = body
    self.else_ = else_
    self._children = ('for', assign, list, body, else_)

  def __repr__(self):
    return "For(%s,%s,%s,%s)" % self._children[1:]

class While(Node):
  nodes['while'] = 'While'

  def __init__(self, test, body, else_):
    self.test = test
    self.body = body
    self.else_ = else_
    self._children = ('while', test, body, else_)

  def __repr__(self):
    return "While(%s,%s,%s)" % self._children[1:]

class If(Node):
  """if: [ (testNode, suiteNode), ...], elseNode"""
  nodes['if'] = 'If'

  def __init__(self, tests, else_):
    self.tests = tests
    self.else_ = else_
    self._children = ('if', tests, else_)

  def __repr__(self):
    return "If(%s,%s)" % self._children[1:]

class Exec(Node):
  nodes['exec'] = 'Exec'

  def __init__(self, expr1, expr2, expr3):
    self.expr1 = expr1
    self.expr2 = expr2
    self.expr3 = expr3
    self._children = ('exec', expr1, expr2, expr3)

  def __repr__(self):
    return "Exec(%s,%s,%s)" % self._children[1:]

class From(Node):
  nodes['from'] = 'From'

  def __init__(self, modname, names):
    self.modname = modname
    self.names = names
    self._children = ('from', modname, names)

  def __repr__(self):
    return "From(%s,%s)" % self._children[1:]

class Import(Node):
  nodes['import'] = 'Import'

  def __init__(self, names):
    self.names = names
    self._children = ('import', names)

  def __repr__(self):
    return "Import(%s)" % self._children[1:]

class Raise(Node):
  nodes['raise'] = 'Raise'

  def __init__(self, expr1, expr2, expr3):
    self.expr1 = expr1
    self.expr2 = expr2
    self.expr3 = expr3
    self._children = ('raise', expr1, expr2, expr3)

  def __repr__(self):
    return "Raise(%s,%s,%s)" % self._children[1:]

class Tryfinally(Node):
  nodes['tryfinally'] = 'Tryfinally'

  def __init__(self, try_, fin):
    self.try_ = try_
    self.fin = fin
    self._children = ('tryfinally', try_, fin)

  def __repr__(self):
    return "Tryfinally(%s,%s)" % self._children[1:]

class Tryexcept(Node):
  nodes['tryexcept'] = 'Tryexcept'

  def __init__(self, try_, excs, else_):
    self.try_ = try_
    self.excs = excs
    self.else_ = else_
    self._children = ('tryexcept', try_, excs, else_)

  def __repr__(self):
    return "Tryexcept(%s,%s,%s)" % self._children[1:]

class Return(Node):
  nodes['return'] = 'Return'

  def __init__(self, value):
    self.value = value
    self._children = ('return', value)

  def __repr__(self):
    return "Return(%s)" % self._children[1:]

class Const(Node):
  nodes['const'] = 'Const'

  def __init__(self, value):
    self.value = value
    self._children = ('const', value)

  def __repr__(self):
    return "Const(%s)" % self._children[1:]

class Print(Node):
  nodes['print'] = 'Print'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('print', nodes)

  def __repr__(self):
    return "Print(%s)" % self._children[1:]

class Printnl(Node):
  nodes['printnl'] = 'Printnl'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('printnl', nodes)

  def __repr__(self):
    return "Printnl(%s)" % self._children[1:]

class Discard(Node):
  nodes['discard'] = 'Discard'

  def __init__(self, expr):
    self.expr = expr
    self._children = ('discard', expr)

  def __repr__(self):
    return "Discard(%s)" % self._children[1:]

class Assign(Node):
  nodes['assign'] = 'Assign'

  def __init__(self, nodes, expr):
    self.nodes = nodes
    self.expr = expr
    self._children = ('assign', nodes, expr)

  def __repr__(self):
    return "Assign(%s,%s)" % self._children[1:]

class AssTuple(Node):
  nodes['ass_tuple'] = 'AssTuple'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('ass_tuple', nodes)

  def __repr__(self):
    return "AssTuple(%s)" % self._children[1:]

class AssList(Node):
  nodes['ass_list'] = 'AssList'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('ass_list', nodes)

  def __repr__(self):
    return "AssList(%s)" % self._children[1:]

class AssName(Node):
  nodes['ass_name'] = 'AssName'

  def __init__(self, name, flags):
    self.name = name
    self.flags = flags
    self._children = ('ass_name', name, flags)

  def __repr__(self):
    return "AssName(%s,%s)" % self._children[1:]

class AssAttr(Node):
  nodes['ass_attr'] = 'AssAttr'

  def __init__(self, expr, attrname, flags):
    self.expr = expr
    self.attrname = attrname
    self.flags = flags
    self._children = ('ass_attr', expr, attrname, flags)

  def __repr__(self):
    return "AssAttr(%s,%s,%s)" % self._children[1:]

class List(Node):
  nodes['list'] = 'List'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('list', nodes)

  def __repr__(self):
    return "List(%s)" % self._children[1:]

class Dict(Node):
  nodes['dict'] = 'Dict'

  def __init__(self, keys):
    self.keys = keys
    self._children = ('dict', keys)

  def __repr__(self):
    return "Dict(%s)" % self._children[1:]

class Not(Node):
  nodes['not'] = 'Not'

  def __init__(self, expr):
    self.expr = expr
    self._children = ('not', expr)

  def __repr__(self):
    return "Not(%s)" % self._children[1:]

class Compare(Node):
  nodes['compare'] = 'Compare'

  def __init__(self, expr, ops):
    self.expr = expr
    self.ops = ops
    self._children = ('compare', expr, ops)

  def __repr__(self):
    return "Compare(%s,%s)" % self._children[1:]

class Name(Node):
  nodes['name'] = 'Name'

  def __init__(self, name):
    self.name = name
    self._children = ('name', name)

  def __repr__(self):
    return "Name(%s)" % self._children[1:]

class Global(Node):
  nodes['global'] = 'Global'

  def __init__(self, names):
    self.names = names
    self._children = ('global', names)

  def __repr__(self):
    return "Global(%s)" % self._children[1:]

class Backquote(Node):
  nodes['backquote'] = 'Backquote'

  def __init__(self, node):
    self.node = node
    self._children = ('backquote', node)

  def __repr__(self):
    return "Backquote(%s)" % self._children[1:]

class Getattr(Node):
  nodes['getattr'] = 'Getattr'

  def __init__(self, expr, attrname):
    self.expr = expr
    self.attrname = attrname
    self._children = ('getattr', expr, attrname)

  def __repr__(self):
    return "Getattr(%s,%s)" % self._children[1:]

class CallFunc(Node):
  nodes['call_func'] = 'CallFunc'

  def __init__(self, node, args):
    self.node = node
    self.args = args
    self._children = ('call_func', node, args)

  def __repr__(self):
    return "CallFunc(%s,%s)" % self._children[1:]

class Keyword(Node):
  nodes['keyword'] = 'Keyword'

  def __init__(self, name, expr):
    self.name = name
    self.expr = expr
    self._children = ('keyword', name, expr)

  def __repr__(self):
    return "Keyword(%s,%s)" % self._children[1:]

class Subscript(Node):
  nodes['subscript'] = 'Subscript'

  def __init__(self, expr, flags, subs):
    self.expr = expr
    self.flags = flags
    self.subs = subs
    self._children = ('subscript', expr, flags, subs)

  def __repr__(self):
    return "Subscript(%s,%s,%s)" % self._children[1:]

class Ellipsis(EmptyNode):
  nodes['ellipsis'] = 'Ellipsis'
  _children = ('ellipsis',)
  def __repr__(self):
    return "Ellipsis()"

class Sliceobj(Node):
  nodes['sliceobj'] = 'Sliceobj'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('sliceobj', nodes)

  def __repr__(self):
    return "Sliceobj(%s)" % self._children[1:]

class Slice(Node):
  nodes['slice'] = 'Slice'

  def __init__(self, expr, flags, lower, upper):
    self.expr = expr
    self.flags = flags
    self.lower = lower
    self.upper = upper
    self._children = ('slice', expr, flags, lower, upper)

  def __repr__(self):
    return "Slice(%s,%s,%s,%s)" % self._children[1:]

class Assert(Node):
  nodes['assert'] = 'Assert'

  def __init__(self, expr1, expr2):
    self.expr1 = expr1
    self.expr2 = expr2
    self._children = ('assert', expr1, expr2)

  def __repr__(self):
    return "Assert(%s,%s)" % self._children[1:]

class Tuple(Node):
  nodes['tuple'] = 'Tuple'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('tuple', nodes)

  def __repr__(self):
    return "Tuple(%s)" % self._children[1:]

class Or(Node):
  nodes['or'] = 'Or'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('or', nodes)

  def __repr__(self):
    return "Or(%s)" % self._children[1:]

class And(Node):
  nodes['and'] = 'And'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('and', nodes)

  def __repr__(self):
    return "And(%s)" % self._children[1:]

class Bitor(Node):
  nodes['bitor'] = 'Bitor'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('bitor', nodes)

  def __repr__(self):
    return "Bitor(%s)" % self._children[1:]

class Bitxor(Node):
  nodes['bitxor'] = 'Bitxor'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('bitxor', nodes)

  def __repr__(self):
    return "Bitxor(%s)" % self._children[1:]

class Bitand(Node):
  nodes['bitand'] = 'Bitand'

  def __init__(self, nodes):
    self.nodes = nodes
    self._children = ('bitand', nodes)

  def __repr__(self):
    return "Bitand(%s)" % self._children[1:]

class LeftShift(Node):
  nodes['<<'] = 'LeftShift'

  def __init__(self, (expr, shift)):
    self.expr = expr
    self.shift = shift
    self._children = ('<<', (expr, shift))

  def __repr__(self):
    return "LeftShift(%s,%s)" % self._children[1:]

class RightShift(Node):
  nodes['>>'] = 'RightShift'

  def __init__(self, (expr, shift)):
    self.expr = expr
    self.shift = shift
    self._children = ('>>', (expr, shift))

  def __repr__(self):
    return "RightShift(%s,%s)" % self._children[1:]

class Add(Node):
  nodes['+'] = 'Add'

  def __init__(self, (left, right)):
    self.left = left
    self.right = right
    self._children = ('+', (left, right))

  def __repr__(self):
    return "Add(%s)" % self._children[1:]

class Sub(Node):
  nodes['-'] = 'Sub'

  def __init__(self, (left, right)):
    self.left = left
    self.right = right
    self._children = ('-', (left, right))

  def __repr__(self):
    return "Sub(%s)" % self._children[1:]

class Mul(Node):
  nodes['*'] = 'Mul'

  def __init__(self, (left, right)):
    self.left = left
    self.right = right
    self._children = ('*', (left, right))

  def __repr__(self):
    return "Mul(%s)" % self._children[1:]

class Div(Node):
  nodes['/'] = 'Div'

  def __init__(self, (left, right)):
    self.left = left
    self.right = right
    self._children = ('/', (left, right))

  def __repr__(self):
    return "Div(%s)" % self._children[1:]

class Mod(Node):
  nodes['%'] = 'Mod'

  def __init__(self, (left, right)):
    self.left = left
    self.right = right
    self._children = ('%', (left, right))

  def __repr__(self):
    return "Mod(%s)" % self._children[1:]

class Power(Node):
  nodes['power'] = 'Power'

  def __init__(self, (left, right)):
    self.left = left
    self.right = right
    self._children = ('power', (left, right))

  def __repr__(self):
    return "Power(%s)" % self._children[1:]

class UnaryAdd(Node):
  nodes['unary+'] = 'UnaryAdd'

  def __init__(self, node):
    self.node = node
    self._children = ('unary+', node)

  def __repr__(self):
    return "UnaryAdd(%s)" % self._children[1:]

class UnarySub(Node):
  nodes['unary-'] = 'UnarySub'

  def __init__(self, node):
    self.node = node
    self._children = ('unary-', node)

  def __repr__(self):
    return "UnarySub(%s)" % self._children[1:]

class Invert(Node):
  nodes['invert'] = 'Invert'

  def __init__(self, node):
    self.node = node
    self._children = ('invert', node)

  def __repr__(self):
    return "Invert(%s)" % self._children[1:]

# now clean up the nodes dictionary
klasses = globals()
for k in nodes.keys():
  nodes[k] = klasses[nodes[k]]

# Local Variables:          
# mode:python               
# indent-tabs-mode: nil     
# py-indent-offset: 2       
# py-smart-indentation: nil 
# End: