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

217 lines, 146 significant
1from asdl import pybase
2from mycpp import mops
3from typing import Optional, List, Tuple, Dict, Any, cast, TYPE_CHECKING
4
5from asdl import runtime # For runtime.NO_SPID
6from asdl.runtime import NewRecord, NewLeaf, TraversalState
7from _devbuild.gen.hnode_asdl import color_e, hnode, hnode_e, hnode_t, Field
8
9class value_e(object):
10 Str = 1
11 Array = 2
12
13_value_str = {
14 1: 'Str',
15 2: 'Array',
16}
17
18def value_str(tag, dot=True):
19 # type: (int, bool) -> str
20 v = _value_str[tag]
21 if dot:
22 return "value.%s" % v
23 else:
24 return v
25
26class value_t(pybase.CompoundObj):
27 def tag(self):
28 # type: () -> int
29 return self._type_tag
30
31class value__Str(value_t):
32 _type_tag = 1
33 __slots__ = ()
34
35 def __init__(self, ):
36 # type: () -> None
37 pass
38
39 def PrettyTree(self, do_abbrev, trav=None):
40 # type: (bool, Optional[TraversalState]) -> hnode_t
41 trav = trav or TraversalState()
42 heap_id = id(self)
43 if heap_id in trav.seen:
44 return hnode.AlreadySeen(heap_id)
45 trav.seen[heap_id] = True
46
47 out_node = NewRecord('value.Str')
48 L = out_node.fields
49
50 return out_node
51
52class value(object):
53 Str = value__Str()
54
55 class Array(value_t):
56 _type_tag = 2
57 __slots__ = ('a',)
58
59 def __init__(self, a):
60 # type: (int) -> None
61 self.a = a
62
63 @staticmethod
64 def CreateNull(alloc_lists=False):
65 # type: () -> value.Array
66 return value.Array(-1)
67
68 def PrettyTree(self, do_abbrev, trav=None):
69 # type: (bool, Optional[TraversalState]) -> hnode_t
70 trav = trav or TraversalState()
71 heap_id = id(self)
72 if heap_id in trav.seen:
73 return hnode.AlreadySeen(heap_id)
74 trav.seen[heap_id] = True
75
76 out_node = NewRecord('value.Array')
77 L = out_node.fields
78
79 x0 = hnode.Leaf(str(self.a), color_e.OtherConst)
80 L.append(Field('a', x0))
81
82 return out_node
83
84 pass
85
86class t2(pybase.CompoundObj):
87 _type_tag = 64
88 __slots__ = ('a', 'b')
89
90 def __init__(self, a, b):
91 # type: (int, int) -> None
92 self.a = a
93 self.b = b
94
95 @staticmethod
96 def CreateNull(alloc_lists=False):
97 # type: () -> t2
98 return t2(-1, -1)
99
100 def PrettyTree(self, do_abbrev, trav=None):
101 # type: (bool, 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
108 out_node = NewRecord('t2')
109 L = out_node.fields
110
111 x0 = hnode.Leaf(str(self.a), color_e.OtherConst)
112 L.append(Field('a', x0))
113
114 x1 = hnode.Leaf(str(self.b), color_e.OtherConst)
115 L.append(Field('b', x1))
116
117 return out_node
118
119class t3(pybase.CompoundObj):
120 _type_tag = 65
121 __slots__ = ('a', 'b')
122
123 def __init__(self, a, b):
124 # type: (int, int) -> None
125 self.a = a
126 self.b = b
127
128 @staticmethod
129 def CreateNull(alloc_lists=False):
130 # type: () -> t3
131 return t3(-1, -1)
132
133 def PrettyTree(self, do_abbrev, trav=None):
134 # type: (bool, Optional[TraversalState]) -> hnode_t
135 trav = trav or TraversalState()
136 heap_id = id(self)
137 if heap_id in trav.seen:
138 return hnode.AlreadySeen(heap_id)
139 trav.seen[heap_id] = True
140
141 out_node = NewRecord('t3')
142 L = out_node.fields
143
144 x0 = hnode.Leaf(str(self.a), color_e.OtherConst)
145 L.append(Field('a', x0))
146
147 x1 = hnode.Leaf(str(self.b), color_e.OtherConst)
148 L.append(Field('b', x1))
149
150 return out_node
151
152class t4(pybase.CompoundObj):
153 _type_tag = 66
154 __slots__ = ('a', 'b')
155
156 def __init__(self, a, b):
157 # type: (int, int) -> None
158 self.a = a
159 self.b = b
160
161 @staticmethod
162 def CreateNull(alloc_lists=False):
163 # type: () -> t4
164 return t4(-1, -1)
165
166 def PrettyTree(self, do_abbrev, trav=None):
167 # type: (bool, Optional[TraversalState]) -> hnode_t
168 trav = trav or TraversalState()
169 heap_id = id(self)
170 if heap_id in trav.seen:
171 return hnode.AlreadySeen(heap_id)
172 trav.seen[heap_id] = True
173
174 out_node = NewRecord('t4')
175 L = out_node.fields
176
177 x0 = hnode.Leaf(str(self.a), color_e.OtherConst)
178 L.append(Field('a', x0))
179
180 x1 = hnode.Leaf(str(self.b), color_e.OtherConst)
181 L.append(Field('b', x1))
182
183 return out_node
184
185class LibToken(pybase.CompoundObj):
186 _type_tag = 67
187 __slots__ = ('s', 'i')
188
189 def __init__(self, s, i):
190 # type: (str, int) -> None
191 self.s = s
192 self.i = i
193
194 @staticmethod
195 def CreateNull(alloc_lists=False):
196 # type: () -> LibToken
197 return LibToken('', -1)
198
199 def PrettyTree(self, do_abbrev, trav=None):
200 # type: (bool, Optional[TraversalState]) -> hnode_t
201 trav = trav or TraversalState()
202 heap_id = id(self)
203 if heap_id in trav.seen:
204 return hnode.AlreadySeen(heap_id)
205 trav.seen[heap_id] = True
206
207 out_node = NewRecord('LibToken')
208 L = out_node.fields
209
210 x0 = NewLeaf(self.s, color_e.StringConst)
211 L.append(Field('s', x0))
212
213 x1 = hnode.Leaf(str(self.i), color_e.OtherConst)
214 L.append(Field('i', x1))
215
216 return out_node
217