补充优化

This commit is contained in:
Guan Inf 2025-06-12 17:27:14 +08:00
parent eef872c3f8
commit d483423803
2 changed files with 120 additions and 15 deletions

View File

@ -1,13 +1,27 @@
from block import *
def is_f(s):
return '.' in s or 'e' in s.lower()
def tp(sth):
if sth ==None:
return -1
if is_f(sth):
return 2
if sth[0] >='0' and sth[0] <='9':
return 2
if sth[0] != '_':
return 0
return 1
return 1
return 0
def f(op,a,b):
if op =='+':
return float(a)+float(b)
elif op =='-':
return float(a)-float(b)
elif op =='*':
return float(a)*float(b)
elif op =='/':
return float(a)/float(b)
return 0
class DAGNode:
cnt = 1
def __init__(self,tag=None,l=None,r=None,op=None):
@ -29,9 +43,12 @@ class DAGNode:
return tag in self.other_tag
class Optimizer:
def __init__(self):
self.nodes = [""]
self.nodes = ["empty"]
self.cnt = 0
self.abset = dict()
self._set = dict()
self.ans= []
self.cnt_abset = 0
def cfind(self,tag):
for i in range(1,self.cnt+1)[::-1]:
if self.nodes[i].has(tag):
@ -53,33 +70,116 @@ class Optimizer:
self.cnt += 1
self.nodes.append(DAGNode(op=op, l=lc, r=rc))
return self.nodes[self.cnt]
def cfindeq(self,tag):
for i in range(1,self.cnt+1)[::-1]:
if self.nodes[i].has(tag):
return [1,self.nodes[i]]
self.cnt += 1
node = DAGNode()
self.nodes.append(node)
return [0,node]
def build(self,lst):
fg = False
for _ in lst:
if tp(_.d)==1 and self.abset.get(_.d) is None:
self.abset[_.d] = True
self.cnt_abset += 1
self.ans = []
for _ in lst:
op,a,b,res = _.a,_.b,_.c,_.d
if op =='=':
# 将ans插入到a的节点中
self.cfind(a).insert(res)
elif op =='+' or op=='*' or op=='&' or op=='^' or op =='|' or op =='&&' or op =='||' or op =='==' or op == '!=':
num,node = self.cfindeq(a)
if num == 0:
node.main_tag = a
node.other_tag.add(res)
node.other_tag.add(a)
else:
node.insert(res)
if node.op is None:
node.op = '='
elif op =='+' or op=='*' or op =='==' or op == '!=':
# 首先找一下有没有a b节点 , 没有就自己造一个
aNode = self.cfind(a)
bNode = self.cfind(b)
if tp(aNode.main_tag) == tp(bNode.main_tag) and tp(aNode.main_tag) == 2:
val = (f(op,aNode.main_tag,bNode.main_tag))
if is_f(aNode.main_tag) or is_f(bNode.main_tag):
val = str(val)
else:
val = str(int(val))
self.cnt += 1
Node = DAGNode(val)
Node.insert(res)
Node.op = '='
self.nodes.append(Node)
continue
# 然后找一下有没有a op b 的节点 , 有的话插进去
# 这里并不能根据a ,b 来查找 , 因为昔人已乘黄鹤去 , 你找到的或许只是曾经的ab
self.cfind_op1(op,aNode,bNode).insert(res)
elif op =='-' or op=='%' or op=='/':
# 这种不换的元素符
self.cfind_op2(op,self.cfind(a),self.cfind(b)).insert(ans)
pass
aNode = self.cfind(a)
bNode = self.cfind(b)
if tp(aNode.main_tag) == tp(bNode.main_tag) and tp(aNode.main_tag) == 2:
val = (f(op,aNode.main_tag,bNode.main_tag))
print(aNode.main_tag,bNode.main_tag)
if is_f(aNode.main_tag) or is_f(bNode.main_tag):
val = str(val)
else:
val = str(int(val))
self.cnt += 1
Node = DAGNode(val)
Node.insert(res)
Node.op = '='
self.nodes.append(Node)
continue
self.cfind_op2(op,self.cfind(a),self.cfind(b)).insert(res)
# 剩下就是while end , if end , el等 , 不用管
for i in range(1,self.cnt+1)[::-1]:
_ = self.nodes[i]
op = _.op
if op is not None:
self.ans.append(FourTuple(op,_.lc.main_tag,_.rc.main_tag,_.main_tag))
if op is not None or len(_.other_tag)>1:
if _.lc is not None and _.rc is not None:
if tp(_.main_tag) == 1:
if self.abset[_.main_tag] == True:
self.ans.append(FourTuple(op,_.lc.main_tag,_.rc.main_tag,_.main_tag))
self.abset[_.main_tag] = False
self.cnt_abset -=1
if self.cnt_abset == 0:
fg = True
break
else:
if self._set.get(_.main_tag) is None:
self._set[_.main_tag] = 1
self.ans.append(FourTuple(op, _.lc.main_tag, _.rc.main_tag, _.main_tag))
self.abset[_.main_tag] = False
if fg:
break
for __ in _.other_tag:
if __ != _.main_tag:
self.ans.append(FourTuple('=',_.main_tag,c='_',d=__))
if tp(__) == 1:
if self.abset[__] == True:
self.ans.append(FourTuple('=', _.main_tag, '_', __))
self.abset[__] = False
self.cnt_abset -= 1
if self.cnt_abset == 0:
fg = True
break
else:
if self._set.get(__) == None:
self._set[__] = 1
self.ans.append(FourTuple('=',_.main_tag,c='_',d=__))
if fg:
break
self.ans = self.ans[::-1]
@ -91,5 +191,5 @@ if __name__ == '__main__':
optimizer.build(l)
for i in optimizer.ans:
print(i)
# print()

View File

@ -1,7 +1,11 @@
l = [
"=,10,_,t1",
"=,20,_,t1",
"+,t1,t1,t2"
'=,1,_,t',
'=,5,_,t',
'=,2,_,_t2',
'=,4,_,_t1',
'+,4,_t2,_t3',
'-,_t2,_t3,_t1',
]
class FourTuple:
cnt =0
@ -47,6 +51,7 @@ if __name__ == '__main__':
divider = BlockDivider(l)
ans = divider.run()
for i in ans:
for j in i:
print(j,end='\t')
print()