mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Make some simplifications and keep PyFlakes happy.
This commit is contained in:
parent
3f59f5f16f
commit
2e09a3a986
2 changed files with 21 additions and 16 deletions
|
@ -18,7 +18,9 @@
|
||||||
# Constant folding and simplification of expressions and statements.
|
# Constant folding and simplification of expressions and statements.
|
||||||
|
|
||||||
import lslcommon
|
import lslcommon
|
||||||
|
from lslcommon import Key, Vector, Quaternion
|
||||||
import lslfuncs
|
import lslfuncs
|
||||||
|
from lslfuncs import NULL_KEY, ZERO_VECTOR, ZERO_ROTATION
|
||||||
import math
|
import math
|
||||||
from lslparse import warning
|
from lslparse import warning
|
||||||
from lslfuncparams import OptimizeParams
|
from lslfuncparams import OptimizeParams
|
||||||
|
@ -108,8 +110,8 @@ class foldconst(object):
|
||||||
parent[index] = {'nt':'!=', 't':'integer', 'ch':[parent[index],
|
parent[index] = {'nt':'!=', 't':'integer', 'ch':[parent[index],
|
||||||
{'nt':'CONST', 't':ctyp, 'value':
|
{'nt':'CONST', 't':ctyp, 'value':
|
||||||
0.0 if ctyp == 'float'
|
0.0 if ctyp == 'float'
|
||||||
else lslfuncs.ZERO_VECTOR if ctyp == 'vector'
|
else ZERO_VECTOR if ctyp == 'vector'
|
||||||
else lslfuncs.ZERO_ROTATION if ctyp == 'rotation'
|
else ZERO_ROTATION if ctyp == 'rotation'
|
||||||
else []}]}
|
else []}]}
|
||||||
parent[index]['SEF'] = 'SEF' in parent[index]['ch'][0]
|
parent[index]['SEF'] = 'SEF' in parent[index]['ch'][0]
|
||||||
|
|
||||||
|
@ -1017,6 +1019,8 @@ class foldconst(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
if nt == 'FNCALL':
|
if nt == 'FNCALL':
|
||||||
|
name = node['name']
|
||||||
|
|
||||||
SEFargs = True
|
SEFargs = True
|
||||||
CONSTargs = True
|
CONSTargs = True
|
||||||
for idx in xrange(len(child)-1, -1, -1):
|
for idx in xrange(len(child)-1, -1, -1):
|
||||||
|
@ -1028,7 +1032,7 @@ class foldconst(object):
|
||||||
if child[idx]['nt'] != 'CONST':
|
if child[idx]['nt'] != 'CONST':
|
||||||
CONSTargs = False
|
CONSTargs = False
|
||||||
|
|
||||||
sym = self.symtab[0][node['name']]
|
sym = self.symtab[0][name]
|
||||||
OptimizeParams(node, sym)
|
OptimizeParams(node, sym)
|
||||||
if 'Fn' in sym and ('SEF' in sym and sym['SEF'] or lslcommon.IsCalc):
|
if 'Fn' in sym and ('SEF' in sym and sym['SEF'] or lslcommon.IsCalc):
|
||||||
# It's side-effect free if the children are and the function
|
# It's side-effect free if the children are and the function
|
||||||
|
@ -1060,13 +1064,13 @@ class foldconst(object):
|
||||||
# make a shallow copy
|
# make a shallow copy
|
||||||
args[argnum] = args[argnum][:]
|
args[argnum] = args[argnum][:]
|
||||||
for i in range(len(args[argnum])):
|
for i in range(len(args[argnum])):
|
||||||
if type(args[argnum][i]) == lslcommon.Quaternion:
|
if type(args[argnum][i]) == Quaternion:
|
||||||
args[argnum][i] = lslfuncs.q2f(args[argnum][i])
|
args[argnum][i] = lslfuncs.q2f(args[argnum][i])
|
||||||
elif type(args[argnum][i]) == lslcommon.Vector:
|
elif type(args[argnum][i]) == Vector:
|
||||||
args[argnum][i] = lslfuncs.v2f(args[argnum][i])
|
args[argnum][i] = lslfuncs.v2f(args[argnum][i])
|
||||||
del argtypes
|
del argtypes
|
||||||
try:
|
try:
|
||||||
if node['name'][:10] == 'llDetected':
|
if name[:10] == 'llDetected':
|
||||||
value = fn(*args, event=self.CurEvent)
|
value = fn(*args, event=self.CurEvent)
|
||||||
else:
|
else:
|
||||||
value = fn(*args)
|
value = fn(*args)
|
||||||
|
@ -1082,17 +1086,17 @@ class foldconst(object):
|
||||||
)
|
)
|
||||||
if generatesTabs:
|
if generatesTabs:
|
||||||
if self.warntabs:
|
if self.warntabs:
|
||||||
warning(u"Can't optimize call to %s because it would generate a tab character (you can force the optimization with the 'foldtabs' option, or disable this warning by disabling the 'warntabs' option)." % node['name'].decode('utf8'))
|
warning(u"Can't optimize call to %s because it would generate a tab character (you can force the optimization with the 'foldtabs' option, or disable this warning by disabling the 'warntabs' option)." % name.decode('utf8'))
|
||||||
return
|
return
|
||||||
parent[index] = {'nt':'CONST', 't':node['t'], 'value':value, 'SEF':True}
|
parent[index] = {'nt':'CONST', 't':node['t'], 'value':value, 'SEF':True}
|
||||||
elif self.optlistlength and node['name'] == 'llGetListLength':
|
elif self.optlistlength and name == 'llGetListLength':
|
||||||
# Convert llGetListLength(expr) to (expr != [])
|
# Convert llGetListLength(expr) to (expr != [])
|
||||||
node = {'nt':'CONST', 't':'list', 'value':[]}
|
node = {'nt':'CONST', 't':'list', 'value':[]}
|
||||||
parent[index] = node = {'nt':'!=', 't':'integer',
|
parent[index] = node = {'nt':'!=', 't':'integer',
|
||||||
'ch':[child[0], node]}
|
'ch':[child[0], node]}
|
||||||
# SEF if the list is
|
# SEF if the list is
|
||||||
node['SEF'] = 'SEF' in child[0]
|
node['SEF'] = 'SEF' in child[0]
|
||||||
elif (node['name'] == 'llDumpList2String'
|
elif (name == 'llDumpList2String'
|
||||||
and child[1]['nt'] == 'CONST'
|
and child[1]['nt'] == 'CONST'
|
||||||
and child[1]['t'] in ('string', 'key')
|
and child[1]['t'] in ('string', 'key')
|
||||||
and child[1]['value'] == u""):
|
and child[1]['value'] == u""):
|
||||||
|
@ -1100,7 +1104,7 @@ class foldconst(object):
|
||||||
node['nt'] = 'CAST'
|
node['nt'] = 'CAST'
|
||||||
del child[1]
|
del child[1]
|
||||||
del node['name']
|
del node['name']
|
||||||
elif SEFargs and 'SEF' in self.symtab[0][node['name']]:
|
elif SEFargs and 'SEF' in self.symtab[0][name]:
|
||||||
# The function is marked as SEF in the symbol table, and the
|
# The function is marked as SEF in the symbol table, and the
|
||||||
# arguments are all side-effect-free. The result is SEF.
|
# arguments are all side-effect-free. The result is SEF.
|
||||||
node['SEF'] = True
|
node['SEF'] = True
|
||||||
|
@ -1151,11 +1155,11 @@ class foldconst(object):
|
||||||
if 'SEF' not in child[idx]:
|
if 'SEF' not in child[idx]:
|
||||||
issef = False
|
issef = False
|
||||||
if isconst:
|
if isconst:
|
||||||
value = [elem['value'] for elem in child]
|
value = [x['value'] for x in child]
|
||||||
if nt == 'VECTOR':
|
if nt == 'VECTOR':
|
||||||
value = lslfuncs.Vector([lslfuncs.ff(x) for x in value])
|
value = Vector([lslfuncs.ff(x) for x in value])
|
||||||
elif nt == 'ROTATION':
|
elif nt == 'ROTATION':
|
||||||
value = lslfuncs.Quaternion([lslfuncs.ff(x) for x in value])
|
value = Quaternion([lslfuncs.ff(x) for x in value])
|
||||||
parent[index] = {'nt':'CONST', 'SEF':True, 't':node['t'],
|
parent[index] = {'nt':'CONST', 'SEF':True, 't':node['t'],
|
||||||
'value':value}
|
'value':value}
|
||||||
return
|
return
|
||||||
|
@ -1376,7 +1380,6 @@ class foldconst(object):
|
||||||
if node['t'] == 'integer' and child[0]['nt'] == 'CONST' \
|
if node['t'] == 'integer' and child[0]['nt'] == 'CONST' \
|
||||||
and not child[0]['value']:
|
and not child[0]['value']:
|
||||||
del node['ch']
|
del node['ch']
|
||||||
child = None
|
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
# Add assignment if vector, rotation or float.
|
# Add assignment if vector, rotation or float.
|
||||||
|
@ -1384,8 +1387,8 @@ class foldconst(object):
|
||||||
typ = node['t']
|
typ = node['t']
|
||||||
node['ch'] = [{'nt':'CONST', 't':typ, 'SEF': True,
|
node['ch'] = [{'nt':'CONST', 't':typ, 'SEF': True,
|
||||||
'value': 0.0 if typ == 'float' else
|
'value': 0.0 if typ == 'float' else
|
||||||
lslfuncs.ZERO_VECTOR if typ == 'vector' else
|
ZERO_VECTOR if typ == 'vector' else
|
||||||
lslfuncs.ZERO_ROTATION}]
|
ZERO_ROTATION}]
|
||||||
# Declarations always have side effects.
|
# Declarations always have side effects.
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -413,9 +413,11 @@ class parser(object):
|
||||||
else:
|
else:
|
||||||
filename = match.group(2)[1:-1]
|
filename = match.group(2)[1:-1]
|
||||||
# TODO: what do we do with the filename?
|
# TODO: what do we do with the filename?
|
||||||
|
filename # keep pyflakes happy
|
||||||
|
|
||||||
del filename
|
del filename
|
||||||
linenum = int(match.group(1))
|
linenum = int(match.group(1))
|
||||||
|
linenum # keep pyflakes happy
|
||||||
# TODO: process line number
|
# TODO: process line number
|
||||||
del linenum
|
del linenum
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue