1 | from asdl import pybase
|
2 | from mycpp import mops
|
3 | from typing import Optional, List, Tuple, Dict, Any, cast, TYPE_CHECKING
|
4 |
|
5 | from asdl import runtime # For runtime.NO_SPID
|
6 | from asdl.runtime import NewRecord, NewLeaf, TraversalState
|
7 | from _devbuild.gen.hnode_asdl import color_e, hnode, hnode_e, hnode_t, Field
|
8 |
|
9 | class nvalue_e(object):
|
10 | Null = 1
|
11 | Bool = 2
|
12 | Int = 3
|
13 | Float = 4
|
14 | Str = 5
|
15 | Symbol = 6
|
16 | List = 7
|
17 | Record = 8
|
18 |
|
19 | _nvalue_str = {
|
20 | 1: 'Null',
|
21 | 2: 'Bool',
|
22 | 3: 'Int',
|
23 | 4: 'Float',
|
24 | 5: 'Str',
|
25 | 6: 'Symbol',
|
26 | 7: 'List',
|
27 | 8: 'Record',
|
28 | }
|
29 |
|
30 | def nvalue_str(tag, dot=True):
|
31 | # type: (int, bool) -> str
|
32 | v = _nvalue_str[tag]
|
33 | if dot:
|
34 | return "nvalue.%s" % v
|
35 | else:
|
36 | return v
|
37 |
|
38 | class nvalue_t(pybase.CompoundObj):
|
39 | def tag(self):
|
40 | # type: () -> int
|
41 | return self._type_tag
|
42 |
|
43 | class nvalue__Null(nvalue_t):
|
44 | _type_tag = 1
|
45 | __slots__ = ()
|
46 |
|
47 | def __init__(self, ):
|
48 | # type: () -> None
|
49 | pass
|
50 |
|
51 | def PrettyTree(self, do_abbrev, trav=None):
|
52 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
53 | trav = trav or TraversalState()
|
54 | heap_id = id(self)
|
55 | if heap_id in trav.seen:
|
56 | return hnode.AlreadySeen(heap_id)
|
57 | trav.seen[heap_id] = True
|
58 |
|
59 | out_node = NewRecord('nvalue.Null')
|
60 | L = out_node.fields
|
61 |
|
62 | return out_node
|
63 |
|
64 | class nvalue(object):
|
65 | Null = nvalue__Null()
|
66 |
|
67 | class Bool(nvalue_t):
|
68 | _type_tag = 2
|
69 | __slots__ = ('b',)
|
70 |
|
71 | def __init__(self, b):
|
72 | # type: (bool) -> None
|
73 | self.b = b
|
74 |
|
75 | @staticmethod
|
76 | def CreateNull(alloc_lists=False):
|
77 | # type: () -> nvalue.Bool
|
78 | return nvalue.Bool(False)
|
79 |
|
80 | def PrettyTree(self, do_abbrev, trav=None):
|
81 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
82 | trav = trav or TraversalState()
|
83 | heap_id = id(self)
|
84 | if heap_id in trav.seen:
|
85 | return hnode.AlreadySeen(heap_id)
|
86 | trav.seen[heap_id] = True
|
87 |
|
88 | out_node = NewRecord('nvalue.Bool')
|
89 | L = out_node.fields
|
90 |
|
91 | x0 = hnode.Leaf('T' if self.b else 'F', color_e.OtherConst)
|
92 | L.append(Field('b', x0))
|
93 |
|
94 | return out_node
|
95 |
|
96 | class Int(nvalue_t):
|
97 | _type_tag = 3
|
98 | __slots__ = ('i',)
|
99 |
|
100 | def __init__(self, i):
|
101 | # type: (int) -> None
|
102 | self.i = i
|
103 |
|
104 | @staticmethod
|
105 | def CreateNull(alloc_lists=False):
|
106 | # type: () -> nvalue.Int
|
107 | return nvalue.Int(-1)
|
108 |
|
109 | def PrettyTree(self, do_abbrev, trav=None):
|
110 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
111 | trav = trav or TraversalState()
|
112 | heap_id = id(self)
|
113 | if heap_id in trav.seen:
|
114 | return hnode.AlreadySeen(heap_id)
|
115 | trav.seen[heap_id] = True
|
116 |
|
117 | out_node = NewRecord('nvalue.Int')
|
118 | L = out_node.fields
|
119 |
|
120 | x0 = hnode.Leaf(str(self.i), color_e.OtherConst)
|
121 | L.append(Field('i', x0))
|
122 |
|
123 | return out_node
|
124 |
|
125 | class Float(nvalue_t):
|
126 | _type_tag = 4
|
127 | __slots__ = ('f',)
|
128 |
|
129 | def __init__(self, f):
|
130 | # type: (float) -> None
|
131 | self.f = f
|
132 |
|
133 | @staticmethod
|
134 | def CreateNull(alloc_lists=False):
|
135 | # type: () -> nvalue.Float
|
136 | return nvalue.Float(0.0)
|
137 |
|
138 | def PrettyTree(self, do_abbrev, trav=None):
|
139 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
140 | trav = trav or TraversalState()
|
141 | heap_id = id(self)
|
142 | if heap_id in trav.seen:
|
143 | return hnode.AlreadySeen(heap_id)
|
144 | trav.seen[heap_id] = True
|
145 |
|
146 | out_node = NewRecord('nvalue.Float')
|
147 | L = out_node.fields
|
148 |
|
149 | x0 = hnode.Leaf(str(self.f), color_e.OtherConst)
|
150 | L.append(Field('f', x0))
|
151 |
|
152 | return out_node
|
153 |
|
154 | class Str(nvalue_t):
|
155 | _type_tag = 5
|
156 | __slots__ = ('s',)
|
157 |
|
158 | def __init__(self, s):
|
159 | # type: (str) -> None
|
160 | self.s = s
|
161 |
|
162 | @staticmethod
|
163 | def CreateNull(alloc_lists=False):
|
164 | # type: () -> nvalue.Str
|
165 | return nvalue.Str('')
|
166 |
|
167 | def PrettyTree(self, do_abbrev, trav=None):
|
168 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
169 | trav = trav or TraversalState()
|
170 | heap_id = id(self)
|
171 | if heap_id in trav.seen:
|
172 | return hnode.AlreadySeen(heap_id)
|
173 | trav.seen[heap_id] = True
|
174 |
|
175 | out_node = NewRecord('nvalue.Str')
|
176 | L = out_node.fields
|
177 |
|
178 | x0 = NewLeaf(self.s, color_e.StringConst)
|
179 | L.append(Field('s', x0))
|
180 |
|
181 | return out_node
|
182 |
|
183 | class Symbol(nvalue_t):
|
184 | _type_tag = 6
|
185 | __slots__ = ('s',)
|
186 |
|
187 | def __init__(self, s):
|
188 | # type: (str) -> None
|
189 | self.s = s
|
190 |
|
191 | @staticmethod
|
192 | def CreateNull(alloc_lists=False):
|
193 | # type: () -> nvalue.Symbol
|
194 | return nvalue.Symbol('')
|
195 |
|
196 | def PrettyTree(self, do_abbrev, trav=None):
|
197 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
198 | trav = trav or TraversalState()
|
199 | heap_id = id(self)
|
200 | if heap_id in trav.seen:
|
201 | return hnode.AlreadySeen(heap_id)
|
202 | trav.seen[heap_id] = True
|
203 |
|
204 | out_node = NewRecord('nvalue.Symbol')
|
205 | L = out_node.fields
|
206 |
|
207 | x0 = NewLeaf(self.s, color_e.StringConst)
|
208 | L.append(Field('s', x0))
|
209 |
|
210 | return out_node
|
211 |
|
212 | class List(nvalue_t):
|
213 | _type_tag = 7
|
214 | __slots__ = ('items',)
|
215 |
|
216 | def __init__(self, items):
|
217 | # type: (List[nvalue_t]) -> None
|
218 | self.items = items
|
219 |
|
220 | @staticmethod
|
221 | def CreateNull(alloc_lists=False):
|
222 | # type: () -> nvalue.List
|
223 | return nvalue.List([] if alloc_lists else cast('List[nvalue_t]', None))
|
224 |
|
225 | def PrettyTree(self, do_abbrev, trav=None):
|
226 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
227 | trav = trav or TraversalState()
|
228 | heap_id = id(self)
|
229 | if heap_id in trav.seen:
|
230 | return hnode.AlreadySeen(heap_id)
|
231 | trav.seen[heap_id] = True
|
232 |
|
233 | out_node = NewRecord('nvalue.List')
|
234 | L = out_node.fields
|
235 |
|
236 | if self.items is not None: # List
|
237 | x0 = hnode.Array([])
|
238 | for i0 in self.items:
|
239 | h = (hnode.Leaf("_", color_e.OtherConst) if i0 is None else
|
240 | i0.PrettyTree(do_abbrev, trav=trav))
|
241 | x0.children.append(h)
|
242 | L.append(Field('items', x0))
|
243 |
|
244 | return out_node
|
245 |
|
246 | class Record(nvalue_t):
|
247 | _type_tag = 8
|
248 | __slots__ = ('name', 'args', 'named')
|
249 |
|
250 | def __init__(self, name, args, named):
|
251 | # type: (str, List[nvalue_t], Dict[str, nvalue_t]) -> None
|
252 | self.name = name
|
253 | self.args = args
|
254 | self.named = named
|
255 |
|
256 | @staticmethod
|
257 | def CreateNull(alloc_lists=False):
|
258 | # type: () -> nvalue.Record
|
259 | return nvalue.Record('', [] if alloc_lists else cast('List[nvalue_t]', None), cast('Dict[str, nvalue_t]', None))
|
260 |
|
261 | def PrettyTree(self, do_abbrev, trav=None):
|
262 | # type: (bool, Optional[TraversalState]) -> hnode_t
|
263 | trav = trav or TraversalState()
|
264 | heap_id = id(self)
|
265 | if heap_id in trav.seen:
|
266 | return hnode.AlreadySeen(heap_id)
|
267 | trav.seen[heap_id] = True
|
268 |
|
269 | out_node = NewRecord('nvalue.Record')
|
270 | L = out_node.fields
|
271 |
|
272 | x0 = NewLeaf(self.name, color_e.StringConst)
|
273 | L.append(Field('name', x0))
|
274 |
|
275 | if self.args is not None: # List
|
276 | x1 = hnode.Array([])
|
277 | for i1 in self.args:
|
278 | h = (hnode.Leaf("_", color_e.OtherConst) if i1 is None else
|
279 | i1.PrettyTree(do_abbrev, trav=trav))
|
280 | x1.children.append(h)
|
281 | L.append(Field('args', x1))
|
282 |
|
283 | if self.named is not None: # Dict
|
284 | unnamed2 = [] # type: List[hnode_t]
|
285 | x2 = hnode.Record("", "{", "}", [], unnamed2)
|
286 | for k2, v2 in self.named.iteritems():
|
287 | unnamed2.append(NewLeaf(k2, color_e.StringConst))
|
288 | unnamed2.append(v2.PrettyTree(do_abbrev, trav=trav))
|
289 | L.append(Field('named', x2))
|
290 |
|
291 | return out_node
|
292 |
|
293 | pass
|
294 |
|