mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-03 00:18:20 +00:00
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:
parent
2047a128fb
commit
25cbb91b6e
1 changed files with 36 additions and 39 deletions
|
@ -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
|
||||||
return
|
node = {'nt':'NEG', 't':optype, 'ch':[node]}
|
||||||
|
|
||||||
if lval['value'] == 2:
|
|
||||||
node = {'nt':'NEG', 't':optype,
|
|
||||||
'ch':[{'nt':'~', 't':optype, 'ch':[rval]}]}
|
|
||||||
if RSEF:
|
if RSEF:
|
||||||
node['ch'][0]['SEF'] = True
|
|
||||||
node['SEF'] = True
|
node['SEF'] = True
|
||||||
parent[index] = node = {'nt':'NEG', 't':optype,
|
else:
|
||||||
'ch':[{'nt':'~', 't':optype, 'ch':[node]}]}
|
node = {'nt':'()', 't':optype, 'ch':[rval]}
|
||||||
if RSEF:
|
if RSEF:
|
||||||
node['ch'][0]['SEF'] = True
|
|
||||||
node['SEF'] = True
|
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
|
return
|
||||||
|
|
||||||
# More than 2 becomes counter-productive.
|
# More than 2 becomes counter-productive.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue