A Comprehensive Python Compiler Toolkit
Project description
PyCompilerX
A Comprehensive Python Compiler Toolkit
PyCompilerX is a comprehensive Python package that integrates various tools and libraries to facilitate the creation, manipulation, and compilation of Python code. This toolkit serves as a complete solution for developers interested in compiler design, code analysis, and optimization.
โจ Features
๐ง Parsing & AST Manipulation
- AST Processing: Built on Python's native Abstract Syntax Tree
- Tokenization: Advanced lexical analysis using Python's tokenize module
- Custom Parsing: Support for custom grammar parsing
- Code Generation: Regenerate Python source code from ASTs
- Syntax Validation: Comprehensive syntax checking and validation
๐ Static Analysis & Type Checking
- Type Analysis: Basic type inference and checking
- Code Statistics: Extract comprehensive code metrics
- Symbol Table Management: Track variables, functions, and classes
- Code Quality Analysis: Detect potential issues and improvements
โก Code Generation & Compilation
- LLVM Integration: Generate LLVM intermediate representation
- Cython Support: Compile Python code to C for performance optimization
- Numba JIT: Apply Just-In-Time compilation for numerical code
- Optimization Analysis: Analyze code for optimization opportunities
๐ฏ Additional Utilities
- Code Visualization: Tools to visualize ASTs and code structure
- Interactive Calculator: Full-featured expression evaluator
- Transformation Engine: Apply custom code transformations
- CLI Tools: Command-line interface for all functionality
๐ Installation
Basic Installation
pip install .
Full Installation (with all optional dependencies)
pip install .[full]
Development Installation
pip install .[dev]
๐ Quick Start
Python API
import pycompilerx as pc
# Parse and analyze code
code = """
def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
"""
# Parse the code
tree = pc.parse_code(code)
print("โ Code parsed successfully")
# Analyze the code
analysis = pc.analyze_code(code)
print(f"Functions: {analysis['functions']}")
print(f"Variables: {len(analysis['variables'])}")
# Type checking
type_result = pc.check_types(code)
print(f"Type checking: {'โ Passed' if type_result['success'] else 'โ Issues found'}")
# Generate LLVM IR
llvm_ir = pc.generate_llvm_ir(code)
print(f"LLVM IR generated: {len(llvm_ir)} characters")
# Compile with Cython
cython_result = pc.compile_cython(code)
print(f"Cython compilation: {'โ Success' if cython_result['success'] else 'โ Failed'}")
Interactive Calculator
from pycompilerx import BasicCalculator
calc = BasicCalculator()
# Basic arithmetic
result = calc.evaluate_expression("2 + 3 * 4")
print(f"2 + 3 * 4 = {result}") # Output: 14
# Variables
calc.set_variable("x", 10)
calc.set_variable("y", 5)
result = calc.evaluate_expression("x ** 2 + y")
print(f"xยฒ + y = {result}") # Output: 105
# Script evaluation
script = """
radius = 5
area = 3.14159 * radius ** 2
perimeter = 2 * 3.14159 * radius
"""
calc.evaluate_script(script)
print("Variables:", calc.list_variables())
Command Line Usage
# Analyze a Python file
pycompilerx analyze script.py --types --stats
# Parse and validate syntax
pycompilerx parse -e "x = 5 + 3" --validate
# Generate LLVM IR
pycompilerx compile script.py --llvm --optimize -o output.ll
# Compile with Cython
pycompilerx compile script.py --cython --optimize -o output.pyx
# Interactive calculator
pycompilerx calc --interactive
# Evaluate expression
pycompilerx calc "2 ** 10 + 3 * 4"
# Analyze optimization potential
pycompilerx analyze script.py --cython --numba
๐ Architecture
PyCompilerX/
โโโ pycompilerx/ # Main package
โ โโโ __init__.py # Package initialization & exports
โ โโโ cli.py # Command-line interface
โ โโโ parsing/ # Parsing and AST tools
โ โ โโโ lexer.py # Lexical analysis
โ โ โโโ parser.py # Parser implementations
โ โ โโโ ast_tools.py # AST manipulation & analysis
โ โโโ analysis/ # Static analysis & type checking
โ โ โโโ type_check.py # Type inference & validation
โ โโโ backend/ # Code generation & compilation
โ โ โโโ llvm.py # LLVM IR generation
โ โ โโโ cython.py # Cython compilation
โ โ โโโ numba.py # Numba JIT integration
โ โโโ examples/ # Examples & demonstrations
โ โโโ basic_calc.py # Calculator implementation
โ โโโ ast_transform.py # AST transformation demos
โโโ tests/ # Comprehensive test suite
โโโ pyproject.toml # Modern Python packaging
โโโ README.md # This file
โโโ LICENSE # MIT license
๐ง API Reference
Core Functions
# Parsing
tree = pycompilerx.parse_code(code) # Parse code to AST
syntax_ok = pycompilerx.validate_syntax(code) # Validate syntax
# Analysis
stats = pycompilerx.analyze_code(code) # Get code statistics
types = pycompilerx.check_types(code) # Type checking
analysis = pycompilerx.analyze_types(code) # Detailed type analysis
# Code Generation
llvm_ir = pycompilerx.generate_llvm_ir(code) # Generate LLVM IR
cython_code = pycompilerx.compile_cython(code) # Compile to Cython
# Utilities
calc = pycompilerx.BasicCalculator() # Create calculator
result = pycompilerx.evaluate_expression("2+3") # Quick evaluation
Examples Module
# Calculator
calc = BasicCalculator()
calc.evaluate_expression("sqrt(16) + 2**3")
calc.set_variable("pi", 3.14159)
# AST Transformations
from pycompilerx.examples import transform_example, analyze_example
transform_example() # Demo code transformations
analyze_example() # Demo code analysis
๐งช Testing
Run the test suite:
# Run all tests
pytest
# Run with coverage
pytest --cov=pycompilerx
# Run specific test categories
pytest -m "not slow" # Skip slow tests
pytest tests/test_parsing.py # Test specific module
๐ Requirements
Minimum Requirements
- Python 3.8+
typing-extensions(for Python < 3.10)
Optional Dependencies (for full functionality)
ply- Python Lex-Yacc for custom parsinglark- Modern parsing librarygast- AST compatibility across Python versionsllvmlite- LLVM integration (Python 3.8+)cython- C compilation supportnumba- JIT compilation (Python 3.8+)mypy- Advanced static type checkingastor- AST to source conversion (Python < 3.9)
๐ฏ Use Cases
For Educators & Students
- Learn compiler construction concepts
- Understand AST manipulation
- Explore code analysis techniques
- Practice optimization algorithms
For Developers
- Build custom code analysis tools
- Create domain-specific languages
- Implement code transformations
- Optimize Python performance
For Researchers
- Experiment with compiler optimizations
- Analyze code patterns and metrics
- Develop new analysis techniques
- Prototype language features
๐ค Contributing
We welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run tests:
pytest - Submit a pull request
Development Setup
git clone https://github.com/your-username/pycompilerx.git
cd pycompilerx
pip install -e .[dev]
pre-commit install
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
PyCompilerX builds upon excellent open-source projects:
- Python's built-in
astandtokenizemodules - PLY (Python Lex-Yacc) for parsing
- Lark parsing library
- LLVM and llvmlite
- Cython for C compilation
- Numba for JIT compilation
- MyPy for static typing
๐ Roadmap
Current (v0.1.0)
- โ Core parsing and AST manipulation
- โ Basic static analysis and type checking
- โ LLVM IR generation (mock)
- โ Cython and Numba integration
- โ Interactive calculator and examples
- โ Comprehensive CLI tools
Upcoming (v0.2.0)
- ๐ Enhanced type inference algorithms
- ๐ Advanced code optimization passes
- ๐ Plugin system for custom backends
- ๐ Web-based AST visualizer
- ๐ Performance benchmarking suite
- ๐ IDE integration plugins
Future
- ๐ Custom DSL support
- ๐ Distributed compilation
- ๐ Machine learning model compilation
- ๐ Advanced debugging tools
Made with โค๏ธ by the PyCompilerX Team
Star โญ this repository if you find it helpful!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycompilerx-0.1.0.tar.gz.
File metadata
- Download URL: pycompilerx-0.1.0.tar.gz
- Upload date:
- Size: 29.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c38d3a0cdb9ca1737c452354ad150e19f27d03d24672b3795c075fb820c76e62
|
|
| MD5 |
50bb35fa6bcee39b1e0d28fe2a126136
|
|
| BLAKE2b-256 |
d7d63c711cb879e84241b4a538846ce24c117622ffbdbdf76e58f6859dc72c96
|
File details
Details for the file pycompilerx-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pycompilerx-0.1.0-py3-none-any.whl
- Upload date:
- Size: 30.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
528afa11f17d0462647efcb6262d0babb3fbf4edadacfe47a8b31726488e9da9
|
|
| MD5 |
039fd0e8fbecf47d6763b735bdfbe50f
|
|
| BLAKE2b-256 |
13ad6c09230b3b8f97227dc9ff127d226f206044688b6ac08a493c8b0bcf13ca
|