The viper code emitter

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
pythoncoder
Posts: 3137
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

The viper code emitter

Post by pythoncoder » Sun Jul 12, 2015 8:24 am

What is the development status of this? Can it be considered stable? Are the rules for writing viper code documented? I have some code which runs normally and with the native emitter but I'm struggling with compile time errors under viper.

Firstly functions seem to be able to return strings or None, but if I try to return an integer I get "ViperTypeError: return expected 'object' but got 'int'". Secondly, to avoid compile time errors it seems necessary to cast function arguments to integers to perform bitwise operations on them (even though at runtime they would be passed as integers). This example yields at compile time "ViperTypeError: can't do binary op between 'object' and 'int'" .:

Code: Select all

@micropython.viper
def foo(num):
    num <<= 1
    return "4" if num == 4 else "not four"
Lastly viper compile time errors don't provide a line number which makes finding them rather challenging.
Peter Hinch

Turbinenreiter
Posts: 274
Joined: Sun May 04, 2014 8:54 am

Re: The viper code emitter

Post by Turbinenreiter » Sun Jul 12, 2015 10:32 am

Code: Select all

>>> @micropython.viper
... def foo(num: int) -> int:
...     num <<= 1
...     return 4 if num == 4 else 0
... 
>>> foo(1)
0
>>> foo(2)
4
>>> foo(3)
0
This worked for me - Viper likes Type Hints.

User avatar
pythoncoder
Posts: 3137
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: The viper code emitter

Post by pythoncoder » Sun Jul 12, 2015 5:27 pm

Thanks for that - it fixes one of my problems! One thing I have discovered: viper produces erroneous results if a function's declaration has an argument which is passed to the function but which isn't used in the function code. I'm not sure if this qualifies as a bug, but it did result in a method which worked undecorated or with the native emitter but produced incorrect results in viper.

EDIT
I now have an example where an unused argument crashes the Pyboard and have submitted an issue.
Peter Hinch

johncblacker
Posts: 2
Joined: Sun Nov 11, 2018 8:11 pm

Re: The viper code emitter

Post by johncblacker » Mon Nov 12, 2018 12:29 am

I tried rebuilding circuitpython with the setting changed to enable the viper emitter and when I used a decorator, the board flashed it's error led at me pointing to the line with the decorator on it! Once I took it off, the program took off again. How can I find out what's wrong and fix it?

danhalbert
Posts: 7
Joined: Mon Jan 16, 2017 3:58 am

Re: The viper code emitter

Post by danhalbert » Mon Nov 12, 2018 9:31 pm

For CircuitPython issues, ask us in https://forums.adafruit.com/viewforum.php?f=60

Post Reply