ImportC can now automatically run the preprocessor

rikki cattermole rikki at cattermole.co.nz
Mon May 16 04:03:22 UTC 2022


```d
void main() {
     import std.stdio;
     writeln("Starting application");

     mixinc(qc{
         #include <stdio.h>
         #include <string.h>
         #include <lua.h>
         #include <lauxlib.h>
         #include <lualib.h>

           char buff[256];
           int error;
           lua_State *L = lua_open();   /* opens Lua */
           luaopen_base(L);             /* opens the basic library */
           luaopen_table(L);            /* opens the table library */
           luaopen_io(L);               /* opens the I/O library */
           luaopen_string(L);           /* opens the string lib. */
           luaopen_math(L);             /* opens the math lib. */

           while (fgets(buff, sizeof(buff), stdin) != NULL) {
             error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
                     lua_pcall(L, 0, 0, 0);
             if (error) {
               fprintf(stderr, "%s", lua_tostring(L, -1));
               lua_pop(L, 1);  /* pop error message from the stack */
             }
           }

           lua_close(L);
     });

     writeln("Program end");
}
```

Copied the function body from[0].

Now imagine some awful macros that exist. It allows you to bridge C into 
D without creating new or modifying any C files.

This is the ultimate tool for porting C to D.

[0] https://www.lua.org/pil/24.1.html


More information about the Digitalmars-d mailing list