Thread overview
Why can't we transpile C++ to D?
Jun 10, 2021
Tejas
Jun 10, 2021
Imperatorn
Jun 10, 2021
evilrat
Jun 10, 2021
Dukc
Jun 10, 2021
Tejas
Jun 10, 2021
evilrat
Jun 10, 2021
Tejas
Jun 10, 2021
evilrat
Jun 10, 2021
sighoya
June 10, 2021

Sorry, I'm rather ignorant when it comes to this, but why can't we use 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?

June 10, 2021

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

June 10, 2021

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

float min(float l, float r)
{
   return (l < r) ? l : r;
}
float foo(float a, float b) {
   return (min)(a,b);
}

and this direct translation to D doesn't works

float foo(float a, float b) {
   return (min)(a,b); // Error: C style cast illegal, use `cast(min)(a , b)`
}

but this does

float foo(float a, float b) {
   return (&min)(a,b); // ok
}
June 10, 2021
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
June 10, 2021

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

June 10, 2021

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

June 10, 2021

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

June 10, 2021
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! :@
June 10, 2021

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