Skip to main content

A Comprehensive Python Compiler Toolkit

Project description

PyCompilerX

A Comprehensive Python Compiler Toolkit

Python License Version

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 parsing
  • lark - Modern parsing library
  • gast - AST compatibility across Python versions
  • llvmlite - LLVM integration (Python 3.8+)
  • cython - C compilation support
  • numba - JIT compilation (Python 3.8+)
  • mypy - Advanced static type checking
  • astor - 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:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes and add tests
  4. Run tests: pytest
  5. 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:

๐Ÿš€ 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycompilerx-0.1.0.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycompilerx-0.1.0-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

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

Hashes for pycompilerx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c38d3a0cdb9ca1737c452354ad150e19f27d03d24672b3795c075fb820c76e62
MD5 50bb35fa6bcee39b1e0d28fe2a126136
BLAKE2b-256 d7d63c711cb879e84241b4a538846ce24c117622ffbdbdf76e58f6859dc72c96

See more details on using hashes here.

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

Hashes for pycompilerx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 528afa11f17d0462647efcb6262d0babb3fbf4edadacfe47a8b31726488e9da9
MD5 039fd0e8fbecf47d6763b735bdfbe50f
BLAKE2b-256 13ad6c09230b3b8f97227dc9ff127d226f206044688b6ac08a493c8b0bcf13ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page