-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.py
272 lines (257 loc) · 8.5 KB
/
bridge.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
# This Program takes a .tfc file as input, generates all possible gate level equations,
# generates all possible input permutations and display all gate level output matrices
import re
def dels():
readFile = open("/home/joy/Desktop/test/genf.py")
lines = readFile.readlines()
readFile.close()
w = open("/home/joy/Desktop/test/genf.py", 'w')
w.writelines(lines[:-3])
w.close()
count = 0
name = '/home/joy/Desktop/test/a.tfc'
with open('/home/joy/Desktop/test/main.txt', 'r') as fin:
data = fin.read().splitlines(True)
with open('/home/joy/Desktop/test/main.txt', 'w') as fout:
fout.writelines(data[1:])
with open('/home/joy/Desktop/test/a.tfc', 'r') as datafile:
for line in datafile:
line1 = line.strip()
if '.v' in line1:
aa = line1
bb = aa
bb = bb[3:]
cc = re.split(',', bb)
asd = len(cc)
asd1 = str(asd)
break
def neg_ctl(hexo):
for mns in range(len(hexo)):
hu = str(hexo[mns])
if hu == ",":
continue
mk = len(hexo)
mk -= 1
if mns == mk:
break
if "'" not in hu:
continue
hs = re.split("'", hu)
hs_fin = list()
hs_fin.append(hs[0])
hs_fin.append(' = not ')
hs_fin.append(hs[0])
hss = ''.join(hs_fin)
jk = 'if ' + hs[0] + ' == 1: '
jk1 = ''.join(jk)
lo = jk1 + hss
garbage.write(' ' + lo + '\n')
def strings(bc):
bc = list(bc)
for j in range(len(bc)):
if bc[j] == ',':
continue
else:
bc[j] = 'str(' + bc[j] + ')'
garbage = open('/home/joy/Desktop/test/genf.py', 'w')
garbage.write("import itertools" + "\n")
garbage.write("import pyexcel as pe" + "\n")
garbage.write("\n")
garbage.write("outs=open('faulty_output.txt', 'w')" + "\n")
garbage.write("a = 2 ** " + asd1 + "\n")
garbage.write("total=list()" + "\n")
garbage.write("\n")
garbage.write("\n")
garbage.write("def truth_push(input_result):" + "\n")
garbage.write(" newstring = str(input_result[0])" + "\n")
garbage.write(" for ch in range(1,len(input_result)):" + "\n")
garbage.write(" newstring+=str(input_result[ch])" + "\n")
garbage.write(" levels.append(newstring)")
garbage.write("\n")
garbage.write("\n")
garbage.write("def truth_fix(input_result):" + "\n")
garbage.write(" for n, i in enumerate(input_result):" + "\n")
garbage.write(" if i == True:" + "\n")
garbage.write(" result[n] = 1" + "\n")
garbage.write(" if i == False:" + "\n")
garbage.write(" result[n] = 0" + "\n")
garbage.write("\n")
garbage.write("def getheader(cc):" + "\n")
garbage.write(" che=['Level-0']" + "\n")
garbage.write(" for ch in range(cc):" + "\n")
garbage.write(" che.append('Level - '+str(ch+1))" + "\n")
garbage.write(" if ch == cc-1:" + "\n")
garbage.write(" che.append('Level-'+str(ch+2))" + "\n")
garbage.write(" return che")
garbage.write("\n")
garbage.write("\n")
garbage.write("def getheaders(cc,lines):"+"\n")
garbage.write(" che=[lines]"+"\n")
garbage.write(" for ch in range(cc):"+"\n")
garbage.write(" che.append(lines)"+"\n")
garbage.write(" if ch == cc-1:"+"\n")
garbage.write(" che.append(lines)"+"\n")
garbage.write(" return che")
garbage.write("\n")
garbage.write("\n")
garbage.write("\n")
garbage.write("lines =str('"+bb+"')")
garbage.write("\n")
garbage.write("lines=lines.replace(',','')")
garbage.write("\n")
garbage.write("\n")
garbage.write("testPatterns = table = list(itertools.product([0, 1], repeat=" + asd1 + "))" + "\n")
garbage.write("for p in testPatterns:" + "\n")
garbage.write(" levels = list()" + "\n")
garbage.write(' ' + bb + ' = p' + '\n')
garbage.write(" result = [" + bb + "]\n")
garbage.write(" truth_push(result)" + "\n")
with open('/home/joy/Desktop/test/main.txt', 'r+') as exp:
for lenn in exp:
test = 0
count += 1
garbage.write("\n")
if len(lenn) == 1:
if lenn == '\n':
count -= 1
continue
if lenn == ' \n':
count -= 1
continue
ui = len(lenn)
ax = lenn
axx = re.split(',', ax)
mn = len(axx)
mn -= 1
axx1 = str(axx[mn])
axx2 = re.split('\n', axx1)
axx[mn] = axx2[0]
mn1 = len(axx)
mn2 = mn1 - 1
axx[mn2] = axx2[0]
lenn = axx
linen = list()
for ii in range(len(lenn)):
linen.append(lenn[ii])
linen.append(',')
del linen[-1]
lenn = linen
final_len = len(lenn)
if len(lenn) == 1:
benn = list(lenn)
benn.append(' =')
benn.append(' not')
benn.append(' ')
benn.append(benn[0])
benn1 = ''.join(benn)
garbage.write(' ' + benn1 + '\n')
if len(lenn) == 3:
tren = list(lenn)
if tren[0] == '0':
garbage.write(' ' + tren[2] + '=0' + '\n')
count -= 1
continue
if tren[0] == '1':
garbage.write(' ' + tren[2] + '=1' + '\n')
count -= 1
continue
nn = len(tren)
tren1 = list(tren[nn - 1])
tren1.append(' = ')
tren1.append(tren[0])
tren1.append(' ')
tren1.append('^')
tren1.append(' ')
tren1.append(tren[nn - 1])
tren2 = ''.join(tren1)
if "'" in tren2:
vss = re.split("'", vs)
vss1 = ''.join(vss)
tren2 = vss1
garbage.write(' ' + tren2 + '\n')
if "'" in str(lenn):
neg_ctl(lenn)
if len(lenn) > 3:
list1 = list(lenn)
if list1[0] == '&':
dels()
garbage.write(' ' + list1[2] + '=' + list1[2] + ' and ' + list1[4])
garbage.write('\n')
garbage.write(' ' + list1[4] + '=' + list1[2])
count -= 1
garbage.write("\n #Bridging\n")
test = 1
continue
if list1[0] == '^':
dels()
garbage.write(' ' + list1[2] + '=' + list1[2] + ' or ' + list1[4])
garbage.write('\n')
garbage.write(' ' + list1[4] + '=' + list1[2])
count -= 1
garbage.write("\n #Bridging\n")
test = 1
continue
num = len(list1)
insert1 = num - 1
list2 = list1[insert1]
list3 = list(list2)
list3.append(' =')
list3.append(' (')
hg = len(list1)
la_el = list1[hg - 1]
list1.insert(0, la_el)
list1.insert(1, ' =')
list1.insert(2, ' (')
hg1 = len(list1)
list1[hg1 - 2] = '^ '
list1.insert(hg1 - 2, ') ')
z = 4
ven = len(list1)
for i in list1:
list1[z] = ' and '
z += 2
if z == ven - 3:
break
qwerty = ''.join(list1)
vs = ''.join(list1)
if "'" in vs:
vss = re.split("'", vs)
vss1 = ''.join(vss)
qwerty = vss1
garbage.write(' ' + qwerty + '\n')
if "'" in str(lenn):
neg_ctl(lenn)
if test == 0:
garbage.write(" result = [" + bb + "]\n")
garbage.write(" truth_fix(result)" + "\n")
garbage.write(" truth_push(result)" + "\n")
garbage.write(" total.append(levels)" + "\n")
garbage.write('count = {0}'.format(str(count-1)))
garbage.write("\n")
garbage.write("\n")
garbage.write("che=list()")
garbage.write("\n")
garbage.write("\n")
garbage.write("che=getheader(count)")
garbage.write("\n")
garbage.write("\n")
garbage.write("\n")
garbage.write("ches=getheaders(count,lines)")
garbage.write("\n")
garbage.write("\n")
garbage.write("total.insert(0,che)")
garbage.write("\n")
garbage.write("\n")
garbage.write("total.insert(1,ches)")
garbage.write("\n")
garbage.write("\n")
garbage.write("sheet=pe.Sheet(total)")
garbage.write("\n")
garbage.write("\n")
garbage.write("outs.write(str(sheet.content))")
garbage.write("\n")
garbage.write("\n")
garbage.write("sheet.save_as('/home/joy/Desktop/test/testsf.csv')")
garbage.write("\n")
garbage.close()
execfile('/home/joy/Desktop/test/genf.py')