ImportC can now automatically run the preprocessor

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Mon May 16 11:02:48 UTC 2022


On Monday, 16 May 2022 at 09:52:21 UTC, deadalnix wrote:
> I don't understand where this is going at all. At first, I 
> thought that this was able being able to import c (as the name 
> would suggest), which seems super useful, but now it seems to 
> be evolving into a full blow C compiler and I have no idea why 
> I would want that. I already have several on my machine, and 
> they are extremely high quality.

Yes, but you need to be able to compile C in order to expand 
C-macros in D code.

It isn't all that difficult (but tedious to implement).

Possible implementation:

1. Tokenize the whole sourcefile before parsing, and mix 
tokenization with macro expansion. In this process you tag all 
non-macro tokens as D, tag all macro-originating tokens as C and 
all macro-parameter tokens as both (unless explicitly tagged as 
either C or D).

2. Study all the productions for C and D so that you know how to 
deal with lookahead.

3. Use a backtracking parser (e.g. Earley parser, but that might 
be overkill) and build the AST.

4. Resolve ambiguous symbols in post parsing, guided by weather 
the originating token was tagget as D, C or both.

This way you get to mix C and D, but also prevent people from 
writing macros containing D code.

As long as all C code can be expressed in the D AST you're good.



More information about the Digitalmars-d mailing list