HomeCloud ComputingPythoC: A brand new option to generate C code from Python

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




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 foremost(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 most likely discover is the block on the backside. The compile_to_executable() operate is strictly what it appears like. Name it, and the present module is compiled to an executable of the identical identify, with all of the @compile-decorated capabilities included.

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

Lastly, whenever you run this program, the generated executable (which reveals up in a construct subdirectory) doesn’t run mechanically; you need to 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments