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:
Sei Lisa 2017-10-17 23:28:14 +02:00
parent 6c73ba95c2
commit 4cbdbefe1b

View file

@ -1125,6 +1125,7 @@ class foldconst(object):
parent[index] = child[b]
# Apply boolean distributivity
applied = False
opposite = '&' if nt == '|' else '|'
if child[0]['nt'] == child[1]['nt'] == opposite:
left = child[0]['ch']
@ -1136,6 +1137,7 @@ class foldconst(object):
opposite = child[1]['nt']
right[d] = left[1 - c]
child[0] = left[c]
applied = True
break
# Apply absorption, possibly after distributivity
@ -1148,8 +1150,13 @@ class foldconst(object):
node = parent[index] = child[c]
nt = node['nt']
child = node['ch'] if 'ch' in node else None
applied = True
break
if applied:
# Re-fold
self.FoldTree(parent, index)
return
if nt == '^':