pycom icon indicating copy to clipboard operation
pycom copied to clipboard

Consider rewriting AST instead

Open adsharma opened this issue 3 years ago • 5 comments

The approach seems to be to tokenize python source and match strings. Why not parse python into an AST and write a visitor to generate code in the target language?

https://github.com/py2many/py2many has several years of work behind it and uses this approach.

adsharma avatar Jul 29 '22 05:07 adsharma

I created the feature branch for this based on the issue. I'll take a look at how well it'll integrate with the current logic.

riebecj avatar Jul 29 '22 13:07 riebecj

@adsharma This is a good idea than to just transpile every bit of code word-for-word. What might be needed is a target-aware compiler to properly structure the target code.

I just gave py2many a try (tested with C++) and the output is not very much correct but is not that bad too.

Input code

print("Hello World")

Output code (c++)

#include <iostream>  // NOLINT(build/include_order)
std::cout << std::string{"Hello World"};
std::cout << std::endl;

Not too bad thought ;)

JosiasAurel avatar Jul 31 '22 09:07 JosiasAurel

Yeah it expects a main program like this one:

https://github.com/py2many/py2many/blob/main/tests/cases/print.py

All the test cases in that directory are transpiled to supported target languages and verified to produce the same output as the python code.

adsharma avatar Aug 05 '22 05:08 adsharma

Yeah it expects a main program like this one:

https://github.com/py2many/py2many/blob/main/tests/cases/print.py

All the test cases in that directory are transpiled to supported target languages and verified to produce the same output as the python code.

Okay I get that... Though I still think it will be good to put in place some defaults for some situation like the one I mentioned above

JosiasAurel avatar Aug 05 '22 09:08 JosiasAurel

So the suggestion is to automatically add a main() function when bare statements are encountered? There is such a rewriter. But it's complicated by cases such as:

print("foo")
def f():
   ...
print("bar")

In many target languages this will be broken by inserting a main()

adsharma avatar Aug 08 '22 15:08 adsharma