PythoC: A brand new strategy to generate C code from Python

Learn extra at:


from pythoc import compile, i32, ptr, i8
from pythoc.libc.stdio import printf

@compile
def add(x: i32, y: i32) -> i32:
    return x + y

@compile
def essential(argc: i32, argv: ptr[ptr[i8]]) -> i32:
    printf("%un", add(10, 20))

if __name__ == "__main__":
    from pythoc import compile_to_executable
    compile_to_executable()

The very first thing you’ll in all probability discover is the block on the backside. The compile_to_executable() perform is strictly what it feels like. Name it, and the present module is compiled to an executable of the identical title, with all of the @compile-decorated features included.

One other distinction is that the essential() perform now has the identical signature because the essential() perform in a C program. This implies the compiled executable will robotically use that as its entry level.

Lastly, while you run this program, the generated executable (which reveals up in a construct subdirectory) doesn’t run robotically; you must run it your self. The purpose right here is to construct a standalone C program, indistinguishable from one you wrote by hand in C, however utilizing Python’s syntax.

Leave a reply

Please enter your comment!
Please enter your name here