Skip to main content

tranp (TRANspiler on Python)

test codecov MIT License

概要

  • 言語非依存(※1)のトランスパイルフレームワーク
  • 入力言語のAST(※2)を元に、出力言語のソースコードをレンダリング
  • トランスパイルのリアルタイム変換やシンボル解析、ASTの解析ツールを付属
  • ※1: 現状はPythonからC++への変換のみ実装
  • ※2: ASTの生成は外部ツールを利用(自由に変更可能)

必須要件

  • 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コードを入力した後、空行を入力するとトランスパイル結果がレンダリング
===============
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コードを入力することで、繰り返し結果を確認することが可能
===============
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が終了
===============
Python 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: data/grammar.lark
template_dirs:
  - data/cpp/template
trans_mapping: data/i18n.yml
input_globs:
  - app/**/*.py
output_dirs:
  - ./
output_language: cpp:h
exclude_patterns: []
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
  • トランスパイル結果
// app/main.h
// @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;
}
// app/sub.py
// @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がレンダリング
==========
Code here. Type `exit` to quit:
a = 1
==========
AST
----------
file_input
  assign
    assign_namelist
      var
        name  2
    number    1

シンボル解析ツール

$ tranp analyze

ライセンス

MIT

  • tranpを用いて生成したトランスパイル後のソースコードに関してはライセンスに含まれません

Metadata

Release files for tranp 0.9.8

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.8
File Size Uploaded
tranp-0.9.8.tar.gz 197.2 kB Details

Built distribution (wheel)

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

Total release size: 500.3 kB

Release files / tranp-0.9.8.tar.gz

Download URL tranp-0.9.8.tar.gz
Size 197.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c004868690e435f6c889208c276959bbf360e54f3a47c715cfa00d583f91644c
BLAKE2b-256 checksum
How to use checksums
488a63fa0af674e4d3eb62de189c5f4ba6cf9c1397c2993e55555aa43d7d7bf6
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.8-py3-none-any.whl

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

Release history Release notifications | RSS feed

0.9.9

2 release files

This release

0.9.8 This release

2 release files

0.9.7

2 release files

0.9.6

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

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