artiq/artiq/compiler/tools.py

26 lines
741 B
Python

import ast
from artiq.language import experiment, units
def eval_ast(expr, symdict=dict()):
if not isinstance(expr, ast.Expression):
expr = ast.Expression(expr)
code = compile(expr, "<ast>", "eval")
return eval(code, symdict)
def value_to_ast(value):
if isinstance(value, int):
return ast.Num(value)
elif isinstance(value, str):
return ast.Str(value)
else:
for kg in experiment.kernel_globals:
if value is getattr(experiment, kg):
return ast.Name(kg, ast.Load())
if isinstance(value, units.Quantity):
return ast.Call(
func=ast.Name("Quantity", ast.Load()),
args=[ast.Num(value.amount), ast.Name("base_"+value.unit.name+"_unit", ast.Load())],
keywords=[], starargs=None, kwargs=None)
return None