Skip to content

Py2CPP - The Ultimate Python to C++ Transpiler πŸš€

Notifications You must be signed in to change notification settings

pro-grammer-SD/py2cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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