2015-04-22 18:18:28 -04:00
|
|
|
# test syntax and type errors specific to viper code generation
|
2015-03-25 19:10:09 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
def test(code):
|
2015-03-25 19:10:09 -04:00
|
|
|
try:
|
|
|
|
exec(code)
|
2017-03-14 07:52:50 -04:00
|
|
|
except (SyntaxError, ViperTypeError, NotImplementedError) as e:
|
2015-04-22 18:18:28 -04:00
|
|
|
print(repr(e))
|
2015-03-25 19:10:09 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2015-03-25 19:10:09 -04:00
|
|
|
# viper: annotations must be identifiers
|
2015-04-22 18:18:28 -04:00
|
|
|
test("@micropython.viper\ndef f(a:1): pass")
|
|
|
|
test("@micropython.viper\ndef f() -> 1: pass")
|
|
|
|
|
2017-03-14 07:52:50 -04:00
|
|
|
# unknown type
|
|
|
|
test("@micropython.viper\ndef f(x:unknown_type): pass")
|
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
# local used before type known
|
2020-03-22 22:26:08 -04:00
|
|
|
test(
|
|
|
|
"""
|
2015-04-22 18:18:28 -04:00
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
print(x)
|
|
|
|
x = 1
|
2020-03-22 22:26:08 -04:00
|
|
|
"""
|
|
|
|
)
|
2015-04-22 18:18:28 -04:00
|
|
|
|
|
|
|
# type mismatch storing to local
|
2020-03-22 22:26:08 -04:00
|
|
|
test(
|
|
|
|
"""
|
2015-04-22 18:18:28 -04:00
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
x = 1
|
|
|
|
y = []
|
|
|
|
x = y
|
2020-03-22 22:26:08 -04:00
|
|
|
"""
|
|
|
|
)
|
2015-04-22 18:18:28 -04:00
|
|
|
|
|
|
|
# can't implicitly convert type to bool
|
2020-03-22 22:26:08 -04:00
|
|
|
test(
|
|
|
|
"""
|
2015-04-22 18:18:28 -04:00
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
x = ptr(0)
|
|
|
|
if x:
|
|
|
|
pass
|
2020-03-22 22:26:08 -04:00
|
|
|
"""
|
|
|
|
)
|
2015-04-22 18:18:28 -04:00
|
|
|
|
|
|
|
# incorrect return type
|
|
|
|
test("@micropython.viper\ndef f() -> int: return []")
|
|
|
|
|
|
|
|
# can't do binary op between incompatible types
|
|
|
|
test("@micropython.viper\ndef f(): 1 + []")
|
2020-06-26 04:26:39 -04:00
|
|
|
test("@micropython.viper\ndef f(x:int, y:uint): x < y")
|
2015-04-22 18:18:28 -04:00
|
|
|
|
|
|
|
# can't load
|
|
|
|
test("@micropython.viper\ndef f(): 1[0]")
|
|
|
|
test("@micropython.viper\ndef f(): 1[x]")
|
|
|
|
|
|
|
|
# can't store
|
|
|
|
test("@micropython.viper\ndef f(): 1[0] = 1")
|
|
|
|
test("@micropython.viper\ndef f(): 1[x] = 1")
|
2017-03-14 07:52:50 -04:00
|
|
|
test("@micropython.viper\ndef f(x:int): x[0] = x")
|
|
|
|
test("@micropython.viper\ndef f(x:ptr32): x[0] = None")
|
|
|
|
test("@micropython.viper\ndef f(x:ptr32): x[x] = None")
|
2015-04-22 18:18:28 -04:00
|
|
|
|
|
|
|
# must raise an object
|
|
|
|
test("@micropython.viper\ndef f(): raise 1")
|
2015-10-08 08:08:59 -04:00
|
|
|
|
|
|
|
# unary ops not implemented
|
|
|
|
test("@micropython.viper\ndef f(x:int): +x")
|
|
|
|
test("@micropython.viper\ndef f(x:int): -x")
|
|
|
|
test("@micropython.viper\ndef f(x:int): ~x")
|
2017-03-14 07:52:50 -04:00
|
|
|
|
|
|
|
# binary op not implemented
|
2020-06-26 04:26:39 -04:00
|
|
|
test("@micropython.viper\ndef f(x:uint, y:uint): res = x // y")
|
|
|
|
test("@micropython.viper\ndef f(x:uint, y:uint): res = x % y")
|
2017-03-14 07:52:50 -04:00
|
|
|
test("@micropython.viper\ndef f(x:int): res = x in x")
|
|
|
|
|
|
|
|
# yield (from) not implemented
|
|
|
|
test("@micropython.viper\ndef f(): yield")
|
|
|
|
test("@micropython.viper\ndef f(): yield from f")
|
|
|
|
|
|
|
|
# passing a ptr to a Python function not implemented
|
|
|
|
test("@micropython.viper\ndef f(): print(ptr(1))")
|
|
|
|
|
|
|
|
# cast of a casting identifier not implemented
|
|
|
|
test("@micropython.viper\ndef f(): int(int)")
|