godot-gdscript-toolkit icon indicating copy to clipboard operation
godot-gdscript-toolkit copied to clipboard

Converting Python AST to GDScript

Open abitrolly opened this issue 2 years ago • 6 comments

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?

abitrolly avatar Jul 23 '22 07:07 abitrolly

For the transformation from Python to GDScript you need a Python parser rather than GDScript one. What you need to do is:

  1. Parse the python source file (e.g. using ast module)
  2. 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
  3. Run gdformat on the resulting GDScript to prettify it or gdparse 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

Scony avatar Jul 23 '22 12:07 Scony

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 avatar Jul 23 '22 12:07 abitrolly

@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.

Scony avatar Jul 23 '22 12:07 Scony

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 avatar Jul 24 '22 09:07 jtiai

@jtiai Python ast module does a pretty good job parsing Python code into AST. The problem is in convenient tools for converting AST tree.

abitrolly avatar Jul 24 '22 10:07 abitrolly

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.

jtiai avatar Jul 24 '22 10:07 jtiai