Skip to main content

tranp (TRANspiler on Python)

test codecov MIT License

概要

  • Pythonで実装されたトランスパイルフレームワーク
  • 入力言語のソースコードからAST(※1)を生成・解析し、テンプレート(※2)を介して出力言語のソースコードを出力するCLIツール
※1: ASTの生成はLarkを利用(自由に変更可能)
※2: テンプレートエンジンはJinja2を利用(自由に変更可能)

特徴

  • 入力・出力言語共に自由に選択できる設計(※1)
  • テンプレートを変更することで、コア実装を改変することなくレンダリング内容を自由に改変可能
  • 難読化されない可読なソースコードを生成
  • 型注釈の解析と型推論の組み合わせによる型の補完に対応
  • トランスパイルのリアルタイム変換(REPL)が可能
  • シンボル解析・AST解析のツールを付属
※1: 現状はPythonからC++への変換のみ実装。新しい組み合わせは個別に実装が必要であり、決して容易ではない点に注意

処理速度

  • キャッシュ済みの状態で約300ファイルを約40秒で変換 (平均0.13秒/1ファイル)
  • 実運用では差分更新となるため、およそ1秒程度で変換が完了 (同時更新するファイル数により増減)

必須要件

動作環境

  • Linux
  • Unix系(MacOSなど)
  • Windows ※1
※1: WindowsではREPLが使用不可 (WSL/Git Bash等の互換環境を使用すれば可能)

インストール

$ 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

シンボル解析ツール

$ tranp analyze

ドキュメント

互換性

  • 本リポジトリーは実験的なプロジェクトであり、作者自身の課題を解決するために開発しています
  • 事前の告知なく破壊的な仕様変更を行う可能性があり、バージョン毎の動作・互換性を保証するものではない旨、予めご理解ください
  • 継続的な利用にあたってはバージョンを固定することを推奨します

ライセンス

  • MIT
  • 本リポジトリー内の既存の成果物に関しては、MITライセンスに従います
  • tranpを利用し、独自に生成した成果物に関しては、tranpのライセンス対象外です

Metadata

Release files for tranp 0.9.25

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tranp 0.9.25
File Size Uploaded Provenance Actions
tranp-0.9.25.tar.gz 204.6 kB Without provenance Details for tranp-0.9.25.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tranp 0.9.25
File Interpreter ABI Platform Actions
tranp-0.9.25-py3-none-any.whl Python 3 none any Details for tranp-0.9.25-py3-none-any.whl

Total release size: 517.9 kB

Release files / tranp-0.9.25.tar.gz

Download URL tranp-0.9.25.tar.gz
Size 204.6 kB
Tags Source
SHA-256 checksum
How to use checksums
01408e9c40b790721a720ebe2b301da1ab405bb5162485a1598a2960d1178b79
BLAKE2b-256 checksum
How to use checksums
19ed3a3e6e880e98d25314d1e4023ea2bb0f01216b3f32ad8509653722bf6b68
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.25-py3-none-any.whl

Download URL tranp-0.9.25-py3-none-any.whl
Size 313.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bda38577cb9e4f075b0766b7d2c0e5b4f577f9f17b087545ee08e7ec85c8071b
BLAKE2b-256 checksum
How to use checksums
f36afa84655384e2c198d93071188b9cfacca35c6f9c665d502523a04462b8f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for tranp 0.9.25

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page