-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps_visitor.py
69 lines (41 loc) · 1.77 KB
/
ps_visitor.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
"""
"""
from visitors import Visitor, AstTypes
class Keys:
CONDITIONS = "conditions"
default_conditions = [(True, True)]
class PathSensitivityVisitor(Visitor):
def __init__(self, ast):
super(PathSensitivityVisitor, self).__init__(ast)
self.super = super(PathSensitivityVisitor, self)
self._current_conditions = default_conditions.copy()
self._conditions = default_conditions.copy()
@property
def conditions(self):
return self._conditions
def visit_body_line(self, node):
node[Keys.CONDITIONS] = self._current_conditions.copy()
return self.super.visit_body_line(node)
def visit_Compare(self, node):
(left, ops, comparators) = self.super.visit_Compare(node)
return f"{left} {','.join(ops)} {','.join(comparators)}"
def visit_While(self, node):
test = self.visit_If_test(node[AstTypes.While.test])
condition = (test, True)
self._conditions.append(condition)
self._current_conditions.append(condition)
body = self.visit_body(node[AstTypes.While.body])
# self._current_conditions.pop() # TODO find when to terminate loop
return test, body
def visit_If(self, node):
test = self.visit_If_test(node[AstTypes.If.test])
condition = (test, True)
self._conditions.append(condition)
self._current_conditions.append(condition)
body = self.visit_body(node[AstTypes.If.body])
condition = (test, False)
self._conditions.append(condition)
self._current_conditions[-1] = condition
orelse = self.visit_body(node[AstTypes.If.orelse])
self._current_conditions.pop()
return test, body, orelse