Thread overview
Pure D frontend as library.
Dec 26, 2022
Alexandru Ermicioi
Dec 27, 2022
Alexandru Ermicioi
Dec 27, 2022
Johan
Dec 27, 2022
Stefan Koch
December 26, 2022

Hi team,

I'd like to ask a lazy question:
How easy is to use D compiler frontend without backend?

How complicated would be to write a transpiler, and from which files should you start modifications?

I'm wondering if something like https://typescripttolua.github.io/ could be done, but with d as language.

From my limited knowledge, I should have an AST visitor that transpiles to target language, and some entry point for application which configures D frontend to use my AST visitor to generate code. I've no idea from where to start. If you know some documentation or tutorials, that would be appreciated.

Thanks,
Alexandru.

December 27, 2022
On 27/12/2022 8:13 AM, Alexandru Ermicioi wrote:
> How easy is to use D compiler frontend without backend?

Easy. https://github.com/dlang/dmd/blob/master/compiler/test/dub_package/frontend.d

> How complicated would be to write a transpiler, and from which files should you start modifications?

That on the other hand... Yeah, things aren't great on that front. The thing you want to implement is what we call glue code and isn't really setup right now for this (nobody has tried like this, ignoring ldc/gdc as they modify it).
December 27, 2022
On Monday, 26 December 2022 at 23:08:59 UTC, Richard (Rikki) Andrew Cattermole wrote:
>...
>
> That on the other hand... Yeah, things aren't great on that front. The thing you want to implement is what we call glue code and isn't really setup right now for this (nobody has tried like this, ignoring ldc/gdc as they modify it).

Hi, thx for information.

Any idea from which file should I start at least learning about this glue code?

Also, since this is not yet done, is there a way to insert this custom glue code without modifying D frontend source code?
If not which file should I register custom changes?

Thanks,
Alexandru.
December 27, 2022
On 27/12/2022 9:34 PM, Alexandru Ermicioi wrote:
> Any idea from which file should I start at least learning about this glue code?

You can look at dmd's... but realistically the work hasn't been done at that level to make it easy to work with.

https://github.com/dlang/dmd/blob/master/compiler/src/dmd/glue.d

https://github.com/dlang/dmd/blob/master/compiler/src/dmd/gluelayer.d

Note how it doesn't let you use your own implementation. This is very much throwing you into the deep end without any help.
December 27, 2022

On Monday, 26 December 2022 at 19:13:01 UTC, Alexandru Ermicioi wrote:

>

Hi team,

I'd like to ask a lazy question:
How easy is to use D compiler frontend without backend?

How complicated would be to write a transpiler, and from which files should you start modifications?

I'm wondering if something like https://typescripttolua.github.io/ could be done, but with d as language.

From my limited knowledge, I should have an AST visitor that transpiles to target language, and some entry point for application which configures D frontend to use my AST visitor to generate code. I've no idea from where to start. If you know some documentation or tutorials, that would be appreciated.

You can have a look at LDC's code, which does what you are saying.
LDC first initializes itself (cppmain()) and then calls mars_mainBody (https://github.com/ldc-developers/ldc/blob/e5c97c6468334c65130a654c8aec819c51dd61d3/dmd/mars.d#L197) which reads source code files, does semantic analysis (create AST; note that there is a ton of calls needed to complete SeMa), and finally outputs object code.

I'd start by copying the initialization and SeMa parts and stopping before the codegen part, removing all things that you think you will not need.
The difficult thing is interpreting the AST. LDC does not use a visitor, don't get stuck on that idea, you don't necessarily need it. Documentation of the AST is not great; to discover what things mean / what needs to be done, consult the handling of AST nodes by LDC or DMD's glue layers.
Of course you don't have to implement everything at the start. Just get function definitions, function calls, and string definitions going first --> that's what your "hello world" needs ;)

I have never looked at GDC's code, but I presume it is quite similar in this regard to LDC, so you can look at that too.

-Johan

December 27, 2022

On Tuesday, 27 December 2022 at 12:22:45 UTC, Johan wrote:
does semantic analysis (create AST; note that there is a ton of calls needed to complete SeMa), and finally outputs object code.

If you want to capitalize the word use SemA. ;)