Metadata-Version: 2.4
Name: turbopython
Version: 0.1.0
Summary: A compiled Python dialect that eliminates CPython overhead without sacrificing Python syntax
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
Project-URL: Homepage, https://github.com/ribalba/TurboPython
Project-URL: Repository, https://github.com/ribalba/TurboPython
Project-URL: Issues, https://github.com/ribalba/TurboPython/issues
Keywords: compiler,performance,native,c,jit
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Compilers
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# TurboPython

A compiled Python dialect that eliminates core sources of CPython overhead — without sacrificing Python's syntax or readability.

TurboPython compiles `.tpy` files to native C via GCC, producing either a shared library (`.so`) callable from Python via `ctypes`, or a standalone executable. New features are strictly opt-in: unannotated code stays valid Python.

This is work in progress. The syntax can still change as we find more things that one could improve.

## The Name

The name has two references. **Turbo** comes from [Turbo Pascal](https://en.wikipedia.org/wiki/Turbo_Pascal) — Borland's legendary 1980s compiler that made Pascal fast enough to write real software on a home computer, in part by making compilation itself instant. The parallel is intentional: TurboPython aims to make Python fast enough for systems-level work without leaving the language behind. **Python** is there because it stays Python — same syntax, same stdlib, same feel.

The short name is **tython**, which is also the name of the drop-in runner. And also the name of a planet (#lightsaber)

---

## How It Works


TurboPython addresses a number independent axes of CPython overhead. Each has its own opt-in syntax. See [docs/turbopython_syntax.md](docs/turbopython_syntax.md) for the full language reference.

### 1. Strict Static Typing

Type annotations are enforced at compile time, not ignored like Python hints. The compiler emits unboxed native arithmetic — no object headers, no dynamic dispatch.

```python
@native
def distance(x: float, y: float) -> float:
    return (x**2 + y**2) ** 0.5
# Emits: double distance(double x, double y) { return sqrt(x*x + y*y); }
```

Unannotated functions fall back to normal CPython — typing is **opt-in**.

### 2. Value Types — `struct`

A new `struct` keyword creates stack-allocated, contiguous-memory types with no heap allocation, no refcount, no GC overhead.

```python
struct Vec3:
    x: float
    y: float
    z: float

points: array[Vec3, 1000]  # 24,000 bytes, one contiguous block
                           # vs. Python list: 1000 pointers + 1000 heap objects
```

Memory comparison: `Vec3` = **24 bytes**. Equivalent Python object ≈ **200+ bytes**.

### 3. Ownership and Borrowing

Rust-inspired ownership eliminates refcount overhead on the hot path. Three modes, all opt-in:

```python
def consume(data: owned list[int]) -> int:   # caller transfers ownership — source invalidated
    return sum(data)

def analyze(data: ref list[int]) -> float:   # immutable borrow — zero-cost, no refcount
    return sum(data) / len(data)

def normalize(data: mut ref list[float]):    # exclusive mutable borrow — no data races
    total = sum(data)
    for i in range(len(data)):
        data[i] /= total
```

The compiler enforces: multiple `ref` borrows are fine; `mut ref` is exclusive — any overlap is a compile error.

### 4. Native Compilation Directives

Explicit control over what compiles to native code:

```python
@native   # AOT compile — all types resolved at compile time
def fib(n: int) -> int: ...

@inline   # static inline in C — zero-overhead small helpers
def clamp(val: float, lo: float, hi: float) -> float:
    return min(max(val, lo), hi)

@jit      # JIT compile on first call, specialize on observed types
def flexible_sum(items): ...

const MAX_ITER: int = 256   # compile-time constant, embedded in binary
```

### 5. True Parallelism — No GIL

`parallel for` partitions loop iterations across all cores via OpenMP. The ownership checker statically guarantees no data races — no locks needed.

```python
parallel for y in range(height):
    for x in range(width):
        pixels[y * width + x] = mandelbrot(cx, cy, 256)
# Emits: #pragma omp parallel for
```

`spawn` launches concurrent tasks with typed channels as the only communication mechanism:

```python
spawn filter_stage(data, chan_filtered)
spawn transform_stage(chan_filtered, chan_results)
```

### 6. Extended Integer Types

`int128` maps to GCC's `__int128`, giving a range of ±1.7 × 10³⁸ — suitable for large combinatorics and cryptographic primitives without external dependencies.

```python
@native
def fib(n: int) -> int128:
    a: int128 = 0
    b: int128 = 1
    for i in range(n):
        tmp: int128 = a + b
        a = b
        b = tmp
    return a
```

### Summary

| Concept | Standard Python | TurboPython |
|---|---|---|
| Type enforcement | Advisory hints | Compile-time enforced |
| Memory layout | `class` (heap, dict-backed) | `struct` (stack, packed, no GC) |
| Integer range | Arbitrary precision (slow) | `int` (64-bit) or `int128` (128-bit) |
| Ownership | Refcounted, implicit sharing | `owned`, `ref`, `mut ref` |
| Compilation | Interpreted bytecode | `@native`, `@jit`, `@inline` |
| Constants | Convention (`UPPER_CASE`) | `const` (compile-time evaluated) |
| Parallelism | `threading` (GIL-bound) | `parallel for`, `spawn`, `Channel` |
| Typed arrays | `list` (boxed, pointer array) | `array[T, N]` (contiguous, unboxed) |

---

## Installation

**Requirements:** Python 3.10+, GCC with OpenMP support.

```bash
git clone https://github.com/ribalba/TurboPython.git
cd TurboPython
python -m turbopython.cli --help
```

No pip install needed — just run from the repo root.

---

## Quick Start

### 1. Write a `.tpy` file

```python
# benchmarks/hello.tpy
@native
def fib(n: int) -> int:
    a: int = 0
    b: int = 1
    for i in range(n):
        tmp: int = a + b
        a = b
        b = tmp
    return a

@native
def main() -> int:
    return fib(40)
```

### 2. Compile to an executable

```bash
python -m turbopython.cli compile benchmarks/hello.tpy --exe
```

Output:
```
✓ Compilation successful
  C source:   benchmarks/hello.c
  Executable: benchmarks/hello
```

Run it like any native binary:

```bash
./benchmarks/hello
time ./benchmarks/hello
```

### 3. Or compile to a shared library and call from Python

```bash
python -m turbopython.cli compile benchmarks/hello.tpy
```

```python
import ctypes

lib = ctypes.CDLL("./benchmarks/hello.so")
lib.fib.argtypes = [ctypes.c_int64]
lib.fib.restype  = ctypes.c_int64

print(lib.fib(40))  # 102334155 — computed in native code
```

### 4. Or use the `tython` runner

```bash
./tython benchmarks/hello.tpy        # compiles + runs main()
./tython myscript.py                 # runs .py with import hook active
```

---

## Benchmarks

```bash
python benchmarks/bench_mandelbrot.py
python benchmarks/bench_hello.py
```

Expected output (numbers vary by machine):

**Mandelbrot** (400×300, 256 iterations):
```
Pure Python  : 0.263s   (checksum: 3303274)
Compiling mandelbrot.tpy... done
TurboPython  : 0.007s   (checksum: 3303274)

Speedup      : 37.1x faster
```

**Fibonacci** (fib_sum(150) × 5000 reps, `int128`):
```
Pure Python  : 1.842s   (result: ...)
Compiling hello.tpy... done
TurboPython  : 0.031s   (result: ...)

Speedup      : 59.4x faster
```

---

## Language Features

### `@native` — AOT compiled functions

All types must be fully resolved at compile time. Emitted as a C symbol with unboxed arithmetic.

```python
@native
def mandelbrot(cx: float, cy: float, max_iter: int) -> int:
    zx: float = 0.0
    zy: float = 0.0
    for i in range(max_iter):
        if zx * zx + zy * zy > 4.0:
            return i
        tx: float = zx * zx - zy * zy + cx
        zy = 2.0 * zx * zy + cy
        zx = tx
    return max_iter
```

### `@inline` — inlined at call sites

Emitted as `static inline` in C. Best for small math helpers.

```python
@inline
def vec3_dot(a: Vec3, b: Vec3) -> float:
    return a.x * b.x + a.y * b.y + a.z * b.z
```

### `struct` — value types with no heap allocation

Stack-allocated, copied on assignment, no GC overhead. A `Vec3` is exactly 24 bytes — vs 200+ bytes for an equivalent Python object.

```python
struct Vec3:
    x: float
    y: float
    z: float

@native
def vec3_length(v: Vec3) -> float:
    return (v.x * v.x + v.y * v.y + v.z * v.z) ** 0.5
```

### `parallel for` — multi-core loops via OpenMP

Emits `#pragma omp parallel for`. The ownership checker enforces that loop bodies do not share mutable state.

```python
parallel for y in range(height):
    for x in range(width):
        cx: float = (x - width / 2.0) / (width / 4.0)
        cy: float = (y - height / 2.0) / (height / 4.0)
        total = total + mandelbrot(cx, cy, max_iter)
```

### Ownership annotations

Rust-inspired, opt-in. Eliminates refcount overhead on the hot path.

```python
def consume(data: owned list[int]) -> int:   # caller's variable is invalidated
    return sum(data)

def analyze(data: ref list[int]) -> float:   # immutable borrow, zero-cost
    return sum(data) / len(data)

def normalize(data: mut ref list[float]):    # exclusive mutable borrow
    total = sum(data)
    for i in range(len(data)):
        data[i] /= total
```

### `const` — compile-time constants

```python
const MAX_ITER: int = 1000
const PI: float = 3.14159265358979
```

---

## CLI Reference

```bash
# Compile to a .so shared library
python -m turbopython.cli compile examples/mandelbrot.tpy

# Compile with verbose output (shows generated C)
python -m turbopython.cli compile examples/mandelbrot.tpy --verbose

# Compile to a specific output directory
python -m turbopython.cli compile examples/mandelbrot.tpy -o build/

# Produce a standalone executable (requires @native def main() -> int)
python -m turbopython.cli compile benchmarks/hello.tpy --exe

# Specify a custom entry-point function name
python -m turbopython.cli compile benchmarks/hello.tpy --exe --entry run

# Inspect all compilation stages without producing output
python -m turbopython.cli inspect examples/vectors.tpy
```

`inspect` prints: original source, preprocessed Python, struct layouts, function signatures with inferred C types, and the full type environment. Useful for debugging codegen.

When `--exe` is passed:
1. Validates that the entry-point function (default: `main`) exists
2. Renames it to `__tp_main` in the generated C to avoid clashing with C's `main`
3. Appends a `int main(int argc, char** argv)` wrapper
4. Compiles without `-shared -fPIC`, producing a native executable

---

## `tython` — Drop-in Runner

`tython` is an executable at the repo root that acts as a Python-aware interpreter for both `.py` and `.tpy` files, with the import hook pre-installed.

```bash
# Compile and run a .tpy file — calls main() and uses its return as exit code
./tython examples/vectors.tpy
./tython benchmarks/hello.tpy

# Run a .py script — .tpy files on sys.path are importable by name
./tython myscript.py

# Inline command
./tython -c "import vectors; print(vectors.compute_total_distance(100))"

# Interactive REPL with import hook active
./tython
```

Inside a `.py` script run via `tython`, any `.tpy` file on `sys.path` imports transparently:

```python
# myscript.py — no special setup needed when run via tython
import vectors
print(vectors.compute_total_distance(1000))
```

---

## Import Hook

The import hook can also be used in any regular Python script without the `tython` runner:

```python
from turbopython.importer import install
install()

import vectors   # finds vectors.tpy on sys.path, compiles to vectors.so
print(vectors.compute_total_distance(1000))
```

`install()` inserts a `sys.meta_path` finder that:
1. Searches `sys.path` for `<module>.tpy` when an import cannot find a `.py`/`.pyc`
2. Compiles the `.tpy` with the full TurboPython pipeline
3. Wraps the resulting `.so` in a module object with `argtypes`/`restype` set automatically from the compiled type signatures
4. Returns the module — the caller uses it as any normal Python module

The `.so` is written next to the `.tpy` file and reused on subsequent runs.

---

## Examples

| File | Demonstrates |
|---|---|
| [examples/mandelbrot.tpy](examples/mandelbrot.tpy) | `@native`, typed arithmetic, `struct` |
| [examples/vectors.tpy](examples/vectors.tpy) | `struct` value types, `@inline`, `@native`, `main` |
| [examples/nbody.tpy](examples/nbody.tpy) | `parallel for`, struct arrays, `main` |
| [benchmarks/hello.tpy](benchmarks/hello.tpy) | `int128`, fibonacci, `main` |
| [benchmarks/hello.py](benchmarks/hello.py) | Pure Python equivalent of hello.tpy |
| [benchmarks/bench_hello.py](benchmarks/bench_hello.py) | Fibonacci benchmark vs pure Python |
| [benchmarks/bench_mandelbrot.py](benchmarks/bench_mandelbrot.py) | Mandelbrot benchmark vs pure Python |

---

## Project Layout

```
TurboPython/
├── README.md
├── tython                     # Drop-in runner / interpreter
├── turbopython/
│   ├── __init__.py
│   ├── cli.py                 # Command-line interface
│   ├── compiler.py            # Pipeline driver
│   ├── preprocessor.py        # Stage 1: syntax → valid Python + metadata
│   ├── type_checker.py        # Stage 2: type resolution and validation
│   ├── ownership.py           # Stage 3: move/borrow checking
│   ├── codegen.py             # Stage 4: C code generation + GCC invocation
│   ├── importer.py            # sys.meta_path hook for transparent .tpy imports
│   └── test_compiler.py       # Test suite
├── examples/
│   ├── mandelbrot.tpy         # Mandelbrot fractal
│   ├── vectors.tpy            # 3D vector math
│   └── nbody.tpy              # N-body gravitational simulation
├── benchmarks/
│   ├── hello.tpy              # int128 fibonacci (compile to .so or executable)
│   ├── hello.py               # Pure Python equivalent
│   ├── bench_hello.py         # Side-by-side benchmark
│   └── bench_mandelbrot.py    # Mandelbrot benchmark
└── docs/
    ├── ARCHITECTURE.md        # Detailed pipeline design
    └── turbopython_syntax.md  # Full language reference
```

---

## Type System

| TurboPython | C type | Range |
|---|---|---|
| `int` | `int64_t` | ±9.2 × 10¹⁸ |
| `int128` | `__int128` | ±1.7 × 10³⁸ |
| `float` | `double` | 64-bit IEEE 754 |
| `bool` | `int` | 0 / 1 |
| `str` | `const char*` | read-only C string |
| `array[float, N]` | `double*` | contiguous heap/stack |
| `struct Foo` | `Foo` (typedef'd struct) | stack-allocated value type |

---

## What Stays Standard Python

- Indentation-based blocks
- `def`, `class`, `for`, `if`, `while`, `with`, `return`, `yield`
- List/dict/set comprehensions
- Standard library imports
- Unannotated functions run as normal CPython

The philosophy: **opt in to performance where it matters**, keep everything else as dynamic and expressive as Python.

---

## Further Reading

- [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) — Pipeline internals, stage-by-stage design, production gaps
- [docs/turbopython_syntax.md](docs/turbopython_syntax.md) — Full language reference with syntax tables
