www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - dmt: Python-like indentation in D programming language

reply Witold Baryluk <witold.baryluk+d gmail.com> writes:
Hi,

`dmt` is an old project of mine from around year 2006. I ported 
it recently from D1 to D2, and added some extra features and 
support for extra keywords, and fixed few bugs here and there.

`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

It is fun and easy to use, and maybe it would be of interested to 
you.

`example.dt`:

```d
def int f(int b):
     int y = 0
     foreach (i; 0..5):
       y += i * (i+b)
     return y

struct A:
     private:
     int a
     public:
     int b_ = 5
     def auto b()  property:
         return b_

def void main():
     import std
     writefln!"%s %s"(f(5), A())
```

```shell
$ DMD=ldc2 dmt -run example.dt
ldc2 -run example.d
80 A(0, 5)
$
```

All D programming language features are supported (including 
exception handling, if/else, switch/case/break, inline asm, 
attribute sections, goto). Converted code is human readable.

You can check more examples in the README.md and in `tests/` 
directory.

`dmt` is not yet self hosting, but that is probably the next 
step. :)

Enjoy.
Nov 16 2021
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 16 November 2021 at 21:58:24 UTC, Witold Baryluk 
wrote:
 Hi,

 `dmt` is an old project of mine from around year 2006. I ported 
 it recently from D1 to D2, and added some extra features and 
 support for extra keywords, and fixed few bugs here and there.

 [...]
Nice, remember to put it on dub
Nov 16 2021
prev sibling next sibling parent reply mw <mingwu gmail.com> writes:
On Tuesday, 16 November 2021 at 21:58:24 UTC, Witold Baryluk 
wrote:
 Hi,

 `dmt` is an old project of mine from around year 2006. I ported 
 it recently from D1 to D2, and added some extra features and 
 support for extra keywords, and fixed few bugs here and there.

 `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
To be frank, by just Python syntax alone I wasn't sure how many people want to try it. I'm wondering if it can translate existing Python code (e.g with a bit py3 type annotations) to D code, then it may attract much more users. What I have also in mind is numpy program to lib Mir.
Nov 17 2021
parent reply Witold Baryluk <witold.baryluk gmail.com> writes:
On Wednesday, 17 November 2021 at 08:28:07 UTC, mw wrote:
 To be frank, by just Python syntax alone I wasn't sure how many 
 people want to try it.
There are good reasons for it. 1) It enforces indentations, so code is harder to screw up. Also because you need to indent, it often is easier to spot too deep nesting, and fix it, instead of hiding it. 2) From my experience with porting few small programs (50-1000 lines) to `dmt`, the resulting code is shorter than original D code, by about 15% on average. This is mostly due to closing `}` being removed: 3) It might be of interest to people who know Python, but do not know C, C++ or Java.
 I'm wondering if it can translate existing Python code (e.g 
 with a bit py3 type annotations) to D code, then it may attract 
 much more users.
No. And not planned. Sparkling some `auto` here and there manually, makes it quite possible tho. I did port on Python program to `dmt` rather quickly. But of course if you use standard library, things are harder. Cheers.
Nov 17 2021
parent reply mw <mingwu gmail.com> writes:
On Wednesday, 17 November 2021 at 17:23:14 UTC, Witold Baryluk 
wrote:
 I'm wondering if it can translate existing Python code (e.g 
 with a bit py3 type annotations) to D code, then it may 
 attract much more users.
No. And not planned. Sparkling some `auto` here and there manually, makes it quite possible tho. I did port on Python program to `dmt` rather quickly. But of course if you use standard library, things are harder.
Just FYI: I found a working Python PEG grammar file here https://github.com/we-like-parsers/pegen/blob/main/data/python.gram it will be a great helper to to trans-compile Python to D. E.g. to try parse Python code and execute the parsed code: ``` git clone https://github.com/we-like-parsers/pegen cd pegen make demo ``` (I did that PR :-)
Mar 28 2022
parent Abdulhaq <alynch4047 gmail.com> writes:
On Monday, 28 March 2022 at 22:22:18 UTC, mw wrote:

 Just FYI: I found a working Python PEG grammar file here

 https://github.com/we-like-parsers/pegen/blob/main/data/python.gram

 it will be a great helper to to trans-compile Python to D.


 E.g. to try parse Python code and execute the parsed code:

 ```
 git clone https://github.com/we-like-parsers/pegen
 cd pegen
 make demo
 ```

 (I did that PR :-)
Transcompilers are fun, but a heads-up before you get too invested in this, you'll either need to restrict the python code to a particular subset of python, or accept that you'll be writing a python interpreter in D, and not transpiling.
Mar 29 2022
prev sibling next sibling parent reply JN <666total wp.pl> writes:
On Tuesday, 16 November 2021 at 21:58:24 UTC, Witold Baryluk 
wrote:
 Hi,

 `dmt` is an old project of mine from around year 2006. I ported 
 it recently from D1 to D2, and added some extra features and 
 support for extra keywords, and fixed few bugs here and there.

 `dmt` is a converter (offline or auto-invoking compiler after 
 conversion) from Python-like indention style to curly braces 
 for D programming language.
I love the idea. Never been a fan of braces to define blocks and I find them to be much more of a pain to match than whitespace. These days however, I just set dfmt to autorun on file save and never worry about formatting again.
Nov 17 2021
parent uranuz <neuranuz gmail.com> writes:
On Wednesday, 17 November 2021 at 11:00:46 UTC, JN wrote:
 On Tuesday, 16 November 2021 at 21:58:24 UTC, Witold Baryluk 
 wrote:
 Hi,

 `dmt` is an old project of mine from around year 2006. I 
 ported it recently from D1 to D2, and added some extra 
 features and support for extra keywords, and fixed few bugs 
 here and there.

 `dmt` is a converter (offline or auto-invoking compiler after 
 conversion) from Python-like indention style to curly braces 
 for D programming language.
I love the idea. Never been a fan of braces to define blocks and I find them to be much more of a pain to match than whitespace. These days however, I just set dfmt to autorun on file save and never worry about formatting again.
I am programming in Python mostly on my job and sometimes in C++ and JavaScript. Personally I like syntax with braces to define blocks more that Python's syntax without blocks. One of the reasons - why?1 Is that very often when I copy-paste some fragments of code then some editors sometimes just tries to autoindent programme and does it wrong. Then it breaks entirely and I get some errors from production just because of this. And just for aestetic reasons I like when blocks are explicitly delimitered by braces. Syntax with braces also allows write "one-liners" easier when it's needed. So personall I shall not use this in D
Nov 20 2021
prev sibling next sibling parent reply bauss <jj_1337 live.dk> writes:
On Tuesday, 16 November 2021 at 21:58:24 UTC, Witold Baryluk 
wrote:
 Hi,

 `dmt` is an old project of mine from around year 2006. I ported 
 it recently from D1 to D2, and added some extra features and 
 support for extra keywords, and fixed few bugs here and there.

 `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

 It is fun and easy to use, and maybe it would be of interested 
 to you.

 `example.dt`:

 ```d
 def int f(int b):
     int y = 0
     foreach (i; 0..5):
       y += i * (i+b)
     return y

 struct A:
     private:
     int a
     public:
     int b_ = 5
     def auto b()  property:
         return b_

 def void main():
     import std
     writefln!"%s %s"(f(5), A())
 ```

 ```shell
 $ DMD=ldc2 dmt -run example.dt
 ldc2 -run example.d
 80 A(0, 5)
 $
 ```

 All D programming language features are supported (including 
 exception handling, if/else, switch/case/break, inline asm, 
 attribute sections, goto). Converted code is human readable.

 You can check more examples in the README.md and in `tests/` 
 directory.

 `dmt` is not yet self hosting, but that is probably the next 
 step. :)

 Enjoy.
Isn't def redundant in this since D already declares types then you can assume if it's not enum, template, mixin template, class, struct or union then it must be a function if it also end with (...): Like: ```d int foo(): return 10 ``` You'd never be in doubt that foo was a function or that: ```d template Foo(T): alias Foo = T ``` Foo in this case was a template. Or like make def optional at least, because I see how it can help porting Python code, but it seems unnecessary if you're writing from scratch. Great project tho!
Nov 19 2021
parent Witold Baryluk <witold.baryluk+d gmail.com> writes:
On Friday, 19 November 2021 at 10:03:00 UTC, bauss wrote:
 Isn't def redundant in this since D already declares types then 
 you can assume if it's not enum, template, mixin template, 
 class, struct or union then it must be a function if it also 
 end with (...):

 Like:

     ```d
     int foo():
         return 10
     ```

 You'd never be in doubt that foo was a function or that:

     ```d
     template Foo(T):
         alias Foo = T
     ```

 Foo in this case was a template.
Technically yes, but I do not have full proper parser or grammar, to do the conversion in all possible cases. The main issue is with the context: `type name ` ... can be also a start of a variable with a lambda, even if it ends with `:`: ```d auto x = delegate(int y): return y * y ``` The return type itself can be a complex template instantiation. `F!(....) foo():` Same with argument types, they might be templates (or maybe even mixins?). Plus it does not necessarily ends with `(...):`, for example it can have attributes: `... (...) pure nothrow bar:` Plus of course all the other things like `static`, `public`, `extern(...)`, ... at the front. So yes, `dmt` is not perfect, but for now it still allows to use full feature set of D, and use simple rules for achieving these goals. If the tool gains some popularity, there is certainly a possibly of adding a proper grammar and parser, and making `def` on functions optional. Proper parser will also help in few other places, like multi line signature definitions, comments in the middle of the multi line definition, enum definitions and array literals. BTW. I forgot to add to my initial post, that `dmt` generates the `#line` directives, making compile errors and warning from `dmd`, `gdc` and `ldc` point back exactly to the proper input `.dt` file and original line number in it. And you can pass multiple file to `dmt` tool, including mix of `.dt` and non-`.dt` files (i.e. `.d`, `.o`) to link them all together by the compiler in expected fashion.
 Great project tho!
Thanks.
Nov 19 2021
prev sibling next sibling parent reply Marcone <marcone email.com> writes:
Your dmt.d not compile for exe in Windows with last version of 
dmd 2.099.0

dmt.d(71): Deprecation: Usage of the `body` keyword is 
deprecated. Use `do` instead.
dmt.d(125): Deprecation: Usage of the `body` keyword is 
deprecated. Use `do` instead.
dmt.d(206): Deprecation: Usage of the `body` keyword is 
deprecated. Use `do` instead.
dmt.d(234): Deprecation: Usage of the `body` keyword is 
deprecated. Use `do` instead.
dmt.d(212): Error: `pure` function `dmt.check_if_can_indent` 
cannot call impure function `dmt.strcmp_first2`
dmt.d(212): Error: ` nogc` function `dmt.check_if_can_indent` 
cannot call non- nogc function `dmt.strcmp_first2`
Apr 01 2022
parent Witold Baryluk <witold.baryluk+d gmail.com> writes:
On Friday, 1 April 2022 at 12:39:15 UTC, Marcone wrote:
 Your dmt.d not compile for exe in Windows with last version of 
 dmd 2.099.0

 dmt.d(71): Deprecation: Usage of the `body` keyword is 
 deprecated. Use `do` instead.
 dmt.d(125): Deprecation: Usage of the `body` keyword is 
 deprecated. Use `do` instead.
 dmt.d(206): Deprecation: Usage of the `body` keyword is 
 deprecated. Use `do` instead.
 dmt.d(234): Deprecation: Usage of the `body` keyword is 
 deprecated. Use `do` instead.
 dmt.d(212): Error: `pure` function `dmt.check_if_can_indent` 
 cannot call impure function `dmt.strcmp_first2`
 dmt.d(212): Error: ` nogc` function `dmt.check_if_can_indent` 
 cannot call non- nogc function `dmt.strcmp_first2`
Thanks for the report. Please next time open a github issue for such things, so I can spot them and fix quickly. The Error part was fixed in https://github.com/baryluk/dmt/commit/748de1d6726c0dbe97ba098b094588b4f13d7045 Something changed in the semantic analysis, that caused `strcmp_first2` to not be inffered correctly. Adding explicit attributes solves that. I am aware of `Deprecation: Usage of the body keyword is deprecated. Use do instead.`. This is intentional because gdc in some older Linux distributions (including Debian stable) do not recognize `do`, but they do `body`. Once the newer versions of `gdc` trickle to more distros (and Debian stable), I will switch it to use `do` and remove use of `body`. The deprecation warning is harmless otherwise.
Apr 23 2022
prev sibling parent Marcone <marcone email.com> writes:
I changed the body to do, and added pure  nogc and your program 
compiled. But I did not like it. I didn't find the program 
useful. I highly doubt this program will please D programmers. I 
have a request. Please make a version of your program that just 
makes the use of the semicolon (;) optional just like in 
JavaScript. This would be very useful for D programmers.
Apr 01 2022