OILS / _devbuild / gen / typed_arith_asdl.py View on Github | oilshell.org

706 lines, 502 significant
1from asdl import pybase
2from mycpp import mops
3from typing import Optional, List, Tuple, Dict, Any, cast, TYPE_CHECKING
4
5
6from asdl import runtime # For runtime.NO_SPID
7from asdl.runtime import NewRecord, NewLeaf, TraversalState
8from _devbuild.gen.hnode_asdl import color_e, hnode, hnode_e, hnode_t, Field
9
10class arith_expr_e(object):
11 NoOp = 1
12 Const = 2
13 Big = 3
14 Var = 4
15 Unary = 5
16 Binary = 6
17 Ternary = 7
18 FuncCall = 8
19 Index = 9
20 Slice = 10
21
22_arith_expr_str = {
23 1: 'NoOp',
24 2: 'Const',
25 3: 'Big',
26 4: 'Var',
27 5: 'Unary',
28 6: 'Binary',
29 7: 'Ternary',
30 8: 'FuncCall',
31 9: 'Index',
32 10: 'Slice',
33}
34
35def arith_expr_str(tag, dot=True):
36 # type: (int, bool) -> str
37 v = _arith_expr_str[tag]
38 if dot:
39 return "arith_expr.%s" % v
40 else:
41 return v
42
43class arith_expr_t(pybase.CompoundObj):
44 def tag(self):
45 # type: () -> int
46 return self._type_tag
47 pass
48
49class arith_expr__NoOp(arith_expr_t):
50 _type_tag = 1
51 __slots__ = ()
52
53 def __init__(self, ):
54 # type: () -> None
55 pass
56
57 def PrettyTree(self, trav=None):
58 # type: (Optional[TraversalState]) -> hnode_t
59 trav = trav or TraversalState()
60 heap_id = id(self)
61 if heap_id in trav.seen:
62 return hnode.AlreadySeen(heap_id)
63 trav.seen[heap_id] = True
64 out_node = NewRecord('arith_expr__NoOp')
65 L = out_node.fields
66
67 return out_node
68
69 def _AbbreviatedTree(self, trav=None):
70 # type: (Optional[TraversalState]) -> hnode_t
71 trav = trav or TraversalState()
72 heap_id = id(self)
73 if heap_id in trav.seen:
74 return hnode.AlreadySeen(heap_id)
75 trav.seen[heap_id] = True
76 out_node = NewRecord('arith_expr__NoOp')
77 L = out_node.fields
78 return out_node
79
80 def AbbreviatedTree(self, trav=None):
81 # type: (Optional[TraversalState]) -> hnode_t
82 return self._AbbreviatedTree(trav=trav)
83
84class arith_expr(object):
85 NoOp = arith_expr__NoOp()
86
87 class Const(arith_expr_t):
88 _type_tag = 2
89 __slots__ = ('i',)
90
91 def __init__(self, i):
92 # type: (int) -> None
93 self.i = i
94
95 @staticmethod
96 def CreateNull(alloc_lists=False):
97 # type: () -> arith_expr.Const
98 return arith_expr.Const(-1)
99
100 def PrettyTree(self, trav=None):
101 # type: (Optional[TraversalState]) -> hnode_t
102 trav = trav or TraversalState()
103 heap_id = id(self)
104 if heap_id in trav.seen:
105 return hnode.AlreadySeen(heap_id)
106 trav.seen[heap_id] = True
107 out_node = NewRecord('arith_expr.Const')
108 L = out_node.fields
109
110 x0 = hnode.Leaf(str(self.i), color_e.OtherConst)
111 L.append(Field('i', x0))
112
113 return out_node
114
115 def _AbbreviatedTree(self, trav=None):
116 # type: (Optional[TraversalState]) -> hnode_t
117 trav = trav or TraversalState()
118 heap_id = id(self)
119 if heap_id in trav.seen:
120 return hnode.AlreadySeen(heap_id)
121 trav.seen[heap_id] = True
122 out_node = NewRecord('arith_expr.Const')
123 L = out_node.fields
124 x0 = hnode.Leaf(str(self.i), color_e.OtherConst)
125 L.append(Field('i', x0))
126
127 return out_node
128
129 def AbbreviatedTree(self, trav=None):
130 # type: (Optional[TraversalState]) -> hnode_t
131 return self._AbbreviatedTree(trav=trav)
132
133 class Big(arith_expr_t):
134 _type_tag = 3
135 __slots__ = ('b',)
136
137 def __init__(self, b):
138 # type: (mops.BigInt) -> None
139 self.b = b
140
141 @staticmethod
142 def CreateNull(alloc_lists=False):
143 # type: () -> arith_expr.Big
144 return arith_expr.Big(mops.BigInt(-1))
145
146 def PrettyTree(self, trav=None):
147 # type: (Optional[TraversalState]) -> hnode_t
148 trav = trav or TraversalState()
149 heap_id = id(self)
150 if heap_id in trav.seen:
151 return hnode.AlreadySeen(heap_id)
152 trav.seen[heap_id] = True
153 out_node = NewRecord('arith_expr.Big')
154 L = out_node.fields
155
156 x0 = hnode.Leaf(mops.ToStr(self.b), color_e.OtherConst)
157 L.append(Field('b', x0))
158
159 return out_node
160
161 def _AbbreviatedTree(self, trav=None):
162 # type: (Optional[TraversalState]) -> hnode_t
163 trav = trav or TraversalState()
164 heap_id = id(self)
165 if heap_id in trav.seen:
166 return hnode.AlreadySeen(heap_id)
167 trav.seen[heap_id] = True
168 out_node = NewRecord('arith_expr.Big')
169 L = out_node.fields
170 x0 = hnode.Leaf(mops.ToStr(self.b), color_e.OtherConst)
171 L.append(Field('b', x0))
172
173 return out_node
174
175 def AbbreviatedTree(self, trav=None):
176 # type: (Optional[TraversalState]) -> hnode_t
177 return self._AbbreviatedTree(trav=trav)
178
179 class Var(arith_expr_t):
180 _type_tag = 4
181 __slots__ = ('name',)
182
183 def __init__(self, name):
184 # type: (str) -> None
185 self.name = name
186
187 @staticmethod
188 def CreateNull(alloc_lists=False):
189 # type: () -> arith_expr.Var
190 return arith_expr.Var('')
191
192 def PrettyTree(self, trav=None):
193 # type: (Optional[TraversalState]) -> hnode_t
194 trav = trav or TraversalState()
195 heap_id = id(self)
196 if heap_id in trav.seen:
197 return hnode.AlreadySeen(heap_id)
198 trav.seen[heap_id] = True
199 out_node = NewRecord('arith_expr.Var')
200 L = out_node.fields
201
202 x0 = NewLeaf(self.name, color_e.StringConst)
203 L.append(Field('name', x0))
204
205 return out_node
206
207 def _AbbreviatedTree(self, trav=None):
208 # type: (Optional[TraversalState]) -> hnode_t
209 trav = trav or TraversalState()
210 heap_id = id(self)
211 if heap_id in trav.seen:
212 return hnode.AlreadySeen(heap_id)
213 trav.seen[heap_id] = True
214 out_node = NewRecord('arith_expr.Var')
215 L = out_node.fields
216 x0 = NewLeaf(self.name, color_e.StringConst)
217 L.append(Field('name', x0))
218
219 return out_node
220
221 def AbbreviatedTree(self, trav=None):
222 # type: (Optional[TraversalState]) -> hnode_t
223 return self._AbbreviatedTree(trav=trav)
224
225 class Unary(arith_expr_t):
226 _type_tag = 5
227 __slots__ = ('op', 'a')
228
229 def __init__(self, op, a):
230 # type: (str, arith_expr_t) -> None
231 self.op = op
232 self.a = a
233
234 @staticmethod
235 def CreateNull(alloc_lists=False):
236 # type: () -> arith_expr.Unary
237 return arith_expr.Unary('', cast(arith_expr_t, None))
238
239 def PrettyTree(self, trav=None):
240 # type: (Optional[TraversalState]) -> hnode_t
241 trav = trav or TraversalState()
242 heap_id = id(self)
243 if heap_id in trav.seen:
244 return hnode.AlreadySeen(heap_id)
245 trav.seen[heap_id] = True
246 out_node = NewRecord('arith_expr.Unary')
247 L = out_node.fields
248
249 x0 = NewLeaf(self.op, color_e.StringConst)
250 L.append(Field('op', x0))
251
252 assert self.a is not None
253 x1 = self.a.PrettyTree(trav=trav)
254 L.append(Field('a', x1))
255
256 return out_node
257
258 def _AbbreviatedTree(self, trav=None):
259 # type: (Optional[TraversalState]) -> hnode_t
260 trav = trav or TraversalState()
261 heap_id = id(self)
262 if heap_id in trav.seen:
263 return hnode.AlreadySeen(heap_id)
264 trav.seen[heap_id] = True
265 out_node = NewRecord('arith_expr.Unary')
266 L = out_node.fields
267 x0 = NewLeaf(self.op, color_e.StringConst)
268 L.append(Field('op', x0))
269
270 assert self.a is not None
271 x1 = self.a.AbbreviatedTree(trav=trav)
272 L.append(Field('a', x1))
273
274 return out_node
275
276 def AbbreviatedTree(self, trav=None):
277 # type: (Optional[TraversalState]) -> hnode_t
278 return self._AbbreviatedTree(trav=trav)
279
280 class Binary(arith_expr_t):
281 _type_tag = 6
282 __slots__ = ('op', 'left', 'right')
283
284 def __init__(self, op, left, right):
285 # type: (str, arith_expr_t, arith_expr_t) -> None
286 self.op = op
287 self.left = left
288 self.right = right
289
290 @staticmethod
291 def CreateNull(alloc_lists=False):
292 # type: () -> arith_expr.Binary
293 return arith_expr.Binary('', cast(arith_expr_t, None), cast(arith_expr_t, None))
294
295 def PrettyTree(self, trav=None):
296 # type: (Optional[TraversalState]) -> hnode_t
297 trav = trav or TraversalState()
298 heap_id = id(self)
299 if heap_id in trav.seen:
300 return hnode.AlreadySeen(heap_id)
301 trav.seen[heap_id] = True
302 out_node = NewRecord('arith_expr.Binary')
303 L = out_node.fields
304
305 x0 = NewLeaf(self.op, color_e.StringConst)
306 L.append(Field('op', x0))
307
308 assert self.left is not None
309 x1 = self.left.PrettyTree(trav=trav)
310 L.append(Field('left', x1))
311
312 assert self.right is not None
313 x2 = self.right.PrettyTree(trav=trav)
314 L.append(Field('right', x2))
315
316 return out_node
317
318 def _AbbreviatedTree(self, trav=None):
319 # type: (Optional[TraversalState]) -> hnode_t
320 trav = trav or TraversalState()
321 heap_id = id(self)
322 if heap_id in trav.seen:
323 return hnode.AlreadySeen(heap_id)
324 trav.seen[heap_id] = True
325 out_node = NewRecord('arith_expr.Binary')
326 L = out_node.fields
327 x0 = NewLeaf(self.op, color_e.StringConst)
328 L.append(Field('op', x0))
329
330 assert self.left is not None
331 x1 = self.left.AbbreviatedTree(trav=trav)
332 L.append(Field('left', x1))
333
334 assert self.right is not None
335 x2 = self.right.AbbreviatedTree(trav=trav)
336 L.append(Field('right', x2))
337
338 return out_node
339
340 def AbbreviatedTree(self, trav=None):
341 # type: (Optional[TraversalState]) -> hnode_t
342 return self._AbbreviatedTree(trav=trav)
343
344 class Ternary(arith_expr_t):
345 _type_tag = 7
346 __slots__ = ('cond', 'true_expr', 'false_expr')
347
348 def __init__(self, cond, true_expr, false_expr):
349 # type: (arith_expr_t, arith_expr_t, arith_expr_t) -> None
350 self.cond = cond
351 self.true_expr = true_expr
352 self.false_expr = false_expr
353
354 @staticmethod
355 def CreateNull(alloc_lists=False):
356 # type: () -> arith_expr.Ternary
357 return arith_expr.Ternary(cast(arith_expr_t, None), cast(arith_expr_t, None), cast(arith_expr_t, None))
358
359 def PrettyTree(self, trav=None):
360 # type: (Optional[TraversalState]) -> hnode_t
361 trav = trav or TraversalState()
362 heap_id = id(self)
363 if heap_id in trav.seen:
364 return hnode.AlreadySeen(heap_id)
365 trav.seen[heap_id] = True
366 out_node = NewRecord('arith_expr.Ternary')
367 L = out_node.fields
368
369 assert self.cond is not None
370 x0 = self.cond.PrettyTree(trav=trav)
371 L.append(Field('cond', x0))
372
373 assert self.true_expr is not None
374 x1 = self.true_expr.PrettyTree(trav=trav)
375 L.append(Field('true_expr', x1))
376
377 assert self.false_expr is not None
378 x2 = self.false_expr.PrettyTree(trav=trav)
379 L.append(Field('false_expr', x2))
380
381 return out_node
382
383 def _AbbreviatedTree(self, trav=None):
384 # type: (Optional[TraversalState]) -> hnode_t
385 trav = trav or TraversalState()
386 heap_id = id(self)
387 if heap_id in trav.seen:
388 return hnode.AlreadySeen(heap_id)
389 trav.seen[heap_id] = True
390 out_node = NewRecord('arith_expr.Ternary')
391 L = out_node.fields
392 assert self.cond is not None
393 x0 = self.cond.AbbreviatedTree(trav=trav)
394 L.append(Field('cond', x0))
395
396 assert self.true_expr is not None
397 x1 = self.true_expr.AbbreviatedTree(trav=trav)
398 L.append(Field('true_expr', x1))
399
400 assert self.false_expr is not None
401 x2 = self.false_expr.AbbreviatedTree(trav=trav)
402 L.append(Field('false_expr', x2))
403
404 return out_node
405
406 def AbbreviatedTree(self, trav=None):
407 # type: (Optional[TraversalState]) -> hnode_t
408 return self._AbbreviatedTree(trav=trav)
409
410 class FuncCall(arith_expr_t):
411 _type_tag = 8
412 __slots__ = ('name', 'args')
413
414 def __init__(self, name, args):
415 # type: (str, List[arith_expr_t]) -> None
416 self.name = name
417 self.args = args
418
419 @staticmethod
420 def CreateNull(alloc_lists=False):
421 # type: () -> arith_expr.FuncCall
422 return arith_expr.FuncCall('', [] if alloc_lists else cast('List[arith_expr_t]', None))
423
424 def PrettyTree(self, trav=None):
425 # type: (Optional[TraversalState]) -> hnode_t
426 trav = trav or TraversalState()
427 heap_id = id(self)
428 if heap_id in trav.seen:
429 return hnode.AlreadySeen(heap_id)
430 trav.seen[heap_id] = True
431 out_node = NewRecord('arith_expr.FuncCall')
432 L = out_node.fields
433
434 x0 = NewLeaf(self.name, color_e.StringConst)
435 L.append(Field('name', x0))
436
437 if self.args is not None: # List
438 x1 = hnode.Array([])
439 for i1 in self.args:
440 h = (hnode.Leaf("_", color_e.OtherConst) if i1 is None else
441 i1.PrettyTree(trav=trav))
442 x1.children.append(h)
443 L.append(Field('args', x1))
444
445 return out_node
446
447 def _AbbreviatedTree(self, trav=None):
448 # type: (Optional[TraversalState]) -> hnode_t
449 trav = trav or TraversalState()
450 heap_id = id(self)
451 if heap_id in trav.seen:
452 return hnode.AlreadySeen(heap_id)
453 trav.seen[heap_id] = True
454 out_node = NewRecord('arith_expr.FuncCall')
455 L = out_node.fields
456 x0 = NewLeaf(self.name, color_e.StringConst)
457 L.append(Field('name', x0))
458
459 if self.args is not None: # List
460 x1 = hnode.Array([])
461 for i1 in self.args:
462 h = (hnode.Leaf("_", color_e.OtherConst) if i1 is None else
463 i1.AbbreviatedTree(trav=trav))
464 x1.children.append(h)
465 L.append(Field('args', x1))
466
467 return out_node
468
469 def AbbreviatedTree(self, trav=None):
470 # type: (Optional[TraversalState]) -> hnode_t
471 return self._AbbreviatedTree(trav=trav)
472
473 class Index(arith_expr_t):
474 _type_tag = 9
475 __slots__ = ('a', 'index')
476
477 def __init__(self, a, index):
478 # type: (arith_expr_t, arith_expr_t) -> None
479 self.a = a
480 self.index = index
481
482 @staticmethod
483 def CreateNull(alloc_lists=False):
484 # type: () -> arith_expr.Index
485 return arith_expr.Index(cast(arith_expr_t, None), cast(arith_expr_t, None))
486
487 def PrettyTree(self, trav=None):
488 # type: (Optional[TraversalState]) -> hnode_t
489 trav = trav or TraversalState()
490 heap_id = id(self)
491 if heap_id in trav.seen:
492 return hnode.AlreadySeen(heap_id)
493 trav.seen[heap_id] = True
494 out_node = NewRecord('arith_expr.Index')
495 L = out_node.fields
496
497 assert self.a is not None
498 x0 = self.a.PrettyTree(trav=trav)
499 L.append(Field('a', x0))
500
501 assert self.index is not None
502 x1 = self.index.PrettyTree(trav=trav)
503 L.append(Field('index', x1))
504
505 return out_node
506
507 def _AbbreviatedTree(self, trav=None):
508 # type: (Optional[TraversalState]) -> hnode_t
509 trav = trav or TraversalState()
510 heap_id = id(self)
511 if heap_id in trav.seen:
512 return hnode.AlreadySeen(heap_id)
513 trav.seen[heap_id] = True
514 out_node = NewRecord('arith_expr.Index')
515 L = out_node.fields
516 assert self.a is not None
517 x0 = self.a.AbbreviatedTree(trav=trav)
518 L.append(Field('a', x0))
519
520 assert self.index is not None
521 x1 = self.index.AbbreviatedTree(trav=trav)
522 L.append(Field('index', x1))
523
524 return out_node
525
526 def AbbreviatedTree(self, trav=None):
527 # type: (Optional[TraversalState]) -> hnode_t
528 return self._AbbreviatedTree(trav=trav)
529
530 class Slice(arith_expr_t):
531 _type_tag = 10
532 __slots__ = ('a', 'begin', 'end', 'stride')
533
534 def __init__(self, a, begin, end, stride):
535 # type: (arith_expr_t, Optional[arith_expr_t], Optional[arith_expr_t], Optional[arith_expr_t]) -> None
536 self.a = a
537 self.begin = begin
538 self.end = end
539 self.stride = stride
540
541 @staticmethod
542 def CreateNull(alloc_lists=False):
543 # type: () -> arith_expr.Slice
544 return arith_expr.Slice(cast(arith_expr_t, None), cast('Optional[arith_expr_t]', None), cast('Optional[arith_expr_t]', None), cast('Optional[arith_expr_t]', None))
545
546 def PrettyTree(self, trav=None):
547 # type: (Optional[TraversalState]) -> hnode_t
548 trav = trav or TraversalState()
549 heap_id = id(self)
550 if heap_id in trav.seen:
551 return hnode.AlreadySeen(heap_id)
552 trav.seen[heap_id] = True
553 out_node = NewRecord('arith_expr.Slice')
554 L = out_node.fields
555
556 assert self.a is not None
557 x0 = self.a.PrettyTree(trav=trav)
558 L.append(Field('a', x0))
559
560 if self.begin is not None: # Optional
561 x1 = self.begin.PrettyTree(trav=trav)
562 L.append(Field('begin', x1))
563
564 if self.end is not None: # Optional
565 x2 = self.end.PrettyTree(trav=trav)
566 L.append(Field('end', x2))
567
568 if self.stride is not None: # Optional
569 x3 = self.stride.PrettyTree(trav=trav)
570 L.append(Field('stride', x3))
571
572 return out_node
573
574 def _AbbreviatedTree(self, trav=None):
575 # type: (Optional[TraversalState]) -> hnode_t
576 trav = trav or TraversalState()
577 heap_id = id(self)
578 if heap_id in trav.seen:
579 return hnode.AlreadySeen(heap_id)
580 trav.seen[heap_id] = True
581 out_node = NewRecord('arith_expr.Slice')
582 L = out_node.fields
583 assert self.a is not None
584 x0 = self.a.AbbreviatedTree(trav=trav)
585 L.append(Field('a', x0))
586
587 if self.begin is not None: # Optional
588 x1 = self.begin.AbbreviatedTree(trav=trav)
589 L.append(Field('begin', x1))
590
591 if self.end is not None: # Optional
592 x2 = self.end.AbbreviatedTree(trav=trav)
593 L.append(Field('end', x2))
594
595 if self.stride is not None: # Optional
596 x3 = self.stride.AbbreviatedTree(trav=trav)
597 L.append(Field('stride', x3))
598
599 return out_node
600
601 def AbbreviatedTree(self, trav=None):
602 # type: (Optional[TraversalState]) -> hnode_t
603 return self._AbbreviatedTree(trav=trav)
604
605 pass
606
607class pipeline(pybase.CompoundObj):
608 _type_tag = 64
609 __slots__ = ('negated',)
610
611 def __init__(self, negated):
612 # type: (bool) -> None
613 self.negated = negated
614
615 @staticmethod
616 def CreateNull(alloc_lists=False):
617 # type: () -> pipeline
618 return pipeline(False)
619
620 def PrettyTree(self, trav=None):
621 # type: (Optional[TraversalState]) -> hnode_t
622 trav = trav or TraversalState()
623 heap_id = id(self)
624 if heap_id in trav.seen:
625 return hnode.AlreadySeen(heap_id)
626 trav.seen[heap_id] = True
627 out_node = NewRecord('pipeline')
628 L = out_node.fields
629
630 x0 = hnode.Leaf('T' if self.negated else 'F', color_e.OtherConst)
631 L.append(Field('negated', x0))
632
633 return out_node
634
635 def _AbbreviatedTree(self, trav=None):
636 # type: (Optional[TraversalState]) -> hnode_t
637 trav = trav or TraversalState()
638 heap_id = id(self)
639 if heap_id in trav.seen:
640 return hnode.AlreadySeen(heap_id)
641 trav.seen[heap_id] = True
642 out_node = NewRecord('pipeline')
643 L = out_node.fields
644 x0 = hnode.Leaf('T' if self.negated else 'F', color_e.OtherConst)
645 L.append(Field('negated', x0))
646
647 return out_node
648
649 def AbbreviatedTree(self, trav=None):
650 # type: (Optional[TraversalState]) -> hnode_t
651 return self._AbbreviatedTree(trav=trav)
652
653#
654# CONCATENATED FILE
655#
656
657"""
658typed_arith_abbrev.py - Abbreviations for pretty-printing typed_arith.asdl.
659"""
660
661from asdl import runtime
662from _devbuild.gen.hnode_asdl import hnode
663
664def _arith_expr__Unary(obj):
665 # type: (arith_expr__Unary) -> hnode.Record
666
667 p_node = runtime.NewRecord('U')
668 p_node.abbrev = True
669 n = runtime.NewLeaf(str(obj.op), color_e.StringConst)
670 p_node.unnamed_fields.append(n)
671 p_node.unnamed_fields.append(obj.a.AbbreviatedTree()) # type: ignore
672 return p_node
673
674
675def _arith_expr__Binary(obj):
676 # type: (arith_expr__Binary) -> Optional[hnode.Record]
677
678 if obj.op == '=': # test for fallback
679 return None
680
681 p_node = runtime.NewRecord('B')
682 p_node.abbrev = True
683 n = runtime.NewLeaf(str(obj.op), color_e.StringConst)
684 p_node.unnamed_fields.append(n)
685 p_node.unnamed_fields.append(obj.left.AbbreviatedTree()) # type: ignore
686 p_node.unnamed_fields.append(obj.right.AbbreviatedTree()) # type: ignore
687 return p_node
688
689
690def _arith_expr__Const(obj):
691 # type: (arith_expr__Const) -> hnode.Record
692 p_node = runtime.NewRecord('')
693 p_node.abbrev = True
694 n = runtime.NewLeaf(str(obj.i), color_e.OtherConst)
695 p_node.unnamed_fields.append(n)
696 return p_node
697
698
699def _arith_expr__Var(obj):
700 # type: (arith_expr__Var) -> hnode.Record
701 p_node = runtime.NewRecord('$')
702 p_node.abbrev = True
703 n = runtime.NewLeaf(str(obj.name), color_e.StringConst)
704 p_node.unnamed_fields.append(n)
705 return p_node
706