On Wed, 21 Sep 2011 22:33:06 +0100, Dmitry Olshansky <[email protected]> wrote:
On 22.09.2011 1:14, Gor F. Gyolchanyan wrote:
I had an idea of a D library for including C headers for a while now.
All i need is to make a compile-time C parser for that. This thing would literally
remove any need for binding.

Translating header file is a one-time job, as in
sh-$: translate fancy.h fancy.di

Why would you need to do it at compile time, except because it's fancy :), I don't quite get.

And, as much as I'm sorry to bring bad news, you'd have to make a C preprocessor as well (or even in the first place). Then you'd have to struggle through vendor extensions but that's doable.

At one time (2+ years back) I started writing a C lexer, and then C preprocessor in D. In part to learn about how compilers work and in part to convert C headers to D (there was no .di at that stage) so I could interface C. The lexer was no trouble, I even managed to make it flexible by having it read a C grammar file but when I got to the preprocessor I lost steam/momentum and it all fell by the way side.

Something I discovered, which may help bootstrap your plans, is that most C compilers will preprocess source for you and give you the resulting stream of text, which you can then lex/parse/etc. However, this results in the C compiler processing macros and following includes, which you often don't actually want it to do - as you're likely trying to replicate the file tree (so want to see includes) and trying to replace macros with CTFE or similar.

So, perhaps a combined approach, tame a compiler and have it preprocess a file at a time and then use that output, plus the original file to produce some D replacement code.. not sure if that would work but it might be worth investigating.

R


--
Using Opera's revolutionary email client: http://www.opera.com/mail/

Reply via email to