mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Fix tuple mess, typecast bug, and boolean bug.
Fix the tuple mess with the right approach. The EXPR tuple should not be changed, only the content. Fix bug with typecasts. (NOTE: Debug code active) Fix bug where <= and >= were returning bool instead of int.
This commit is contained in:
parent
d856f62253
commit
0b74093255
1 changed files with 16 additions and 17 deletions
|
@ -10,6 +10,8 @@ class optimizer(object):
|
||||||
binary_ops = frozenset(('+','-','*','/','%','<<','>>','<','<=','>','>=',
|
binary_ops = frozenset(('+','-','*','/','%','<<','>>','<','<=','>','>=',
|
||||||
'==','!=','|','^','&','||','&&'))
|
'==','!=','|','^','&','||','&&'))
|
||||||
assign_ops = frozenset(('=','+=','-=','*=','/=','%=','&=','|=','^=','<<=','>>='))
|
assign_ops = frozenset(('=','+=','-=','*=','/=','%=','&=','|=','^=','<<=','>>='))
|
||||||
|
LSL2PythonType = {'integer':int, 'float':float, 'string':unicode, 'key':lslfuncs.Key,
|
||||||
|
'vector':lslfuncs.Vector, 'rotation':lslfuncs.Quaternion, 'list':list}
|
||||||
|
|
||||||
def FoldAndRemoveEmptyStmts(self, lst):
|
def FoldAndRemoveEmptyStmts(self, lst):
|
||||||
"""Utility function for elimination of useless expressions in FOR"""
|
"""Utility function for elimination of useless expressions in FOR"""
|
||||||
|
@ -39,7 +41,12 @@ class optimizer(object):
|
||||||
Also optimizes away IF, WHILE, etc.
|
Also optimizes away IF, WHILE, etc.
|
||||||
"""
|
"""
|
||||||
while code[0] == 'EXPR':
|
while code[0] == 'EXPR':
|
||||||
code[:] = code[2]
|
if type(code) == tuple:
|
||||||
|
# just enter
|
||||||
|
code = code[2]
|
||||||
|
else:
|
||||||
|
# unfold
|
||||||
|
code[:] = code[2]
|
||||||
|
|
||||||
code0 = code[0]
|
code0 = code[0]
|
||||||
|
|
||||||
|
@ -53,7 +60,11 @@ class optimizer(object):
|
||||||
# Enable key constants. We'll typecast them back on output, but
|
# Enable key constants. We'll typecast them back on output, but
|
||||||
# this enables some optimizations.
|
# this enables some optimizations.
|
||||||
#if code[1] != 'key': # key constants not possible
|
#if code[1] != 'key': # key constants not possible
|
||||||
code[:] = [CONSTANT, code[1], lslfuncs.typecast(code[2][2])]
|
|
||||||
|
#DEBUG
|
||||||
|
print repr(type(code[2][2])), code[1]
|
||||||
|
|
||||||
|
code[:] = [CONSTANT, code[1], lslfuncs.typecast(code[2][2], self.LSL2PythonType[code[1]])]
|
||||||
return
|
return
|
||||||
|
|
||||||
if code0 == 'NEG':
|
if code0 == 'NEG':
|
||||||
|
@ -110,7 +121,7 @@ class optimizer(object):
|
||||||
else:
|
else:
|
||||||
result = lslfuncs.less(op1, op2)
|
result = lslfuncs.less(op1, op2)
|
||||||
if op in ('>=', '<='):
|
if op in ('>=', '<='):
|
||||||
result = not result
|
result = 1-result
|
||||||
elif op == '|':
|
elif op == '|':
|
||||||
result = op1 | op2
|
result = op1 | op2
|
||||||
elif op == '^':
|
elif op == '^':
|
||||||
|
@ -122,7 +133,7 @@ class optimizer(object):
|
||||||
elif op == '&&':
|
elif op == '&&':
|
||||||
result = int(op1 and op2)
|
result = int(op1 and op2)
|
||||||
else:
|
else:
|
||||||
raise Exception(u'Internal error: Operator not found: ' + op.decode('utf8'))
|
raise Exception(u'Internal error: Operator not found: ' + op.decode('utf8')) # pragma: no cover
|
||||||
code[:] = [CONSTANT, code[1], result]
|
code[:] = [CONSTANT, code[1], result]
|
||||||
elif code[0] == '-' and code[2][1] in ('integer', 'float') and code[3][1] in ('integer', 'float'):
|
elif code[0] == '-' and code[2][1] in ('integer', 'float') and code[3][1] in ('integer', 'float'):
|
||||||
# Change - to + - for int/float
|
# Change - to + - for int/float
|
||||||
|
@ -298,20 +309,8 @@ class optimizer(object):
|
||||||
|
|
||||||
def Fold(self, code, IsGlobal = True):
|
def Fold(self, code, IsGlobal = True):
|
||||||
assert type(code[2]) == tuple
|
assert type(code[2]) == tuple
|
||||||
tree = list(code[2])
|
|
||||||
self.globalmode = IsGlobal and len(code) == 3
|
self.globalmode = IsGlobal and len(code) == 3
|
||||||
self.FoldTree(tree)
|
self.FoldTree(code[2])
|
||||||
# As a special case, we fold the constants that are keys,
|
|
||||||
# because the folder
|
|
||||||
|
|
||||||
if type(code) == tuple:
|
|
||||||
code = list(code)
|
|
||||||
code[2] = tuple(tree)
|
|
||||||
code = tuple(code)
|
|
||||||
else:
|
|
||||||
assert False
|
|
||||||
code[2] = tuple(tree)
|
|
||||||
|
|
||||||
del self.globalmode
|
del self.globalmode
|
||||||
|
|
||||||
def optimize(self, symtab, functions):
|
def optimize(self, symtab, functions):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue