-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc_visitor.py
72 lines (43 loc) · 2.2 KB
/
src_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
70
71
72
import visitors
class SrcVisitor(visitors.Visitor):
def visit_Module(self, node):
self.indentation_level=0
return "\n".join(self.visit_body(node['body']))
def visit_Assign(self, node):
targets, value = super(SrcVisitor, self).visit_Assign(node)
return f"{self.indentation_level * SrcVisitor.INDENTATION}{','.join(targets)} = {value}"
def visit_Call(self, node):
func, args = super(SrcVisitor, self).visit_Call(node)
return f"{func}({','.join(args)})"
def visit_Expr(self, node):
value = super(SrcVisitor, self).visit_Expr(node)
return f"{self.indentation_level * SrcVisitor.INDENTATION}{value}"
def visit_BinOp(self, node):
(left, op, right) = super(SrcVisitor, self).visit_BinOp(node)
return f"{left} {op} {right}"
def visit_Compare(self, node):
(left, ops, comparators) = super(SrcVisitor, self).visit_Compare(node)
return f"{left} {','.join(ops)} {','.join(comparators)}"
def visit_While(self, node):
indentation = self.indentation_level * SrcVisitor.INDENTATION
self.indentation_level += 1
(test, body) = super(SrcVisitor, self).visit_While(node)
self.indentation_level -= 1
representation = f"{indentation}while ({test}):\n"
representation += "\n".join(body)
return representation
def visit_If(self, node):
indentation = self.indentation_level * SrcVisitor.INDENTATION
self.indentation_level += 1
(test, body, orelse) = super(SrcVisitor, self).visit_If(node)
self.indentation_level -= 1
representation = f"{indentation}if({test}):\n"
representation += "\n".join(body)
if orelse:
representation += f"\n{indentation}else:\n"
representation += "\n".join(orelse)
return representation
def visit_Break(self, node):
return f"{self.indentation_level * SrcVisitor.INDENTATION}{super(SrcVisitor, self).visit_Break(node)}"
def visit_Continue(self, node):
return f"{self.indentation_level * SrcVisitor.INDENTATION}{super(SrcVisitor, self).visit_Continue(node)}"