Fix bug where n*3+1 was optimized as -~n*3 rather than as -~(n*3). Also merge -2 with -1 and +2 with +1.

This commit is contained in:
Sei Lisa 2015-02-27 20:05:14 +01:00
parent 2047a128fb
commit 25cbb91b6e

View file

@ -410,39 +410,29 @@ class foldconst(object):
# Swap the vars to deal with const in lval always # Swap the vars to deal with const in lval always
lval, lnt, rval, rnt = rval, rnt, lval, lnt lval, lnt, rval, rnt = rval, rnt, lval, lnt
RSEF = 'SEF' in rval RSEF = 'SEF' in rval
if lval['value'] == -1:
if rnt == 'NEG':
node = {'nt':'~', 't':optype, 'ch':rval['ch']}
if RSEF:
node['SEF'] = True
else:
node = {'nt':'NEG', 't':optype, 'ch':[rval]}
if RSEF:
node['SEF'] = True
node = {'nt':'~', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
parent[index] = node
return
if lval['value'] == -2: # Fix n*5+1 outputing -~n*5 instead of -~(n*5).
# This would be ideally fixed through smart parentheses
# output rather than introducing the parens in the tree.
if lval['value'] == -1 or lval['value'] == -2:
if rnt == 'NEG': # Cancel the NEG if rnt == 'NEG': # Cancel the NEG
node = {'nt':'~', 't':optype, 'ch':rval['ch']} node = {'nt':'()', 't':optype, 'ch':rval['ch']}
if RSEF:
node['SEF'] = True
node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF: if RSEF:
node['SEF'] = True node['SEF'] = True
node = {'nt':'~', 't':optype, 'ch':[node]} node = {'nt':'~', 't':optype, 'ch':[node]}
if RSEF: if RSEF:
node['SEF'] = True node['SEF'] = True
else: # Add the NEG else: # Add the NEG
node = {'nt':'NEG', 't':optype, 'ch':[rval]} node = {'nt':'()', 't':optype, 'ch':[rval]}
if RSEF:
node['SEF'] = True
node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF: if RSEF:
node['SEF'] = True node['SEF'] = True
node = {'nt':'~', 't':optype, 'ch':[node]} node = {'nt':'~', 't':optype, 'ch':[node]}
if RSEF: if RSEF:
node['SEF'] = True node['SEF'] = True
if lval['value'] == -2:
node = {'nt':'NEG', 't':optype, 'ch':[node]} node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF: if RSEF:
node['SEF'] = True node['SEF'] = True
@ -452,25 +442,32 @@ class foldconst(object):
parent[index] = node parent[index] = node
return return
if lval['value'] == 1: if lval['value'] == 1 or lval['value'] == 2:
parent[index] = node = {'nt':'NEG', 't':optype, if rnt == '~': # Cancel the ~
'ch':[{'nt':'~', 't':optype, 'ch':[rval]}]} node = {'nt':'()', 't':optype, 'ch':rval['ch']}
if RSEF: if RSEF:
node['ch'][0]['SEF'] = True node['SEF'] = True
node['SEF'] = True node = {'nt':'NEG', 't':optype, 'ch':[node]}
return if RSEF:
node['SEF'] = True
if lval['value'] == 2: else:
node = {'nt':'NEG', 't':optype, node = {'nt':'()', 't':optype, 'ch':[rval]}
'ch':[{'nt':'~', 't':optype, 'ch':[rval]}]} if RSEF:
if RSEF: node['SEF'] = True
node['ch'][0]['SEF'] = True node = {'nt':'~', 't':optype, 'ch':[node]}
node['SEF'] = True if RSEF:
parent[index] = node = {'nt':'NEG', 't':optype, node['SEF'] = True
'ch':[{'nt':'~', 't':optype, 'ch':[node]}]} node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF: if RSEF:
node['ch'][0]['SEF'] = True node['SEF'] = True
node['SEF'] = True if lval ['value'] == 2:
node = {'nt':'~', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
parent[index] = node
return return
# More than 2 becomes counter-productive. # More than 2 becomes counter-productive.