All Projects → adsharma → py2many

adsharma / py2many

Licence: MIT license
Transpiler of Python to many other languages

Programming Languages

python
139335 projects - #7 most used programming language
rust
11053 projects
C++
36643 projects - #6 most used programming language
dart
5743 projects
julia
2034 projects
go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to py2many

Headache
Programming Language that compiles to 8 Bit Brainfuck
Stars: ✭ 59 (-85.95%)
Mutual labels:  ast, transpiler
vast
A simple tool for vlang, generate v source file to AST json file
Stars: ✭ 23 (-94.52%)
Mutual labels:  ast, vlang
Ratel Core
High performance JavaScript to JavaScript compiler with a Rust core
Stars: ✭ 367 (-12.62%)
Mutual labels:  ast, transpiler
cito
Ć programming language. Translated automatically to C, C++, C#, Java, JavaScript, Python, Swift, TypeScript and OpenCL C.
Stars: ✭ 1,372 (+226.67%)
Mutual labels:  transpiler
natsu-clr
il2cpp transpiler and runtime compatible with .Net Core
Stars: ✭ 76 (-81.9%)
Mutual labels:  transpiler
Jikka
an automated solver for problems of competitive programming
Stars: ✭ 143 (-65.95%)
Mutual labels:  transpiler
clickhouse-ast-parser
AST parser and visitor for ClickHouse SQL
Stars: ✭ 60 (-85.71%)
Mutual labels:  ast
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (-94.52%)
Mutual labels:  ast
ts-transform-react-constant-elements
A TypeScript AST Transformer that can speed up reconciliation and reduce garbage collection pressure by hoisting React elements to the highest possible scope.
Stars: ✭ 44 (-89.52%)
Mutual labels:  ast
predeclared
Find definitions and declarations in Go source code that shadow predeclared identifiers
Stars: ✭ 26 (-93.81%)
Mutual labels:  ast
flutter ast
Flutter and Dart AST Analyzer/Parser
Stars: ✭ 87 (-79.29%)
Mutual labels:  ast
coro-scratch
A Scratch to Python transpiler that makes extensive use of coroutines
Stars: ✭ 22 (-94.76%)
Mutual labels:  transpiler
reflect
🪞 Runtime reflection for V (vlang)
Stars: ✭ 26 (-93.81%)
Mutual labels:  vlang
esp32-transpiler
Transpile Golang into Arduino code to use fully automated testing at your IoT projects.
Stars: ✭ 53 (-87.38%)
Mutual labels:  transpiler
ts-transform-react-jsx-source
TypeScript AST Transformer that adds source file and line number to JSX elements
Stars: ✭ 12 (-97.14%)
Mutual labels:  ast
vargs
Simple argument parsing library for V.
Stars: ✭ 36 (-91.43%)
Mutual labels:  vlang
markright
A customizable markdown parser in Elixir: pure pattern matching.
Stars: ✭ 14 (-96.67%)
Mutual labels:  ast
code summarization public
source code for 'Improving automatic source code summarization via deep reinforcement learning'
Stars: ✭ 71 (-83.1%)
Mutual labels:  ast
6umpukc
Тулинг для разработки сайтов и решений на Bitrix под Ubuntu/Windows
Stars: ✭ 13 (-96.9%)
Mutual labels:  transpiler
toast
Plugin-driven CLI utility for code generation using Go source as IDL
Stars: ✭ 52 (-87.62%)
Mutual labels:  ast

py2many: Python to many CLike languages transpiler

Build License

Why

Python is popular, easy to program in, but has poor runtime performance. We can fix that by transpiling a subset of the language into a more performant, statically typed language.

A second benefit is security. Writing security sensitive code in a low level language like C is error prone and could lead to privilege escalation. Specialized languages such as wuffs exist to address this use case. py2many can be a more general purpose solution to the problem where you can verify the source via unit tests before you transpile.

A third potential use case is to accelerate python code by transpiling it into an extension

Swift and Kotlin dominate the mobile app development workflow. However, there is no one solution that works well for lower level libraries where there is desire to share code between platforms. Kotlin Mobile Multiplatform (KMM) is a player in this place, but it hasn't really caught on. py2many provides an alternative.

Lastly, it's a great educational tool to learn a new language by implementing a backend for your favorite language.

Status

Rust is the language where the focus of development has been.

C++14 is historically the first language to be supported. C++17 is now required for some features.

Preliminary support exists for Julia, Kotlin, Nim, Go and Dart.

py2many can also emit Python 3 code that includes inferred type annotations, and revisions to the syntax intended to simplify parsing of the code.

History

Based on Julian Konchunas' pyrs http://github.com/konchunas/pyrs

Based on Lukas Martinelli Py14 (https://github.com/lukasmartinelli/py14) and Py14/python-3 (https://github.com/ProgVal/py14/tree/python-3) branch by Valentin Lorentz.

Example

Original Python version.

def fib(i: int) -> int:
    if i == 0 or i == 1:
        return 1
    return fib(i - 1) + fib(i - 2)

Transpiled Rust code:

fn fib(i: i32) -> i32 {
    if i == 0 || i == 1 {
        return 1;
    }
    return (fib((i - 1)) + fib((i - 2)));
}

Transpiled code for other languages:

https://github.com/adsharma/py2many/tree/main/tests/expected (fib*)

Trying it out

Requirements:

  • Python 3.8+

Local installation:

./setup.py install --user  # installs to $HOME/.local

OR

sudo ./setup.py install  # installs systemwide

Add the py2many script to your $PATH and run:

Transpiling:

py2many --cpp=1 tests/cases/fib.py
py2many --rust=1 tests/cases/fib.py
py2many --julia=1 tests/cases/fib.py
py2many --kotlin=1 tests/cases/fib.py
py2many --nim=1 tests/cases/fib.py
py2many --dart=1 tests/cases/fib.py
py2many --go=1 tests/cases/fib.py

Compiling:

clang tests/cases/fib.cpp
rustc tests/cases/fib.rs
...

Many of the transpilers rely on a language specific formatter to parse the output and reformat it. Typically this is the most prominent formatter for the language, such as rustfmt for Rust.

Most of the transpilers also rely on external libraries to provide bridges from Python constructs to the target language.

The steps to install these external libraries can be found in .github/workflows/main.yml.

Contributing

See CONTRIBUTING.md for how to test your changes and contribute to this project.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].