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

254 lines, 180 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 tok_t(pybase.SimpleObj):
10 pass
11
12class tok_e(object):
13 Const = tok_t(1)
14 Var = tok_t(2)
15 Op1 = tok_t(3)
16 Op2 = tok_t(4)
17 Paren = tok_t(5)
18 Eof = tok_t(6)
19 Invalid = tok_t(7)
20
21_tok_str = {
22 1: 'Const',
23 2: 'Var',
24 3: 'Op1',
25 4: 'Op2',
26 5: 'Paren',
27 6: 'Eof',
28 7: 'Invalid',
29}
30
31def tok_str(val, dot=True):
32 # type: (tok_t, bool) -> str
33 v = _tok_str[val]
34 if dot:
35 return "tok.%s" % v
36 else:
37 return v
38
39class expr_e(object):
40 Const = 1
41 Var = 2
42 Binary = 3
43
44_expr_str = {
45 1: 'Const',
46 2: 'Var',
47 3: 'Binary',
48}
49
50def expr_str(tag, dot=True):
51 # type: (int, bool) -> str
52 v = _expr_str[tag]
53 if dot:
54 return "expr.%s" % v
55 else:
56 return v
57
58class expr_t(pybase.CompoundObj):
59 def tag(self):
60 # type: () -> int
61 return self._type_tag
62
63class expr(object):
64 class Const(expr_t):
65 _type_tag = 1
66 __slots__ = ('i',)
67
68 def __init__(self, i):
69 # type: (int) -> None
70 self.i = i
71
72 @staticmethod
73 def CreateNull(alloc_lists=False):
74 # type: () -> expr.Const
75 return expr.Const(-1)
76
77 def PrettyTree(self, do_abbrev, trav=None):
78 # type: (bool, Optional[TraversalState]) -> hnode_t
79 trav = trav or TraversalState()
80 heap_id = id(self)
81 if heap_id in trav.seen:
82 return hnode.AlreadySeen(heap_id)
83 trav.seen[heap_id] = True
84
85 out_node = NewRecord('expr.Const')
86 L = out_node.fields
87
88 x0 = hnode.Leaf(str(self.i), color_e.OtherConst)
89 L.append(Field('i', x0))
90
91 return out_node
92
93 class Var(expr_t):
94 _type_tag = 2
95 __slots__ = ('name',)
96
97 def __init__(self, name):
98 # type: (str) -> None
99 self.name = name
100
101 @staticmethod
102 def CreateNull(alloc_lists=False):
103 # type: () -> expr.Var
104 return expr.Var('')
105
106 def PrettyTree(self, do_abbrev, trav=None):
107 # type: (bool, Optional[TraversalState]) -> hnode_t
108 trav = trav or TraversalState()
109 heap_id = id(self)
110 if heap_id in trav.seen:
111 return hnode.AlreadySeen(heap_id)
112 trav.seen[heap_id] = True
113
114 out_node = NewRecord('expr.Var')
115 L = out_node.fields
116
117 x0 = NewLeaf(self.name, color_e.StringConst)
118 L.append(Field('name', x0))
119
120 return out_node
121
122 class Binary(expr_t):
123 _type_tag = 3
124 __slots__ = ('op', 'left', 'right')
125
126 def __init__(self, op, left, right):
127 # type: (str, expr_t, expr_t) -> None
128 self.op = op
129 self.left = left
130 self.right = right
131
132 @staticmethod
133 def CreateNull(alloc_lists=False):
134 # type: () -> expr.Binary
135 return expr.Binary('', cast('expr_t', None), cast('expr_t', None))
136
137 def PrettyTree(self, do_abbrev, trav=None):
138 # type: (bool, Optional[TraversalState]) -> hnode_t
139 trav = trav or TraversalState()
140 heap_id = id(self)
141 if heap_id in trav.seen:
142 return hnode.AlreadySeen(heap_id)
143 trav.seen[heap_id] = True
144
145 out_node = NewRecord('expr.Binary')
146 L = out_node.fields
147
148 x0 = NewLeaf(self.op, color_e.StringConst)
149 L.append(Field('op', x0))
150
151 assert self.left is not None
152 x1 = self.left.PrettyTree(do_abbrev, trav=trav)
153 L.append(Field('left', x1))
154
155 assert self.right is not None
156 x2 = self.right.PrettyTree(do_abbrev, trav=trav)
157 L.append(Field('right', x2))
158
159 return out_node
160
161 pass
162
163class Measure_v(pybase.CompoundObj):
164 _type_tag = 65
165 __slots__ = ('a', 'b')
166
167 def __init__(self, a, b):
168 # type: (int, int) -> None
169 self.a = a
170 self.b = b
171
172 @staticmethod
173 def CreateNull(alloc_lists=False):
174 # type: () -> Measure_v
175 return Measure_v(-1, -1)
176
177 def PrettyTree(self, do_abbrev, trav=None):
178 # type: (bool, Optional[TraversalState]) -> hnode_t
179 trav = trav or TraversalState()
180 heap_id = id(self)
181 if heap_id in trav.seen:
182 return hnode.AlreadySeen(heap_id)
183 trav.seen[heap_id] = True
184
185 out_node = NewRecord('Measure_v')
186 L = out_node.fields
187
188 x0 = hnode.Leaf(str(self.a), color_e.OtherConst)
189 L.append(Field('a', x0))
190
191 x1 = hnode.Leaf(str(self.b), color_e.OtherConst)
192 L.append(Field('b', x1))
193
194 return out_node
195
196class MeasuredDoc(pybase.CompoundObj):
197 _type_tag = 66
198 __slots__ = ('s', 'measure')
199
200 def __init__(self, s, measure):
201 # type: (str, Measure_v) -> None
202 self.s = s
203 self.measure = measure
204
205 @staticmethod
206 def CreateNull(alloc_lists=False):
207 # type: () -> MeasuredDoc
208 return MeasuredDoc('', cast('Measure_v', None))
209
210 def PrettyTree(self, do_abbrev, trav=None):
211 # type: (bool, Optional[TraversalState]) -> hnode_t
212 trav = trav or TraversalState()
213 heap_id = id(self)
214 if heap_id in trav.seen:
215 return hnode.AlreadySeen(heap_id)
216 trav.seen[heap_id] = True
217
218 out_node = NewRecord('MeasuredDoc')
219 L = out_node.fields
220
221 x0 = NewLeaf(self.s, color_e.StringConst)
222 L.append(Field('s', x0))
223
224 assert self.measure is not None
225 x1 = self.measure.PrettyTree(do_abbrev, trav=trav)
226 L.append(Field('measure', x1))
227
228 return out_node
229
230class CompoundWord(pybase.CompoundObj, List[str]):
231 _type_tag = 64
232 @staticmethod
233 def New():
234 # type: () -> CompoundWord
235 return CompoundWord()
236
237 @staticmethod
238 def Take(plain_list):
239 # type: (List[str]) -> CompoundWord
240 result = CompoundWord(plain_list)
241 del plain_list[:]
242 return result
243
244 def PrettyTree(self, do_abbrev, trav=None):
245 # type: (bool, Optional[TraversalState]) -> hnode_t
246 trav = trav or TraversalState()
247 heap_id = id(self)
248 if heap_id in trav.seen:
249 return hnode.AlreadySeen(heap_id)
250 trav.seen[heap_id] = True
251 h = runtime.NewRecord('CompoundWord')
252 h.unnamed_fields = [c.PrettyTree(do_abbrev) for c in self]
253 return h
254