mirror of https://github.com/exaloop/codon.git
967 lines
19 KiB
Python
967 lines
19 KiB
Python
#%% pass,barebones
|
|
pass
|
|
|
|
#%% continue_error,barebones
|
|
continue #! continue outside of a loop
|
|
|
|
#%% break_error,barebones
|
|
break #! break outside of a loop
|
|
|
|
#%% assign,barebones
|
|
a = 1
|
|
print a #: 1
|
|
a = 2
|
|
print a #: 2
|
|
|
|
x, y = 1, 2
|
|
print x, y #: 1 2
|
|
(x, y) = (3, 4)
|
|
print x, y #: 3 4
|
|
x, y = (1, 2)
|
|
print x, y #: 1 2
|
|
(x, y) = 3, 4
|
|
print x, y #: 3 4
|
|
(x, y) = [3, 4]
|
|
print x, y #: 3 4
|
|
[x, y] = [1, 2]
|
|
print x, y #: 1 2
|
|
[x, y] = (4, 3)
|
|
print x, y #: 4 3
|
|
|
|
l = list(iter(range(10)))
|
|
[a, b, *lx, c, d] = l
|
|
print a, b, lx, c, d #: 0 1 [2, 3, 4, 5, 6, 7] 8 9
|
|
a, b, *lx = l
|
|
print a, b, lx #: 0 1 [2, 3, 4, 5, 6, 7, 8, 9]
|
|
*lx, a, b = l
|
|
print lx, a, b #: [0, 1, 2, 3, 4, 5, 6, 7] 8 9
|
|
*xz, a, b = (1, 2, 3, 4, 5)
|
|
print xz, a, b #: (1, 2, 3) 4 5
|
|
(*ex,) = [1, 2, 3]
|
|
print ex #: [1, 2, 3]
|
|
|
|
#%% assign_str,barebones
|
|
sa, sb = 'XY'
|
|
print sa, sb #: X Y
|
|
(sa, sb), sc = 'XY', 'Z'
|
|
print sa, sb, sc #: X Y Z
|
|
sa, *la = 'X'
|
|
print sa, la, 1 #: X 1
|
|
sa, *la = 'XYZ'
|
|
print sa, la #: X YZ
|
|
(xa,xb), *xc, xd = [1,2],'this'
|
|
print xa, xb, xc, xd #: 1 2 () this
|
|
(a, b), (sc, *sl) = [1,2], 'this'
|
|
print a, b, sc, sl #: 1 2 t his
|
|
|
|
#%% assign_index_dot,barebones
|
|
class Foo:
|
|
a: int
|
|
def __setitem__(self, i: int, t: int):
|
|
self.a += i * t
|
|
f = Foo()
|
|
f.a = 5
|
|
print f.a #: 5
|
|
f[3] = 5
|
|
print f.a #: 20
|
|
f[1] = -8
|
|
print f.a #: 12
|
|
|
|
#%% assign_err_1,barebones
|
|
a, *b, c, *d = 1,2,3,4,5 #! multiple unpack expressions
|
|
|
|
#%% assign_err_2,barebones
|
|
a = [1, 2, 3]
|
|
a[1]: int = 3 #! syntax error
|
|
|
|
#%% assign_err_3,barebones
|
|
a = 5
|
|
a.x: int = 3 #! syntax error
|
|
|
|
#%% assign_err_4,barebones
|
|
*x = range(5) #! invalid assignment
|
|
|
|
#%% assign_err_5,barebones
|
|
try:
|
|
(sa, sb), sc = 'XYZ'
|
|
except IndexError:
|
|
print "assign failed" #: assign failed
|
|
|
|
#%% assign_comprehension,barebones
|
|
g = ((b, a, c) for a, *b, c in ['ABC','DEEEEF','FHGIJ'])
|
|
x, *q, y = list(g) # TODO: auto-unroll as in Python
|
|
print x, y, q #: ('B', 'A', 'C') ('HGI', 'F', 'J') [('EEEE', 'D', 'F')]
|
|
|
|
#%% assign_shadow,barebones
|
|
a = 5
|
|
print a #: 5
|
|
a : str = 's'
|
|
print a #: s
|
|
|
|
#%% assign_err_must_exist,barebones
|
|
a = 1
|
|
def foo():
|
|
a += 2 #! variable 'a' is not global
|
|
|
|
#%% assign_rename,barebones
|
|
y = int
|
|
z = y(5)
|
|
print z #: 5
|
|
|
|
def foo(x): return x + 1
|
|
x = foo
|
|
print x(1) #: 2
|
|
|
|
#%% assign_err_6,barebones
|
|
x = bar #! identifier 'bar' not found
|
|
|
|
#%% assign_err_7,barebones
|
|
foo() += bar #! invalid assignment
|
|
|
|
#%% assign_update_eq,barebones
|
|
a = 5
|
|
a += 3
|
|
print a #: 8
|
|
a -= 1
|
|
print a #: 7
|
|
|
|
class Foo:
|
|
a: int
|
|
def __add__(self, i: int):
|
|
print 'add!'
|
|
return Foo(self.a + i)
|
|
def __iadd__(self, i: int):
|
|
print 'iadd!'
|
|
self.a += i
|
|
return self
|
|
def __str__(self):
|
|
return str(self.a)
|
|
f = Foo(3)
|
|
print f + 2 #: add!
|
|
#: 5
|
|
f += 6 #: iadd!
|
|
print f #: 9
|
|
|
|
#%% del,barebones
|
|
a = 5
|
|
del a
|
|
print a #! identifier 'a' not found
|
|
|
|
#%% del_index,barebones
|
|
y = [1, 2]
|
|
del y[0]
|
|
print y #: [2]
|
|
|
|
#%% del_error,barebones
|
|
a = [1]
|
|
del a.ptr #! invalid del statement
|
|
|
|
#%% assert,barebones
|
|
assert True
|
|
assert True, "blah"
|
|
|
|
try:
|
|
assert False
|
|
except AssertionError as e:
|
|
print e.message[:15], e.message[-24:] #: Assert failed ( simplify_stmt.codon:164)
|
|
|
|
try:
|
|
assert False, f"hehe {1}"
|
|
except AssertionError as e:
|
|
print e.message[:23], e.message[-24:] #: Assert failed: hehe 1 ( simplify_stmt.codon:169)
|
|
|
|
#%% print,barebones
|
|
print 1,
|
|
print 1, 2 #: 1 1 2
|
|
|
|
print 1, 2 #: 1 2
|
|
print(3, "4", sep="-", end=" !\n") #: 3-4 !
|
|
|
|
print(1, 2) #: 1 2
|
|
print (1, 2) #: (1, 2)
|
|
|
|
def foo(i, j):
|
|
return i + j
|
|
print 3 |> foo(1) #: 4
|
|
|
|
#%% return_fail,barebones
|
|
return #! expected function body
|
|
|
|
#%% yield_fail,barebones
|
|
yield 5 #! expected function body
|
|
|
|
#%% yield_fail_2,barebones
|
|
(yield) #! expected function body
|
|
|
|
#%% while_else,barebones
|
|
a = 1
|
|
while a:
|
|
print a #: 1
|
|
a -= 1
|
|
else:
|
|
print 'else' #: else
|
|
a = 1
|
|
while a:
|
|
print a #: 1
|
|
a -= 1
|
|
else not break:
|
|
print 'else' #: else
|
|
while True:
|
|
print 'infinite' #: infinite
|
|
break
|
|
else:
|
|
print 'nope'
|
|
|
|
#%% for_assignment,barebones
|
|
l = [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11]]
|
|
for a, *m, b in l:
|
|
print a + b, len(m)
|
|
#: 5 2
|
|
#: 14 3
|
|
#: 21 0
|
|
|
|
#%% for_else,barebones
|
|
for i in [1]:
|
|
print i #: 1
|
|
else:
|
|
print 'else' #: else
|
|
for i in [1]:
|
|
print i #: 1
|
|
else not break:
|
|
print 'else' #: else
|
|
for i in [1]:
|
|
print i #: 1
|
|
break
|
|
else:
|
|
print 'nope'
|
|
|
|
#%% match
|
|
def foo(x):
|
|
match x:
|
|
case 1:
|
|
print 'int'
|
|
case 2 ... 10:
|
|
print 'range'
|
|
case 'ACGT':
|
|
print 'string'
|
|
case (a, 1):
|
|
print 'tuple_wild', a
|
|
case []:
|
|
print 'list'
|
|
case [[]]:
|
|
print 'list list'
|
|
case [1, 2]:
|
|
print 'list 2'
|
|
case [1, z, ...] if z < 5:
|
|
print 'list 3', z
|
|
case [1, _, ..., zz] | (1, zz):
|
|
print 'list 4', zz
|
|
case (1 ... 10, s := ('ACGT', 1 ... 4)):
|
|
print 'complex', s
|
|
case _:
|
|
print 'else'
|
|
foo(1) #: int
|
|
foo(5) #: range
|
|
foo('ACGT') #: string
|
|
foo((9, 1)) #: tuple_wild 9
|
|
foo(List[int]()) #: list
|
|
foo([List[int]()]) #: list list
|
|
foo([1, 2]) #: list 2
|
|
foo([1, 3]) #: list 3 3
|
|
foo([1, 5]) #: else
|
|
foo([1, 5, 10]) #: list 4 10
|
|
foo((1, 33)) #: list 4 33
|
|
foo((9, ('ACGT', 3))) #: complex ('ACGT', 3)
|
|
foo(range(10)) #: else
|
|
|
|
#%% match_err_1,barebones
|
|
match [1, 2]:
|
|
case [1, ..., 2, ..., 3]: pass
|
|
#! cannot have multiple ranges in a pattern
|
|
|
|
#%% match_err_2,barebones
|
|
match [1, 2]:
|
|
case 1 | ...: pass
|
|
#! unexpected ellipsis expression
|
|
|
|
#%% global,barebones
|
|
a = 1
|
|
def foo():
|
|
global a
|
|
a += 1
|
|
print a,
|
|
foo()
|
|
print a #: 1 2
|
|
|
|
#%% global_err,barebones
|
|
a = 1
|
|
global a #! global outside of a function
|
|
|
|
#%% global_err_2,barebones
|
|
def foo():
|
|
global b #! identifier 'b' not found
|
|
|
|
#%% global_err_3,barebones
|
|
def foo():
|
|
b = 1
|
|
def bar():
|
|
global b #! not a top-level variable
|
|
|
|
#%% global_err_4,barebones
|
|
a = 1
|
|
def foo():
|
|
a += 1
|
|
foo() #! variable 'a' is not global
|
|
|
|
#%% global_ref,barebones
|
|
a = [1]
|
|
def foo():
|
|
a.append(2)
|
|
foo()
|
|
print a #: [1, 2]
|
|
|
|
#%% yield_from,barebones
|
|
def foo():
|
|
yield from range(3)
|
|
yield from range(10, 13)
|
|
yield -1
|
|
print list(foo()) #: [0, 1, 2, 10, 11, 12, -1]
|
|
|
|
#%% with,barebones
|
|
class Foo:
|
|
i: int
|
|
def __enter__(self: Foo):
|
|
print '> foo! ' + str(self.i)
|
|
def __exit__(self: Foo):
|
|
print '< foo! ' + str(self.i)
|
|
def foo(self: Foo):
|
|
print 'woof'
|
|
class Bar:
|
|
s: str
|
|
def __enter__(self: Bar):
|
|
print '> bar! ' + self.s
|
|
def __exit__(self: Bar):
|
|
print '< bar! ' + self.s
|
|
def bar(self: Bar):
|
|
print 'meow'
|
|
with Foo(0) as f:
|
|
#: > foo! 0
|
|
f.foo() #: woof
|
|
#: < foo! 0
|
|
with Foo(1) as f, Bar('s') as b:
|
|
#: > foo! 1
|
|
#: > bar! s
|
|
f.foo() #: woof
|
|
b.bar() #: meow
|
|
#: < bar! s
|
|
#: < foo! 1
|
|
with Foo(2), Bar('t') as q:
|
|
#: > foo! 2
|
|
#: > bar! t
|
|
print 'eeh' #: eeh
|
|
q.bar() #: meow
|
|
#: < bar! t
|
|
#: < foo! 2
|
|
|
|
#%% import_c,barebones
|
|
from C import sqrt(float) -> float
|
|
print sqrt(4.0) #: 2
|
|
|
|
from C import puts(cobj)
|
|
puts("hello".ptr) #: hello
|
|
|
|
from C import atoi(cobj) -> int as s2i
|
|
print s2i("11".ptr) #: 11
|
|
|
|
@C
|
|
def log(x: float) -> float:
|
|
pass
|
|
print log(5.5) #: 1.70475
|
|
|
|
#%% import_c_dylib,barebones
|
|
from internal.dlopen import dlext
|
|
RT = "./libcodonrt." + dlext()
|
|
from C import RT.seq_str_int(int) -> str as sp
|
|
print sp(65) #: 65
|
|
|
|
#%% import_c_dylib_error,barebones
|
|
from C import "".seq_print(str) as sp
|
|
sp("hi!") #! syntax error
|
|
|
|
#%% import,barebones
|
|
zoo, _zoo = 1, 1
|
|
print zoo, _zoo, __name__ #: 1 1 __main__
|
|
|
|
import a #: a
|
|
a.foo() #: a.foo
|
|
|
|
from a import foo, bar as b
|
|
foo() #: a.foo
|
|
b() #: a.bar
|
|
|
|
print str(a)[:9], str(a)[-18:] #: <module ' a/__init__.codon'>
|
|
|
|
import a.b
|
|
print a.b.c #: a.b.c
|
|
a.b.har() #: a.b.har a.b.__init__ a.b.c
|
|
|
|
print a.b.A.B.b_foo().__add__(1) #: a.b.A.B.b_foo()
|
|
#: 2
|
|
|
|
print str(a.b)[:9], str(a.b)[-20:] #: <module ' a/b/__init__.codon'>
|
|
|
|
from a.b import *
|
|
har() #: a.b.har a.b.__init__ a.b.c
|
|
a.b.har() #: a.b.har a.b.__init__ a.b.c
|
|
fx() #: a.foo
|
|
|
|
from a import *
|
|
print zoo, _zoo, __name__ #: 5 1 __main__
|
|
|
|
#%% import_order,barebones
|
|
def foo():
|
|
import a
|
|
a.foo()
|
|
def bar():
|
|
import a
|
|
a.bar()
|
|
|
|
bar() #: a
|
|
#: a.bar
|
|
foo() #: a.foo
|
|
|
|
#%% import_class
|
|
import sys
|
|
print str(sys)[:20] #: <module 'sys' from '
|
|
print sys.maxsize #: 9223372036854775807
|
|
|
|
#%% import_rec,barebones
|
|
from a.b.rec1 import bar
|
|
#: import rec1
|
|
#: import rec2
|
|
#: done rec2
|
|
#: rec2.x
|
|
#: done rec1
|
|
bar()
|
|
#: rec1.bar
|
|
|
|
#%% import_rec_err,barebones
|
|
from a.b.rec1_err import bar
|
|
#! symbol 'bar' not found in
|
|
|
|
#%% import_err_1,barebones
|
|
class Foo:
|
|
import bar #! only function and class definitions are allowed within classes
|
|
|
|
#%% import_err_2,barebones
|
|
import "".a.b.c #! syntax error
|
|
|
|
#%% import_err_3,barebones
|
|
from a.b import foo() #! invalid import statement
|
|
|
|
#%% import_err_4,barebones
|
|
from a.b.c import hai.hey #! invalid import statement
|
|
|
|
#%% import_err_4_x,barebones
|
|
import whatever #! cannot locate import 'whatever'
|
|
|
|
#%% import_err_5,barebones
|
|
import a.b
|
|
print a.b.x #! identifier 'x' not found in
|
|
|
|
#%% import_err_6,barebones
|
|
from a.b import whatever #! symbol 'whatever' not found in
|
|
|
|
#%% function_err_0,barebones
|
|
def foo(a, b, a):
|
|
pass #! 'a' declared twice
|
|
|
|
#%% function_err_0b,barebones
|
|
def foo(a, b=1, c):
|
|
pass #! non-default argument 'c' after a default argument
|
|
|
|
#%% function_err_0b_ok,barebones
|
|
def foo(a, b=1, *c):
|
|
pass
|
|
|
|
#%% function_err_0c,barebones
|
|
def foo(a, b=1, *c, *d):
|
|
pass #! invalid *args
|
|
|
|
#%% function_err_0e,barebones
|
|
def foo(a, b=1, *c = 1):
|
|
pass #! invalid *args
|
|
|
|
#%% function_err_0f,barebones
|
|
def foo(a, b=1, **c, **kwargs):
|
|
pass #! invalid **kwargs
|
|
|
|
#%% function_err_0h,barebones
|
|
def foo(a, b=1, **c = 1):
|
|
pass #! invalid **kwargs
|
|
|
|
#%% function_err_0i,barebones
|
|
def foo(a, **c, d):
|
|
pass #! invalid **kwargs
|
|
|
|
#%% function_err_1,barebones
|
|
def foo():
|
|
@__force__
|
|
def bar(): pass #! builtins must be defined at the toplevel
|
|
|
|
#%% function_err_2,barebones
|
|
def f[T: Static[float]]():
|
|
pass
|
|
#! only static integers and strings are supported
|
|
|
|
#%% function_err_3,barebones
|
|
def f(a, b=a):
|
|
pass
|
|
#! identifier 'a' not found
|
|
|
|
#%% function_llvm_err_1,barebones
|
|
@llvm
|
|
def foo():
|
|
blah
|
|
#! LLVM functions must have a return type
|
|
|
|
#%% function_llvm_err_2,barebones
|
|
@llvm
|
|
def foo() -> int:
|
|
a{={=}}
|
|
#! invalid LLVM substitution
|
|
|
|
#%% function_llvm_err_3,barebones
|
|
a = 5
|
|
@llvm
|
|
def foo() -> int:
|
|
a{=a}
|
|
foo()
|
|
#! not a type or static expression
|
|
#! while realizing foo (arguments foo)
|
|
|
|
#%% function_llvm_err_4,barebones
|
|
a = 5
|
|
@llvm
|
|
def foo() -> int:
|
|
a{=a
|
|
#! invalid LLVM substitution
|
|
|
|
#%% function_self,barebones
|
|
class Foo:
|
|
def foo(self):
|
|
return 'F'
|
|
f = Foo()
|
|
print f.foo() #: F
|
|
|
|
#%% function_self_err,barebones
|
|
class Foo:
|
|
def foo(self):
|
|
return 'F'
|
|
Foo.foo(1) #! cannot unify int and Foo
|
|
|
|
#%% function_nested,barebones
|
|
def foo(v):
|
|
value = v
|
|
def bar():
|
|
return value
|
|
return bar
|
|
baz = foo(2)
|
|
print baz() #: 2
|
|
|
|
def f(x):
|
|
a=1
|
|
def g(y):
|
|
return a+y
|
|
return g(x)
|
|
print f(5) #: 6
|
|
|
|
#%% function_err_nested,barebones
|
|
def f[T]():
|
|
def g():
|
|
return T()
|
|
g()
|
|
f(int) #! identifier 'T' not found
|
|
|
|
#%% class_err_1,barebones
|
|
@extend
|
|
@foo
|
|
class Foo:
|
|
pass
|
|
#! extend cannot be combined with other decorators
|
|
|
|
#%% class_err_1b,barebones
|
|
size_t = i32
|
|
@extend
|
|
class size_t:
|
|
pass
|
|
#! cannot extend type alias or an instantiation (size_t)
|
|
|
|
#%% class_err_2,barebones
|
|
def foo():
|
|
@extend
|
|
class Foo:
|
|
pass
|
|
#! extend is only allowed at the toplevel
|
|
|
|
#%% class_nested,barebones
|
|
class Foo:
|
|
foo: int
|
|
class Bar:
|
|
bar: int
|
|
b: Optional[Foo.Bar]
|
|
c: Optional[int]
|
|
class Moo:
|
|
# TODO: allow nested class reference to the upclass
|
|
# x: Foo.Bar
|
|
x: int
|
|
y = Foo(1)
|
|
z = Foo.Bar(2, None, 4)
|
|
m = Foo.Bar.Moo(5)
|
|
print y.foo #: 1
|
|
print z.bar, z.b.__bool__(), z.c, m.x #: 2 False 4 5
|
|
|
|
#%% class_nested_2,barebones
|
|
@tuple
|
|
class Foo:
|
|
@tuple
|
|
class Bar:
|
|
x: int
|
|
x: int
|
|
b: Bar
|
|
c: Foo.Bar
|
|
f = Foo(5, Foo.Bar(6), Foo.Bar(7))
|
|
print(f) #: (x: 5, b: (x: 6), c: (x: 7))
|
|
|
|
#%% class_nested_err,barebones
|
|
class Foo:
|
|
class Bar:
|
|
b: Ptr[Bar]
|
|
#! identifier 'Bar' not found
|
|
|
|
#%% class_err_4,barebones
|
|
@extend
|
|
class Foo:
|
|
pass
|
|
#! cannot find type 'Foo' to extend
|
|
|
|
#%% class_err_5,barebones
|
|
class Foo[T, U]:
|
|
pass
|
|
@extend
|
|
class Foo[T]:
|
|
pass
|
|
#! extensions cannot be generic or declare members
|
|
|
|
#%% class_err_7,barebones
|
|
class Foo:
|
|
a: int
|
|
a: int
|
|
#! 'a' declared twice
|
|
|
|
#%% class_err_tuple_no_recursive,barebones
|
|
@tuple
|
|
class Foo:
|
|
a: Foo
|
|
#! identifier 'Foo' not found
|
|
|
|
#%% class_err_8,barebones
|
|
class Foo:
|
|
while True: pass
|
|
#! only function and class definitions are allowed within classes
|
|
|
|
#%% class_err_9,barebones
|
|
class F[T: Static[float]]:
|
|
pass
|
|
#! only static integers and strings are supported
|
|
|
|
#%% class_err_10,barebones
|
|
def foo[T]():
|
|
class A:
|
|
x: T
|
|
#! identifier 'T' not found (cannot access outer function identifiers)
|
|
|
|
#%% class_err_11,barebones
|
|
def foo(x):
|
|
class A:
|
|
def bar():
|
|
print x
|
|
#! cannot access non-global variable 'x'
|
|
|
|
#%% class_err_12,barebones
|
|
def foo(x):
|
|
T = type(x)
|
|
class A:
|
|
def bar():
|
|
print T()
|
|
#! identifier 'T' not found (cannot access outer function identifiers)
|
|
|
|
#%% recursive_class,barebones
|
|
class Node[T]:
|
|
data: T
|
|
children: List[Node[T]]
|
|
def __init__(self, data: T):
|
|
self.data = data
|
|
self.children = List[Node[T]]()
|
|
print Node(2).data #: 2
|
|
|
|
class Node2:
|
|
data: int
|
|
children: List[Node2]
|
|
def __init__(self, data: int):
|
|
self.data = data
|
|
self.children = List[Node2]()
|
|
print Node2(3).data #: 3
|
|
|
|
#%% class_auto_init,barebones
|
|
class X[T]:
|
|
a: int = 4
|
|
b: int
|
|
c: T
|
|
d: str = 'oops'
|
|
def __str__(self):
|
|
return f'X({self.a},{self.b},{self.c},{self.d})'
|
|
x = X[float]()
|
|
print x #: X(4,0,0,oops)
|
|
y = X(c='darius',a=5)
|
|
print y #: X(5,0,darius,oops)
|
|
|
|
#%% magic,barebones
|
|
@tuple
|
|
class Foo:
|
|
x: int
|
|
y: int
|
|
a, b = Foo(1, 2), Foo(1, 3)
|
|
print a, b #: (x: 1, y: 2) (x: 1, y: 3)
|
|
print a.__len__() #: 2
|
|
print a.__hash__(), b.__hash__() #: 175247769363 175247769360
|
|
print a == a, a == b #: True False
|
|
print a != a, a != b #: False True
|
|
print a < a, a < b, b < a #: False True False
|
|
print a <= a, a <= b, b <= a #: True True False
|
|
print a > a, a > b, b > a #: False False True
|
|
print a >= a, a >= b, b >= a #: True False True
|
|
print a.__getitem__(1) #: 2
|
|
print list(a.__iter__()) #: [1, 2]
|
|
|
|
#%% magic_class,barebones
|
|
@dataclass(eq=True, order=True)
|
|
class Foo:
|
|
x: int
|
|
y: int
|
|
def __str__(self): return f'{self.x}_{self.y}'
|
|
a, b = Foo(1, 2), Foo(1, 3)
|
|
print a, b #: 1_2 1_3
|
|
print a == a, a == b #: True False
|
|
print a != a, a != b #: False True
|
|
print a < a, a < b, b < a #: False True False
|
|
print a <= a, a <= b, b <= a #: True True False
|
|
print a > a, a > b, b > a #: False False True
|
|
print a >= a, a >= b, b >= a #: True False True
|
|
|
|
#%% magic_2,barebones
|
|
@tuple
|
|
class Foo:
|
|
pass
|
|
a, b = Foo(), Foo()
|
|
print a, b #: () ()
|
|
print a.__len__() #: 0
|
|
print a.__hash__(), b.__hash__() #: 0 0
|
|
print a == a, a == b #: True True
|
|
print a != a, a != b #: False False
|
|
print a < a, a < b, b < a #: False False False
|
|
print a <= a, a <= b, b <= a #: True True True
|
|
print a > a, a > b, b > a #: False False False
|
|
print a >= a, a >= b, b >= a #: True True True
|
|
|
|
# TODO: pickle / to_py / from_py
|
|
|
|
#%% magic_contains,barebones
|
|
sponge = (1, 'z', 1.55, 'q', 48556)
|
|
print 1.1 in sponge #: False
|
|
print 'q' in sponge #: True
|
|
print True in sponge #: False
|
|
|
|
bob = (1, 2, 3)
|
|
print 1.1 in sponge #: False
|
|
print 1 in sponge #: True
|
|
print 0 in sponge #: False
|
|
|
|
#%% magic_err_2,barebones
|
|
@tuple
|
|
class Foo:
|
|
pass
|
|
print Foo().__getitem__(1)
|
|
#! expression with void type
|
|
#! while realizing __internal__.tuple_getitem
|
|
#! while realizing Foo.__getitem__
|
|
|
|
#%% magic_empty_tuple,barebones
|
|
@tuple
|
|
class Foo:
|
|
pass
|
|
print list(Foo().__iter__()) #: []
|
|
|
|
#%% magic_err_4,barebones
|
|
@tuple(eq=False)
|
|
class Foo:
|
|
x: int
|
|
Foo(1).__eq__(Foo(1)) #! cannot find '__eq__' in Foo
|
|
|
|
#%% magic_err_5,barebones
|
|
@tuple(pickle=False)
|
|
class Foo:
|
|
x: int
|
|
p = Ptr[byte]()
|
|
Foo(1).__pickle__(p) #! cannot find '__pickle__' in Foo
|
|
|
|
#%% magic_err_6,barebones
|
|
@tuple(container=False)
|
|
class Foo:
|
|
x: int
|
|
Foo(1).__getitem__(0) #! cannot find '__getitem__' in Foo
|
|
|
|
#%% magic_err_7,barebones
|
|
@tuple(python=False)
|
|
class Foo:
|
|
x: int
|
|
p = Ptr[byte]()
|
|
Foo(1).__to_py__(p) #! cannot find '__to_py__' in Foo
|
|
|
|
#%% python
|
|
from python import os
|
|
print os.name #: posix
|
|
|
|
from python import datetime
|
|
z = datetime.datetime.utcfromtimestamp(0)
|
|
print z #: 1970-01-01 00:00:00
|
|
|
|
#%% python_numpy
|
|
from python import numpy as np
|
|
a = np.arange(9).reshape(3, 3)
|
|
print a
|
|
#: [[0 1 2]
|
|
#: [3 4 5]
|
|
#: [6 7 8]]
|
|
print a.dtype.name #: int64
|
|
print np.transpose(a)
|
|
#: [[0 3 6]
|
|
#: [1 4 7]
|
|
#: [2 5 8]]
|
|
n = np.array([[1, 2], [3, 4]])
|
|
print n[0], n[0][0] + 1 #: [1 2] 2
|
|
|
|
#%% python_import_fn
|
|
from python import re.split(str, str) -> List[str] as rs
|
|
print rs(r'\W+', 'Words, words, words.') #: ['Words', 'words', 'words', '']
|
|
|
|
#%% python_import_void
|
|
from python import os.system(str) -> void
|
|
system("echo 'hello!'") #: hello!
|
|
|
|
#%% python_pydef
|
|
@python
|
|
def test_pydef(n) -> str:
|
|
return ''.join(map(str,range(n)))
|
|
print test_pydef(5) #: 01234
|
|
|
|
#%% python_pydef_nested
|
|
def foo():
|
|
@python
|
|
def pyfoo():
|
|
return 1
|
|
print pyfoo() #: 1
|
|
if True:
|
|
@python
|
|
def pyfoo2():
|
|
return 2
|
|
print pyfoo2() #: 2
|
|
pass
|
|
@python
|
|
def pyfoo3():
|
|
if 1:
|
|
return 3
|
|
return str(pyfoo3())
|
|
print foo() #: 3
|
|
|
|
#%% typeof_definition_error,barebones
|
|
a = 1
|
|
class X:
|
|
b: type(a) #! type() not allowed in definitions
|
|
|
|
#%% typeof_definition_error_2,barebones
|
|
def foo(a)->type(a): pass #! type() not allowed in definitions
|
|
|
|
#%% typeof_definition_error_3,barebones
|
|
a=1
|
|
b: type(a) = 1 #! type() not allowed in definitions
|
|
|
|
#%% assign_underscore,barebones
|
|
_ = 5
|
|
_ = 's'
|
|
|
|
#%% inherit_class,barebones
|
|
class defdict[K,V](Dict[K,V]):
|
|
fx: Function[[],V]
|
|
def __init__(self, d: Dict[K,V], fx: Function[[], V]):
|
|
self.__init__()
|
|
for k,v in d.items(): self[k] = v
|
|
self.fx = fx
|
|
def __getitem__(self, key: K) -> V:
|
|
if key in self:
|
|
return self.values[self.keys.index(key)]
|
|
else:
|
|
self[key] = self.fx()
|
|
return self[key]
|
|
z = defdict({'ha':1}, lambda: -1)
|
|
print z
|
|
print z['he']
|
|
print z
|
|
#: {'ha': 1}
|
|
#: -1
|
|
#: {'ha': 1, 'he': -1}
|
|
|
|
class Foo:
|
|
x: int
|
|
def foo(self):
|
|
return f'foo {self.x}'
|
|
class Bar[T]:
|
|
y: T
|
|
def bar(self):
|
|
return f'bar {self.y}/{self.y.__class__}'
|
|
class FooBarBaz[T](Foo, Bar[T]):
|
|
def baz(self):
|
|
return f'baz! {self.foo()} {self.bar()}'
|
|
print FooBarBaz[str]().foo() #: foo 0
|
|
print FooBarBaz[float]().bar() #: bar 0/float
|
|
print FooBarBaz[str]().baz() #: baz! foo 0 bar /str
|
|
|
|
#%% inherit_class_2,barebones
|
|
class defdict(Dict[str,float]):
|
|
def __init__(self, d: Dict[str, float]):
|
|
self.__init__(d.items())
|
|
z = defdict()
|
|
z[1.1] #! cannot unify float and str
|
|
|
|
|
|
#%% inherit_class_err_1,barebones
|
|
class defdict(Array[int]):
|
|
pass #! tuples cannot inherit reference classes (and vice versa)
|
|
|
|
#%% inherit_class_err_2,barebones
|
|
@tuple
|
|
class defdict(int):
|
|
pass #! cannot inherit internal types
|
|
|
|
#%% inherit_class_err_3,barebones
|
|
class defdict(Dict[int, float, float]):
|
|
pass #! wrong number of generics
|
|
|
|
#%% inherit_class_err_4,barebones
|
|
class Foo:
|
|
x: int
|
|
class Bar:
|
|
x: float
|
|
class FooBar(Foo, Bar):
|
|
pass #! 'x' declared twice
|