-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniquify.py
executable file
·163 lines (135 loc) · 5.26 KB
/
uniquify.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
import compiler
from compiler.ast import *
from explicate import Bool
nameList = [{}]
functionClassNameList = []
nameCounter = 0
def uniquify(n):
global nameDictionary
global nameCounter
if isinstance(n, Const):
return Const(n.value)
elif isinstance(n, Module):
return Module(None, uniquify(n.node))
elif isinstance(n, Stmt):
nameList.append({})
uniquifiedStmt = []
for i in range(0,len(n.nodes)):
val = uniquify(n.nodes[i])
uniquifiedStmt.append(val)
nameList.pop()
return Stmt(uniquifiedStmt)
elif isinstance(n, Bool):
return Bool(uniquify(n.expr))
elif isinstance(n, Name):
inList = False
for dic in functionClassNameList:
if (n.name in dic.keys()):
inList = True
if n.name in nameList[len(nameList) - 1]:
return Name(nameList[len(nameList) - 1][n.name])
elif inList == True:
for dic in functionClassNameList:
if (n.name in dic.keys()):
return Name(dic[n.name])
else:
return Name(n.name)
elif isinstance(n, Add):
lft = uniquify(n.left)
rgt = uniquify(n.right)
return Add((lft, rgt))
elif isinstance(n, CallFunc):
name = uniquify(n.node)
args = []
for i in range(0,len(n.args)):
val = uniquify(n.args[i])
args.append(val)
return CallFunc(name,args,None,None)
elif isinstance(n, Printnl):
uniquifiedPrintStmt = []
for i in range(0,len(n.nodes)):
val = uniquify(n.nodes[i])
uniquifiedPrintStmt.append(val)
return Printnl(uniquifiedPrintStmt,None)
elif isinstance(n, Assign):
val = uniquify(n.expr)
nodes = uniquify(n.nodes[0])
#varName = expr.nodes[0].name
return Assign([nodes],val)
elif isinstance(n, AssName):
if n.name in nameList[len(nameList) - 1]:
newName = nameList[len(nameList) - 1][n.name]
else:
newName = n.name + "_" + str(nameCounter)
nameList[len(nameList) - 1][n.name] = newName
nameCounter = nameCounter + 1
return AssName(newName, "OP_ASSIGN")
elif isinstance(n, Discard):
return Discard(uniquify(n.expr))
elif isinstance(n, UnarySub):
return UnarySub(uniquify(n.expr))
elif isinstance(n, Compare):
return Compare(uniquify(n.expr), [(n.ops[0][0], uniquify(n.ops[0][1]))])
elif isinstance(n, List):
nodes = []
for node in n.nodes:
uniquifiedNode = uniquify(node)
nodes.append(uniquifiedNode)
return List(nodes)
elif isinstance(n, Function):
newArgNames = []
if n.name in nameList[len(nameList) - 1]:
newName = nameList[len(nameList) - 1][n.name]
functionClassNameList.append({n.name : newName})
else:
newName = n.name + "_" + str(nameCounter)
nameList[len(nameList) - 1][n.name] = newName
nameCounter = nameCounter + 1
functionClassNameList.append({n.name : newName})
for i in range(0, len(n.argnames)):
if i in nameList[len(nameList) - 1]:
newArgNames.append(nameList[len(nameList) - 1][n.argnames[i]])
else:
newArgName = str(n.argnames[i]) + "_" + str(nameCounter)
nameList[len(nameList) - 1][n.argnames[i]] = newArgName
nameCounter = nameCounter + 1
newArgNames.append(newArgName)
functionClassNameList.append({n.argnames[i] : newArgName})
return Function(None, newName, newArgNames, [], 0, None, uniquify(n.code))
elif isinstance(n, Lambda):
newArgNames = []
for i in range(0, len(n.argnames)):
if i in nameList[len(nameList) - 1]:
newArgNames.append(nameList[len(nameList) - 1][n.argnames[i]])
else:
newArgName = str(n.argnames[i]) + "_" + str(nameCounter)
nameList[len(nameList) - 1][n.argnames[i]] = newArgName
nameCounter = nameCounter + 1
newArgNames.append(newArgName)
return Lambda(newArgNames, [], 0, uniquify(n.code))
elif isinstance(n, IfExp):
return IfExp(uniquify(n.test), uniquify(n.then), uniquify(n.else_))
elif isinstance(n, Return):
return Return(uniquify(n.value))
elif isinstance(n, Class):
if n.name in nameList[len(nameList) - 1]:
newName = nameList[len(nameList) - 1][n.name]
else:
newName = n.name + "_" + str(nameCounter)
nameList[len(nameList) - 1][n.name] = newName
nameCounter = nameCounter + 1
functionClassNameList.append({n.name : newName})
return Class(newName, n.bases, n.doc, uniquify(n.code))
elif isinstance(n, AssAttr):
return n
elif isinstance(n, Getattr):
return n
elif isinstance(n, If):
return If([(n.tests[0][0], uniquify(n.tests[0][1]))], uniquify(n.else_))
elif isinstance(n, While):
pass
'''
ast = compiler.parseFile("/Users/rb/GoogleDrive/School/Dropbox/CSCI4555/pyyc-foomybar/mytests/test16.py")
print "Orig: "+str(ast)
print "Uniq: "+str(uniquify(ast))
'''