codon/test/parser/llvm.codon

206 lines
5.2 KiB
Python
Raw Normal View History

2021-09-27 18:02:44 +00:00
#%% ptr,barebones
import internal.gc as gc
print gc.sizeof(Ptr[int]) #: 8
print gc.atomic(Ptr[int]) #: False
y = Ptr[int](1)
y[0] = 11
print y[0] #: 11
_y = y.as_byte()
print int(_y[0]) #: 11
y = Ptr[int](5)
y[0] = 1; y[1] = 2; y[2] = 3; y[3] = 4; y[4] = 5
z = Ptr[int](y)
print y[1], z[2] #: 2 3
z = Ptr[int](y.as_byte())
print y[1], z[2] #: 2 3
print z.__bool__() #: True
z.__int__() # big number...
zz = z.__copy__() # this is not a deep copy!
zz[2] = 10
print z[2], zz[2] #: 10 10
print y.__getitem__(1) #: 2
y.__setitem__(1, 3)
print y[1] #: 3
print y.__add__(1)[0] #: 3
print (y + 3).__sub__(y + 1) #: 2
print y.__eq__(z) #: True
print y.__eq__(zz) #: True
print y.as_byte().__eq__('abc'.ptr) #: False
print y.__ne__(z) #: False
print y.__lt__(y+1) #: True
print y.__gt__(y+1) #: False
print (y+1).__le__(y) #: False
print y.__ge__(y) #: True
y.__prefetch_r0__()
y.__prefetch_r1__()
y.__prefetch_r2__()
y.__prefetch_r3__()
y.__prefetch_w0__()
y.__prefetch_w1__()
y.__prefetch_w2__()
y.__prefetch_w3__()
#%% int,barebones
a = int()
b = int(5)
c = int(True)
d = int(byte(1))
e = int(1.1)
print a, b, c, d, e #: 0 5 1 1 1
2021-10-12 20:16:24 +00:00
print a.__repr__() #: 0
2021-09-27 18:02:44 +00:00
print b.__copy__() #: 5
print b.__hash__() #: 5
print a.__bool__(), b.__bool__() #: False True
print a.__pos__() #: 0
print b.__neg__() #: -5
print (-b).__abs__() #: 5
print c.__truediv__(5) #: 0.2
print b.__lshift__(1) #: 10
print b.__rshift__(1) #: 2
print b.__truediv__(5.15) #: 0.970874
print a.__add__(1) #: 1
print a.__add__(1.1) #: 1.1
print a.__sub__(1) #: -1
print a.__sub__(1.1) #: -1.1
print b.__mul__(1) #: 5
print b.__mul__(1.1) #: 5.5
print b.__floordiv__(2) #: 2
print b.__floordiv__(1.1) #: 4
print b.__mod__(2) #: 1
print b.__mod__(1.1) #: 0.6
print a.__eq__(1) #: False
print a.__eq__(1.1) #: False
print a.__ne__(1) #: True
print a.__ne__(1.1) #: True
print a.__lt__(1) #: True
print a.__lt__(1.1) #: True
print a.__le__(1) #: True
print a.__le__(1.1) #: True
print a.__gt__(1) #: False
print a.__gt__(1.1) #: False
print a.__ge__(1) #: False
print a.__ge__(1.1) #: False
#%% uint,barebones
au = Int[123](15)
a = UInt[123]()
b = UInt[123](a)
a = UInt[123](15)
a = UInt[123](au)
print a.__copy__() #: 15
print a.__hash__() #: 15
print a.__bool__() #: True
print a.__pos__() #: 15
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-12 22:13:54 +00:00
print a.__neg__() #: 10633823966279326983230456482242756593
print a.__invert__() #: 10633823966279326983230456482242756592
2021-09-27 18:02:44 +00:00
m = UInt[123](4)
print a.__add__(m), a.__sub__(m), a.__mul__(m), a.__floordiv__(m), a.__truediv__(m) #: 19 11 60 3 3.75
print a.__mod__(m), a.__lshift__(m), a.__rshift__(m) #: 3 240 0
print a.__eq__(m), a.__ne__(m), a.__lt__(m), a.__gt__(m), a.__le__(m), a.__ge__(m) #: False True False True False True
print a.__and__(m), a.__or__(m), a.__xor__(m) #: 4 15 11
print a, a.popcnt() #: 15 4
ac = Int[128](5)
bc = Int[32](5)
print ac, bc, int(ac), int(bc) #: 5 5 5 5
print int(Int[12](12)) #: 12
print int(Int[122](12)) #: 12
print int(Int[64](12)) #: 12
print int(UInt[12](12)) #: 12
print int(UInt[122](12)) #: 12
print int(UInt[64](12)) #: 12
print Int[32](212) #: 212
print Int[64](212) #: 212
print Int[66](212) #: 212
print UInt[32](112) #: 112
print UInt[64](112) #: 112
print UInt[66](112) #: 112
#%% float,barebones
z = float.__new__()
z = 5.5
2021-10-12 20:16:24 +00:00
print z.__repr__() #: 5.5
2021-09-27 18:02:44 +00:00
print z.__copy__() #: 5.5
print z.__bool__(), z.__pos__(), z.__neg__() #: True 5.5 -5.5
f = 1.3
print z.__floordiv__(f), z.__floordiv__(2) #: 4 2
print z.__truediv__(f), z.__truediv__(2) #: 4.23077 2.75
print z.__pow__(2.2), z.__pow__(2) #: 42.54 30.25
print z.__add__(2) #: 7.5
print z.__sub__(2) #: 3.5
print z.__mul__(2) #: 11
print z.__truediv__(2) #: 2.75
print z.__mod__(2) #: 1.5
print z.__eq__(2) #: False
print z.__ne__(2) #: True
print z.__lt__(2) #: False
print z.__gt__(2) #: True
print z.__le__(2) #: False
print z.__ge__(2) #: True
#%% bool,barebones
z = bool.__new__()
2021-10-12 20:16:24 +00:00
print z.__repr__() #: False
2021-09-27 18:02:44 +00:00
print z.__copy__() #: False
print z.__bool__(), z.__invert__() #: False True
print z.__eq__(True) #: False
print z.__ne__(True) #: True
print z.__lt__(True) #: True
print z.__gt__(True) #: False
print z.__le__(True) #: True
print z.__ge__(True) #: False
print z.__and__(True), z.__or__(True), z.__xor__(True) #: False True True
#%% byte,barebones
z = byte.__new__()
z = byte(65)
2021-10-13 16:21:40 +00:00
print z.__repr__() #: byte('A')
2021-09-27 18:02:44 +00:00
print z.__bool__() #: True
print z.__eq__(byte(5)) #: False
print z.__ne__(byte(5)) #: True
print z.__lt__(byte(5)) #: False
print z.__gt__(byte(5)) #: True
print z.__le__(byte(5)) #: False
print z.__ge__(byte(5)) #: True
#%% array,barebones
a = Array[float](5)
pa = Ptr[float](3)
z = Array[float](pa, 3)
z.__copy__()
print z.__len__() #: 3
print z.__bool__() #: True
z.__setitem__(0, 1.0)
z.__setitem__(1, 2.0)
z.__setitem__(2, 3.0)
print z.__getitem__(1) #: 2
print z.slice(0, 2).len #: 2
#%% optional,barebones
a = Optional[float]()
print bool(a) #: False
a = Optional[float](0.0)
print bool(a) #: False
2021-09-27 18:02:44 +00:00
a = Optional[float](5.5)
print a.__bool__(), a.__val__() #: True 5.5
2021-09-27 18:02:44 +00:00
#%% generator,barebones
def foo():
yield 1
yield 2
yield 3
z = foo()
y = z.__iter__()
print str(y.__raw__())[:2] #: 0x
print y.__done__() #: False
print y.__promise__()[0] #: 0
y.__resume__()
2021-10-12 20:16:24 +00:00
print y.__repr__()[:16] #: <generator at 0x
2021-09-27 18:02:44 +00:00
print y.next() #: 1
print y.done() #: False
y.send(1)
y.destroy()
print y.done() #: True