2015-06-13 15:29:26 +08:00
|
|
|
"""
|
2015-06-24 16:46:15 +08:00
|
|
|
The :mod:`builtins` module contains the builtin Python
|
|
|
|
and ARTIQ types, such as int or float.
|
2015-06-13 15:29:26 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
from . import types
|
|
|
|
|
2015-06-24 16:28:24 +08:00
|
|
|
# Types
|
|
|
|
|
2015-06-13 15:29:26 +08:00
|
|
|
class TNone(types.TMono):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__("NoneType")
|
|
|
|
|
|
|
|
class TBool(types.TMono):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__("bool")
|
|
|
|
|
|
|
|
class TInt(types.TMono):
|
|
|
|
def __init__(self, width=None):
|
|
|
|
if width is None:
|
|
|
|
width = types.TVar()
|
|
|
|
super().__init__("int", {"width": width})
|
|
|
|
|
|
|
|
class TFloat(types.TMono):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__("float")
|
|
|
|
|
|
|
|
class TList(types.TMono):
|
|
|
|
def __init__(self, elt=None):
|
|
|
|
if elt is None:
|
|
|
|
elt = types.TVar()
|
|
|
|
super().__init__("list", {"elt": elt})
|
|
|
|
|
2015-06-26 23:53:20 +08:00
|
|
|
class TRange(types.TMono):
|
|
|
|
def __init__(self, elt=None):
|
|
|
|
if elt is None:
|
|
|
|
elt = types.TVar()
|
|
|
|
super().__init__("range", {"elt": elt})
|
|
|
|
|
2015-06-26 16:16:08 +08:00
|
|
|
def fn_bool():
|
|
|
|
return types.TBuiltin("class bool")
|
|
|
|
|
|
|
|
def fn_int():
|
|
|
|
return types.TBuiltin("class int")
|
|
|
|
|
|
|
|
def fn_float():
|
|
|
|
return types.TBuiltin("class float")
|
|
|
|
|
|
|
|
def fn_list():
|
|
|
|
return types.TBuiltin("class list")
|
|
|
|
|
2015-06-26 23:53:20 +08:00
|
|
|
def fn_range():
|
|
|
|
return types.TBuiltin("function range")
|
|
|
|
|
2015-06-24 16:28:24 +08:00
|
|
|
def fn_len():
|
2015-06-24 16:46:15 +08:00
|
|
|
return types.TBuiltin("function len")
|
2015-06-24 16:28:24 +08:00
|
|
|
|
|
|
|
def fn_round():
|
2015-06-24 16:46:15 +08:00
|
|
|
return types.TBuiltin("function round")
|
2015-06-24 16:28:24 +08:00
|
|
|
|
|
|
|
def fn_syscall():
|
2015-06-24 16:46:15 +08:00
|
|
|
return types.TBuiltin("function syscall")
|
2015-06-24 16:28:24 +08:00
|
|
|
|
|
|
|
# Accessors
|
2015-06-13 15:29:26 +08:00
|
|
|
|
2015-06-14 17:07:13 +08:00
|
|
|
def is_none(typ):
|
|
|
|
return types.is_mono(typ, "NoneType")
|
|
|
|
|
|
|
|
def is_bool(typ):
|
|
|
|
return types.is_mono(typ, "bool")
|
|
|
|
|
2015-06-13 18:45:09 +08:00
|
|
|
def is_int(typ, width=None):
|
|
|
|
if width:
|
|
|
|
return types.is_mono(typ, "int", {"width": width})
|
|
|
|
else:
|
|
|
|
return types.is_mono(typ, "int")
|
|
|
|
|
2015-06-14 17:07:13 +08:00
|
|
|
def get_int_width(typ):
|
|
|
|
if is_int(typ):
|
2015-06-15 13:40:37 +08:00
|
|
|
return types.get_value(typ.find()["width"])
|
2015-06-14 17:07:13 +08:00
|
|
|
|
|
|
|
def is_float(typ):
|
|
|
|
return types.is_mono(typ, "float")
|
|
|
|
|
2015-06-13 15:29:26 +08:00
|
|
|
def is_numeric(typ):
|
2015-06-14 17:07:13 +08:00
|
|
|
typ = typ.find()
|
2015-06-13 15:29:26 +08:00
|
|
|
return isinstance(typ, types.TMono) and \
|
|
|
|
typ.name in ('int', 'float')
|
2015-06-14 17:07:13 +08:00
|
|
|
|
|
|
|
def is_list(typ, elt=None):
|
|
|
|
if elt:
|
|
|
|
return types.is_mono(typ, "list", {"elt": elt})
|
|
|
|
else:
|
|
|
|
return types.is_mono(typ, "list")
|
|
|
|
|
2015-06-26 23:53:20 +08:00
|
|
|
def is_range(typ, elt=None):
|
|
|
|
if elt:
|
|
|
|
return types.is_mono(typ, "range", {"elt": elt})
|
|
|
|
else:
|
|
|
|
return types.is_mono(typ, "range")
|
|
|
|
|
|
|
|
def is_iterable(typ):
|
|
|
|
typ = typ.find()
|
|
|
|
return isinstance(typ, types.TMono) and \
|
|
|
|
typ.name in ('list', 'range')
|
|
|
|
|
|
|
|
def get_iterable_elt(typ):
|
|
|
|
if is_iterable(typ):
|
|
|
|
return typ.find()["elt"]
|
|
|
|
|
2015-06-14 17:07:13 +08:00
|
|
|
def is_collection(typ):
|
|
|
|
typ = typ.find()
|
|
|
|
return isinstance(typ, types.TTuple) or \
|
|
|
|
types.is_mono(typ, "list")
|
2015-06-24 16:46:15 +08:00
|
|
|
|
2015-06-26 16:16:08 +08:00
|
|
|
def is_builtin(typ, name):
|
2015-06-24 16:46:15 +08:00
|
|
|
typ = typ.find()
|
|
|
|
return isinstance(typ, types.TBuiltin) and \
|
2015-06-26 16:16:08 +08:00
|
|
|
typ.name == name
|