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
|
|
|
"""
|
|
|
|
|
2015-07-16 21:03:24 +08:00
|
|
|
from collections import OrderedDict
|
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")
|
|
|
|
|
2015-07-16 21:03:24 +08:00
|
|
|
@staticmethod
|
|
|
|
def zero():
|
|
|
|
return False
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def one():
|
|
|
|
return True
|
|
|
|
|
2015-06-13 15:29:26 +08:00
|
|
|
class TInt(types.TMono):
|
|
|
|
def __init__(self, width=None):
|
|
|
|
if width is None:
|
|
|
|
width = types.TVar()
|
|
|
|
super().__init__("int", {"width": width})
|
|
|
|
|
2015-07-16 21:03:24 +08:00
|
|
|
@staticmethod
|
|
|
|
def zero():
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def one():
|
|
|
|
return 1
|
|
|
|
|
2015-06-13 15:29:26 +08:00
|
|
|
class TFloat(types.TMono):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__("float")
|
|
|
|
|
2015-07-16 21:03:24 +08:00
|
|
|
@staticmethod
|
|
|
|
def zero():
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def one():
|
|
|
|
return 1.0
|
|
|
|
|
2015-07-21 19:27:48 +08:00
|
|
|
class TStr(types.TMono):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__("str")
|
|
|
|
|
2015-06-13 15:29:26 +08:00
|
|
|
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-07-16 19:57:44 +08:00
|
|
|
self.attributes = OrderedDict([
|
|
|
|
("start", elt),
|
|
|
|
("stop", elt),
|
|
|
|
("step", elt),
|
|
|
|
])
|
2015-06-26 23:53:20 +08:00
|
|
|
|
2015-06-29 03:48:15 +08:00
|
|
|
class TException(types.TMono):
|
2015-07-25 10:37:37 +08:00
|
|
|
# All exceptions share the same internal layout:
|
|
|
|
# * Pointer to the unique global with the name of the exception (str)
|
|
|
|
# (which also serves as the EHABI type_info).
|
|
|
|
# * File, line and column where it was raised (str, int, int).
|
|
|
|
# * Message, which can contain substitutions {0}, {1} and {2} (str).
|
|
|
|
# * Three 64-bit integers, parameterizing the message (int(width=64)).
|
|
|
|
|
|
|
|
|
|
|
|
# Keep this in sync with the function ARTIQIRGenerator.alloc_exn.
|
|
|
|
attributes = OrderedDict([
|
|
|
|
("__name__", TStr()),
|
|
|
|
("__file__", TStr()),
|
|
|
|
("__line__", TInt(types.TValue(32))),
|
|
|
|
("__col__", TInt(types.TValue(32))),
|
2015-08-08 21:01:08 +08:00
|
|
|
("__func__", TStr()),
|
2015-07-25 10:37:37 +08:00
|
|
|
("__message__", TStr()),
|
|
|
|
("__param0__", TInt(types.TValue(64))),
|
|
|
|
("__param1__", TInt(types.TValue(64))),
|
|
|
|
("__param2__", TInt(types.TValue(64))),
|
|
|
|
])
|
|
|
|
|
2015-07-16 19:58:40 +08:00
|
|
|
def __init__(self, name="Exception"):
|
|
|
|
super().__init__(name)
|
|
|
|
|
2015-06-26 16:16:08 +08:00
|
|
|
def fn_bool():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TConstructor(TBool())
|
2015-06-26 16:16:08 +08:00
|
|
|
|
|
|
|
def fn_int():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TConstructor(TInt())
|
2015-06-26 16:16:08 +08:00
|
|
|
|
|
|
|
def fn_float():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TConstructor(TFloat())
|
2015-06-26 16:16:08 +08:00
|
|
|
|
2015-07-21 19:27:48 +08:00
|
|
|
def fn_str():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TConstructor(TStr())
|
2015-07-21 19:27:48 +08:00
|
|
|
|
2015-06-26 16:16:08 +08:00
|
|
|
def fn_list():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TConstructor(TList())
|
2015-06-26 16:16:08 +08:00
|
|
|
|
2015-06-29 03:48:15 +08:00
|
|
|
def fn_Exception():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TExceptionConstructor(TException("Exception"))
|
2015-06-29 03:48:15 +08:00
|
|
|
|
2015-07-16 19:58:40 +08:00
|
|
|
def fn_IndexError():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TExceptionConstructor(TException("IndexError"))
|
2015-07-16 19:58:40 +08:00
|
|
|
|
|
|
|
def fn_ValueError():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TExceptionConstructor(TException("ValueError"))
|
2015-07-16 19:58:40 +08:00
|
|
|
|
2015-07-23 06:26:50 +08:00
|
|
|
def fn_ZeroDivisionError():
|
2015-08-15 21:45:16 +08:00
|
|
|
return types.TExceptionConstructor(TException("ZeroDivisionError"))
|
2015-07-23 06:26:50 +08:00
|
|
|
|
2015-06-26 23:53:20 +08:00
|
|
|
def fn_range():
|
2015-06-29 03:40:57 +08:00
|
|
|
return types.TBuiltinFunction("range")
|
2015-06-26 23:53:20 +08:00
|
|
|
|
2015-06-24 16:28:24 +08:00
|
|
|
def fn_len():
|
2015-06-29 03:40:57 +08:00
|
|
|
return types.TBuiltinFunction("len")
|
2015-06-24 16:28:24 +08:00
|
|
|
|
|
|
|
def fn_round():
|
2015-06-29 03:40:57 +08:00
|
|
|
return types.TBuiltinFunction("round")
|
2015-06-24 16:28:24 +08:00
|
|
|
|
2015-07-22 03:32:10 +08:00
|
|
|
def fn_print():
|
|
|
|
return types.TBuiltinFunction("print")
|
|
|
|
|
2015-08-07 12:54:35 +08:00
|
|
|
def fn_kernel():
|
|
|
|
return types.TBuiltinFunction("kernel")
|
|
|
|
|
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):
|
2015-06-29 03:48:15 +08:00
|
|
|
if width is not None:
|
2015-08-09 07:17:19 +08:00
|
|
|
return types.is_mono(typ, "int", width=width)
|
2015-06-13 18:45:09 +08:00
|
|
|
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-07-21 19:27:48 +08:00
|
|
|
def is_str(typ):
|
|
|
|
return types.is_mono(typ, "str")
|
|
|
|
|
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):
|
2015-06-29 03:48:15 +08:00
|
|
|
if elt is not None:
|
2015-08-09 07:17:19 +08:00
|
|
|
return types.is_mono(typ, "list", elt=elt)
|
2015-06-14 17:07:13 +08:00
|
|
|
else:
|
|
|
|
return types.is_mono(typ, "list")
|
|
|
|
|
2015-06-26 23:53:20 +08:00
|
|
|
def is_range(typ, elt=None):
|
2015-06-29 03:48:15 +08:00
|
|
|
if elt is not None:
|
2015-06-26 23:53:20 +08:00
|
|
|
return types.is_mono(typ, "range", {"elt": elt})
|
|
|
|
else:
|
|
|
|
return types.is_mono(typ, "range")
|
|
|
|
|
2015-07-25 10:37:37 +08:00
|
|
|
def is_exception(typ, name=None):
|
|
|
|
if name is None:
|
|
|
|
return isinstance(typ.find(), TException)
|
|
|
|
else:
|
|
|
|
return isinstance(typ.find(), TException) and \
|
|
|
|
typ.name == name
|
2015-07-17 21:05:02 +08:00
|
|
|
|
2015-06-26 23:53:20 +08:00
|
|
|
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):
|
2015-07-17 21:05:02 +08:00
|
|
|
return typ.find()["elt"].find()
|
2015-06-26 23:53:20 +08:00
|
|
|
|
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-07-21 19:27:48 +08:00
|
|
|
def is_allocated(typ):
|
2015-07-04 09:16:37 +08:00
|
|
|
return typ.fold(False, lambda accum, typ:
|
2015-08-15 23:04:12 +08:00
|
|
|
accum or not (is_none(typ) or is_bool(typ) or is_int(typ) or
|
|
|
|
is_float(typ) or is_range(typ) or
|
|
|
|
types.is_c_function(typ) or types.is_rpc_function(typ) or
|
2015-08-19 13:39:22 +08:00
|
|
|
types.is_method(typ) or
|
2015-08-15 23:04:12 +08:00
|
|
|
types.is_value(typ)))
|