Skip to main content

cpile ⚡

PyPI version Python versions License: MIT

cpile transpiles type-annotated Python code into clean, readable C99 that compiles with GCC for native performance.

Write Python with standard type hints, get human-readable C code, and compile to native binaries without touching boilerplate C headers or manual memory setups.


Installation

pip install cpile

Requirements: Python >= 3.13, GCC (for native compilation).


Command Line Interface (CLI)

cpile comes with a command-line tool to transpile and build directly from your terminal:

# 1. Transpile Python to C and print to terminal
cpile transpile script.py

# 2. Transpile and save directly to a .c file
cpile transpile script.py -o output.c

# 3. Transpile and compile directly into an executable binary using GCC
cpile build script.py -o myapp
./myapp

Quickstart (Python API)

You can also use cpile directly inside your Python projects:

import cpile

code = """
def fibonacci(n: int) -> int:
    if n <= 1:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)
"""

# Transpile Python source string to C
c_code = cpile.transpile(code)
print(c_code)

Generated C Output:

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int32_t fibonacci (int32_t n) {
  if (n <= 1) {
    return n;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
}

Supported Features

1. Type System & Mapping

Standard Python type hints map directly to fixed-width C types:

Python Type C99 Equivalent
int int32_t (<stdint.h>)
float double
str char*
bool bool (<stdbool.h>)
None void

2. Classes as C Structs

Define custom data types using Python classes with annotations:

class Vector:
    x: int
    y: int

v: Vector = Vector(10, 20)
v.x = 30

Transpiles to:

typedef struct {
  int32_t x;
  int32_t y;
} Vector;

Vector v = (Vector){10, 20};
v.x = 30;

3. Arrays & 2D Arrays

Supports both 1D and 2D arrays with fixed size detection:

matrix: list[list[int]] = [[1, 2], [3, 4]]

Transpiles to:

int32_t matrix[2][2] = {{1, 2}, {3, 4}};

4. Control Flow & Loops

  • if, elif, and else blocks with proper C indentation and scoping.
  • while loops and for loops using range(stop), range(start, stop), and range(start, stop, step).
  • break and continue statement support.

5. Strings & Comparison

Equality checks on strings automatically generate clean strcmp calls:

def is_admin(user: str) -> bool:
    if user == "root":
        return True
    return False

Transpiles to:

bool is_admin (char* user) {
  if (strcmp(user, "root") == 0) {
    return true;
  }
  return false;
}

6. Math Functions & Built-ins

Supports standard mathematical functions (sqrt, pow, abs, fabs, floor, ceil) from <math.h> automatically.

7. Ternary & Compound Operators

  • Augmented assignments: +=, -=, *=, /=
  • One-line ternary expressions: max_val: int = a if a > b else b → int32_t max_val = a > b ? a : b;

Project Structure

cpile/
├── src/
│   └── cpile/
│       ├── __init__.py      # Package export (transpile, transpile_file, compile_c)
│       ├── analyzer.py      # AST visitor for type analysis and symbol table
│       ├── cli.py           # Command line interface (cpile transpile / build)
│       ├── codegen.py       # AST-to-C code generator
│       ├── errors.py        # Custom transpilation exception classes
│       ├── transpiler.py    # Public API & GCC compilation integration
│       └── types.py         # Python-to-C type mappings and type inference
├── test/
│   ├── examples/            # End-to-end Python test examples
│   │   ├── arrays.py        # 1D array operations
│   │   ├── bubble_sort.py   # In-place sorting algorithm
│   │   ├── conditions.py    # Branching & logic operators
│   │   ├── fibonacci.py     # Recursion benchmark
│   │   ├── loops.py         # While & for range iterations
│   │   ├── math_ops.py      # Math functions (<math.h>)
│   │   ├── strings.py       # String operations & comparisons
│   │   ├── structs.py       # Class-to-struct transpilation
│   │   └── swap.py          # Value swaps
│   ├── test_all_examples.py # End-to-end transpile + GCC compile test runner
│   ├── test_analyze.py      # Analyzer & symbol table unit tests
│   ├── test_array.py        # Array code generation tests
│   ├── test_codegen.py      # Core generator tests
│   ├── test_str_comp.py     # String comparison tests
│   ├── test_struct.py       # Struct definition tests
│   └── test_type.py         # Type mapping & inference tests
├── pyproject.toml           # Packaging metadata & entry points
└── README.md

Running Verification Tests

Run the full end-to-end test suite to transpile all 9 examples and compile each with GCC:

python3 test/test_all_examples.py

License

MIT License. See LICENSE for details.

Metadata

Release files for cpile 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cpile 0.2.1
File Size Uploaded Provenance Actions
cpile-0.2.1.tar.gz 11.5 kB Without provenance Details for cpile-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cpile 0.2.1
File Interpreter ABI Platform Actions
cpile-0.2.1-py3-none-any.whl Python 3 none any Details for cpile-0.2.1-py3-none-any.whl

Total release size: 23.6 kB

Release files / cpile-0.2.1.tar.gz

Download URL cpile-0.2.1.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f3cc4f6af663bcd47df2fb15c3d711333b32ff86e25bd0be2b94763b60f15181
BLAKE2b-256 checksum
How to use checksums
5112a34e33d6be2030cc0602f87d85c2b411f8cabcff5baf1bc02bea8aebd6a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.2 CPython/3.14.6 Darwin/27.0.0

Release files / cpile-0.2.1-py3-none-any.whl

Download URL cpile-0.2.1-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc5b0c4334da551809c74f1633663bc924ef6dd0c3e428651e3154ccec70ecdb
BLAKE2b-256 checksum
How to use checksums
27d090d2dd0baf7844bfb6ddcbc2cf5e8ed55639a93d007cbf38c05311989ff1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.2 CPython/3.14.6 Darwin/27.0.0

Security for cpile 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page