Pure D frontend as library.

Johan j at j.nl
Tue Dec 27 12:22:45 UTC 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


More information about the Digitalmars-d-learn mailing list