codon/stdlib/internal/builtin.codon

559 lines
13 KiB
Python
Raw Normal View History

# Copyright (C) 2022-2023 Exaloop Inc. <https://exaloop.io>
2022-01-24 14:47:43 +08:00
2021-09-28 02:02:44 +08:00
class object:
2022-10-23 08:53:25 +08:00
def __init__(self):
pass
2022-01-24 14:47:43 +08:00
def __repr__(self) -> str:
2022-10-23 08:53:25 +08:00
return f"<{self.__class__.__name__} object at {self.__raw__()}>"
2022-01-24 14:47:43 +08:00
def id(x) -> int:
2021-09-28 02:02:44 +08:00
if isinstance(x, ByRef):
return int(x.__raw__())
else:
return 0
_stdout = _C.seq_stdout()
2022-01-24 14:47:43 +08:00
2022-02-16 23:51:16 +08:00
def print(*args, sep: str = " ", end: str = "\n", file=_stdout, flush: bool = False):
2021-09-28 02:02:44 +08:00
"""
Print args to the text stream file.
"""
fp = cobj()
Typechecker refactoring (#20) * Initial refactor commit * Support external vars * Simplify refactor; Python scoping [wip] * Python scoping [wip] * Python scoping [fix loops; wip] * Fix lambdas * Python scoping [test fixes; wip] * Fix scoping [wip] * Fix basic tests [no-ci] * Fix tests * CallExpr refactoring [wip] * CallExpr refactoring [wip] * Remove activeUnbounds tracking * Add core.codon * Move Function and other core types to core.codon; Revamp Function and Callable types * Refactor IntExpr, FloatExpr and CallExpr * Refactor ClassStmt * Refactor context, IdExpr and DotExpr * Refactor DotExpr and AssignStmt * Refactor ImportStmt * Refactor FunctionStmt * Refactor * Remove UpdateStmt * Refactor AssignReplacementVisitor * Make SimplifyVisitor in-place * Fix new scoping * Fix import type alias handling * Add docstrings; Complete Simplify refactoring * Fixes for seqtest * Refactor typecheck [wip] * Refactor typecheck [wip] * Refactor typecheck/access; Remove void anduse NoneType; Fix #18 * Refactor typecheck/assign * clang-format and cmake-format * Fix none types in IR * Multi-error support in simplify * Fix IR tests for new void * Simplify ClassStmt * Refactor cond.cpp * Refactor error.cpp * Refactor function.cpp and simplify unbounds * Refactor op.cpp * Refactor call.cpp [wip] [no-ci] * seqassertn updates [noci] * Refactor call.cpp * Refactor call.cpp * Refactor call.cpp * Refactor typecheck * clang-tidy updates [noci] * Refactor infer.cpp [wip] * Refactor infer.cpp * Refactor wrapExpr * Remove visitedAsts * Remove old base logic * Refactor typecheck ctx * Fix JIT bug * Fix JIT tests * Scoping fixes [wip] [noci] * Fix ImperativeForFlow var store * Add newlines [noci] * Dump IR module with log flag * Fix scoping bugs; Add &, ^ and | static operations; Address stylistic review issues * Fix side effect analysis for for-loops * Add support for class variables and ClassVar * Refactor special dot-member cases * Add codon app tests * Fix class variables; clang-tidy * Fix __argv__ * Add datetime constants and update tests * Fix #25; Add Py_None, Py_True and Py_False; External var support [wip] * External var support [wip] * Dump LLVM IR when debug flags are active * clang-format * Fix arg var construction * Extern var fixes * Undo extern var changes related to stdout etc. * Fix tuple magics * Fix extern vars and tuple magics * Fix duplicate var name error * Fix extern vars * Fix #16 * Fix side-effect analysis for try-catch * Move test C var to test executable * Add staticmethod * Fix var status for try-catch * Fix tests * Fix shell var name * Fix test * Fix app test * Fix scoping issue (remove dominated identifier from stack) * Fix no-pie issue * Use PIC when building library object * Don't use -no-pie when building library [noci] * Use -relocation-model=pic in test * Fix lib build on Linux * Fix lib build * Update exceptions to use subclasses vs. header * Fix __repr__ * Fix tests * Fix exceptions test * Don't build docs Co-authored-by: A. R. Shajii <ars@ars.me>
2022-07-27 04:06:00 +08:00
if isinstance(file, cobj):
2021-09-28 02:02:44 +08:00
fp = file
else:
fp = file.fp
i = 0
for a in args:
if i and sep:
_C.seq_print_full(sep, fp)
_C.seq_print_full(str(a), fp)
i += 1
_C.seq_print_full(end, fp)
if flush:
_C.fflush(fp)
Dynamic Polymorphism (#58) * Use Static[] for static inheritance * Support .seq extension * Fix #36 * Polymorphic typechecking; vtables [wip] * v-table dispatch [wip] * vtable routing [wip; bug] * vtable routing [MVP] * Fix texts * Add union type support * Update FAQs * Clarify * Add BSL license * Add makeUnion * Add IR UnionType * Update union representation in LLVM * Update README * Update README.md * Update README * Update README.md * Add benchmarks * Add more benchmarks and README * Add primes benchmark * Update benchmarks * Fix cpp * Clean up list * Update faq.md * Add binary trees benchmark * Add fannkuch benchmark * Fix paths * Add PyPy * Abort on fail * More benchmarks * Add cpp word_count * Update set_partition cpp * Add nbody cpp * Add TAQ cpp; fix word_count timing * Update CODEOWNERS * Update README * Update README.md * Update CODEOWNERS * Fix bench script * Update binary_trees.cpp * Update taq.cpp * Fix primes benchmark * Add mandelbrot benchmark * Fix OpenMP init * Add Module::unsafeGetUnionType * UnionType [wip] [skip ci] * Integrate IR unions and Union * UnionType refactor [skip ci] * Update README.md * Update docs * UnionType [wip] [skip ci] * UnionType and automatic unions * Add Slack * Update faq.md * Refactor types * New error reporting [wip] * New error reporting [wip] * peglib updates [wip] [skip_ci] * Fix parsing issues * Fix parsing issues * Fix error reporting issues * Make sure random module matches Python * Update releases.md * Fix tests * Fix #59 * Fix #57 * Fix #50 * Fix #49 * Fix #26; Fix #51; Fix #47; Fix #49 * Fix collection extension methods * Fix #62 * Handle *args/**kwargs with Callable[]; Fix #43 * Fix #43 * Fix Ptr.__sub__; Fix polymorphism issues * Add typeinfo * clang-format * Upgrade fmtlib to v9; Use CPM for fmtlib; format spec support; __format__ support * Use CPM for semver and toml++ * Remove extension check * Revamp str methods * Update str.zfill * Fix thunk crashes [wip] [skip_ci] * Fix str.__reversed__ * Fix count_with_max * Fix vtable memory allocation issues * Add poly AST tests * Use PDQsort when stability does not matter * Fix dotted imports; Fix issues * Fix kwargs passing to Python * Fix #61 * Fix #37 * Add isinstance support for unions; Union methods return Union type if different * clang-format * Nicely format error tracebacks * Fix build issues; clang-format * Fix OpenMP init * Fix OpenMP init * Update README.md * Fix tests * Update license [skip ci] * Update license [ci skip] * Add copyright header to all source files * Fix super(); Fix error recovery in ClassStmt * Clean up whitespace [ci skip] * Use Python 3.9 on CI * Print info in random test * Fix single unions * Update random_test.codon * Fix polymorhic thunk instantiation * Fix random test * Add operator.attrgetter and operator.methodcaller * Add code documentation * Update documentation * Update README.md * Fix tests * Fix random init Co-authored-by: A. R. Shajii <ars@ars.me>
2022-12-05 08:45:21 +08:00
@extend
class __internal__:
def print(*args):
print(*args, flush=True, file=_C.seq_stdout())
def min(*args, key=None, default=None):
2021-09-28 02:02:44 +08:00
if staticlen(args) == 0:
compile_error("min() expected at least 1 argument, got 0")
elif staticlen(args) > 1 and default is not None:
compile_error("min() 'default' argument only allowed for iterables")
elif staticlen(args) == 1:
2021-09-28 02:02:44 +08:00
x = args[0].__iter__()
if not x.done():
s = x.next()
while not x.done():
i = x.next()
if key is None:
if i < s:
s = i
else:
if key(i) < key(s):
s = i
2021-09-28 02:02:44 +08:00
x.destroy()
return s
else:
x.destroy()
if default is None:
raise ValueError("min() arg is an empty sequence")
else:
return default
2021-09-28 02:02:44 +08:00
elif staticlen(args) == 2:
a, b = args
if key is None:
return a if a <= b else b
else:
return a if key(a) <= key(b) else b
2021-09-28 02:02:44 +08:00
else:
m = args[0]
for i in args[1:]:
if key is None:
if i < m:
m = i
else:
if key(i) < key(m):
m = i
2021-09-28 02:02:44 +08:00
return m
def max(*args, key=None, default=None):
2021-09-28 02:02:44 +08:00
if staticlen(args) == 0:
compile_error("max() expected at least 1 argument, got 0")
elif staticlen(args) > 1 and default is not None:
compile_error("max() 'default' argument only allowed for iterables")
elif staticlen(args) == 1:
2021-09-28 02:02:44 +08:00
x = args[0].__iter__()
if not x.done():
s = x.next()
while not x.done():
i = x.next()
if key is None:
if i > s:
s = i
else:
if key(i) > key(s):
s = i
2021-09-28 02:02:44 +08:00
x.destroy()
return s
else:
x.destroy()
if default is None:
raise ValueError("max() arg is an empty sequence")
else:
return default
2021-09-28 02:02:44 +08:00
elif staticlen(args) == 2:
a, b = args
if key is None:
return a if a >= b else b
else:
return a if key(a) >= key(b) else b
2021-09-28 02:02:44 +08:00
else:
m = args[0]
for i in args[1:]:
if key is None:
if i > m:
m = i
else:
if key(i) > key(m):
m = i
2021-09-28 02:02:44 +08:00
return m
2022-01-24 14:47:43 +08:00
def len(x) -> int:
2021-09-28 02:02:44 +08:00
"""
Return the length of x
"""
return x.__len__()
def iter(x):
"""
Return an iterator for the given object
"""
return x.__iter__()
def abs(x):
"""
Return the absolute value of x
"""
return x.__abs__()
2022-01-24 14:47:43 +08:00
def hash(x) -> int:
2021-09-28 02:02:44 +08:00
"""
Returns hashed value only for immutable objects
"""
return x.__hash__()
2022-01-24 14:47:43 +08:00
def ord(s: str) -> int:
2021-09-28 02:02:44 +08:00
"""
Return an integer representing the Unicode code point of s
"""
if len(s) != 1:
2022-01-24 14:47:43 +08:00
raise TypeError(
2022-02-16 23:51:16 +08:00
f"ord() expected a character, but string of length {len(s)} found"
2022-01-24 14:47:43 +08:00
)
2021-09-28 02:02:44 +08:00
return int(s.ptr[0])
2021-10-10 05:07:41 +08:00
def divmod(a, b):
if hasattr(a, "__divmod__"):
return a.__divmod__(b)
else:
return (a // b, a % b)
2022-01-24 14:47:43 +08:00
def chr(i: int) -> str:
2021-09-28 02:02:44 +08:00
"""
Return a string representing a character whose Unicode
code point is an integer
"""
p = cobj(1)
p[0] = byte(i)
return str(p, 1)
2022-01-24 14:47:43 +08:00
def next(g: Generator[T], default: Optional[T] = None, T: type) -> T:
2021-09-28 02:02:44 +08:00
"""
Return the next item from g
"""
if g.done():
if default is not None:
return default.__val__()
2021-09-28 02:02:44 +08:00
else:
raise StopIteration()
return g.next()
2022-01-24 14:47:43 +08:00
def any(x: Generator[T], T: type) -> bool:
2021-09-28 02:02:44 +08:00
"""
Returns True if any item in x is true,
False otherwise
"""
for a in x:
if a:
return True
return False
2022-01-24 14:47:43 +08:00
def all(x: Generator[T], T: type) -> bool:
2021-09-28 02:02:44 +08:00
"""
Returns True when all elements in x are true,
False otherwise
"""
for a in x:
if not a:
return False
return True
def zip(*args):
"""
Returns a zip object, which is an iterator of tuples
that aggregates elements based on the iterables passed
"""
if staticlen(args) == 0:
yield from List[int]()
else:
iters = tuple(iter(i) for i in args)
done = False
while not done:
for i in iters:
if i.done():
done = True
if not done:
yield tuple(i.next() for i in iters)
for i in iters:
i.destroy()
2022-01-24 14:47:43 +08:00
def filter(f: Callable[[T], bool], x: Generator[T], T: type) -> Generator[T]:
2021-09-28 02:02:44 +08:00
"""
Returns all a from the iterable x that are filtered by f
"""
for a in x:
if f(a):
yield a
def map(f, *args):
"""
Applies a function on all a in x and returns map object
"""
if staticlen(args) == 0:
compile_error("map() expects at least one iterator")
elif staticlen(args) == 1:
for a in args[0]:
yield f(a)
else:
for a in zip(*args):
yield f(*a)
def enumerate(x, start: int = 0):
"""
Creates a tuple containing a count (from start which defaults
to 0) and the values obtained from iterating over x
"""
i = start
for a in x:
2022-01-24 14:47:43 +08:00
yield (i, a)
2021-09-28 02:02:44 +08:00
i += 1
v0.16 (#335) * Add Python extension lowering pass * Add DocstringAttribute * Add extension module codegen * Handle different argument counts efficiently * Add warnings to extension lowering * Fix module name * Fix extension codegen * Fix argument check * Auto-convert Codon exceptions to Python exceptions * Fix #183 * Fix #162; Fix #135 * Fix #155 * Fix CPython interface in codegen * Fix #191 * Fix #187 * Fix #189 * Generate object file in pyext mode * Convert Codon exceptions to Python exceptions * Fix vtable init; Fix failing tests on Linux * Fix #190 * Fix #156 * Fix union routing * Remove need for import python * Automatic @export and wrapping for toplevel functions * Reorganize API * Add Python extension IR structs * Add special calls for no-suspend yield-expr * Add special calls for no-suspend yield-expr * pyextension.h support [wip] * pyextension.h support [wip] * pyextension.h support * pyextension.h support for toplevel functions * clang-format * Add PyFunction::nargs field * Update pyextension codegen (WIP) * SUpport nargs * Add support for @pycapture * PyType codegen (WIP) * Py method codegen (WIP) * Add type ptr hook * Add getset codegen * Add type alloc function * Add type pointer hook codegen * Re-organize codegen * Add member codegen * Update module init codegen * Update module init codegen * Add support for typePtrHook and new to/from_py hooks * Fix extension codegen * Fix init codegen * Fix init codegen; add "tp_new" slot * Fix type hook * Add extra flags * Specialized wrappers (PyType specs) * Add static Python link option * Fix C imports * Add guards * Remove unused field * Python mode only when pyExt set * Update python module * Fix assert * Update codegen/passes * Fix tuple parsing in index expression * Fix empty tuple unification * Do not Cythonize underscore fns * clang-format * Fix switch * Add Py support for cmp/setitem * Add Py support for cmp/setitem * Add type is support * GetSet support * clang-format * GetSet support (fixes) * Avoid useless vtable alloc * Add iter support * Fix size_t capture bug * clang-format * Fix POD type unification with tuples * Add __try_from_py__ API * Fix annotation * Add static reflection methods (setattr; internal.static.*); refactor PyExt to python.codon; handle errors and kwargs in PyExt * Python compat fixes * Update Python object conversions * Fix PyErrors * clang-format; add copyright * Add PyFunction::keywords field * Fix JIT MRO handling; Refactor out Jupyter support * Refactor out Jupyter support * Add support for custom linking args (link=[]) to TOML plugins * Fix tests * Use g++ instead of gcc * Fix Jupyter CMAKE * Fix Jupyter CMAKE * Add _PyArg_Parser definition * Add complex64 type * Add extra complex64 tests * Fix Python calls; add staticenumerate * Fix call * Fix calls * Update pyext wrappers * Fix staticenumerate; Support static calls in tuple() * Fix pyext routing * Add add/mul for tuples * clang-format * Fix pyext codegen * Fix wrap_multiple * Add seq_alloc_atomic_uncollectable * Fix default generics issue * Add binary/ternary ops * Fix missing generic issue * Fix number slots * Update pow * Remove unnecessary pyobj * Fix allocation * Refactor errors * Add test extension * Fix formatting * clang-format * Fix getitem/setitem/delitem in pyext * Fix pyext iterators * Add builtin pow() (fix #294) * Fix #244 * Fix #231 * Fix #229 * Fix #205 * Update docs * Fix error message * Add pyext tests * Add pyext support for @property * Add pyext support for toplevel fns and @tuple classes * More pyext tests * More pyext tests * Fix file error checking * More pyext tests * Update pyext tests * Update docs * Add pyext test to CI * Add pyext support for @tuple.__new__ * Add pyext support for @tuple.__new__ * Fix hetero-tuple issue with fn_overloads * More pyext tests * Bump versions * Fix del magic in pyext * Fix init magic for tuples in pyext * Have test-pypi only run on develop branch * Make exception type indices unnamed-addr * Fix #316; Fix #317 (slash issue) * Use uncollectible-alloc for vtable * Fix #249 * Add pyext docs * Fix #249; Fix clashing vtables; Fix super() and class_copy * Add content-atomic type property instruction * __contents_atomic__ support * Update internal functions * Use PIC when generating Python extension * Cleanup * Add Dockerfile & fix -fPIC * Cleanup * Fix setup.py * Fix pyext fn iteration * Fix CI * clang-format * Update long conversions in Py bridge * Support wide-int to str conversions * Fix test * Add pow for arbitrary-width ints * Fix Linux backtraces * Cleanup * Add more tests * Fix docs; Remove tuple.__add__ for scalars * Update docs --------- Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
2023-04-13 06:13:54 +08:00
def staticenumerate(tup):
i = -1
return tuple(((i := i + 1), t) for t in tup)
i
2021-09-28 02:02:44 +08:00
def echo(x):
"""
Print and return argument
"""
print x
return x
def reversed(x):
"""
Return an iterator that accesses x in the reverse order
"""
if hasattr(x, "__reversed__"):
return x.__reversed__()
else:
i = x.__len__() - 1
while i >= 0:
yield x[i]
i -= 1
2021-09-28 02:02:44 +08:00
2022-01-24 14:47:43 +08:00
def round(x, n=0):
2021-09-28 02:02:44 +08:00
"""
Return the x rounded off to the given
n digits after the decimal point.
"""
nx = float.__pow__(10.0, n)
return float.__round__(x * nx) / nx
def _sum_start(x, start):
if isinstance(x.__iter__(), Generator[float]) and isinstance(start, int):
return float(start)
else:
return start
def sum(x, start=0):
2021-09-28 02:02:44 +08:00
"""
Return the sum of the items added together from x
2021-09-28 02:02:44 +08:00
"""
s = _sum_start(x, start)
for a in x:
# don't use += to avoid calling iadd
if isinstance(a, bool):
s = s + (1 if a else 0)
else:
s = s + a
return s
2021-09-28 02:02:44 +08:00
def repr(x):
2022-01-24 14:47:43 +08:00
"""Return the string representation of x"""
2021-09-28 02:02:44 +08:00
return x.__repr__()
2022-01-24 14:47:43 +08:00
def _int_format(a: int, base: int, prefix: str = ""):
assert base == 2 or base == 8 or base == 10 or base == 16
2022-01-24 14:47:43 +08:00
chars = "0123456789abcdef-"
b = a
digits = 0
while b != 0:
digits += 1
b //= base
sz = digits + (1 if a <= 0 else 0) + len(prefix)
p = Ptr[byte](sz)
q = p
if a < 0:
q[0] = chars[-1].ptr[0]
q += 1
if prefix:
str.memcpy(q, prefix.ptr, len(prefix))
q += len(prefix)
if digits != 0:
b = a
q += digits - 1
i = 1
while b != 0:
i += 1
q[0] = chars.ptr[abs(b % base)]
q += -1
b //= base
else:
q[0] = chars.ptr[0]
return str(p, sz)
def bin(n):
2022-01-24 14:47:43 +08:00
return _int_format(n.__index__(), 2, "0b")
def oct(n):
2022-01-24 14:47:43 +08:00
return _int_format(n.__index__(), 8, "0o")
def hex(n):
2022-01-24 14:47:43 +08:00
return _int_format(n.__index__(), 16, "0x")
v0.16 (#335) * Add Python extension lowering pass * Add DocstringAttribute * Add extension module codegen * Handle different argument counts efficiently * Add warnings to extension lowering * Fix module name * Fix extension codegen * Fix argument check * Auto-convert Codon exceptions to Python exceptions * Fix #183 * Fix #162; Fix #135 * Fix #155 * Fix CPython interface in codegen * Fix #191 * Fix #187 * Fix #189 * Generate object file in pyext mode * Convert Codon exceptions to Python exceptions * Fix vtable init; Fix failing tests on Linux * Fix #190 * Fix #156 * Fix union routing * Remove need for import python * Automatic @export and wrapping for toplevel functions * Reorganize API * Add Python extension IR structs * Add special calls for no-suspend yield-expr * Add special calls for no-suspend yield-expr * pyextension.h support [wip] * pyextension.h support [wip] * pyextension.h support * pyextension.h support for toplevel functions * clang-format * Add PyFunction::nargs field * Update pyextension codegen (WIP) * SUpport nargs * Add support for @pycapture * PyType codegen (WIP) * Py method codegen (WIP) * Add type ptr hook * Add getset codegen * Add type alloc function * Add type pointer hook codegen * Re-organize codegen * Add member codegen * Update module init codegen * Update module init codegen * Add support for typePtrHook and new to/from_py hooks * Fix extension codegen * Fix init codegen * Fix init codegen; add "tp_new" slot * Fix type hook * Add extra flags * Specialized wrappers (PyType specs) * Add static Python link option * Fix C imports * Add guards * Remove unused field * Python mode only when pyExt set * Update python module * Fix assert * Update codegen/passes * Fix tuple parsing in index expression * Fix empty tuple unification * Do not Cythonize underscore fns * clang-format * Fix switch * Add Py support for cmp/setitem * Add Py support for cmp/setitem * Add type is support * GetSet support * clang-format * GetSet support (fixes) * Avoid useless vtable alloc * Add iter support * Fix size_t capture bug * clang-format * Fix POD type unification with tuples * Add __try_from_py__ API * Fix annotation * Add static reflection methods (setattr; internal.static.*); refactor PyExt to python.codon; handle errors and kwargs in PyExt * Python compat fixes * Update Python object conversions * Fix PyErrors * clang-format; add copyright * Add PyFunction::keywords field * Fix JIT MRO handling; Refactor out Jupyter support * Refactor out Jupyter support * Add support for custom linking args (link=[]) to TOML plugins * Fix tests * Use g++ instead of gcc * Fix Jupyter CMAKE * Fix Jupyter CMAKE * Add _PyArg_Parser definition * Add complex64 type * Add extra complex64 tests * Fix Python calls; add staticenumerate * Fix call * Fix calls * Update pyext wrappers * Fix staticenumerate; Support static calls in tuple() * Fix pyext routing * Add add/mul for tuples * clang-format * Fix pyext codegen * Fix wrap_multiple * Add seq_alloc_atomic_uncollectable * Fix default generics issue * Add binary/ternary ops * Fix missing generic issue * Fix number slots * Update pow * Remove unnecessary pyobj * Fix allocation * Refactor errors * Add test extension * Fix formatting * clang-format * Fix getitem/setitem/delitem in pyext * Fix pyext iterators * Add builtin pow() (fix #294) * Fix #244 * Fix #231 * Fix #229 * Fix #205 * Update docs * Fix error message * Add pyext tests * Add pyext support for @property * Add pyext support for toplevel fns and @tuple classes * More pyext tests * More pyext tests * Fix file error checking * More pyext tests * Update pyext tests * Update docs * Add pyext test to CI * Add pyext support for @tuple.__new__ * Add pyext support for @tuple.__new__ * Fix hetero-tuple issue with fn_overloads * More pyext tests * Bump versions * Fix del magic in pyext * Fix init magic for tuples in pyext * Have test-pypi only run on develop branch * Make exception type indices unnamed-addr * Fix #316; Fix #317 (slash issue) * Use uncollectible-alloc for vtable * Fix #249 * Add pyext docs * Fix #249; Fix clashing vtables; Fix super() and class_copy * Add content-atomic type property instruction * __contents_atomic__ support * Update internal functions * Use PIC when generating Python extension * Cleanup * Add Dockerfile & fix -fPIC * Cleanup * Fix setup.py * Fix pyext fn iteration * Fix CI * clang-format * Update long conversions in Py bridge * Support wide-int to str conversions * Fix test * Add pow for arbitrary-width ints * Fix Linux backtraces * Cleanup * Add more tests * Fix docs; Remove tuple.__add__ for scalars * Update docs --------- Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
2023-04-13 06:13:54 +08:00
def pow(base: float, exp: float):
return base ** exp
@overload
def pow(base: int, exp: int, mod: Optional[int] = None):
if exp < 0:
raise ValueError("pow() negative int exponent not supported")
if mod is not None:
if mod == 0:
raise ValueError("pow() 3rd argument cannot be 0")
base %= mod
result = 1
while exp > 0:
if exp & 1:
x = result * base
result = x % mod if mod is not None else x
y = base * base
base = y % mod if mod is not None else y
exp >>= 1
return result % mod if mod is not None else result
2021-09-28 02:02:44 +08:00
@extend
class int:
def _from_str(s: str, base: int):
from internal.gc import alloc_atomic, free
2022-01-24 14:47:43 +08:00
2021-09-28 02:02:44 +08:00
if base < 0 or base > 36 or base == 1:
raise ValueError("int() base must be >= 2 and <= 36, or 0")
s0 = s
s = s.strip()
2021-09-28 02:02:44 +08:00
buf = __array__[byte](32)
n = len(s)
2022-01-24 14:47:43 +08:00
need_dyn_alloc = n >= len(buf)
2021-09-28 02:02:44 +08:00
p = alloc_atomic(n + 1) if need_dyn_alloc else buf.ptr
str.memcpy(p, s.ptr, n)
p[n] = byte(0)
end = cobj()
result = _C.strtoll(p, __ptr__(end), i32(base))
2021-09-28 02:02:44 +08:00
if need_dyn_alloc:
free(p)
if n == 0 or end != p + n:
2022-01-24 14:47:43 +08:00
raise ValueError(
f"invalid literal for int() with base {base}: {s0.__repr__()}"
2022-01-24 14:47:43 +08:00
)
2021-09-28 02:02:44 +08:00
return result
2021-11-22 21:32:49 +08:00
@extend
class float:
def _from_str(s: str) -> float:
s0 = s
s = s.strip()
buf = __array__[byte](32)
n = len(s)
need_dyn_alloc = n >= len(buf)
p = alloc_atomic(n + 1) if need_dyn_alloc else buf.ptr
str.memcpy(p, s.ptr, n)
p[n] = byte(0)
end = cobj()
result = _C.strtod(p, __ptr__(end))
if need_dyn_alloc:
free(p)
if n == 0 or end != p + n:
raise ValueError(f"could not convert string to float: {s0.__repr__()}")
return result
2023-12-06 04:48:34 +08:00
@extend
class complex:
def _from_str(v: str) -> complex:
def parse_error():
raise ValueError("complex() arg is a malformed string")
buf = __array__[byte](32)
n = len(v)
need_dyn_alloc = n >= len(buf)
s = alloc_atomic(n + 1) if need_dyn_alloc else buf.ptr
str.memcpy(s, v.ptr, n)
s[n] = byte(0)
x = 0.0
y = 0.0
z = 0.0
got_bracket = False
start = s
end = cobj()
while str._isspace(s[0]):
s += 1
if s[0] == byte(40): # '('
got_bracket = True
s += 1
while str._isspace(s[0]):
s += 1
z = _C.strtod(s, __ptr__(end))
if end != s:
s = end
if s[0] == byte(43) or s[0] == byte(45): # '+' '-'
x = z
y = _C.strtod(s, __ptr__(end))
if end != s:
s = end
else:
y = 1.0 if s[0] == byte(43) else -1.0
s += 1
if not (s[0] == byte(106) or s[0] == byte(74)): # 'j' 'J'
if need_dyn_alloc:
free(s)
parse_error()
s += 1
elif s[0] == byte(106) or s[0] == byte(74): # 'j' 'J'
s += 1
y = z
else:
x = z
else:
if s[0] == byte(43) or s[0] == byte(45): # '+' '-'
y = 1.0 if s[0] == byte(43) else -1.0
s += 1
else:
y = 1.0
if not (s[0] == byte(106) or s[0] == byte(74)): # 'j' 'J'
if need_dyn_alloc:
free(s)
parse_error()
s += 1
while str._isspace(s[0]):
s += 1
if got_bracket:
if s[0] != byte(41): # ')'
if need_dyn_alloc:
free(s)
parse_error()
s += 1
while str._isspace(s[0]):
s += 1
if s - start != n:
if need_dyn_alloc:
free(s)
parse_error()
if need_dyn_alloc:
free(s)
return complex(x, y)
@extend
class float32:
def _from_str(s: str) -> float32:
return float32(float._from_str(s))
@extend
class float16:
def _from_str(s: str) -> float16:
return float16(float._from_str(s))
@extend
class bfloat16:
def _from_str(s: str) -> bfloat16:
return bfloat16(float._from_str(s))
@extend
class complex64:
def _from_str(s: str) -> complex64:
return complex64(complex._from_str(s))
2022-01-19 13:19:52 +08:00
def _jit_display(x, s: Static[str], bundle: Set[str] = Set[str]()):
Typechecker refactoring (#20) * Initial refactor commit * Support external vars * Simplify refactor; Python scoping [wip] * Python scoping [wip] * Python scoping [fix loops; wip] * Fix lambdas * Python scoping [test fixes; wip] * Fix scoping [wip] * Fix basic tests [no-ci] * Fix tests * CallExpr refactoring [wip] * CallExpr refactoring [wip] * Remove activeUnbounds tracking * Add core.codon * Move Function and other core types to core.codon; Revamp Function and Callable types * Refactor IntExpr, FloatExpr and CallExpr * Refactor ClassStmt * Refactor context, IdExpr and DotExpr * Refactor DotExpr and AssignStmt * Refactor ImportStmt * Refactor FunctionStmt * Refactor * Remove UpdateStmt * Refactor AssignReplacementVisitor * Make SimplifyVisitor in-place * Fix new scoping * Fix import type alias handling * Add docstrings; Complete Simplify refactoring * Fixes for seqtest * Refactor typecheck [wip] * Refactor typecheck [wip] * Refactor typecheck/access; Remove void anduse NoneType; Fix #18 * Refactor typecheck/assign * clang-format and cmake-format * Fix none types in IR * Multi-error support in simplify * Fix IR tests for new void * Simplify ClassStmt * Refactor cond.cpp * Refactor error.cpp * Refactor function.cpp and simplify unbounds * Refactor op.cpp * Refactor call.cpp [wip] [no-ci] * seqassertn updates [noci] * Refactor call.cpp * Refactor call.cpp * Refactor call.cpp * Refactor typecheck * clang-tidy updates [noci] * Refactor infer.cpp [wip] * Refactor infer.cpp * Refactor wrapExpr * Remove visitedAsts * Remove old base logic * Refactor typecheck ctx * Fix JIT bug * Fix JIT tests * Scoping fixes [wip] [noci] * Fix ImperativeForFlow var store * Add newlines [noci] * Dump IR module with log flag * Fix scoping bugs; Add &, ^ and | static operations; Address stylistic review issues * Fix side effect analysis for for-loops * Add support for class variables and ClassVar * Refactor special dot-member cases * Add codon app tests * Fix class variables; clang-tidy * Fix __argv__ * Add datetime constants and update tests * Fix #25; Add Py_None, Py_True and Py_False; External var support [wip] * External var support [wip] * Dump LLVM IR when debug flags are active * clang-format * Fix arg var construction * Extern var fixes * Undo extern var changes related to stdout etc. * Fix tuple magics * Fix extern vars and tuple magics * Fix duplicate var name error * Fix extern vars * Fix #16 * Fix side-effect analysis for try-catch * Move test C var to test executable * Add staticmethod * Fix var status for try-catch * Fix tests * Fix shell var name * Fix test * Fix app test * Fix scoping issue (remove dominated identifier from stack) * Fix no-pie issue * Use PIC when building library object * Don't use -no-pie when building library [noci] * Use -relocation-model=pic in test * Fix lib build on Linux * Fix lib build * Update exceptions to use subclasses vs. header * Fix __repr__ * Fix tests * Fix exceptions test * Don't build docs Co-authored-by: A. R. Shajii <ars@ars.me>
2022-07-27 04:06:00 +08:00
if isinstance(x, None):
return
2022-01-05 02:13:59 +08:00
if hasattr(x, "_repr_mimebundle_") and s == "jupyter":
d = x._repr_mimebundle_(bundle)
# TODO: pick appropriate mime
mime = next(d.keys()) # just pick first
print(f"\x00\x00__codon/mime__\x00{mime}\x00{d[mime]}", end='')
2021-11-22 21:32:49 +08:00
elif hasattr(x, "__repr__"):
2022-01-05 02:13:59 +08:00
print(x.__repr__(), end='')
2021-11-22 21:32:49 +08:00
elif hasattr(x, "__str__"):
2022-01-05 02:13:59 +08:00
print(x.__str__(), end='')