2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-24 19:04:02 +08:00

compiler/fold_constants: support int, int64, round and round64

This commit is contained in:
Sebastien Bourdeauducq 2014-08-18 22:52:19 +08:00
parent 219aa23d25
commit 56ccd054eb

View File

@ -1,6 +1,7 @@
import ast, operator
from artiq.compiler.tools import *
from artiq.language.core import int64, round64
_ast_unops = {
ast.Invert: operator.inv,
@ -57,5 +58,18 @@ class _ConstantFolder(ast.NodeTransformer):
return node
return ast.copy_location(result, node)
def visit_Call(self, node):
self.generic_visit(node)
fn = node.func.id
if fn in ("int", "int64", "round", "round64"):
try:
arg = eval_constant(node.args[0])
except NotConstant:
return node
result = value_to_ast(globals()[fn](arg))
return ast.copy_location(result, node)
else:
return node
def fold_constants(node):
_ConstantFolder().visit(node)