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

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