tranp (TRANspiler on Python)
概要
- Pythonによるトランスパイルフレームワークの実装
- 入力言語のAST(※1)を元に、出力言語のソースコードをレンダリング
- ※1: ASTの生成はlarkを利用(自由に変更可能)
特徴
- 入出力言語を自由に選択可能(※1)
- テンプレート(※2)を変更することで、コア実装を改変することなくレンダリング内容を自由に改変可能
- 難読化されない可読なソースコードを生成
- 型注釈の解析と型推論の組み合わせによる型の補完に対応
- トランスパイルのリアルタイム変換・シンボル解析・AST解析ツールを付属
- ※1: 現状はPythonからC++への変換のみ実装。新しい組み合わせは個別に実装が必要
- ※2: テンプレートエンジンはJinja2を利用(自由に変更可能)
必須要件
- Python 3.13
- Lark 1.3.1
- Jinja2 3.1.4
- PyYAML 6.0.2
インストール
$ pip install tranp
トランスパイル (Python to C++)
リアルタイム変換
-itオプションを指定して実行することでREPLが起動
$ tranp -it
- Pythonコードを入力した後、空行を入力するとトランスパイル結果がレンダリング
==========
Code here. Type `exit` to quit:
a = 1
==========
Result:
----------
// @tranp.meta: {"version":"0.9.n","module":{"hash":"dummy","path":"__main__"},"transpiler":{"version":"0.9.n","module":"tranp.app.implements.cpp.transpiler.py2cpp.Py2Cpp"}}
#pragma once
int a = 1;
- 再びPythonコードを入力することで、繰り返し結果を確認することが可能
==========
Code here. Type `exit` to quit:
def main() -> int:
return 1
==========
Result:
----------
// @tranp.meta: {"version":"0.9.n","module":{"hash":"dummy","path":"__main__"},"transpiler":{"version":"0.9.n","module":"tranp.app.implements.cpp.transpiler.py2cpp.Py2Cpp"}}
#pragma once
/** main */
int main() {
return 1;
}
exitのみ入力するとREPLが終了 (またはCtrl + C)
==========
Code here. Type `exit` to quit:
exit
Quit
一括変換
-cオプションでコンフィグを指定することで、設定に従って一括変換を行う- チュートリアルで順を追って解説
$ tranp -c path/to/config.yml
チュートリアル
- 以下の様な構成のプロジェクトを用意
app/
sub.py
main.py
config.yml
# app/main.py
from app.sub import hello
def main() -> int:
hello()
return 1
# app/sub.py
def hello() -> None:
print('hello world!')
# config.yml
grammar: ${tranp_dir}/data/grammar.lark
trans_mapping: ${tranp_dir}/data/i18n.yml
template_dirs:
- ${tranp_dir}/data/cpp/template
input_globs:
- app/**/*.py
exclude_patterns: []
output_dirs:
- ./
output_language: cpp:h
env:
transpiler:
include_dirs:
- app/
view:
immutable_param_types:
- std::string
- std::vector
- std::map
- std::function
- トランスパイルを実行
$ tranp -c config.yml
- トランスパイル後の配置
app/
sub.py
sub.h <- NEW!
main.py
main.h <- NEW!
config.yml
- トランスパイル結果
// @tranp.meta: {"version":"0.9.n","module":{"hash":"6012cab9fec85852b56d7b7b8deb6bd9","path":"app.main"},"transpiler":{"version":"0.9.n","module":"tranp.app.implements.cpp.transpiler.py2cpp.Py2Cpp"}}
#pragma once
#include "sub.h"
/** main */
int main() {
hello();
return 1;
}
// @tranp.meta: {"version":"0.9.n","module":{"hash":"96178f75f71ca63c3102afc3e041a5af","path":"app.sub"},"transpiler":{"version":"0.9.n","module":"tranp.app.implements.cpp.transpiler.py2cpp.Py2Cpp"}}
#pragma once
/** hello */
void hello() {
printf("hello world!");
}
解析ツール
AST解析ツール
astコマンドを指定して実行するとREPLが起動- ※終了方法はトランスパイラーと同様
$ tranp ast
- Pythonコードを入力することでASTがレンダリング
- ※出力結果はlarkのASTに依存
==========
Code here. Type `exit` to quit:
a = 1
==========
AST
----------
file_input
assign
assign_namelist
var
name 2
number 1
シンボル解析ツール
analyzeコマンドを指定して実行するとREPLが起動- 詳細はシンボル解析ツールで解説
$ tranp analyze
ドキュメント
ライセンス
- MIT
- tranpを用いて生成したトランスパイル後のソースコードに関してはライセンスに含まれません
Metadata
Release files for tranp 0.9.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tranp-0.9.11.tar.gz | 199.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tranp-0.9.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 505.8 kB
Release files / tranp-0.9.11.tar.gz
| Download URL | tranp-0.9.11.tar.gz |
|---|---|
| Size | 199.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c0380e718b21f417475749b33233b9f12e466e39d88ee8215b020b9980b73ade
|
|
BLAKE2b-256 checksum How to use checksums |
e84bcc0015bf6a024d30101390fc9e417ab84fd63a40fbcd7c5a34e74d213ce4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / tranp-0.9.11-py3-none-any.whl
| Download URL | tranp-0.9.11-py3-none-any.whl |
|---|---|
| Size | 306.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e8f17a6e4bb0f410db367f2796dc4565e6dffb4d039533294dd0b0335da2af22
|
|
BLAKE2b-256 checksum How to use checksums |
03fa8c63e7b4bf25df9133fa695c65325195296d69a59e793f22270113d184f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|