mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Improve CompareTrees.
Reorganize into different statements with early return. Add constants, unary operators and binary operators. Check if operator is commutative and check with operands swapped when so. Constant equality is somewhat sketchy at the moment: just compare the values with Python's ==.
This commit is contained in:
parent
1cdf9f7ff0
commit
4251d4c7f9
1 changed files with 35 additions and 7 deletions
|
@ -130,21 +130,49 @@ class foldconst(object):
|
||||||
# They MUST be SEF and stable.
|
# They MUST be SEF and stable.
|
||||||
if 'SEF' not in node1 or 'SEF' not in node2:
|
if 'SEF' not in node1 or 'SEF' not in node2:
|
||||||
return False
|
return False
|
||||||
# So far it's only accepted if both are identifiers or function calls,
|
if node1['t'] != node2['t']:
|
||||||
# recursively.
|
return False
|
||||||
return (node1['nt'] == node2['nt'] == 'IDENT'
|
# It's not complete yet.
|
||||||
|
nt1 = node1['nt']
|
||||||
|
if nt1 == node2['nt']:
|
||||||
|
if (nt1 == 'IDENT'
|
||||||
and node1['name'] == node2['name']
|
and node1['name'] == node2['name']
|
||||||
and node1['scope'] == node2['scope']
|
and node1['scope'] == node2['scope']
|
||||||
or node1['nt'] == node2['nt'] == 'FNCALL'
|
):
|
||||||
|
return True
|
||||||
|
if (nt1 == 'FNCALL'
|
||||||
and node1['name'] == node2['name']
|
and node1['name'] == node2['name']
|
||||||
and 'uns' not in self.symtab[0][node1['name']]
|
and 'uns' not in self.symtab[0][node1['name']]
|
||||||
and all(self.CompareTrees(node1['ch'][i],
|
and all(self.CompareTrees(node1['ch'][i],
|
||||||
node2['ch'][i])
|
node2['ch'][i])
|
||||||
for i in xrange(len(node1['ch'])))
|
for i in xrange(len(node1['ch'])))
|
||||||
or node1['nt'] == node2['nt'] == 'CAST'
|
):
|
||||||
and node1['t'] == node2['t']
|
return True
|
||||||
|
if (nt1 == 'CAST'
|
||||||
and self.CompareTrees(node1['ch'][0], node2['ch'][0])
|
and self.CompareTrees(node1['ch'][0], node2['ch'][0])
|
||||||
)
|
):
|
||||||
|
return True
|
||||||
|
if nt1 == 'CONST' and node1['value'] == node2['value']:
|
||||||
|
return True
|
||||||
|
if (nt1 in ('!', '~', 'NEG')
|
||||||
|
and self.CompareTrees(node1['ch'][0], node2['ch'][0])
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
if (nt1 in self.binary_ops
|
||||||
|
and self.CompareTrees(node1['ch'][0], node2['ch'][0])
|
||||||
|
and self.CompareTrees(node1['ch'][1], node2['ch'][1])
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
if ((nt1 in ('*', '^', '&', '|', '==') # commutative
|
||||||
|
or nt1 == '+'
|
||||||
|
and node1['ch'][0]['t'] not in ('list', 'string')
|
||||||
|
and node2['ch'][0]['t'] not in ('list', 'string')
|
||||||
|
)
|
||||||
|
and self.CompareTrees(node1['ch'][0], node2['ch'][1])
|
||||||
|
and self.CompareTrees(node1['ch'][1], node2['ch'][0])
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def FnSEF(self, node):
|
def FnSEF(self, node):
|
||||||
'''Applied to function call nodes, return whether the node corresponds
|
'''Applied to function call nodes, return whether the node corresponds
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue