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
|
|
|
|
2015-04-22 18:18:28 -04:00
|
|
|
def test(code):
|
2015-03-25 19:10:09 -04:00
|
|
|
try:
|
|
|
|
exec(code)
|
2015-04-22 18:18:28 -04:00
|
|
|
except (SyntaxError, ViperTypeError) as e:
|
|
|
|
print(repr(e))
|
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")
|
|
|
|
|
|
|
|
# local used before type known
|
|
|
|
test("""
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
print(x)
|
|
|
|
x = 1
|
|
|
|
""")
|
|
|
|
|
|
|
|
# type mismatch storing to local
|
|
|
|
test("""
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
x = 1
|
|
|
|
y = []
|
|
|
|
x = y
|
|
|
|
""")
|
|
|
|
|
|
|
|
# can't implicitly convert type to bool
|
|
|
|
test("""
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
x = ptr(0)
|
|
|
|
if x:
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
|
|
|
|
# incorrect return type
|
|
|
|
test("@micropython.viper\ndef f() -> int: return []")
|
|
|
|
|
|
|
|
# can't do binary op between incompatible types
|
|
|
|
test("@micropython.viper\ndef f(): 1 + []")
|
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
|
|
|
# must raise an object
|
|
|
|
test("@micropython.viper\ndef f(): raise 1")
|