PCC is a Python-to-C-to-Executable compiler that translates a subset of Python into C code and compiles it to native executables. It features arbitrary-precision integer arithmetic (BigInt), string operations, control flow, and function support.
- Features
- Project Structure
- Installation
- Usage
- Supported Python Subset
- Examples
- Architecture
- Development
- Testing
- Troubleshooting
- Contributing
- License
- Arbitrary-Precision Integers: Support for integers of any size with no overflow
- String Operations: String literals, variables, and concatenation
- Control Flow: if/else, while loops, for-range loops, break, continue
- Functions: Define and call functions with support for recursion
- Type Safety: Compile-time type checking
- Cross-Platform: Supports Windows (MSVC, clang-cl) and Linux/macOS (GCC)
- Clean C Output: Generates readable, structured C code
pcc/
├── pcc/ # Main Python package
│ ├── __init__.py # Package initialization
│ ├── __main__.py # Entry point for `python -m pcc`
│ ├── cli.py # Command-line interface
│ ├── ir/ # Intermediate Representation
│ │ ├── __init__.py
│ │ └── nodes.py # IR node definitions
│ ├── core/ # Core compiler components
│ │ ├── __init__.py
│ │ ├── parser.py # Python AST to IR parser
│ │ └── compiler.py # Main compiler orchestration
│ ├── backend/ # Code generation
│ │ ├── __init__.py
│ │ └── codegen.py # C code generator
│ └── utils/ # Utility modules
│ ├── __init__.py
│ ├── toolchain.py # Toolchain detection
│ └── settings.py # Configuration settings
├── runtime/ # C runtime library
│ ├── runtime.h # Main runtime header
│ ├── rt_config.h # Configuration and platform detection
│ ├── rt_error.h/.c # Error handling
│ ├── rt_string.h/.c # String operations
│ └── rt_bigint.h/.c # BigInt operations
├── tests/ # Test suite
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ ├── e2e/ # End-to-end tests
│ └── fixtures/ # Test fixtures
├── scripts/ # Build scripts
│ ├── build.ps1 # PowerShell build script
│ └── run_tests.ps1 # Test runner script
├── README.md # This file
└── .gitignore
- Python 3.10 or higher
- A C compiler:
- Windows: Visual Studio Build Tools (cl.exe) or LLVM (clang-cl)
- Linux/macOS: GCC or Clang
pip install pytest # For running testswinget install -e --id Microsoft.VisualStudio.2022.BuildToolsAfter installation, use "Developer PowerShell for VS 2022" or ensure cl.exe is in your PATH.
winget install -e --id LLVM.LLVM
winget install -e --id Microsoft.WindowsSDK.11sudo apt-get install gccxcode-select --installBuild a Python file to an executable:
python -m pcc build input.py -o output.exeOptions:
-o, --output: Output executable path (required)--toolchain: Compiler to use (auto,msvc,clang-cl,gcc)--emit-c-only: Only generate C code, skip compilation-v, --verbose: Enable verbose output
# Basic build
python -m pcc build example.py -o example.exe
# Specify toolchain
python -m pcc build example.py -o example.exe --toolchain msvc
# Only generate C code
python -m pcc build example.py -o example.exe --emit-c-only
# Show version
python -m pcc versionfrom pcc import Compiler
compiler = Compiler()
# Build to executable
result = compiler.build(
input_py=Path("input.py"),
out_exe=Path("output.exe"),
toolchain="auto"
)
if result.success:
print(f"Built: {result.executable_path}")
else:
print(f"Error: {result.error_message}")
# Or use individual steps
ir = compiler.parse("print(1 + 2)")
c_source = compiler.generate_c(ir)
print(c_source.c_source)- Integers: Arbitrary precision (BigInt)
- Operations:
+,-,*,//,% - Comparisons:
==,!=,<,<=,>,>=
- Operations:
- Strings: Literals, variables, concatenation (
+) - Booleans: Result of comparisons (0/1 integers)
x = expr- Variable assignmentprint(expr)- Print expressionreturn expr- Return from function- Expression statements (function calls)
if condition:/else:- Conditional executionwhile condition:- While loopfor i in range(start, stop, step):- For-range loopbreak- Exit loopcontinue- Skip to next iteration
def function_name(param1, param2):
# function body
return expression- No
//or%in expressions (only in statements) - No
int + strorstr + intmixing - No string comparison
- No classes, lists, dictionaries, tuples
- No exception handling, imports, or decorators
- No keyword arguments
# BigInt arithmetic
a = 100000000000000000000
b = 99999999999999999999
print(a + b) # 199999999999999999999s = "hello"
t = "world"
print(s + " " + t) # hello worldx = 10
if x > 5:
print("big")
else:
print("small")
for i in range(5):
print(i)
n = 5
while n > 0:
print(n)
n = n - 1def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(5)) # 120PCC follows a traditional compiler architecture:
- Parsing: Python source → AST → IR (Intermediate Representation)
- Code Generation: IR → C source code
- Compilation: C source → Native executable
The IR is a simplified AST that represents the supported Python subset:
- Expressions:
IntConst,StrConst,Var,BinOp,CmpOp,Call - Statements:
Assign,Print,If,While,ForRange,Return,Break,Continue - Module-level:
FunctionDef,ModuleIR
The C runtime provides:
- BigInt (
rt_int): Arbitrary-precision integer arithmetic - String (
rt_str): String operations with proper memory management - Error Handling: Structured error codes and messages
# Run all tests
pytest
# Run with coverage
pytest --cov=pcc --cov-report=html
# Run specific test file
pytest tests/unit/test_parser.py
# Run with verbose output
pytest -v# Clone the repository
git clone https://github.com/yourusername/pcc.git
cd pcc
# Install development dependencies
pip install pytest pytest-cov
# Run tests to verify setup
pytest- Python: Follow PEP 8
- C: Follow Linux kernel style (tabs, 80-column limit)
- All code should have docstrings/comments
The test suite includes:
-
Unit Tests (
tests/unit/): Test individual componentstest_ir.py: IR node teststest_parser.py: Parser teststest_codegen.py: Code generator tests
-
Integration Tests (
tests/integration/): Test component interactions -
End-to-End Tests (
tests/e2e/): Test full compilation pipeline -
Test Fixtures (
tests/fixtures/): Sample Python files for testing
Target: 85%+ code coverage
Current coverage can be checked with:
pytest --cov=pcc --cov-report=term-missingOpen "Developer PowerShell for VS 2022" or run:
& "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"Install a C compiler:
- Windows: Visual Studio Build Tools or LLVM
- Linux:
sudo apt-get install gcc - macOS:
xcode-select --install
Check that the runtime library is in the include path:
# Runtime should be at runtime/runtime.h
ls runtime/runtime.hEnsure your Python code uses only the supported subset. See Supported Python Subset.
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Write tests for new features
- Maintain 85%+ test coverage
- Update documentation for API changes
- Follow existing code style
Please include:
- Python version
- Operating system
- Compiler version
- Minimal code to reproduce the issue
- Error messages
This project is licensed under the MIT License - see the LICENSE file for details.
- Authors: @hi_tyc, @hi_zcy
- Inspired by Python's simplicity and C's performance
- BigInt implementation based on base-10^9 limb representation
- Float support
- List/dict support
- Exception handling
- Module imports
- Optimization passes
- LLVM backend option
- WebAssembly target
Note: This is an MVP (Minimum Viable Product) implementation. The supported Python subset is intentionally limited for simplicity and performance.