Skip to content

Commit

Permalink
Fine grain customization of constant folded nodes
Browse files Browse the repository at this point in the history
Do not fold nodes that are irrelevant.
  • Loading branch information
serge-sans-paille committed Mar 21, 2017
1 parent b8a8a11 commit fa0b98b
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions pythran/optimizations/constant_folding.py
Expand Up @@ -70,18 +70,29 @@ def prepare(self, node, ctx):

super(ConstantFolding, self).prepare(node, ctx)

def skip(self, node):
return node

visit_Num = visit_Name = skip

visit_List = visit_Set = Transformation.generic_visit
visit_Dict = visit_Tuple = Transformation.generic_visit

def visit_Index(self, node):
value = self.visit(node.value)
if value is not node.value:
return ast.Index(value)
else:
return node

def generic_visit(self, node):
if node in self.constant_expressions:
if isinstance(node, ast.expr) and node in self.constant_expressions:
fake_node = ast.Expression(node)
code = compile(ast.gast_to_ast(fake_node),
'<constant folding>', 'eval')
try:
fake_node = ast.Expression(
node.value if isinstance(node, ast.Index) else node)
code = compile(ast.gast_to_ast(fake_node),
'<constant folding>', 'eval')
value = eval(code, self.env)
new_node = to_ast(value)
if(isinstance(node, ast.Index) and
not isinstance(new_node, ast.Index)):
new_node = ast.Index(new_node)
try:
if not ASTMatcher(node).search(new_node):
self.update = True
Expand Down

0 comments on commit fa0b98b

Please sign in to comment.