mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 07:38:21 +00:00
Optimize list addition when one list is known to have one element.
list + [element] -> list + element list + (list)element -> list + element [element] + list -> element + list (list)element + list -> element + list
This commit is contained in:
parent
44e0db96d2
commit
e0fa1678a7
2 changed files with 39 additions and 1 deletions
|
@ -561,11 +561,48 @@ class foldconst(object):
|
|||
# "" + expr -> expr
|
||||
# [] + expr -> expr
|
||||
parent[index] = self.Cast(rval, optype)
|
||||
elif rnt == 'CONST' and not rval['value']:
|
||||
return
|
||||
if rnt == 'CONST' and not rval['value']:
|
||||
# expr + 0. -> expr
|
||||
# expr + "" -> expr
|
||||
# expr + [] -> expr
|
||||
parent[index] = self.Cast(lval, optype)
|
||||
return
|
||||
|
||||
if ltype == rtype == 'list':
|
||||
|
||||
if (rnt == 'LIST' and len(rval['ch']) == 1
|
||||
or rnt == 'CAST'):
|
||||
# list + (list)element -> list + element
|
||||
# list + [element] -> list + element
|
||||
while True:
|
||||
# Remove nested typecasts: (list)(list)x -> x
|
||||
rval = parent[index]['ch'][1] = rval['ch'][0]
|
||||
if rval['nt'] != 'CAST' or rval['t'] != 'list':
|
||||
break
|
||||
return
|
||||
if rnt == 'CONST' and len(rval['value']) == 1:
|
||||
# list + [constant] -> list + constant
|
||||
rval['value'] = rval['value'][0]
|
||||
rtype = rval['t'] = self.PythonType2LSL[type(rval['value'])]
|
||||
return
|
||||
|
||||
if (lnt == 'LIST' and len(lval['ch']) == 1
|
||||
or lnt == 'CAST'):
|
||||
# (list)element + list -> element + list
|
||||
# [element] + list -> element + list
|
||||
while True:
|
||||
# Remove nested typecasts: (list)(list)x -> x
|
||||
lval = parent[index]['ch'][0] = lval['ch'][0]
|
||||
if lval['nt'] != 'CAST' or lval['t'] != 'list':
|
||||
break
|
||||
return
|
||||
if lnt == 'CONST' and len(lval['value']) == 1:
|
||||
# [constant] + list -> constant + list
|
||||
lval['value'] = lval['value'][0]
|
||||
ltype = lval['t'] = self.PythonType2LSL[type(lval['value'])]
|
||||
return
|
||||
|
||||
return
|
||||
|
||||
# Must be two integers. This allows for a number of
|
||||
|
|
|
@ -1599,6 +1599,7 @@ list lazy_list_set(list L, integer i, list v)
|
|||
self.expect(';')
|
||||
self.NextToken()
|
||||
return jumpnode
|
||||
|
||||
if tok0 == 'STATE':
|
||||
self.NextToken()
|
||||
if self.tok[0] not in ('DEFAULT', 'IDENT'):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue