PythoC bridges Python's ease of use with the performance of C by compiling statically-typed Python code into LLVM IR. Offering C-equivalent runtime capabilities along with advanced compile-time metaprogramming features, this innovative DSL empowers developers to write safe, fast code while maintaining Python's familiar syntax.
PythoC: Write C in Python
PythoC is an innovative Python Domain-Specific Language (DSL) compiler that translates statically-typed Python code into LLVM Intermediate Representation (IR). This unique approach provides the runtime capabilities equivalent to C while preserving the familiar syntax of Python, making it easier for developers to leverage the power of both languages.
This simple example illustrates how to create and compile a basic addition function.
from pythoc import compile, i32
@compile
def add(x: i32, y: i32) -> i32:
return x + y
# Compile to native code
@compile
def main() -> i32:
return add(10, 20)
# Call the compiled dynamic library from Python directly
result = main()
PythoC stands out with its ability to map directly to C constructs effectively, as shown in the implementation of a binary tree. The following code creates a tree structure and functions to manipulate it:
from pythoc import i32, ptr, compile, nullptr, seq, sizeof
from pythoc.libc.stdlib import malloc, free
@compile
class TreeNode:
left: ptr[TreeNode]
right: ptr[TreeNode]
@compile
def NewTreeNode(left: ptr[TreeNode], right: ptr[TreeNode]) -> ptr[TreeNode]:
new: ptr[TreeNode] = ptr[TreeNode](https://github.com/1flei/PythoC/blob/master/malloc(sizeof(TreeNode)))
new.left = left
new.right = right
return new
# Additional methods for tree operations can be implemented similarly.
Poly library, allowing for a flexible programming structure.For further details and examples, explore the full suite of test cases and documentation located in the test/ directory and the documentation folder. Embrace the power of PythoC to optimize your coding experience by combining the simplicity of Python with the performance of C.
No comments yet.
Sign in to be the first to comment.