Page 1 of 1

The viper code emitter

Posted: Sun Jul 12, 2015 8:24 am
by pythoncoder
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.

Re: The viper code emitter

Posted: Sun Jul 12, 2015 10:32 am
by Turbinenreiter

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.

Re: The viper code emitter

Posted: Sun Jul 12, 2015 5:27 pm
by pythoncoder
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.

Re: The viper code emitter

Posted: Mon Nov 12, 2018 12:29 am
by johncblacker
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?

Re: The viper code emitter

Posted: Mon Nov 12, 2018 9:31 pm
by danhalbert
For CircuitPython issues, ask us in https://forums.adafruit.com/viewforum.php?f=60