mirror of https://github.com/m-labs/artiq.git
compiler: factorize operation of NodeTransformer on statement lists
This commit is contained in:
parent
6400e28e4c
commit
c3092ae13f
|
@ -1,7 +1,7 @@
|
||||||
import ast, operator
|
import ast, operator
|
||||||
|
|
||||||
from artiq.language import units
|
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):
|
class _NotConstant(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -79,7 +79,4 @@ class _ConstantFolder(ast.NodeTransformer):
|
||||||
return node
|
return node
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def fold_constants(stmts):
|
fold_constants = make_stmt_transformer(_ConstantFolder)
|
||||||
constant_folder = _ConstantFolder()
|
|
||||||
new_stmts = [constant_folder.visit(stmt) for stmt in stmts]
|
|
||||||
stmts[:] = new_stmts
|
|
||||||
|
|
|
@ -23,3 +23,10 @@ def value_to_ast(value):
|
||||||
args=[ast.Num(value.amount), ast.Name("base_"+value.unit.name+"_unit", ast.Load())],
|
args=[ast.Num(value.amount), ast.Name("base_"+value.unit.name+"_unit", ast.Load())],
|
||||||
keywords=[], starargs=None, kwargs=None)
|
keywords=[], starargs=None, kwargs=None)
|
||||||
return 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
|
||||||
|
|
Loading…
Reference in New Issue