-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug_ast.py
252 lines (202 loc) · 6.23 KB
/
bug_ast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
class BaseAST:
def __init__(self):
self.parent = None
self.children = []
def add_child(self, child):
self.children.append(child)
if isinstance(child, BaseAST):
child.parent = self
def remove_child(self, child):
self.children.remove(child)
child.parent = None
def replace_child(self, old_child, new_child):
index = self.children.index(old_child)
self.children[index] = new_child
new_child.parent = self
old_child.parent = None
def get_root(self):
node = self
while node.parent is not None:
node = node.parent
return node
def get_siblings(self):
return [child for child in self.parent.children if child != self]
def get_children(self):
return self.children
def get_parent(self):
return self.parent
def get_ancestors(self):
ancestors = []
node = self.parent
while node is not None:
ancestors.append(node)
node = node.parent
return ancestors
def get_descendants(self):
descendants = []
for child in self.children:
descendants.append(child)
descendants.extend(child.get_descendants())
return descendants
def __str__(self):
return self.__class__.__name__ + "(" + ", ".join(str(child) for child in self.children) + ")"
__repr__ = __str__
class ModuleAST(BaseAST):
def __init__(self, decls):
super().__init__()
for decl in decls:
self.add_child(decl)
class DeclAST(BaseAST):
def __init__(self):
super().__init__()
class FnDeclAST(DeclAST):
def __init__(self, name, params, ret_type, body):
super().__init__()
self.add_child(name)
self.add_child(params)
self.add_child(ret_type)
self.add_child(body)
class VarDeclAST(DeclAST):
def __init__(self, name, _type, value):
super().__init__()
self.add_child(name)
self.add_child(_type)
self.add_child(value)
class StructDeclAST(DeclAST):
def __init__(self, name, fields):
super().__init__()
self.add_child(name)
self.add_child(fields)
class EnumDeclAST(DeclAST):
def __init__(self, name, variants):
super().__init__()
self.add_child(name)
self.add_child(variants)
class ParamAST(BaseAST):
def __init__(self, name, _type):
super().__init__()
self.add_child(name)
self.add_child(_type)
class FieldAST(BaseAST):
def __init__(self, name, _type):
super().__init__()
self.add_child(name)
self.add_child(_type)
class VariantAST(BaseAST):
def __init__(self, name, value):
super().__init__()
self.add_child(name)
self.add_child(value)
class TypeAST(BaseAST):
def __init__(self, _type, *args):
super().__init__()
self.add_child(_type)
for arg in args:
self.add_child(arg)
class ExprAST(BaseAST):
def __init__(self):
super().__init__()
class GeneralExprAST(ExprAST):
def __init__(self, expr):
super().__init__()
self.add_child(expr)
class CallExprAST(ExprAST):
def __init__(self, name, args):
super().__init__()
self.add_child(name)
self.add_child(args)
class FieldAccessExprAST(ExprAST):
def __init__(self, expr, field):
super().__init__()
self.add_child(expr)
self.add_child(field)
class ArrayAccessExprAST(ExprAST):
def __init__(self, expr, index):
super().__init__()
self.add_child(expr)
self.add_child(index)
class MatchExprAST(ExprAST):
def __init__(self, expr, patterns):
super().__init__()
self.add_child(expr)
self.add_child(patterns)
class ListAST(ExprAST):
def __init__(self, elements):
super().__init__()
for element in elements:
self.add_child(element)
class NewStructAST(ExprAST):
def __init__(self, name, fields):
super().__init__()
self.add_child(name)
self.add_child(fields)
class FieldValueAST(BaseAST):
def __init__(self, name, expr):
super().__init__()
self.add_child(name)
self.add_child(expr)
class LiteralAST(ExprAST):
def __init__(self, value, _type):
super().__init__()
self.add_child(value)
self.add_child(_type)
class BinOpAST(ExprAST):
def __init__(self, op, left, right):
super().__init__()
self.add_child(op)
self.add_child(left)
self.add_child(right)
class UnOpAST(ExprAST):
def __init__(self, op, expr):
super().__init__()
self.add_child(op)
self.add_child(expr)
class VarRefAST(ExprAST):
def __init__(self, name):
super().__init__()
self.add_child(name)
class PatternAST(BaseAST):
def __init__(self):
super().__init__()
class WildcardPatternAST(PatternAST):
def __init__(self):
super().__init__()
class ExprPatternAST(PatternAST):
def __init__(self, expr):
super().__init__()
self.add_child(expr)
class PatternCaseAST(BaseAST):
def __init__(self, pattern, expr):
super().__init__()
self.add_child(pattern)
self.add_child(expr)
class StatementAST(BaseAST):
def __init__(self, *args, **kwargs):
super().__init__()
class ExprStmtAST(StatementAST):
def __init__(self, expr):
super().__init__()
self.add_child(expr)
class ReturnStmtAST(StatementAST):
def __init__(self, expr):
super().__init__()
self.add_child(expr)
class IfStmtAST(StatementAST):
def __init__(self, cond, then_body, else_body=None, elseif_body=None):
super().__init__()
self.add_child(cond)
self.add_child(then_body)
if else_body is not None:
self.add_child(else_body)
if elseif_body is not None:
self.add_child(elseif_body)
class LoopStmtAST(StatementAST):
def __init__(self, body, cond):
super().__init__()
self.add_child(body)
self.add_child(cond)
class AssignStmtAST(StatementAST):
def __init__(self, name, expr):
super().__init__()
self.add_child(name)
self.add_child(expr)