www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why can't we transpile C++ to D?

reply Tejas <notrealemail gmail.com> writes:
Sorry, I'm rather ignorant when it comes to this, but why can't 
we use [pegged](https://github.com/PhilippeSigaud/Pegged) to 
transpile C++ code to D? Then we won't need a nogc compatible std 
library and so many other things could get easier, like getting 
legacy code to use Dlang. It might not be worth it for C+17 and 
beyond, but older codebases could benefit significantly, right?
Jun 10
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 10 June 2021 at 15:09:02 UTC, Tejas wrote:
 Sorry, I'm rather ignorant when it comes to this, but why can't 
 we use [pegged](https://github.com/PhilippeSigaud/Pegged) to 
 transpile C++ code to D? Then we won't need a nogc compatible 
 std library and so many other things could get easier, like 
 getting legacy code to use Dlang. It might not be worth it for 
 C+17 and beyond, but older codebases could benefit 
 significantly, right?
I'm guessing it's hard because of the grammar
Jun 10
parent evilrat <evilrat666 gmail.com> writes:
On Thursday, 10 June 2021 at 15:57:44 UTC, Imperatorn wrote:
 On Thursday, 10 June 2021 at 15:09:02 UTC, Tejas wrote:
 Sorry, I'm rather ignorant when it comes to this, but why 
 can't we use 
 [pegged](https://github.com/PhilippeSigaud/Pegged) to 
 transpile C++ code to D? Then we won't need a nogc compatible 
 std library and so many other things could get easier, like 
 getting legacy code to use Dlang. It might not be worth it for 
 C+17 and beyond, but older codebases could benefit 
 significantly, right?
I'm guessing it's hard because of the grammar
Well, still a grammar, though it was my little WTF moment. for example this C++ grammar(often used in STL) where min is simple minimum function ```cpp float min(float l, float r) { return (l < r) ? l : r; } ``` ```cpp float foo(float a, float b) { return (min)(a,b); } ``` and this direct translation to D doesn't works ```d float foo(float a, float b) { return (min)(a,b); // Error: C style cast illegal, use `cast(min)(a , b)` } ``` but this does ```d float foo(float a, float b) { return (&min)(a,b); // ok } ```
Jun 10
prev sibling next sibling parent reply Dukc <ajieskola gmail.com> writes:
On Thursday, 10 June 2021 at 15:09:02 UTC, Tejas wrote:
 Sorry, I'm rather ignorant when it comes to this, but why can't 
 we use [pegged](https://github.com/PhilippeSigaud/Pegged) to 
 transpile C++ code to D? Then we won't need a nogc compatible 
 std library and so many other things could get easier, like 
 getting legacy code to use Dlang. It might not be worth it for 
 C+17 and beyond, but older codebases could benefit 
 significantly, right?
This is article is about transpiling old C++ to newer C++, but the same problem applies with translating C++ to D: https://scottmeyers.blogspot.com/2015/11/the-brick-wall-of-c-source-code.html
Jun 10
parent Tejas <notrealemail gmail.com> writes:
On Thursday, 10 June 2021 at 16:49:59 UTC, Dukc wrote:
 On Thursday, 10 June 2021 at 15:09:02 UTC, Tejas wrote:
 Sorry, I'm rather ignorant when it comes to this, but why 
 can't we use 
 [pegged](https://github.com/PhilippeSigaud/Pegged) to 
 transpile C++ code to D? Then we won't need a nogc compatible 
 std library and so many other things could get easier, like 
 getting legacy code to use Dlang. It might not be worth it for 
 C+17 and beyond, but older codebases could benefit 
 significantly, right?
This is article is about transpiling old C++ to newer C++, but the same problem applies with translating C++ to D: https://scottmeyers.blogspot.com/2015/11/the-brick-wall-of-c-source-code.html
Damn that preprocessor! :
Jun 10
prev sibling next sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Thursday, 10 June 2021 at 15:09:02 UTC, Tejas wrote:
 Sorry, I'm rather ignorant when it comes to this, but why can't 
 we use [pegged](https://github.com/PhilippeSigaud/Pegged) to 
 transpile C++ code to D? Then we won't need a nogc compatible 
 std library and so many other things could get easier, like 
 getting legacy code to use Dlang. It might not be worth it for 
 C+17 and beyond, but older codebases could benefit 
 significantly, right?
I leave this here, it does converts C++ to D to some extent https://github.com/Superbelko/ohmygentool
Jun 10
parent reply Tejas <notrealemail gmail.com> writes:
On Thursday, 10 June 2021 at 16:50:41 UTC, evilrat wrote:
 On Thursday, 10 June 2021 at 15:09:02 UTC, Tejas wrote:
 Sorry, I'm rather ignorant when it comes to this, but why 
 can't we use 
 [pegged](https://github.com/PhilippeSigaud/Pegged) to 
 transpile C++ code to D? Then we won't need a nogc compatible 
 std library and so many other things could get easier, like 
 getting legacy code to use Dlang. It might not be worth it for 
 C+17 and beyond, but older codebases could benefit 
 significantly, right?
I leave this here, it does converts C++ to D to some extent https://github.com/Superbelko/ohmygento
Yes I saw the announcement earlier. I was hoping for something native, rather than bindings. As you said youself, it's not exactly stable. Plus I don't want a hamster's death on my conscience. :( Please don't take this as me belittling your work, it is better than having nothing. But how scalable will this be? We have to get real D code to enrich our ambiguously-defined-small ecosystem.
Jun 10
parent evilrat <evilrat666 gmail.com> writes:
On Thursday, 10 June 2021 at 19:06:42 UTC, Tejas wrote:
 But how scalable will this be? We have to get real D code to 
 enrich our ambiguously-defined-small ecosystem.
It says bindings generator, but it has to convert the code keeping the semantic as close as possible. It does direct translation on AST level (AST-to-source currently), there is quirks here and there, semantic discrepancies(for example sizeof/alignof differs in D vs C++), lack of struct inheritance and multiple inheritance, and other annoying stuff like implicit casts or implicit constructors, but overall this should work. Definitely better than doing it all by hand. I tried to run it on clang, phew, got 241k lines, and that's without .cpp files. Haven't even bothered with fixing it to compilable state (even then it will definitely have linking issues).
Jun 10
prev sibling parent sighoya <sighoya gmail.com> writes:
On Thursday, 10 June 2021 at 15:09:02 UTC, Tejas wrote:
 Sorry, I'm rather ignorant when it comes to this, but why can't 
 we use [pegged](https://github.com/PhilippeSigaud/Pegged) to 
 transpile C++ code to D?
See https://stackoverflow.com/questions/14589346/is-c-context-free-or-context-sensitive#answer-14589567 Mixing semantic with parsing isn't necessarily a good idea.
 Then we won't need a nogc compatible std library and so many 
 other things could get easier, like getting legacy code to use 
 Dlang.
That doesn't solve the ABI compatibility to C++, only if source is available, but they may be compiled with compilers which can't build anymore with the current tool chain at least without to rebuild the tool chain.
 It might not be worth it for C+17 and beyond, but older 
 codebases could benefit significantly, right?
The problem is that old looking code doesn't become modern when it is transpired to D as the concepts to do things have changed.
Jun 10