Skip to main content

Arafura - C as Python Transpiler

A transpiler that converts Python syntax with C semantics into C code.

Overview

Arafura allows you to write C programs using Python syntax. The code is syntactically valid Python (passes ast.parse) but has C semantics. The transpiler reads the Python AST and emits C code.

Features

  • Valid Python syntax: All source files are valid Python that can be parsed
  • C semantics: Primitive types, manual memory management, pointers, etc.
  • Local AST translation: No type inference, purely syntactic transformation
  • Full C feature support: Structs, unions, enums, macros, preprocessor directives, goto/labels

Installation

# Install in development mode
pip install -e .

# Or install with dev dependencies
pip install -e ".[dev]"

Usage

# Print C code to stdout
arafura input.py

# Write to output file
arafura input.py -o output.c

# Check syntax without generating output
arafura input.py --check

Example

Input (example.py):

from stdio import *

class Node:
    data: int
    next: -Node

def create_node(value: int) -> -Node:
    node: -Node = malloc(sizeof(Node))
    if node == None:
        return None
    node._.data = value
    node._.next = None
    return node

def main() -> int:
    head: -Node = create_node(42)
    printf("%d\n", head._.data)
    return 0

Output:

#include <stdio.h>

struct Node {
    int data;
    struct Node *next;
};

struct Node *create_node(int value) {
    struct Node *node = malloc(sizeof(struct Node));
    if (node == NULL) {
        return NULL;
    }
    node->data = value;
    node->next = NULL;
    return node;
}

int main(void) {
    struct Node *head = create_node(42);
    printf("%d\n", head->data);
    return 0;
}

Language Reference

Types

  • Basic types: int, char, float, double, long, short, void
  • Pointers: -int for int*, --int for int**
  • Arrays: int[10] for int[10]
  • Pointer-to-array: +int[10] for int (*)[10]
  • Qualifiers: const[int], volatile[int], unsigned[int]
  • Storage class: static[int], extern[int]

Special _ Forms

The underscore is reserved for special operations:

  • Address-of: _.x → &x
  • Dereference: ptr._ → *ptr
  • Pointer member: ptr._.field → ptr->field
  • Increment: i ** _ → i++, _ ** i → ++i
  • Decrement: i // _ → i--, _ // i → --i
  • Compound literals: _(x=1, y=2) → designated initializer

Composite Types

# Struct
class Point:
    x: int
    y: int

# Union
class Data(Union):
    i: int
    f: float

# Enum
class Color(Enum):
    RED = 0
    GREEN = 1
    BLUE = 2

Control Flow

# Regular if/elif/else
if x > 0:
    pass
elif x < 0:
    pass
else:
    pass

# While loop
while x < 10:
    x += 1

# C-style for loop
for i in int(i := 0)(i < 10)(i ** _):
    printf("%d\n", i)

# Do-while loop
while ():
    x ** _
    if x < 10:
        continue

# Goto and labels
LOOP: label
raise LOOP  # goto LOOP

Functions and Macros

# Function (all params and return annotated)
def add(a: int, b: int) -> int:
    return a + b

# Macro (no annotations)
def MAX(a, b):
    a if a > b else b

# Constant macro
PI: macro = 3.14159

Preprocessor

# Includes
import stdio              # #include "stdio.h"
from stdio import *       # #include <stdio.h>

# Conditional compilation
if [DEBUG]:               # #ifdef DEBUG
    printf("debug\n")

Other Features

  • Casts: [int](3.14) → (int)3.14
  • sizeof: sizeof(int) → sizeof(int)
  • NULL: None → NULL
  • Ternary: a if x > 0 else b → (x > 0 ? a : b)
  • Walrus operator: (x := 5) → (x = 5)

Implementation

The transpiler is implemented in Python using the ast module. It:

  1. Parses the input Python code into an AST
  2. Performs a first pass to collect type names (structs, unions, enums)
  3. Walks the AST and emits C code based on local patterns

No type inference is performed - all types must be explicitly annotated.

Project Structure

arafura/
├── src/
│   └── arafura/
│       ├── __init__.py       # Package exports
│       ├── transpiler.py     # Main transpiler implementation
│       └── cli.py            # Command-line interface
├── tests/
│   ├── fixtures/             # Test input files (.py)
│   ├── golden_outputs/       # Expected output files (.c)
│   ├── conftest.py          # Pytest configuration
│   ├── test_golden_master.py # Golden master tests
│   └── test_transpiler.py   # Unit tests
├── pyproject.toml           # Project metadata and dependencies
└── README.md

Testing

Run all tests:

# Install dev dependencies first
pip install -e ".[dev]"

# Run all tests
pytest

# Run with verbose output
pytest -v

# Run only golden master tests
pytest tests/test_golden_master.py

# Run only unit tests
pytest tests/test_transpiler.py

Golden Master Tests

The project uses golden master testing to ensure transpiler output stability. Golden outputs are stored in tests/golden_outputs/ and are compared against current transpiler output.

To regenerate golden outputs after intentional changes:

python src/arafura/transpiler.py tests/fixtures/example.py > tests/golden_outputs/example.c
python src/arafura/transpiler.py tests/fixtures/features.py > tests/golden_outputs/features.c
python src/arafura/transpiler.py tests/fixtures/typedef.py > tests/golden_outputs/typedef.c

Design Philosophy

  • Python syntax, C semantics: Write code that looks like Python but behaves like C
  • No magic: Every construct has a clear, predictable mapping to C
  • Local transformation: No global analysis or type inference needed
  • Explicit over implicit: All types, casts, and operations must be explicit

Limitations

  • No Python runtime features (no garbage collection, no dynamic typing, etc.)
  • Requires explicit type annotations
  • Some C features may require verbose syntax
  • Error messages reference Python AST, not C concepts

License

See DESIGN.md for the complete language specification.

Release files for arafura 0.1.0

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

Source distribution (sdist)

Source distribution for arafura 0.1.0
File Size Uploaded
arafura-0.1.0.tar.gz 18.0 kB Details

Built distribution (wheel)

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

Total release size: 31.9 kB

Release files / arafura-0.1.0.tar.gz

Download URL arafura-0.1.0.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
93163a465edf20a6be622d466e091a0490ea3d0985aa45536069a777f82969bb
BLAKE2b-256 checksum
How to use checksums
46893693abfd46cbba0abfa9d1a762dd2c04babd6f7f7e6aa40214ed1251c67f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2025.

Transparency log

Release files / arafura-0.1.0-py3-none-any.whl

Download URL arafura-0.1.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b7fc413c2583cc68c7326570f0ce17f48ff456a0efb69970149b57496b194cd3
BLAKE2b-256 checksum
How to use checksums
46cbf5cb3f4b0aaf5e5d1789061803a9732dff86d29bf7ce229b7e1518b0cf36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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