ImportC can now automatically run the preprocessor
max haughton
maxhaton at gmail.com
Sun May 15 21:28:17 UTC 2022
On Sunday, 15 May 2022 at 20:34:25 UTC, Adam Ruppe wrote:
> 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)
So far it looks like mixinC is semantically functional, the only
slight hiccup with the scheme is that currently you can't have a
C struct initializer as the input to one so you may need to use a
mixinC declaration when in C an "expression" on the RHS would
have sufficed.
A large subset of macros will be simple enough to turn into D
ASTs directly. If it can't be then the identifier could he
recognized and an error message shown to the user. I say this
directly because so far ImportC ignores things it does not
understand which will have to stop at some point.
More information about the Digitalmars-d
mailing list