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.
Key Features
- C-Compatible Runtime: PythoC compiles code directly into native machine code, ensuring C-level performance and control. It offers full access to low-level operations, such as pointers and manual memory management, without additional runtime overhead.
- Compile-Time Metaprogramming: Harness the full power of Python for code generation, generics, and sophisticated metaprogramming capabilities.
- Zero-Cost Abstractions: Abstractions defined at the Python level are compiled away completely, ensuring that users benefit from high-level programming without sacrificing performance.
- Explicit Typing: PythoC requires all types to be annotated, enforcing clarity similar to C but with Python syntax.
- Optional Safety Features: Implement linear types and refinement types to provide memory safety guarantees without introducing hidden control flows such as implicit exceptions or destructors. This feature is fully optional, minimizing runtime overhead.
- Enhanced Python-C Interoperability: Easily integrate PythoC compiled functions into Python code at runtime, or use Python for compile-time metaprogramming.
Example Usage
Hello World
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()
Binary Tree Example
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.
Additional Features
- Advanced Type System: PythoC supports algebraic data types (ADTs), linear types, and refinement types, enabling developers to enforce type safety at compile time without compromising runtime efficiency.
- Polymorphism: Includes compile-time and runtime polymorphism using a provided
Polylibrary, allowing for a flexible programming structure. - Inline Functions and Closures: Facilitates zero-overhead abstractions through inline functions and supports closures that capture variables effectively for inlining at the call site.
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.