Translate a strict subset of Python scripts into Rust programs.
Project description
Transplang
Translate a strict subset of Python scripts into compilable Rust programs.
Example
script.py
def total(nums):
s = 0
for n in nums:
s += n
return s
print(total([1, 2, 3, 4]))
Generated out.rs
fn total(nums: Vec<i32>) -> i32 {
let mut s: i32 = 0;
for n in nums {
s = s + n;
}
s
}
fn main() {
println!("{}", total(vec![1, 2, 3, 4]));
}
Supported Python constructs
| Construct | Notes |
|---|---|
def functions |
scalar and Vec parameters |
| Integer literals | mapped to i32 |
| String literals | mapped to &str |
| List literals | mapped to vec![…] |
| Variable assignment | let / let mut |
| Augmented assignment | +=, -=, *=, /= |
| Arithmetic | +, -, *, / |
for x in y loops |
iterating over lists or Vec params |
return |
last return rendered as tail expression |
print() |
mapped to println! |
Unsupported (rejected with a clear error)
importstatementsclassdefinitionsif/while/with/tryasync/await- Decorators
- Comprehensions
- Dicts, sets
- Attribute access (
obj.attr) - Subscript access (
x[i])
Architecture
Python file
↓
ast.parse() [parser.py]
↓
SemanticModel [analyzer.py]
↓
RustIR [mapper.py]
↓
Rust source [rust_generator.py]
No runtime dependencies; stdlib only (ast, argparse, pathlib).
Install
pip install transplang
Or from source:
git clone https://github.com/calchiwo/transplang.git
cd transplang
pip install -e .
Usage
transplang translate script.py --to rust
Write output to a file:
transplang translate script.py --to rust --output out.rs
rustc out.rs && ./out
Running tests
pip install pytest
pytest
Or without pytest:
python -m unittest discover -s tests
Scope
Transplang currently focuses on a very small subset of Python and Rust.
The goal of this version is simple: demonstrate that structured translation between programming languages can work in practice when the surface area is tightly controlled.
Only a limited set of constructs are supported:
- functions
- integers
- lists
- loops
- basic arithmetic
print
Anything outside this scope is intentionally rejected.
This constraint keeps the system deterministic and makes it easier to reason about translation correctness.
Why I started small
Real software systems are complex. Languages differ in runtime models, type systems, and ecosystem conventions.
Attempting to translate entire applications immediately would produce unreliable results.
Instead, Transplang begins with a narrow slice of functionality to test the core idea: that a program can be analyzed structurally and rebuilt in another language.
If this approach proves reliable on small programs, the surface area can expand over time.
Long-term direction
Programs written in different languages ultimately describe the same underlying elements:
- state
- transformations
- control flow
- side effects
Transplang explores whether those elements can be extracted and reconstructed in another language environment.
The current prototype should be viewed as a minimal experiment around that idea.
Author
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 transplang-0.1.0.tar.gz.
File metadata
- Download URL: transplang-0.1.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2596b1eb8461d08110dca19e1cf9a2ccceff595ffded24c1794694b0f711ba54
|
|
| MD5 |
6c09d005f3591f4e905e109bff7365e4
|
|
| BLAKE2b-256 |
54d0d9cc774a8c092a958aacc8c3eed3d3d749331feef3233ad590866a5b67be
|
File details
Details for the file transplang-0.1.0-py3-none-any.whl.
File metadata
- Download URL: transplang-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
712dc79c8875e897903745df838402bb2862fcfe8d5af79ace4217e79d105bca
|
|
| MD5 |
5a2738e663bc876f3993e6360949c191
|
|
| BLAKE2b-256 |
d2a93c56fa0be10a65eb52ab8ad95f833dda78a393e386ee1bf3113e74ceaa4a
|