1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
import micropython
# viper function taking and returning ints
@micropython.viper
def viper_int(x: int, y: int) -> int:
return x + y + 3
print(viper_int(1, 2))
# viper function taking and returning objects
@micropython.viper
def viper_object(x: object, y: object) -> object:
return x + y
print(viper_object(1, 2))
# return None as non-object (should return 0)
@micropython.viper
def viper_ret_none() -> int:
return None
print(viper_ret_none())
# return Ellipsis as object
@micropython.viper
def viper_ret_ellipsis() -> object:
return ...
print(viper_ret_ellipsis())
# 3 args
@micropython.viper
def viper_3args(a: int, b: int, c: int) -> int:
return a + b + c
print(viper_3args(1, 2, 3))
# 4 args
@micropython.viper
def viper_4args(a: int, b: int, c: int, d: int) -> int:
return a + b + c + d
# viper call with 4 args not yet supported
# print(viper_4args(1, 2, 3, 4))
# a local (should have automatic type int)
@micropython.viper
def viper_local(x: int) -> int:
y = 4
return x + y
print(viper_local(3))
# without type annotation, types should default to object
@micropython.viper
def viper_no_annotation(x, y):
return x * y
print(viper_no_annotation(4, 5))
# a for loop
@micropython.viper
def viper_for(a: int, b: int) -> int:
total = 0
for x in range(a, b):
total += x
return total
print(viper_for(10, 10000))
# accessing a global
@micropython.viper
def viper_access_global():
global gl
gl = 1
return gl
print(viper_access_global(), gl)
# calling print with object and int types
@micropython.viper
def viper_print(x, y: int):
print(x, y + 1)
viper_print(1, 2)
# convert constants to objects in tuple
@micropython.viper
def viper_tuple_consts(x):
return (x, 1, False, True)
print(viper_tuple_consts(0))
# making a tuple from an object and an int
@micropython.viper
def viper_tuple(x, y: int):
return (x, y + 1)
print(viper_tuple(1, 2))
# making a list from an object and an int
@micropython.viper
def viper_list(x, y: int):
return [x, y + 1]
print(viper_list(1, 2))
# making a set from an object and an int
@micropython.viper
def viper_set(x, y: int):
return {x, y + 1}
print(sorted(list(viper_set(1, 2))))
# raising an exception
@micropython.viper
def viper_raise(x: int):
raise OSError(x)
try:
viper_raise(1)
except OSError as e:
print(repr(e))
# calling GC after defining the function
@micropython.viper
def viper_gc() -> int:
return 1
print(viper_gc())
import gc
gc.collect()
print(viper_gc())
|