2014-08-15 11:45:41 -04:00
|
|
|
import micropython
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-15 11:45:41 -04:00
|
|
|
# viper function taking and returning ints
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_int(x: int, y: int) -> int:
|
2014-08-15 11:45:41 -04:00
|
|
|
return x + y + 3
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_int(1, 2))
|
2014-08-15 11:45:41 -04:00
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-15 11:45:41 -04:00
|
|
|
# viper function taking and returning objects
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_object(x: object, y: object) -> object:
|
2014-08-15 11:45:41 -04:00
|
|
|
return x + y
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_object(1, 2))
|
2014-08-15 11:45:41 -04:00
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
# return None as non-object (should return 0)
|
|
|
|
@micropython.viper
|
|
|
|
def viper_ret_none() -> int:
|
|
|
|
return None
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
print(viper_ret_none())
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2015-10-13 13:24:36 -04:00
|
|
|
# return Ellipsis as object
|
|
|
|
@micropython.viper
|
|
|
|
def viper_ret_ellipsis() -> object:
|
|
|
|
return ...
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2015-10-13 13:24:36 -04:00
|
|
|
print(viper_ret_ellipsis())
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
# 3 args
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_3args(a: int, b: int, c: int) -> int:
|
2015-04-22 18:18:28 -04:00
|
|
|
return a + b + c
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
print(viper_3args(1, 2, 3))
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
# 4 args
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_4args(a: int, b: int, c: int, d: int) -> int:
|
2015-04-22 18:18:28 -04:00
|
|
|
return a + b + c + d
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
# viper call with 4 args not yet supported
|
2021-03-15 09:57:36 -04:00
|
|
|
# print(viper_4args(1, 2, 3, 4))
|
2015-04-22 18:18:28 -04:00
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-15 17:39:08 -04:00
|
|
|
# a local (should have automatic type int)
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_local(x: int) -> int:
|
2014-08-15 17:39:08 -04:00
|
|
|
y = 4
|
|
|
|
return x + y
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_local(3))
|
2014-08-15 17:39:08 -04:00
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-15 17:39:08 -04:00
|
|
|
# without type annotation, types should default to object
|
|
|
|
@micropython.viper
|
2014-08-16 17:06:11 -04:00
|
|
|
def viper_no_annotation(x, y):
|
2014-08-15 17:39:08 -04:00
|
|
|
return x * y
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_no_annotation(4, 5))
|
2014-08-15 17:39:08 -04:00
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-15 17:39:08 -04:00
|
|
|
# a for loop
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_for(a: int, b: int) -> int:
|
2014-08-15 17:39:08 -04:00
|
|
|
total = 0
|
|
|
|
for x in range(a, b):
|
|
|
|
total += x
|
|
|
|
return total
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_for(10, 10000))
|
2014-08-15 17:39:08 -04:00
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-15 18:47:59 -04:00
|
|
|
# accessing a global
|
|
|
|
@micropython.viper
|
2014-08-16 17:06:11 -04:00
|
|
|
def viper_access_global():
|
2014-08-15 18:47:59 -04:00
|
|
|
global gl
|
|
|
|
gl = 1
|
|
|
|
return gl
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_access_global(), gl)
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
# calling print with object and int types
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_print(x, y: int):
|
2014-08-16 17:06:11 -04:00
|
|
|
print(x, y + 1)
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
viper_print(1, 2)
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
# convert constants to objects in tuple
|
|
|
|
@micropython.viper
|
|
|
|
def viper_tuple_consts(x):
|
|
|
|
return (x, 1, False, True)
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
print(viper_tuple_consts(0))
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
# making a tuple from an object and an int
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_tuple(x, y: int):
|
2014-08-16 17:06:11 -04:00
|
|
|
return (x, y + 1)
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_tuple(1, 2))
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
# making a list from an object and an int
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_list(x, y: int):
|
2014-08-16 17:06:11 -04:00
|
|
|
return [x, y + 1]
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(viper_list(1, 2))
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
# making a set from an object and an int
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_set(x, y: int):
|
2014-08-16 17:06:11 -04:00
|
|
|
return {x, y + 1}
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
print(sorted(list(viper_set(1, 2))))
|
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
# raising an exception
|
|
|
|
@micropython.viper
|
2021-03-15 09:57:36 -04:00
|
|
|
def viper_raise(x: int):
|
2014-10-22 14:42:55 -04:00
|
|
|
raise OSError(x)
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
try:
|
|
|
|
viper_raise(1)
|
2014-10-22 14:42:55 -04:00
|
|
|
except OSError as e:
|
2014-08-16 17:06:11 -04:00
|
|
|
print(repr(e))
|
2014-08-15 18:47:59 -04:00
|
|
|
|
2023-02-01 03:08:41 -05:00
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
# calling GC after defining the function
|
|
|
|
@micropython.viper
|
|
|
|
def viper_gc() -> int:
|
|
|
|
return 1
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
print(viper_gc())
|
|
|
|
import gc
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
gc.collect()
|
|
|
|
print(viper_gc())
|