292 lines
12 KiB
Python
292 lines
12 KiB
Python
import sys
|
|
|
|
ap_count = 0
|
|
|
|
file_write = ["module Prop where\nimport Clash.Prelude\nimport ProcessingElement(processingElement)\nimport Queue(queue,queuetest)\nimport Queue30(queue30,queuetest30)\n\n"]
|
|
tt_temp = {'!':["not","(1,(31,31,0,0,31,31,31,31,31,31),(0,0,31,31,31,31,31,31,31,31))"],
|
|
'G':["box","(1,(31,31,31,31,0,0,t2,t2,31,31),(31,31,t1,t2,0,0,31,31,31,31))"],
|
|
'F':["diamond","(1,(t1,t2,31,31,0,0,31,31,31,31),(31,31,31,31,0,0,31,31,t2,t2))"],
|
|
'H':["box","(1,(31,31,31,31,0,0,t2,t2,31,31),(31,31,0,t2,0,0,31,31,31,31))"],
|
|
'E':["diamond","(1,(0,t2,31,31,0,0,31,31,31,31),(31,31,31,31,0,0,31,31,t2,t2))"],
|
|
'X':["next","(1,(1,1,31,31,0,0,31,31,31,31),(31,31,1,1,0,0,31,31,31,31))"],
|
|
'v':["or","(0,(0,0,31,31,31,31,31,31,31,31),(31,31,0,0,31,31,31,31,31,31))"],
|
|
'U':["(1,(31,31,31,31,0,0,31,31,31,31),(31,31,0,t1-1,31,31,31,31,31,31))",
|
|
"(1,(31,31,31,31,31,31,t1,t2,31,31),(31,31,31,31,31,31,31,31,t2,t2))",
|
|
"(0,(31,31,31,31,31,31,31,31,31,31),(31,31,31,31,31,31,31,31,t1,t2))"]}
|
|
|
|
|
|
class node:
|
|
def __init__(self,val,left=None,right=None,t1=0,t2=0,isbinary=0,isunary=0,isap=0,q_no=0,q_mealy="",pe_mealy=""):
|
|
self.val=val
|
|
self.left=left
|
|
self.right=right
|
|
self.t1 = t1
|
|
self.t2 = t2
|
|
self.isbinary = isbinary
|
|
self.isunary = isunary
|
|
self.isap = isap
|
|
self.q_no = q_no
|
|
self.q_mealy = q_mealy
|
|
self.pe_mealy = pe_mealy
|
|
|
|
def precedence(op):
|
|
if op == '!':
|
|
return 11
|
|
elif op == 'G':
|
|
return 10
|
|
elif op == 'H':
|
|
return 9
|
|
elif op == 'F':
|
|
return 8
|
|
elif op == 'E':
|
|
return 7
|
|
elif op == 'X':
|
|
return 6
|
|
elif op == 'U':
|
|
return 5
|
|
elif op == '^':
|
|
return 4
|
|
elif op == 'v':
|
|
return 3
|
|
elif op == '>':
|
|
return 2
|
|
else:
|
|
return 1
|
|
|
|
root = [node('')]
|
|
|
|
def construc_tree(r,expr):
|
|
cur = r
|
|
parent = []
|
|
i = 0
|
|
while i < len(expr):
|
|
if expr[i] in ['E','H','X','!','G','F']:
|
|
cur.isunary = 1
|
|
if expr[i] in ['E','H']:
|
|
cur.t1 = 0
|
|
cur.t2 = int(expr[i+1])
|
|
elif expr[i] in ['G','F']:
|
|
temp = expr[i:-1].index(']')+i
|
|
cur.t1, cur.t2 = map(int , expr[i+2:temp].split(','))
|
|
elif expr[i] in ['U','^','v','>']:
|
|
cur.isbinary = 1
|
|
if expr[i] in ['G','F','U']:
|
|
temp = expr[i:-1].index(']')+i
|
|
cur.t1, cur.t2 = map(int , expr[i+2:temp].split(','))
|
|
if expr[i] in ['^','v','>','U','G','F','H','E','U','X','!']:
|
|
# first check the precedence of the operator to his parent operator
|
|
# if the precedence of the operator is less than the parent operator
|
|
# then pop till the parent operator and make it the left child of the operator
|
|
# if the parent is empty then make the operator as the root
|
|
# if the parent is not empty then make the operator as the right child of the parent
|
|
# and make the parent as the parent of the operator
|
|
# make the operator as the current node
|
|
par = None
|
|
ch = False
|
|
|
|
while parent and (precedence(expr[i]) < precedence(parent[-1].val)):
|
|
par = parent.pop()
|
|
ch = True
|
|
|
|
if ch:
|
|
new = node(expr[i])
|
|
if parent:
|
|
temp = parent[-1].right
|
|
parent[-1].right = new
|
|
new.left = temp
|
|
else:
|
|
new.left = root[0]
|
|
root[0] = new
|
|
|
|
parent.append(new)
|
|
new.right = node('')
|
|
cur = new.right
|
|
else:
|
|
cur.val = expr[i]
|
|
cur.right = node('')
|
|
parent.append(cur)
|
|
cur = cur.right
|
|
else:
|
|
if expr[i].isalpha():
|
|
cur.left = node(expr[i])
|
|
cur.left.isap = 1
|
|
|
|
i =i+ 1
|
|
|
|
|
|
def manage_implies_and(root):
|
|
if root != None and root != "":
|
|
manage_implies_and(root.left)
|
|
manage_implies_and(root.right)
|
|
if root.val == '>':
|
|
new = node('!')
|
|
new.isunary = 1
|
|
root.val = 'v'
|
|
root.isbinary = 1
|
|
new.left = root.left
|
|
elif root.val == '^':
|
|
new1 = node('!')
|
|
new1.isunary = 1
|
|
new2 = node('!')
|
|
new2.isunary = 1
|
|
new3 = node('v')
|
|
new3.isbinary = 1
|
|
root.val = '!'
|
|
new1.left = root.left
|
|
new2.left = root.right
|
|
new3.left = new1
|
|
new3.right = new2
|
|
root.left = new3
|
|
root.isbinary = 0
|
|
root.isunary = 1
|
|
root.right = None
|
|
|
|
|
|
file = "property.txt"
|
|
f = open(file, "r")
|
|
data = f.readlines()
|
|
ex = data[0][:-1]
|
|
construc_tree(root[0],ex)
|
|
manage_implies_and(root[0])
|
|
|
|
Q_ID = 1
|
|
|
|
def post_order(root):
|
|
global Q_ID
|
|
global ap_count
|
|
if root != None:
|
|
post_order(root.left)
|
|
post_order(root.right)
|
|
if root != None and root != "" and (not (root.isap == 0 and root.isbinary == 0 and root.isunary == 0)) and (root.val != ""):
|
|
print("-" + root.val+"-")
|
|
print(root.t1)
|
|
print(root.t2)
|
|
print(root.isap)
|
|
print(root.isunary)
|
|
print(root.isbinary)
|
|
|
|
if root.isap == 0:
|
|
t = '0:>Nil'
|
|
t1 = ""
|
|
for x in range(root.t2):
|
|
t2 = "0:>"
|
|
t1 = t1 + t2
|
|
t = t1+t
|
|
if root.val != 'U':
|
|
print("queue_mealy_"+str(Q_ID)+" inp = mealy queue ("+t+") inp")
|
|
file_write.append("queue_mealy_"+str(Q_ID)+" inp = mealy queue ("+t+") inp\n")
|
|
|
|
elif root.val == 'U':
|
|
print("queue_mealy_"+str(Q_ID)+" inp = mealy queue30 ("+t+") inp")
|
|
file_write.append("queue_mealy_"+str(Q_ID)+" inp = mealy queue30 ("+t+") inp\n")
|
|
root.q_mealy = "queue_mealy_"+str(Q_ID)
|
|
root.q_no = Q_ID
|
|
Q_ID+=1
|
|
elif root.isap == 1:
|
|
print('inp'+root.val)
|
|
root.q_mealy = 'inp'+root.val
|
|
ap_count+=1
|
|
|
|
if root.val in ['H','E']:
|
|
tt_temp[root.val][1] = tt_temp[root.val][1].replace("t2",str(root.t2))
|
|
elif root.val in ['G','F']:
|
|
tt_temp[root.val][1] = tt_temp[root.val][1].replace("t1",str(root.t1))
|
|
tt_temp[root.val][1] = tt_temp[root.val][1].replace("t2",str(root.t2))
|
|
elif root.val in ['U']:
|
|
tt_temp[root.val][0] = tt_temp[root.val][0].replace("t1-1",str(root.t1-1))
|
|
tt_temp[root.val][1] = tt_temp[root.val][1].replace("t1",str(root.t1))
|
|
tt_temp[root.val][1] = tt_temp[root.val][1].replace("t2",str(root.t2))
|
|
tt_temp[root.val][2] = tt_temp[root.val][2].replace("t1",str(root.t1))
|
|
tt_temp[root.val][2] = tt_temp[root.val][2].replace("t2",str(root.t2))
|
|
|
|
if root.isap == 0:
|
|
if root.val != 'U':
|
|
file_write.append("processingElement_mealy_"+tt_temp[root.val][0]+str(root.q_no)+" inp = mealy processingElement "+tt_temp[root.val][1] +" inp\n\n")
|
|
print("processingElement_mealy_"+tt_temp[root.val][0]+str(root.q_no)+" inp = mealy processingElement "+tt_temp[root.val][1] +" inp\n")
|
|
root.pe_mealy = "processingElement_mealy_"+tt_temp[root.val][0]+str(root.q_no)
|
|
elif root.val == 'U':
|
|
file_write.append("processingElement_mealy_until1"+str(root.q_no)+" inp = mealy processingElement "+ tt_temp[root.val][0]+" inp\n")
|
|
file_write.append("processingElement_mealy_until2"+str(root.q_no)+" inp = mealy processingElement "+ tt_temp[root.val][1]+" inp\n")
|
|
file_write.append("processingElement_mealy_until3"+str(root.q_no)+" inp = mealy processingElement "+ tt_temp[root.val][2]+" inp\n\n")
|
|
file_write.append("until_out"+str(root.q_no)+" (inp1,inp2) = "+root.q_mealy+" (bundle (processingElement_mealy_until1"+str(root.q_no)+" (bundle ( inp1, inp1)), processingElement_mealy_until2"+str(root.q_no)+" (bundle ( inp2, inp2)), processingElement_mealy_until3"+str(root.q_no)+" (bundle ( inp1, inp2))))\n\n")
|
|
print("processingElement_mealy_until1"+str(root.q_no)+" inp = mealy processingElement "+ tt_temp[root.val][0]+" inp")
|
|
print("processingElement_mealy_until2"+str(root.q_no)+" inp = mealy processingElement "+ tt_temp[root.val][1]+" inp")
|
|
print("processingElement_mealy_until3"+str(root.q_no)+" inp = mealy processingElement "+ tt_temp[root.val][2]+" inp\n")
|
|
print("until_out"+str(root.q_no)+" (inp1,inp2) = "+root.q_mealy+" (bundle (processingElement_mealy_until1"+str(root.q_no)+" (bundle ( inp1, inp1)), processingElement_mealy_until2"+str(root.q_no)+" (bundle ( inp2, inp2)), processingElement_mealy_until3"+str(root.q_no)+" (bundle ( inp1, inp2))))\n")
|
|
root.pe_mealy = "until_out"+str(root.q_no)
|
|
elif root.isap == 1:
|
|
print('inp'+root.val)
|
|
root.pe_mealy = 'inp'+root.val
|
|
print('\n')
|
|
|
|
|
|
post_order(root[0])
|
|
|
|
prop = ""
|
|
|
|
def final_prop(root):
|
|
if root != None and root != "" and (not (root.isap == 0 and root.isbinary == 0 and root.isunary == 0)) and (root.val != ""):
|
|
if root.val != 'U':
|
|
if root.isap == 1:
|
|
return(root.pe_mealy)
|
|
elif root.isunary == 1:
|
|
if root.left != None:
|
|
return(root.q_mealy + " ( " + root.pe_mealy + " ( bundle ( (" + final_prop(root.left) + ") , (" + final_prop(root.left) + ") ) ) )")
|
|
if root.right != None:
|
|
return(root.q_mealy + " ( " + root.pe_mealy + " ( bundle ( (" + final_prop(root.right) + ") , (" + final_prop(root.right) + ") ) ) )")
|
|
elif root.isbinary == 1:
|
|
return(root.q_mealy + " ( " + root.pe_mealy + " ( bundle ( (" + final_prop(root.left) + ") , (" + final_prop(root.right) + ") ) ) )")
|
|
elif root.val == 'U':
|
|
return(root.pe_mealy+" ( "+"("+final_prop(root.left)+") , ("+final_prop(root.right)+") )")
|
|
elif root != None and root.val == "":
|
|
if root.left != None:
|
|
return(final_prop(root.left))
|
|
|
|
else:
|
|
return(final_prop(root.right))
|
|
else:
|
|
return("")
|
|
|
|
|
|
property = "prop ("
|
|
|
|
def prop_lhs(root):
|
|
global property
|
|
if root != None and root != "":
|
|
prop_lhs(root.left)
|
|
prop_lhs(root.right)
|
|
if root.isap == 1:
|
|
property = property + "inp"+root.val+","
|
|
|
|
prop_lhs(root[0])
|
|
|
|
#https://stackoverflow.com/questions/423379/using-global-variables-in-a-function
|
|
def replacer(s, newstring, index, nofail=False):
|
|
# raise an error if index is outside of the string
|
|
if not nofail and index not in range(len(s)):
|
|
raise ValueError("index outside given string")
|
|
# if not erroring, but the index is still not in the correct range..
|
|
if index < 0: # add it to the beginning
|
|
return newstring + s
|
|
if index > len(s): # add it to the end
|
|
return s + newstring
|
|
# insert the new string between "slices" of the original
|
|
return s[:index] + newstring + s[index + 1:]
|
|
|
|
property = replacer(property,') = ',len(property)-1)
|
|
|
|
property = property + final_prop(root[0]) + '\n\n'
|
|
file_write.append(property)
|
|
print(property)
|
|
|
|
n_ips = ""
|
|
for i in range(ap_count):
|
|
if ap_count-1 == i:
|
|
n_ips = n_ips + "Signal System (Bool)"
|
|
else:
|
|
n_ips = "Signal System (Bool)" + " , " + n_ips
|
|
file_write.append("topEntity\n :: Clock System\n -> Reset System\n -> Enable System\n -> ("+n_ips+")\n -> Signal System ( Bool)\ntopEntity = exposeClockResetEnable prop\n\n")
|
|
|
|
f = open("Prop.hs", "w")
|
|
f.writelines(file_write)
|
|
f.close() |