Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Latest commit

 

History

History
152 lines (104 loc) · 3.84 KB

File metadata and controls

152 lines (104 loc) · 3.84 KB

Py2CPP - The Ultimate Python→C++ Transpiler 🚀

License: MIT

Overview

Py2Cpp is a production-grade Python to C++20 transpiler. It converts Python scripts into optimized, memory-safe C++ code with zero dependencies on the core AST translation engine. Perfect for turning Python prototypes into high-performance C++ binaries.

Features

  • ✅ Zero-dependency core AST translation
  • ✅ Intelligent type inference engine
  • ✅ Memory-safe C++20 generation
  • ✅ Full scope & symbol tracking
  • ✅ Optimized g++ compilation pipeline
  • ✅ Comprehensive error handling
  • 🔥 CLI interface for rapid transpilation and execution

Installation

Step 1: Install Python Dependencies

pip install typer

Step 2: Install g++ Compiler (via MSYS2 on Windows)

  1. Download and install MSYS2

  2. Update package database:

pacman -Syu
  1. Install g++ and make:
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make
  1. Add C:\msys64\mingw64\bin to your PATH.

Step 3: Install Python Requirements

pip install -r requirements.txt

requirements.txt should contain:

typer

Step 4: Verify Installation

py2cpp --help

You should see the CLI usage instructions.

Usage

Command-line Interface

py2cpp <input_file.py> [OPTIONS]

Options

Option Shortcut Description
--output -o Specify output binary path
--std Set C++ standard (default: c++20)
--optimize -O Optimization level (O0, O1, O2, O3, Os)
--run -r Run binary after compilation
--keep-cpp -k Keep generated .cpp file
--verbose -v Enable verbose logging

Example

py2cpp main.py --run --optimize O3

How It Works 🛠️

  1. AST Parsing & Analysis: Parses Python AST, extracts type information, and analyzes scopes.
  2. Type Inference: Infers C++ types for variables, functions, containers, and constants.
  3. C++ Code Generation: Generates fully scoped, memory-safe C++20 code including runtime preamble for Python builtins.
  4. Compilation: Invokes g++ with selected optimization level and generates executable binary.
  5. Execution: Optionally runs the generated binary and reports execution stats.

Supported Python Constructs

  • Variables, constants, and basic types
  • Lists (list) → std::vector
  • Dicts (dict) → std::map
  • Loops: for, while
  • Conditional statements: if, elif, else
  • Functions & methods, including __init__
  • String formatting, arithmetic, comparisons, boolean operations
  • Return type inference

Limitations

  • Some dynamic Python constructs may fall back to std::any
  • Certain built-in functions are mapped explicitly (input, print, len, range)
  • Does not yet support async or coroutine features

Logging & Reporting 📊

  • ℹ️ Info messages

  • Success

  • ⚠️ Warnings

  • Errors

  • Final report includes:

    • Duration
    • Nodes processed
    • Warnings & errors
    • Status (SUCCESS/FAILED)

Example Generated C++ Snippet

std::vector<int> nums = {1, 2, 3};
for (int i = 0; i < 3; i++) {
    print(nums[i]);
}

License

MIT License © 2026

Authors

  • Soumalya - Creator & Maintainer

Contact


Build Python code | 🚀 Compile C++ | ⚡ Run binaries | 💥 All in one with Py2CPP