Consider rewriting AST instead
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.
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.
@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 ;)
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.
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
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()