compiler: factorize operation of NodeTransformer on statement lists

This commit is contained in:
Sebastien Bourdeauducq 2014-06-21 00:40:52 +02:00
parent 6400e28e4c
commit c3092ae13f
2 changed files with 9 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import ast, operator
from artiq.language import units
from artiq.compiler.tools import value_to_ast
from artiq.compiler.tools import value_to_ast, make_stmt_transformer
class _NotConstant(Exception):
pass
@ -79,7 +79,4 @@ class _ConstantFolder(ast.NodeTransformer):
return node
return result
def fold_constants(stmts):
constant_folder = _ConstantFolder()
new_stmts = [constant_folder.visit(stmt) for stmt in stmts]
stmts[:] = new_stmts
fold_constants = make_stmt_transformer(_ConstantFolder)

View File

@ -23,3 +23,10 @@ def value_to_ast(value):
args=[ast.Num(value.amount), ast.Name("base_"+value.unit.name+"_unit", ast.Load())],
keywords=[], starargs=None, kwargs=None)
return None
def make_stmt_transformer(transformer_class):
def stmt_transformer(stmts):
transformer = transformer_class()
new_stmts = [transformer.visit(stmt) for stmt in stmts]
stmts[:] = new_stmts
return stmt_transformer