godot-gdscript-toolkit
godot-gdscript-toolkit copied to clipboard
Converting Python AST to GDScript
I understand that it is not possible to port all Python features to GDScript, but for simple functions implementing algorithms without external dependencies, is it possible to use https://github.com/Scony/godot-gdscript-toolkit/wiki/2.-Parser to reconstruct GDScript AST from Python AST?
For the transformation from Python to GDScript you need a Python parser rather than GDScript one. What you need to do is:
- Parse the python source file (e.g. using
ast
module) - Walk the resulting python AST and create GDScript out of it (one, big, multiline string) - please note you don't have to care about long lines and stuff as you can use
gdformat
on the output from this step - Run
gdformat
on the resulting GDScript to prettify it orgdparse
to create GDScript AST
The above should not be that complex. I'm doing quite a similar thing but in opposite direction (creating Python code from the GDScript code) here: https://github.com/Scony/godot-gdscript-toolkit/blob/master/gdtoolkit/gd2py/init.py
I wonder if there are any best practices for matching parts of AST, so that the conversion could be done at least partially in a more declarative way. Or it just doesn't exist, and the only way that works is walking it manually with if instructions?
@abitrolly I don't think comparing ASTs of different languages is a good idea - this would be very sensitive to the grammar differences. Actually, if you think about it, even if you're working on the same language, different ASTs obtained from different grammars may vary a lot. I don't think that's the way to go.
There exists excellent pyparsing library to do text file parsing (I have written couple of interpreters with that). Maybe trying to make gdscript parsing library with using that? I could give a shot if there is enough interest.
There also exists EBNF for grammar: https://docs.godotengine.org/en/stable/development/file_formats/gdscript_grammar.html which makes writing the parser easier (provided that reference grammar doesn't have bugs)
@jtiai Python ast
module does a pretty good job parsing Python code into AST. The problem is in convenient tools for converting AST tree.
That's why I have used pyparsing numerous times for parsing. You can build easily any kind of text parsers with pythonic BNF like format. And then do what ever you want to do with results. Build AST or do something on the fly.