forked from M-Labs/artiq
1
0
Fork 0

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
1 changed files with 14 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import ast, operator import ast, operator
from artiq.compiler.tools import * from artiq.compiler.tools import *
from artiq.language.core import int64, round64
_ast_unops = { _ast_unops = {
ast.Invert: operator.inv, ast.Invert: operator.inv,
@ -57,5 +58,18 @@ class _ConstantFolder(ast.NodeTransformer):
return node return node
return ast.copy_location(result, 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): def fold_constants(node):
_ConstantFolder().visit(node) _ConstantFolder().visit(node)