Jump to page: 1 2
Thread overview
Recommendations on porting Python to D
Apr 24
Tim
Apr 25
Sergey
Apr 25
mw
Apr 25
mw
15 hours ago
Chris Piker
15 hours ago
mw
Apr 24
Sergey
April 24

Hi D

I have a somewhat extensive CGI based web service written in Python and I'd like to port it to D. I can do this manually of course, and maybe that's the best way, but for a rough start, is anyone aware of any tools that generate an abstract syntax tree which could then be converted to somewhat equivalent D code? This might give me a jump-start on the manual conversion process. Then later I can work on removing the CGI dependency.

I'm aware that this wouldn't work in general due to all the third party modules typically used in python, but most of this code is self contained Python2 and doesn't depend on many imports.

I can just call my old C code from D, but the old Python is another story.

Thanks for any advice you may have,

April 24

On Wednesday, 24 April 2024 at 19:50:45 UTC, Chris Piker wrote:

>

I have a somewhat extensive CGI based web service written in Python and I'd like to port it to D. I can do this manually of course, and maybe that's the best way, but for a rough start, is anyone aware of any tools that generate an abstract syntax tree which could then be converted to somewhat equivalent D code?

My parser generator has an example with a grammar for Python: https://github.com/tim-dlang/dparsergen/tree/master/examples/python

April 24

On Wednesday, 24 April 2024 at 19:50:45 UTC, Chris Piker wrote:

>

is anyone aware of any tools that generate an abstract syntax tree which could then be converted to somewhat equivalent D code? This might give me a jump-start on the manual conversion process. Then later I can work on removing the CGI dependency.

I haven't used Python much in recent years, but my recollection is that Python 2 had an ast module that would spit out the ast for you.

April 24

On Wednesday, 24 April 2024 at 19:50:45 UTC, Chris Piker wrote:

>

Hi D

I have a somewhat extensive CGI based web service written in

There is also https://code.dlang.org/packages/arsd-official%3Acgi

April 24

On Wednesday, 24 April 2024 at 20:13:26 UTC, Lance Bachmeier wrote:

>

I haven't used Python much in recent years, but my recollection is that Python 2 had an ast module that would spit out the ast for you.

Thanks for the pointer! So I ran one of my modules through and generated an AST, and get results similar to:

Module(
   body=[
      Import(
         names=[
            alias(name='sys')]),
      FunctionDef(
         name='pout',
         args=arguments(
            posonlyargs=[],
            args=[
               arg(arg='item')],
            kwonlyargs=[],
            kw_defaults=[],
            defaults=[]),
         body=[

etc.

I presume I'll now need to write something that parses this into D source (maybe with the assistance of a module provided above). Before I do that, is this syntax general enough that a Python-AST to D source converter may already exist? Obvious searches in google and the D package index didn't turn up anything.

I have no background at all in working with ASTs, in fact my formal education is not even in CS, so I'm way outside my wheelhouse at this point.

April 25

On Wednesday, 24 April 2024 at 22:07:41 UTC, Chris Piker wrote:

>

Python-AST to D source converter may already exist?

Another possible way maybe is using C :)
Python -> C -> D
https://wiki.python.org/moin/PythonImplementations#Compilers

April 25

On Thursday, 25 April 2024 at 07:04:13 UTC, Sergey wrote:

>

On Wednesday, 24 April 2024 at 22:07:41 UTC, Chris Piker wrote:

>

Python-AST to D source converter may already exist?

Another possible way maybe is using C :)
Python -> C -> D
https://wiki.python.org/moin/PythonImplementations#Compilers

Thanks for the info, though I think going to a low-level language in the middle will make code that has almost no high level structure. Converting that back in to class-style D would likely take a while, in which case a manual port is a better option. Both python and D offer garbage collection so going through a non-GC language first would probably obfuscate the intended structure.

Maybe converting this AST to another AST format and then using a D code generator would be a better route.

So backing up, are there any AST -> D source generators in existence?

April 25

On Wednesday, 24 April 2024 at 22:07:41 UTC, Chris Piker wrote:

>

Python-AST to D source converter may already exist?

https://github.com/joortcom/eiffel_rename/tree/main/yi

A rudimentary converter from (extended) Python to D. Maybe you can use it as a starting point.

It uses: PEG parser generator for (standard) Python (to extend Python syntax):

https://github.com/we-like-parsers/pegen

Another thing you can try (but both the Python-like syntax and parser is home-made I think):

dmt is a converter (offline or auto-invoking compiler after conversion) from Python-like indention style to curly braces for D programming language.

https://github.com/baryluk/dmt

ref:

https://forum.dlang.org/thread/vtftlolshtrtwhlhgubn@forum.dlang.org?page=1

April 25

BTW, maybe you can also try Mojo:

https://github.com/modularml/mojo

April 26

On Wednesday, 24 April 2024 at 19:50:45 UTC, Chris Piker wrote:

>

Hi D

I have a somewhat extensive CGI based web service written in Python and I'd like to port it to D. I can do this manually of course, and maybe that's the best way, but for a rough start, is anyone aware of any tools that generate an abstract syntax tree which could then be converted to somewhat equivalent D code? This might give me a jump-start on the manual conversion process. Then later I can work on removing the CGI dependency.

I'm aware that this wouldn't work in general due to all the third party modules typically used in python, but most of this code is self contained Python2 and doesn't depend on many imports.

I can just call my old C code from D, but the old Python is another story.

Thanks for any advice you may have,

A strategy roughly along the lines of:

Test what you can, then port the tests, then just let chatgpt have at it can go further than one might reasonably expect (I have used chatgpt to convert to and from languages that don't even exist in public and it can basically get the gist of most things).

Treat it interactively rather than like a CLI tool, it must be said.

« First   ‹ Prev
1 2