Re: Recommendations on porting Python to D

2024-04-25 Thread max haughton via Digitalmars-d-learn

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.





Re: Recommendations on porting Python to D

2024-04-25 Thread mw via Digitalmars-d-learn

BTW, maybe you can also try Mojo:

https://github.com/modularml/mojo




Re: Recommendations on porting Python to D

2024-04-25 Thread mw via Digitalmars-d-learn

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/vtftlolshtrtwhlhg...@forum.dlang.org?page=1


Re: Recommendations on porting Python to D

2024-04-25 Thread Chris Piker via Digitalmars-d-learn

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?


Re: How to make project with main application and cli application in the same folder?

2024-04-25 Thread alex via Digitalmars-d-learn

On Sunday, 21 April 2024 at 16:41:08 UTC, Mike Parker wrote:

On Sunday, 21 April 2024 at 08:44:38 UTC, alex wrote:
Hi guys. Trying to play with vibe-d and want to create 
separate web app, and cli app which can add admin users. When 
I just keep both files app.d and cli.d in source folder, I get 
an error that I can't have more then 1 main function.




You can do this using configurations. Whichever you list first 
will be the default. Then you can use `-c configName` or 
`--config=configName` to build the other one.


You'll want to exclude one of the main functions when building 
the configuration to which it doesn't belong. You can do that 
with version specifications (e.g., add a `cli` version in the 
cli configuration, then `vesrion(cli) void main {...}` in the 
code). Alternatively, if the files the main functions are in 
are self-contained, then you can just exclude the one you don't 
need in each configuration with the `excludeSourceFiles` 
directive.


Configurations:
https://dub.pm/dub-guide/recipe/#configurations


Thanks, that really helped me.

For everyone who has the same trouble I can attach my working 
solution based on sub packages and configurations. Here is my 
dub.json:


```json
{
"name": "cool",
"subPackages": [
{
"name": "app",
"sourcePaths": ["source"],
"mainSourceFile": "source/app.d",
"targetType": "executable",
"configurations": [
{
"name": "app",
"targetType": "executable",
"versions": ["app"]
}
]
},
{
"name": "cli",
"sourcePaths": ["source"],
"mainSourceFile": "source/cli.d",
"targetType": "executable",
"configurations": [
{
"name": "cli",
"targetType": "executable",
"versions": ["cli"]
}
]
}
]
}
```

Main functions:
app.d:
```d
  version(app)
  void main() {...}
```

cli.d:
```d
  version(cli)
  void main() {...}
```

And here how I run/build it:
```make

dub run :cli
dub run :app

```
OR
```make

dub build :cli
dub build :app

```

During building, it will create executable like 
"{name_of_project}_app" or "{name_of_project}_cli".


Re: Recommendations on porting Python to D

2024-04-25 Thread Sergey via Digitalmars-d-learn

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