1 | from asdl import pybase
|
2 | from mycpp import mops
|
3 | from typing import Optional, List, Tuple, Dict, Any, cast, TYPE_CHECKING
|
4 |
|
5 |
|
6 | from asdl import runtime # For runtime.NO_SPID
|
7 | from asdl.runtime import NewRecord, NewLeaf, TraversalState
|
8 | from _devbuild.gen.hnode_asdl import color_e, hnode, hnode_e, hnode_t, Field
|
9 |
|
10 | class nvalue_e(object):
|
11 | Null = 1
|
12 | Bool = 2
|
13 | Int = 3
|
14 | Float = 4
|
15 | Str = 5
|
16 | Symbol = 6
|
17 | List = 7
|
18 | Record = 8
|
19 |
|
20 | _nvalue_str = {
|
21 | 1: 'Null',
|
22 | 2: 'Bool',
|
23 | 3: 'Int',
|
24 | 4: 'Float',
|
25 | 5: 'Str',
|
26 | 6: 'Symbol',
|
27 | 7: 'List',
|
28 | 8: 'Record',
|
29 | }
|
30 |
|
31 | def nvalue_str(tag, dot=True):
|
32 | # type: (int, bool) -> str
|
33 | v = _nvalue_str[tag]
|
34 | if dot:
|
35 | return "nvalue.%s" % v
|
36 | else:
|
37 | return v
|
38 |
|
39 | class nvalue_t(pybase.CompoundObj):
|
40 | def tag(self):
|
41 | # type: () -> int
|
42 | return self._type_tag
|
43 | pass
|
44 |
|
45 | class nvalue__Null(nvalue_t):
|
46 | _type_tag = 1
|
47 | __slots__ = ()
|
48 |
|
49 | def __init__(self, ):
|
50 | # type: () -> None
|
51 | pass
|
52 |
|
53 | def PrettyTree(self, trav=None):
|
54 | # type: (Optional[TraversalState]) -> hnode_t
|
55 | trav = trav or TraversalState()
|
56 | heap_id = id(self)
|
57 | if heap_id in trav.seen:
|
58 | return hnode.AlreadySeen(heap_id)
|
59 | trav.seen[heap_id] = True
|
60 | out_node = NewRecord('nvalue__Null')
|
61 | L = out_node.fields
|
62 |
|
63 | return out_node
|
64 |
|
65 | def _AbbreviatedTree(self, trav=None):
|
66 | # type: (Optional[TraversalState]) -> hnode_t
|
67 | trav = trav or TraversalState()
|
68 | heap_id = id(self)
|
69 | if heap_id in trav.seen:
|
70 | return hnode.AlreadySeen(heap_id)
|
71 | trav.seen[heap_id] = True
|
72 | out_node = NewRecord('nvalue__Null')
|
73 | L = out_node.fields
|
74 | return out_node
|
75 |
|
76 | def AbbreviatedTree(self, trav=None):
|
77 | # type: (Optional[TraversalState]) -> hnode_t
|
78 | return self._AbbreviatedTree(trav=trav)
|
79 |
|
80 | class nvalue(object):
|
81 | Null = nvalue__Null()
|
82 |
|
83 | class Bool(nvalue_t):
|
84 | _type_tag = 2
|
85 | __slots__ = ('b',)
|
86 |
|
87 | def __init__(self, b):
|
88 | # type: (bool) -> None
|
89 | self.b = b
|
90 |
|
91 | @staticmethod
|
92 | def CreateNull(alloc_lists=False):
|
93 | # type: () -> nvalue.Bool
|
94 | return nvalue.Bool(False)
|
95 |
|
96 | def PrettyTree(self, trav=None):
|
97 | # type: (Optional[TraversalState]) -> hnode_t
|
98 | trav = trav or TraversalState()
|
99 | heap_id = id(self)
|
100 | if heap_id in trav.seen:
|
101 | return hnode.AlreadySeen(heap_id)
|
102 | trav.seen[heap_id] = True
|
103 | out_node = NewRecord('nvalue.Bool')
|
104 | L = out_node.fields
|
105 |
|
106 | x0 = hnode.Leaf('T' if self.b else 'F', color_e.OtherConst)
|
107 | L.append(Field('b', x0))
|
108 |
|
109 | return out_node
|
110 |
|
111 | def _AbbreviatedTree(self, trav=None):
|
112 | # type: (Optional[TraversalState]) -> hnode_t
|
113 | trav = trav or TraversalState()
|
114 | heap_id = id(self)
|
115 | if heap_id in trav.seen:
|
116 | return hnode.AlreadySeen(heap_id)
|
117 | trav.seen[heap_id] = True
|
118 | out_node = NewRecord('nvalue.Bool')
|
119 | L = out_node.fields
|
120 | x0 = hnode.Leaf('T' if self.b else 'F', color_e.OtherConst)
|
121 | L.append(Field('b', x0))
|
122 |
|
123 | return out_node
|
124 |
|
125 | def AbbreviatedTree(self, trav=None):
|
126 | # type: (Optional[TraversalState]) -> hnode_t
|
127 | return self._AbbreviatedTree(trav=trav)
|
128 |
|
129 | class Int(nvalue_t):
|
130 | _type_tag = 3
|
131 | __slots__ = ('i',)
|
132 |
|
133 | def __init__(self, i):
|
134 | # type: (int) -> None
|
135 | self.i = i
|
136 |
|
137 | @staticmethod
|
138 | def CreateNull(alloc_lists=False):
|
139 | # type: () -> nvalue.Int
|
140 | return nvalue.Int(-1)
|
141 |
|
142 | def PrettyTree(self, trav=None):
|
143 | # type: (Optional[TraversalState]) -> hnode_t
|
144 | trav = trav or TraversalState()
|
145 | heap_id = id(self)
|
146 | if heap_id in trav.seen:
|
147 | return hnode.AlreadySeen(heap_id)
|
148 | trav.seen[heap_id] = True
|
149 | out_node = NewRecord('nvalue.Int')
|
150 | L = out_node.fields
|
151 |
|
152 | x0 = hnode.Leaf(str(self.i), color_e.OtherConst)
|
153 | L.append(Field('i', x0))
|
154 |
|
155 | return out_node
|
156 |
|
157 | def _AbbreviatedTree(self, trav=None):
|
158 | # type: (Optional[TraversalState]) -> hnode_t
|
159 | trav = trav or TraversalState()
|
160 | heap_id = id(self)
|
161 | if heap_id in trav.seen:
|
162 | return hnode.AlreadySeen(heap_id)
|
163 | trav.seen[heap_id] = True
|
164 | out_node = NewRecord('nvalue.Int')
|
165 | L = out_node.fields
|
166 | x0 = hnode.Leaf(str(self.i), color_e.OtherConst)
|
167 | L.append(Field('i', x0))
|
168 |
|
169 | return out_node
|
170 |
|
171 | def AbbreviatedTree(self, trav=None):
|
172 | # type: (Optional[TraversalState]) -> hnode_t
|
173 | return self._AbbreviatedTree(trav=trav)
|
174 |
|
175 | class Float(nvalue_t):
|
176 | _type_tag = 4
|
177 | __slots__ = ('f',)
|
178 |
|
179 | def __init__(self, f):
|
180 | # type: (float) -> None
|
181 | self.f = f
|
182 |
|
183 | @staticmethod
|
184 | def CreateNull(alloc_lists=False):
|
185 | # type: () -> nvalue.Float
|
186 | return nvalue.Float(0.0)
|
187 |
|
188 | def PrettyTree(self, trav=None):
|
189 | # type: (Optional[TraversalState]) -> hnode_t
|
190 | trav = trav or TraversalState()
|
191 | heap_id = id(self)
|
192 | if heap_id in trav.seen:
|
193 | return hnode.AlreadySeen(heap_id)
|
194 | trav.seen[heap_id] = True
|
195 | out_node = NewRecord('nvalue.Float')
|
196 | L = out_node.fields
|
197 |
|
198 | x0 = hnode.Leaf(str(self.f), color_e.OtherConst)
|
199 | L.append(Field('f', x0))
|
200 |
|
201 | return out_node
|
202 |
|
203 | def _AbbreviatedTree(self, trav=None):
|
204 | # type: (Optional[TraversalState]) -> hnode_t
|
205 | trav = trav or TraversalState()
|
206 | heap_id = id(self)
|
207 | if heap_id in trav.seen:
|
208 | return hnode.AlreadySeen(heap_id)
|
209 | trav.seen[heap_id] = True
|
210 | out_node = NewRecord('nvalue.Float')
|
211 | L = out_node.fields
|
212 | x0 = hnode.Leaf(str(self.f), color_e.OtherConst)
|
213 | L.append(Field('f', x0))
|
214 |
|
215 | return out_node
|
216 |
|
217 | def AbbreviatedTree(self, trav=None):
|
218 | # type: (Optional[TraversalState]) -> hnode_t
|
219 | return self._AbbreviatedTree(trav=trav)
|
220 |
|
221 | class Str(nvalue_t):
|
222 | _type_tag = 5
|
223 | __slots__ = ('s',)
|
224 |
|
225 | def __init__(self, s):
|
226 | # type: (str) -> None
|
227 | self.s = s
|
228 |
|
229 | @staticmethod
|
230 | def CreateNull(alloc_lists=False):
|
231 | # type: () -> nvalue.Str
|
232 | return nvalue.Str('')
|
233 |
|
234 | def PrettyTree(self, trav=None):
|
235 | # type: (Optional[TraversalState]) -> hnode_t
|
236 | trav = trav or TraversalState()
|
237 | heap_id = id(self)
|
238 | if heap_id in trav.seen:
|
239 | return hnode.AlreadySeen(heap_id)
|
240 | trav.seen[heap_id] = True
|
241 | out_node = NewRecord('nvalue.Str')
|
242 | L = out_node.fields
|
243 |
|
244 | x0 = NewLeaf(self.s, color_e.StringConst)
|
245 | L.append(Field('s', x0))
|
246 |
|
247 | return out_node
|
248 |
|
249 | def _AbbreviatedTree(self, trav=None):
|
250 | # type: (Optional[TraversalState]) -> hnode_t
|
251 | trav = trav or TraversalState()
|
252 | heap_id = id(self)
|
253 | if heap_id in trav.seen:
|
254 | return hnode.AlreadySeen(heap_id)
|
255 | trav.seen[heap_id] = True
|
256 | out_node = NewRecord('nvalue.Str')
|
257 | L = out_node.fields
|
258 | x0 = NewLeaf(self.s, color_e.StringConst)
|
259 | L.append(Field('s', x0))
|
260 |
|
261 | return out_node
|
262 |
|
263 | def AbbreviatedTree(self, trav=None):
|
264 | # type: (Optional[TraversalState]) -> hnode_t
|
265 | return self._AbbreviatedTree(trav=trav)
|
266 |
|
267 | class Symbol(nvalue_t):
|
268 | _type_tag = 6
|
269 | __slots__ = ('s',)
|
270 |
|
271 | def __init__(self, s):
|
272 | # type: (str) -> None
|
273 | self.s = s
|
274 |
|
275 | @staticmethod
|
276 | def CreateNull(alloc_lists=False):
|
277 | # type: () -> nvalue.Symbol
|
278 | return nvalue.Symbol('')
|
279 |
|
280 | def PrettyTree(self, trav=None):
|
281 | # type: (Optional[TraversalState]) -> hnode_t
|
282 | trav = trav or TraversalState()
|
283 | heap_id = id(self)
|
284 | if heap_id in trav.seen:
|
285 | return hnode.AlreadySeen(heap_id)
|
286 | trav.seen[heap_id] = True
|
287 | out_node = NewRecord('nvalue.Symbol')
|
288 | L = out_node.fields
|
289 |
|
290 | x0 = NewLeaf(self.s, color_e.StringConst)
|
291 | L.append(Field('s', x0))
|
292 |
|
293 | return out_node
|
294 |
|
295 | def _AbbreviatedTree(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('nvalue.Symbol')
|
303 | L = out_node.fields
|
304 | x0 = NewLeaf(self.s, color_e.StringConst)
|
305 | L.append(Field('s', x0))
|
306 |
|
307 | return out_node
|
308 |
|
309 | def AbbreviatedTree(self, trav=None):
|
310 | # type: (Optional[TraversalState]) -> hnode_t
|
311 | return self._AbbreviatedTree(trav=trav)
|
312 |
|
313 | class List(nvalue_t):
|
314 | _type_tag = 7
|
315 | __slots__ = ('items',)
|
316 |
|
317 | def __init__(self, items):
|
318 | # type: (List[nvalue_t]) -> None
|
319 | self.items = items
|
320 |
|
321 | @staticmethod
|
322 | def CreateNull(alloc_lists=False):
|
323 | # type: () -> nvalue.List
|
324 | return nvalue.List([] if alloc_lists else cast('List[nvalue_t]', None))
|
325 |
|
326 | def PrettyTree(self, trav=None):
|
327 | # type: (Optional[TraversalState]) -> hnode_t
|
328 | trav = trav or TraversalState()
|
329 | heap_id = id(self)
|
330 | if heap_id in trav.seen:
|
331 | return hnode.AlreadySeen(heap_id)
|
332 | trav.seen[heap_id] = True
|
333 | out_node = NewRecord('nvalue.List')
|
334 | L = out_node.fields
|
335 |
|
336 | if self.items is not None: # List
|
337 | x0 = hnode.Array([])
|
338 | for i0 in self.items:
|
339 | h = (hnode.Leaf("_", color_e.OtherConst) if i0 is None else
|
340 | i0.PrettyTree(trav=trav))
|
341 | x0.children.append(h)
|
342 | L.append(Field('items', x0))
|
343 |
|
344 | return out_node
|
345 |
|
346 | def _AbbreviatedTree(self, trav=None):
|
347 | # type: (Optional[TraversalState]) -> hnode_t
|
348 | trav = trav or TraversalState()
|
349 | heap_id = id(self)
|
350 | if heap_id in trav.seen:
|
351 | return hnode.AlreadySeen(heap_id)
|
352 | trav.seen[heap_id] = True
|
353 | out_node = NewRecord('nvalue.List')
|
354 | L = out_node.fields
|
355 | if self.items is not None: # List
|
356 | x0 = hnode.Array([])
|
357 | for i0 in self.items:
|
358 | h = (hnode.Leaf("_", color_e.OtherConst) if i0 is None else
|
359 | i0.AbbreviatedTree(trav=trav))
|
360 | x0.children.append(h)
|
361 | L.append(Field('items', x0))
|
362 |
|
363 | return out_node
|
364 |
|
365 | def AbbreviatedTree(self, trav=None):
|
366 | # type: (Optional[TraversalState]) -> hnode_t
|
367 | return self._AbbreviatedTree(trav=trav)
|
368 |
|
369 | class Record(nvalue_t):
|
370 | _type_tag = 8
|
371 | __slots__ = ('name', 'args', 'named')
|
372 |
|
373 | def __init__(self, name, args, named):
|
374 | # type: (str, List[nvalue_t], Dict[str, nvalue_t]) -> None
|
375 | self.name = name
|
376 | self.args = args
|
377 | self.named = named
|
378 |
|
379 | @staticmethod
|
380 | def CreateNull(alloc_lists=False):
|
381 | # type: () -> nvalue.Record
|
382 | return nvalue.Record('', [] if alloc_lists else cast('List[nvalue_t]', None), cast('Dict[str, nvalue_t]', None))
|
383 |
|
384 | def PrettyTree(self, trav=None):
|
385 | # type: (Optional[TraversalState]) -> hnode_t
|
386 | trav = trav or TraversalState()
|
387 | heap_id = id(self)
|
388 | if heap_id in trav.seen:
|
389 | return hnode.AlreadySeen(heap_id)
|
390 | trav.seen[heap_id] = True
|
391 | out_node = NewRecord('nvalue.Record')
|
392 | L = out_node.fields
|
393 |
|
394 | x0 = NewLeaf(self.name, color_e.StringConst)
|
395 | L.append(Field('name', x0))
|
396 |
|
397 | if self.args is not None: # List
|
398 | x1 = hnode.Array([])
|
399 | for i1 in self.args:
|
400 | h = (hnode.Leaf("_", color_e.OtherConst) if i1 is None else
|
401 | i1.PrettyTree(trav=trav))
|
402 | x1.children.append(h)
|
403 | L.append(Field('args', x1))
|
404 |
|
405 | if self.named is not None: # Dict
|
406 | m = hnode.Leaf("Dict", color_e.OtherConst)
|
407 | x2 = hnode.Array([m])
|
408 | for k2, v2 in self.named.iteritems():
|
409 | x2.children.append(NewLeaf(k2, color_e.StringConst))
|
410 | x2.children.append(v2.PrettyTree(trav=trav))
|
411 | L.append(Field('named', x2))
|
412 |
|
413 | return out_node
|
414 |
|
415 | def _AbbreviatedTree(self, trav=None):
|
416 | # type: (Optional[TraversalState]) -> hnode_t
|
417 | trav = trav or TraversalState()
|
418 | heap_id = id(self)
|
419 | if heap_id in trav.seen:
|
420 | return hnode.AlreadySeen(heap_id)
|
421 | trav.seen[heap_id] = True
|
422 | out_node = NewRecord('nvalue.Record')
|
423 | L = out_node.fields
|
424 | x0 = NewLeaf(self.name, color_e.StringConst)
|
425 | L.append(Field('name', x0))
|
426 |
|
427 | if self.args is not None: # List
|
428 | x1 = hnode.Array([])
|
429 | for i1 in self.args:
|
430 | h = (hnode.Leaf("_", color_e.OtherConst) if i1 is None else
|
431 | i1.AbbreviatedTree(trav=trav))
|
432 | x1.children.append(h)
|
433 | L.append(Field('args', x1))
|
434 |
|
435 | if self.named is not None: # Dict
|
436 | m = hnode.Leaf("Dict", color_e.OtherConst)
|
437 | x2 = hnode.Array([m])
|
438 | for k2, v2 in self.named.iteritems():
|
439 | x2.children.append(NewLeaf(k2, color_e.StringConst))
|
440 | x2.children.append(v2.AbbreviatedTree(trav=trav))
|
441 | L.append(Field('named', x2))
|
442 |
|
443 | return out_node
|
444 |
|
445 | def AbbreviatedTree(self, trav=None):
|
446 | # type: (Optional[TraversalState]) -> hnode_t
|
447 | return self._AbbreviatedTree(trav=trav)
|
448 |
|
449 | pass
|
450 |
|