2023-01-09 03:24:10 +08:00
|
|
|
# Copyright (C) 2022-2023 Exaloop Inc. <https://exaloop.io>
|
2022-09-16 03:40:00 +08:00
|
|
|
|
2024-03-02 06:11:23 +08:00
|
|
|
@tuple
|
|
|
|
@__internal__
|
|
|
|
class type[T]:
|
2024-03-21 04:47:50 +08:00
|
|
|
# __new__ / __init__: always done internally
|
|
|
|
# __repr__
|
|
|
|
# __call__
|
|
|
|
# __name__
|
|
|
|
# __module__
|
|
|
|
# __doc__
|
2024-03-02 06:11:23 +08:00
|
|
|
pass
|
|
|
|
|
2022-07-27 04:06:00 +08:00
|
|
|
@__internal__
|
|
|
|
class __internal__:
|
|
|
|
pass
|
2023-05-03 04:09:19 +08:00
|
|
|
@__internal__
|
|
|
|
class __magic__:
|
|
|
|
pass
|
2022-07-27 04:06:00 +08:00
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class bool:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class byte:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class int:
|
2022-09-16 03:40:00 +08:00
|
|
|
MAX = 9223372036854775807
|
2022-07-27 04:06:00 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class float:
|
2022-09-16 03:40:00 +08:00
|
|
|
MIN_10_EXP = -307
|
2022-07-27 04:06:00 +08:00
|
|
|
pass
|
|
|
|
|
2022-09-16 03:40:00 +08:00
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-09-16 03:40:00 +08:00
|
|
|
class float32:
|
|
|
|
MIN_10_EXP = -37
|
|
|
|
pass
|
|
|
|
|
2022-07-27 04:06:00 +08:00
|
|
|
@tuple
|
|
|
|
@__internal__
|
|
|
|
class NoneType:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class Function[T, TR]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
|
|
|
class Callable[T, TR]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class Ptr[T]:
|
|
|
|
pass
|
|
|
|
cobj = Ptr[byte]
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class Generator[T]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class Optional:
|
|
|
|
T: type = NoneType
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class Int[N: Static[int]]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
2023-04-13 06:13:54 +08:00
|
|
|
@__notuple__
|
2022-07-27 04:06:00 +08:00
|
|
|
class UInt[N: Static[int]]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@__internal__
|
|
|
|
class pyobj:
|
|
|
|
p: Ptr[byte]
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
|
|
|
class str:
|
|
|
|
ptr: Ptr[byte]
|
|
|
|
len: int
|
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
|
|
|
class Array:
|
|
|
|
len: int
|
|
|
|
ptr: Ptr[T]
|
|
|
|
T: type
|
|
|
|
|
|
|
|
@extend
|
|
|
|
class type:
|
|
|
|
def __new__(obj):
|
|
|
|
pass
|
|
|
|
function = Function
|
|
|
|
|
2022-12-05 08:45:21 +08:00
|
|
|
@__internal__
|
|
|
|
class Ref[T]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tuple
|
2023-04-13 06:13:54 +08:00
|
|
|
@__internal__
|
|
|
|
@__notuple__
|
2022-12-05 08:45:21 +08:00
|
|
|
class Union[TU]:
|
2023-02-06 07:53:15 +08:00
|
|
|
# compiler-generated
|
|
|
|
def __new__(val):
|
|
|
|
TU
|
2022-12-05 08:45:21 +08:00
|
|
|
|
2023-12-14 09:23:50 +08:00
|
|
|
@extend
|
|
|
|
class Function:
|
|
|
|
@llvm
|
|
|
|
def __new__() -> Function[T, TR]:
|
|
|
|
ret ptr null
|
|
|
|
|
2022-07-27 04:06:00 +08:00
|
|
|
# dummy
|
|
|
|
@__internal__
|
2022-09-16 03:40:00 +08:00
|
|
|
class TypeVar[T]: pass
|
2022-07-27 04:06:00 +08:00
|
|
|
@__internal__
|
|
|
|
class ByVal: pass
|
|
|
|
@__internal__
|
|
|
|
class ByRef: pass
|
|
|
|
@__internal__
|
|
|
|
class Tuple: pass
|
|
|
|
|
|
|
|
@__internal__
|
|
|
|
class ClassVar[T]:
|
|
|
|
pass
|
|
|
|
|
2023-05-02 07:18:18 +08:00
|
|
|
@__internal__
|
|
|
|
class RTTI:
|
|
|
|
id: int
|
|
|
|
|
|
|
|
@__internal__
|
2022-07-27 04:06:00 +08:00
|
|
|
@tuple
|
2023-05-10 21:28:25 +08:00
|
|
|
class ellipsis:
|
2024-03-10 10:18:10 +08:00
|
|
|
@llvm
|
2023-05-10 21:28:25 +08:00
|
|
|
def __new__() -> ellipsis:
|
2024-03-10 10:18:10 +08:00
|
|
|
ret {} {}
|
2023-05-10 21:28:25 +08:00
|
|
|
Ellipsis = ellipsis()
|
2022-07-27 04:06:00 +08:00
|
|
|
|
|
|
|
@tuple
|
|
|
|
@__internal__
|
|
|
|
class __array__:
|
|
|
|
T: type
|
|
|
|
def __new__(sz: Static[int]) -> Array[T]:
|
|
|
|
pass
|
|
|
|
|
2023-06-25 06:17:52 +08:00
|
|
|
@tuple
|
|
|
|
@__internal__
|
|
|
|
class Import:
|
2024-01-18 13:33:13 +08:00
|
|
|
__ipath__: str
|
|
|
|
__iname__: str
|
|
|
|
P: Static[str]
|
2024-03-10 10:18:10 +08:00
|
|
|
|
|
|
|
@llvm
|
2024-03-19 07:15:36 +08:00
|
|
|
def __new__(path: str, name: str, P: Static[str]) -> Import[P]:
|
2024-03-10 10:18:10 +08:00
|
|
|
%0 = insertvalue { {=str}, {=str} } undef, {=str} %path, 0
|
|
|
|
%1 = insertvalue { {=str}, {=str} } %0, {=str} %name, 1
|
|
|
|
ret { {=str}, {=str} } %1
|
2023-06-25 06:17:52 +08:00
|
|
|
|
2022-07-27 04:06:00 +08:00
|
|
|
def __ptr__(var):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def staticlen(obj):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def compile_error(msg: Static[str]):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def isinstance(obj, what):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@__attribute__
|
|
|
|
def overload():
|
|
|
|
pass
|
|
|
|
|
2023-02-06 07:53:15 +08:00
|
|
|
def hasattr(obj, attr: Static[str], *args, **kwargs):
|
2022-07-27 04:06:00 +08:00
|
|
|
"""Special handling"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def hasattr(obj, attr: Static[str]):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getattr(obj, attr: Static[str]):
|
|
|
|
pass
|
|
|
|
|
2023-04-13 06:13:54 +08:00
|
|
|
def setattr(obj, attr: Static[str], what):
|
|
|
|
pass
|
|
|
|
|
2022-07-27 04:06:00 +08:00
|
|
|
def tuple(iterable):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def super():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def superf(*args):
|
|
|
|
"""Special handling"""
|
|
|
|
pass
|
2022-09-16 03:40:00 +08:00
|
|
|
|
|
|
|
def __realized__(fn, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def statictuple(*args):
|
|
|
|
return args
|
2023-04-28 04:03:14 +08:00
|
|
|
|
|
|
|
def __has_rtti__(T: type):
|
|
|
|
pass
|
2023-07-10 17:24:33 +08:00
|
|
|
|
2023-08-01 19:27:17 +08:00
|
|
|
#(init=False, repr=False, eq=False, order=False, hash=False, pickle=False, python=False, gpu=False, container=False)
|
|
|
|
@__internal__
|
|
|
|
@tuple
|
|
|
|
class NamedTuple:
|
|
|
|
args: T
|
|
|
|
N: Static[int] # name cache ID
|
|
|
|
T: type
|
|
|
|
|
|
|
|
# todo)) nicer way to express type(())
|
|
|
|
def __new__(args: T = (), T: type) -> NamedTuple[0, T]:
|
2024-03-10 10:18:10 +08:00
|
|
|
return NamedTuple[0, T](())
|
2023-08-01 19:27:17 +08:00
|
|
|
|
2024-03-10 10:18:10 +08:00
|
|
|
@llvm
|
2023-08-01 19:27:17 +08:00
|
|
|
def __new__(args: T, N: Static[int], T: type) -> NamedTuple[N, T]:
|
2024-03-10 10:18:10 +08:00
|
|
|
%0 = insertvalue { {=T} } undef, {=T} %args, 0
|
|
|
|
ret { {=T} } %0
|
2023-08-01 19:27:17 +08:00
|
|
|
|
|
|
|
def __getitem__(self, key: Static[str]):
|
|
|
|
return getattr(self, key)
|
|
|
|
|
2023-11-21 13:09:50 +08:00
|
|
|
def __keys__(self):
|
|
|
|
return __internal__.namedkeys(N)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
keys = self.__keys__()
|
|
|
|
values = [v.__repr__() for v in self.args]
|
|
|
|
s = ', '.join(f"{keys[i]}: {values[i]}" for i in range(len(keys)))
|
|
|
|
return f"({s})"
|
|
|
|
|
2023-08-01 19:27:17 +08:00
|
|
|
|
|
|
|
@__internal__
|
|
|
|
@tuple
|
|
|
|
class Partial:
|
2024-03-10 10:18:10 +08:00
|
|
|
args: T # format: (arg1, arg2, ..., (star_args...))
|
2023-08-01 19:27:17 +08:00
|
|
|
kwargs: K
|
|
|
|
|
2024-03-22 02:29:07 +08:00
|
|
|
# Function argument is handled in Codon
|
2023-08-01 19:27:17 +08:00
|
|
|
M: Static[str] # mask
|
|
|
|
T: type # Tuple
|
|
|
|
K: type # NamedTuple
|
|
|
|
|
2024-03-10 10:18:10 +08:00
|
|
|
@llvm
|
2024-03-22 02:29:07 +08:00
|
|
|
def __new__(args: T, kwargs: K, M: Static[str], T: type, K: type) -> Partial[M, T, K]:
|
2024-03-10 10:18:10 +08:00
|
|
|
%0 = insertvalue { {=T}, {=K} } undef, {=T} %args, 0
|
|
|
|
%1 = insertvalue { {=T}, {=K} } %0, {=K} %kwargs, 1
|
|
|
|
ret { {=T}, {=K} } %1
|
2023-08-01 19:27:17 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return __magic__.repr_partial(self)
|