ImportC can now automatically run the preprocessor

Adam Ruppe destructionator at gmail.com
Sun May 15 20:34:25 UTC 2022


On Sunday, 15 May 2022 at 17:44:48 UTC, Walter Bright wrote:
> On 5/15/2022 6:13 AM, Adam D Ruppe wrote:
>> http://dpldocs.info/experimental-docs/mixinc.html
>
> It's an interesting idea, but I'm a little unsure how it works. 
> Does it propose running D code through the C preprocessor? That 
> does not work, as the C preprocessor will fail with D tokens.

Of course not. The whole point is to keep this separate: the D 
code is just normal D code. The embedded C code is just a string 
- just like with a D mixin.

But the mixinC construct, instead of passing that string through 
the D parser, passes it through the C preprocessor and C parser.

So it would be similar to a compile time function:

Node mixinC(string c_code) {
     c_code = c_preprocess(c_code);
     return parse(c_code);
}


It must form a complete ast node - just like a D mixin. So you 
can't

mixinC("#define START {");
mixinC("START");
// other code here
}


That won't work, since you can't make an ast node out of {. Same 
as how `mixin("{")` fails to compile.

But a complete node inside the string can be done, parsed as C, 
then have its node injected into D.

Max Haughton has a proof of concept PR already.


(my full proposal also includes exposing the preprocessor state 
as an immutable D object, so you can introspect the macros and 
define an order of operation through the return value. But even 
the basic thing with implied global state might be a step forward)


More information about the Digitalmars-d mailing list