I want to make C ++ code from Python code!

Google has published Grumpy to convert Python code to Go.

Google Open Source Blog: Grumpy: Go running Python! google/grumpy: Grumpy is a Python to Go source code transcompiler and runtime.

Python has an ast module as standard, which allows direct access to parsing results.

import ast
node = ast.parse('''
def main() -> int:
    print("Hello World!")
    return 0
''')

print(ast.dump(node))
# Module(body=[FunctionDef(name='main', args=arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Str(s='Hello World!')], keywords=[])), Return(value=Num(n=0))], decorator_list=[], returns=Name(id='int', ctx=Load()))])

It seems that Grumpy also generates code while visiting the syntax tree with NodeVisitor. If you read it roughly, it seems that the result of visiting with Visitor is returned as a character string.

py2cpp

https://github.com/mugwort-rc/py2cpp

I remembered that I was working on a tool called py2cpp a while ago because I wanted to do the same thing in C ++.

Personally, when writing porting code, I often read it as the promised syntax of the porting language, but I was worried about how to express that processing, and when I visited with NodeVisitor, Transformer (preprocessing) and Hook (post processing) ), And output the wrapped Node so that it can be kneaded in various ways later. (Output tuple with std :: make_tuple, replace the ** operator with std :: pow ...)

Hello World

$ cat samples/helloworld.py
def main() -> int:
    print("Hello World!")
    return 0

$ python -m py2cpp samples/helloworld.py
#include "py2cpp/py2cpp.hpp"

int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
}

range-based for

$ cat samples/range.py
def main() -> int:
    x = 0
    for i in range(100):
        x += i
    print(x)
    return 0

$ python -m py2cpp samples/range.py
#include "py2cpp/py2cpp.hpp"

int main() {
    x = 0;
    for (auto i : py2cpp::range(100)) {
        x += i;
    }
    std::cout << x << std::endl;
    return 0;
}

The problem is that the first x = 0 type is missing, but other than that, I think that C ++ code that does not feel strange is being spit out. I will give information on the type information later, or I think that auto is sufficient for this part, so I would like to improve it.

py2cpp :: range is an alias that calls boost :: irange using Variadic Templates.

Type hint

$ cat samples/add.py
def add(x: float, y: float) -> float:
    return x + y

def main() -> int:
    print("2.0 + 3.0 =", add(2.0, 3.0))
    return 0

$ python -m py2cpp samples/add.py
#include "py2cpp/py2cpp.hpp"

double add(double x, double y) {
    return x + y;
}

int main() {
    std::cout << "2.0 + 3.0 =" << add(2.0, 3.0) << std::endl;
    return 0;
}

Introduced PEP483 type hints from Python 3.5.

This allows you to add type information to function arguments and return values.

Isn't it behaving pretty well?

If it is a program with simple logic, it may be near the future to write it in Python, convert it to C ++, and call it in Python using boost.python or [pybind11] 4.

license

py2cpp itself is published under GPLv3.

The products output using py2cpp are ** not GPL-infected, just like the GCC products **.

Py2cpp.hpp under include is published under Boost Software License.

Looking for Pururiku

I just remembered that I had a lot of things to do, such as determining the type and implementing the built-in function, so if you want to make Python code C ++ like me, edit it for a while and send a pull request. I would appreciate it if you could. It would be helpful if Pururiku is in Japanese. As you can see from Git's comments, English is inconvenient ...

reference

[Bookworm: Google announces Grumpy, Python implementation by Go] 3

Recommended Posts

I want to make C ++ code from Python code!
I want to use jar from python
I want to make a parameter list from CloudFormation code (yaml)
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to make a game with Python
I want to make fits from my head
I want to use ceres solver from python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to write in Python! (1) Code format check
[Python3] I want to generate harassment names from Japanese!
[Python] I want to make a nested list a tuple
I felt that I ported the Python code to C ++ 98.
Execute Python code from C # GUI
I want to debug with Python
I want to start a lot of processes from python
I want to send a message from Python to LINE Bot
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I want to build a Python environment
I want to play with aws with python
I want to make an automation program!
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to make a generator that generates a C # container class from CSV with Python
I want to make matplotlib a dark theme
I want to do Dunnett's test in Python
I want to use MATLAB feval with python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
I want to memoize including Python keyword arguments
Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
I want to create a window in Python
Rewrite Python2 code to Python3 (2to3)
I want to perform SageMaker inference from PHP
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
I want to merge nested dicts in Python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
What I did when updating from Python 2.6 to 2.7
I want to sell Mercari by scraping python
I want to write to a file with Python
I want to format and check Python code to my liking on VS Code
I want to display the progress in Python!
I want to make a web application using React and Python flask
I tried to execute Python code from .Net using Pythonnet (Hallo World edition)
I want to see the file name from DataLoader
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to detect images of cats from Instagram
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to handle optimization with python and cplex
Machine learning python code summary (updated from time to time)
[Python] I want to merge Excel files anyway (pandas.merge)
I want to write in Python! (2) Let's write a test
Continuation ・ I tried to make Slackbot after studying Python3
I wanted to use the Python library from MATLAB
I want to randomly sample a file in Python
I want to work with a robot in python.
How to make a Python package using VS Code
I want to write in Python! (3) Utilize the mock