-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniGConv.py
255 lines (231 loc) · 8.62 KB
/
UniGConv.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
"""
Universal Gate Converter
Author: Duc Doan
Version: 1.0 - 04/16/2021
A script to convert logical expressions in Logisim format into NAND/NOR-only expressions.
v1.0 - 04/16/2021: support verbose mode 0 - output expression contains AND/OR, NOT, NAND/NOR
(not 100% universal gates but human readable)
"""
import re
import sys
def preprocess(r):
"""
Function to remove all redundant spaces except for those meaning operator AND
:param r: string : input logical expression
:return: logical expression removed all redundant spaces
"""
r = re.sub(r"(?<=[(+~])\s*|\s*(?=[)+])", "", r) # remove space after/before certain characters
r = re.sub(r"\s+", " ", r) # remove other redundant spaces, leaving only 1
return r
def has_global_paren(exp):
if exp[:2] == "~(" or exp[0] == '(':
i = 0
level = 0
while i < len(exp):
if exp[i] == '(':
level += 1
elif exp[i] == ')':
level -= 1
if level == 0 and i != len(exp) - 1:
return False
i += 1
return True
else:
return False
def conv(r, mode, _verbose=0):
"""
Wrapper function to convert a logical expression to universal gates format
:param r: string : input logic expression containing ~/./+ only
:param mode: string : "nand" or "nor"
:param verbose: int : 0 : results may include and/or, not, nand/nor (default),
1 : results may include not, nand/nor,
2 : results only include nand/nor
:return: the expression in nand/nor format
"""
if mode == "nand":
return raw_to_nand(r, _verbose)
elif mode == "nor":
return raw_to_nor(r, _verbose)
else:
raise Exception("Mode can only be nand or nor")
def raw_to_nand(r, _verbose=0):
r = preprocess(r)
r = '(' + r + ')'
if _verbose == 0:
i = 0
i_stack = [] # index stack
t_stack = [] # sub-expression stack
inverted = False # does this sub-expression already have a ~?
t = "" # sub-expression
while i < len(r):
ch = r[i]
if ch == '(':
i_stack.append(i)
t_stack.append(t)
t = ""
if i > 0 and r[i-1] == '~':
inverted = True
elif ch == ')':
start_index = i_stack.pop()
t_prev = t_stack.pop()
# process t
if '+' not in t:
if inverted:
pass
else: # break redundant parentheses
try:
r = r[:start_index] + t + r[i+1:]
i -= 2
except IndexError:
r = r[:start_index] + t
# return here?
else:
dm = deMorgan(t, "nand")
if start_index == 0:
return dm
if inverted:
r = r[:start_index-1] + dm[2:-1] + r[i+1:] # invert invert = no invert
i += len(dm)-3 - (i - start_index + 1) - 1
start_index -= 1 # disregard the ~
t_prev = t_prev[:-1] # disregard the ~
else:
r = r[:start_index] + dm[:-1] + r[i:] # do not include opening paren
i += len(dm)-1 - (i - start_index)
t = t_prev + r[start_index:i+1]
else:
t += ch
i += 1
return r[1:-1] if has_global_paren(r) and r[0] == '(' else r
else:
# placeholder
return ""
def raw_to_nor(r, _verbose=0):
r = raw_to_nand(r)
r = '(' + r + ')'
if _verbose == 0:
i = 0
i_stack = [] # index stack
t_stack = [] # sub-expression stack
inverted = False # does this sub-expression already have a ~?
t = "" # sub-expression
while i < len(r):
ch = r[i]
if ch == '(':
i_stack.append(i)
t_stack.append(t)
t = ""
if i > 0 and r[i - 1] == '~':
inverted = True
elif ch == ')':
start_index = i_stack.pop()
t_prev = t_stack.pop()
# process t
if ' ' not in t:
if inverted:
pass
else: # break redundant parentheses
try:
r = r[:start_index] + t + r[i + 1:]
i -= 2
except IndexError:
r = r[:start_index] + t
# return here?
else:
dm = deMorgan(t, "nor")
if start_index == 0:
return dm
if inverted:
r = r[:start_index - 1] + dm[2:-1] + r[i + 1:] # invert invert = no invert
i += len(dm) - 3 - (i - start_index + 1) - 1
start_index -= 1 # disregard the ~
t_prev = t_prev[:-1] # disregard the ~
else:
r = r[:start_index] + dm[:-1] + r[i:] # do not include opening paren
i += len(dm) - 1 - (i - start_index)
t = t_prev + r[start_index:i + 1]
else:
t += ch
i += 1
return r[1:-1] if has_global_paren(r) and r[0] == '(' else r
else:
# placeholder
return ""
def deMorgan(expression, mode):
"""
Implementation of de Morgan's theorem
:param expression: string : the logic expression
:param mode: string : target gate (nand/nor)
:return: the transformed expression using de Morgan theorem
"""
res_operands = []
if mode == "nand":
operands = expression.split('+')
for op in operands:
# if this operand contains more than 1 term
if ' ' in op:
if has_global_paren(op):
if op[0] == '~':
res_operands.append(op[2:-1])
else:
res_operands.append('~' + op)
else:
res_operands.append('~({})'.format(op))
else:
if op[0] == '~':
res_operands.append(op[1:])
else:
res_operands.append('~' + op)
return "~(" + ' '.join(res_operands) + ')'
elif mode == "nor":
operands = expression.split(' ')
for op in operands:
# if this operand contains more than 1 term
if '+' in op:
# TODO: make sure that all operands of + must be parenthesized,
# below's just a quick fix and may have bug
# if op[0] == '~':
# res_operands.append(op[2:-1])
# elif op[0] == '(':
# res_operands.append('~'+op)
# else:
# res_operands.append('~({})'.format(op))
# lazy fix
if has_global_paren(op):
if op[0] == '~':
res_operands.append(op[2:-1])
else:
res_operands.append('~' + op)
else:
res_operands.append('~({})'.format(op))
else:
if op[0] == '~':
res_operands.append(op[1:])
else:
res_operands.append('~' + op)
return "~(" + '+'.join(res_operands) + ')'
else:
raise Exception("Mode can only be nand or nor")
if __name__ == "__main__":
verbose = 0
args = sys.argv[1:]
if len(args) == 0:
pass
elif args[0] == "-v" or args[0] == "--verbose":
print("Verbose modes other than 0 is not available in this version. Defaulting to 0")
# verbose = int(args[1])
verbose = 0
else:
raise Exception("Invalid argument")
print("UniGConv v1.0")
print("Verbose mode: {}".format(verbose))
print("Enter [mode]:[expression] to convert, q to quit. Note: [mode] is nand/nor, [expression] is in Logisim format")
print()
print("Your command: ")
ip = input()
while ip != 'q':
ip_arr = ip.split(':')
res = conv(ip_arr[1], ip_arr[0], verbose)
print("Result: {}".format(res))
print("\nYour command: ")
ip = input()
quit()