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
lval, lnt, rval, rnt = rval, rnt, lval, lnt
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
node = {'nt':'~', 't':optype, 'ch':rval['ch']}
if RSEF:
node['SEF'] = True
node = {'nt':'NEG', 't':optype, 'ch':[node]}
node = {'nt':'()', 't':optype, 'ch':rval['ch']}
if RSEF:
node['SEF'] = True
node = {'nt':'~', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
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:
node['SEF'] = True
node = {'nt':'~', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
if lval['value'] == -2:
node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
@ -452,25 +442,32 @@ class foldconst(object):
parent[index] = node
return
if lval['value'] == 1:
parent[index] = node = {'nt':'NEG', 't':optype,
'ch':[{'nt':'~', 't':optype, 'ch':[rval]}]}
if RSEF:
node['ch'][0]['SEF'] = True
node['SEF'] = True
return
if lval['value'] == 2:
node = {'nt':'NEG', 't':optype,
'ch':[{'nt':'~', 't':optype, 'ch':[rval]}]}
if RSEF:
node['ch'][0]['SEF'] = True
node['SEF'] = True
parent[index] = node = {'nt':'NEG', 't':optype,
'ch':[{'nt':'~', 't':optype, 'ch':[node]}]}
if RSEF:
node['ch'][0]['SEF'] = True
node['SEF'] = True
if lval['value'] == 1 or lval['value'] == 2:
if rnt == '~': # Cancel the ~
node = {'nt':'()', 't':optype, 'ch':rval['ch']}
if RSEF:
node['SEF'] = True
node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
else:
node = {'nt':'()', 't':optype, 'ch':[rval]}
if RSEF:
node['SEF'] = True
node = {'nt':'~', 't':optype, 'ch':[node]}
if RSEF:
node['SEF'] = True
node = {'nt':'NEG', 't':optype, 'ch':[node]}
if RSEF:
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
# More than 2 becomes counter-productive.