Fold again after applying distributibity or absortion.
Failure to do so was blocking some constant folding, like in: if (a&1 || a&2 || a&4) -> if (a&(3|4)) Fixed. Now it correctly outputs if (a&7).
This commit is contained in:
@@ -1125,6 +1125,7 @@ class foldconst(object):
|
|||||||
parent[index] = child[b]
|
parent[index] = child[b]
|
||||||
|
|
||||||
# Apply boolean distributivity
|
# Apply boolean distributivity
|
||||||
|
applied = False
|
||||||
opposite = '&' if nt == '|' else '|'
|
opposite = '&' if nt == '|' else '|'
|
||||||
if child[0]['nt'] == child[1]['nt'] == opposite:
|
if child[0]['nt'] == child[1]['nt'] == opposite:
|
||||||
left = child[0]['ch']
|
left = child[0]['ch']
|
||||||
@@ -1136,6 +1137,7 @@ class foldconst(object):
|
|||||||
opposite = child[1]['nt']
|
opposite = child[1]['nt']
|
||||||
right[d] = left[1 - c]
|
right[d] = left[1 - c]
|
||||||
child[0] = left[c]
|
child[0] = left[c]
|
||||||
|
applied = True
|
||||||
break
|
break
|
||||||
|
|
||||||
# Apply absorption, possibly after distributivity
|
# Apply absorption, possibly after distributivity
|
||||||
@@ -1148,8 +1150,13 @@ class foldconst(object):
|
|||||||
node = parent[index] = child[c]
|
node = parent[index] = child[c]
|
||||||
nt = node['nt']
|
nt = node['nt']
|
||||||
child = node['ch'] if 'ch' in node else None
|
child = node['ch'] if 'ch' in node else None
|
||||||
|
applied = True
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if applied:
|
||||||
|
# Re-fold
|
||||||
|
self.FoldTree(parent, index)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if nt == '^':
|
if nt == '^':
|
||||||
|
|||||||
Reference in New Issue
Block a user