-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitors.py
451 lines (376 loc) · 11.9 KB
/
visitors.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""
"""
class AstTypes:
class Generic:
ast_type = 'ast_type'
lineno = 'lineno'
end_lineno = 'end_lineno'
col_offset = 'col_offset'
end_col_offset = 'end_col_offset'
class Add:
Key = 'Add'
class Assign:
Key = 'Assign'
targets = 'targets'
value = 'value'
type_comment = 'type_comment'
class BinOp:
Key = 'BinOp'
left = 'left'
op = 'op'
right = 'right'
class Break:
Key = 'Break'
class Call:
Key = 'Call'
func = 'func'
args = 'args'
starargs = 'starargs'
kwargs = 'kwargs'
keywords = 'keywords'
class Compare:
Key = 'Compare'
left = 'left'
comparators = 'comparators'
ops = 'ops'
class Constant:
Key = 'Constant'
kind = 'kind'
value = 'value'
class Eq:
Key = 'Eq'
class Expr:
Key = 'Expr'
value = 'value'
class Gt:
Key = 'Gt'
class If:
Key = 'If'
test = 'test'
body = 'body'
orelse = 'orelse'
class Load:
Key = 'Load'
class Lt:
Key = 'Lt'
class Module:
Key = 'Module'
type_ignores = 'type_ignores'
body = 'body'
class Name:
Key = 'Name'
ctx = 'ctx'
id = 'id'
class NotEq:
Key = 'NotEq'
class Store:
Key = 'Store'
class While:
Key = 'While'
test = 'test'
body = 'body'
orelse = 'orelse'
class Visitor:
INDENTATION = " "
def __init__(self, ast):
self.ast = ast
def visit_ast(self):
return self.visit_Module(self.ast)
def visit_Module(self, node):
"""
:param node: {
'ast_type': 'Module',
'body': [...]
}
:return:
"""
return self.visit_body(node['body'])
def visit_body_line(self, node):
ast_type = node[AstTypes.Generic.ast_type]
if ast_type == AstTypes.Expr.Key:
return self.visit_Expr(node)
elif ast_type == AstTypes.Assign.Key:
return self.visit_Assign(node)
elif ast_type == AstTypes.If.Key:
return self.visit_If(node)
elif ast_type == AstTypes.While.Key:
# print("RETURNING HERE: ", )
return self.visit_While(node)
elif ast_type == AstTypes.Break.Key:
return self.visit_Break(node)
def visit_body(self, nodes):
return tuple(self.visit_body_line(node) for node in nodes)
def visit_Compare_ops_op(self, node):
ast_type = node[AstTypes.Generic.ast_type]
if ast_type == AstTypes.NotEq.Key:
op = self.visit_NotEq(node)
elif ast_type == AstTypes.Eq.Key:
op = self.visit_Eq(node)
elif ast_type == AstTypes.Gt.Key:
op = self.visit_Gt(node)
elif ast_type == AstTypes.Lt.Key:
op = self.visit_Lt(node)
return op
def visit_Compare_ops(self, nodes):
return tuple(self.visit_Compare_ops_op(node) for node in nodes)
def visit_If_test(self, node):
return {
AstTypes.Compare.Key: self.visit_Compare,
AstTypes.Expr.Key: self.visit_Expr,
AstTypes.Name.Key: self.visit_Name,
AstTypes.BinOp.Key: self.visit_BinOp,
AstTypes.Constant.Key: self.visit_Constant
}[node[AstTypes.Generic.ast_type]](node)
def visit_Assign_target(self, node):
return self.visit_Name(node)
def visit_Assign_targets(self, nodes):
return tuple(self.visit_Assign_target(node) for node in nodes)
def visit_Assign_value(self, node):
ast_type = node[AstTypes.Generic.ast_type]
if ast_type == AstTypes.Constant.Key:
return self.visit_Constant(node)
elif ast_type == AstTypes.Name.Key:
return self.visit_Name(node)
elif ast_type == AstTypes.Call.Key:
return self.visit_Call(node)
elif ast_type == AstTypes.BinOp.Key:
return self.visit_BinOp(node)
def visit_Assign(self, node):
"""
:param node:
:return:
"""
value = self.visit_Assign_value(node[AstTypes.Assign.value])
targets = self.visit_Assign_targets(node[AstTypes.Assign.targets])
return targets, value
def visit_Call_func(self, node):
"""
:param node: {
'ast_type': 'Name',
'col_offset': 2,
'ctx': {'ast_type': 'Load'},
'id': 'c',
'lineno': 2
}
:return:
"""
return self.visit_Name(node)
def visit_Call_arg(self, node):
ast_type = node[AstTypes.Generic.ast_type]
if ast_type == AstTypes.Constant.Key:
return self.visit_Constant(node)
elif ast_type == AstTypes.Name.Key:
arg = self.visit_Name(node)
elif ast_type == AstTypes.Call.Key:
arg = self.visit_Call(node)
elif ast_type == AstTypes.BinOp.Key:
arg = self.visit_BinOp(node)
return arg
def visit_Call_args(self, nodes):
return list(self.visit_Call_arg(node) for node in nodes)
def visit_Call(self, node):
"""
:param node:
{'args': [],
'ast_type': 'Call',
'col_offset': 2,
'func': {'ast_type': 'Name',
'col_offset': 2,
'ctx': {'ast_type': 'Load'},
'id': 'c',
'lineno': 2},
'keywords': [],
'kwargs': None,
'lineno': 2,
'starargs': None
}
:return:
"""
func = self.visit_Call_func(node[AstTypes.Call.func])
args = self.visit_Call_args(node[AstTypes.Call.args])
return func, args
def visit_Expr_value(self, node):
ast_type = node[AstTypes.Generic.ast_type]
if ast_type == AstTypes.Call.Key:
return self.visit_Call(node)
elif ast_type == AstTypes.BinOp.Key:
return self.visit_BinOp(node)
elif ast_type == AstTypes.Name.Key:
return self.visit_Name(node)
elif ast_type == AstTypes.Constant.Key:
return self.visit_Constant(node)
def visit_Expr(self, node):
"""
:param node:
:return:
"""
return self.visit_Expr_value(node[AstTypes.Expr.value])
def visit_BinOp_operand(self, node):
ast_type = node[AstTypes.Generic.ast_type]
if ast_type == AstTypes.Constant.Key:
return self.visit_Constant(node)
elif ast_type == AstTypes.Name.Key:
return self.visit_Name(node)
elif ast_type == AstTypes.Call.Key:
return self.visit_Call(node)
elif ast_type == AstTypes.BinOp.Key:
return self.visit_BinOp(node)
def visit_BinOp_op(self, node):
ast_type = node[AstTypes.Generic.ast_type]
if ast_type == AstTypes.Add.Key:
return self.visit_Add(node)
else:
raise NotImplementedError
def visit_BinOp(self, node):
"""
:param node:
:return:
"""
left = self.visit_BinOp_operand(node[AstTypes.BinOp.left])
op = self.visit_BinOp_op(node[AstTypes.BinOp.op])
right = self.visit_BinOp_operand(node[AstTypes.BinOp.right])
return (left, op, right)
def visit_Compare_operand(self, node):
return {
AstTypes.Call.Key: self.visit_Call,
AstTypes.Name.Key: self.visit_Name,
AstTypes.BinOp.Key: self.visit_BinOp,
AstTypes.Constant.Key: self.visit_Constant
}[node[AstTypes.Generic.ast_type]](node)
def visit_Compare_comparators(self, nodes):
return tuple(self.visit_Compare_operand(node) for node in nodes)
def visit_Compare(self, node):
"""
:param node:
:return:
"""
left = self.visit_Compare_operand(node[AstTypes.Compare.left])
comparators = self.visit_Compare_comparators(
node[AstTypes.Compare.comparators])
ops = self.visit_Compare_ops(node[AstTypes.Compare.ops])
return (left, ops, comparators)
def visit_While(self, node):
"""
:param node:
:return:
"""
print("HERE")
test = self.visit_If_test(node[AstTypes.While.test])
body = self.visit_body(node[AstTypes.While.body])
# orelse = self.visit_body(node['orelse']) #TODO propagate to other visitors...
print("HERE RETURN BODY: ", body)
return test, body
def visit_If(self, node):
"""
:param node:
:return:
"""
test = self.visit_If_test(node[AstTypes.If.test])
body = self.visit_body(node[AstTypes.If.body])
orelse = self.visit_body(node[AstTypes.If.orelse])
return test, body, orelse
def visit_Name(self, node):
"""
:param node: {
'ast_type': 'Name',
'col_offset': 2,
'ctx': {'ast_type': 'Load'},
'id': 'b',
'lineno': 4
}
:return:
"""
return node[AstTypes.Name.id]
def visit_Constant(self, node):
"""
"""
return repr(node[AstTypes.Constant.value])
def visit_Break(self, node):
"""
:param node:
:return:
"""
return "break"
def visit_Continue(self, node):
"""
:param node:
:return:
"""
return "continue"
def visit_Add(self, node):
"""
:param node:
:return:
"""
return '+'
def visit_NotEq(self, node):
"""
:param node:
:return:
"""
return "!="
def visit_Eq(self, node):
"""
:param node:
:return:
"""
return "=="
def visit_Gt(self, node):
"""
:param node:
:return:
"""
return ">"
def visit_Lt(self, node):
"""
:param node:
:return:
"""
return "<"
def visit_Load(self, node):
"""
:param node:
:return:
"""
raise NotImplementedError(f"visitor for ast_type Load not implemented")
def visit_Store(self, node):
"""
:param node:
:return:
"""
# key = "k"
# return node[key]
raise NotImplementedError(
f"visitor for ast_type Store not implemented")
class Driver:
@staticmethod
def parser():
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("asts", type=str, nargs='+',
help="path of a JSON file containing the program slice to analyse, represented in the form of an Abstract Syntax Tree")
return parser
@staticmethod
def get_asts():
from utilities import load_json
from pathlib import Path
args = Driver.parser().parse_args()
asts = dict()
for ast in args.asts:
p = Path(ast)
asts[p] = load_json(p)
return asts
@staticmethod
def print_visit(visitor):
for p, ast in Driver.get_asts().items():
print(p)
print(visitor(ast).visit_ast())
@staticmethod
def print_ast(visitor):
from pprint import pprint
for p, ast in Driver.get_asts().items():
print(p)
visitor(ast).visit_ast()
pprint(ast)
if __name__ == '__main__':
Driver.drive(Visitor)