4 ms·
> I've almost finished implementing C preprocessor in just one day. It's actually a port from my previous attempt to write a compiler. The preprocessor is fien
by WalterBright 7y ago
> I've almost finished implementing C preprocessor in just one day. It's actually a port from my previous attempt to write a compiler.
The preprocessor is fiendishly tricky to write. I wondered how he did it in such a short time :-) I had to scrap mine and reimplement it 3 times.
I wish I had had https://www.spinellis.gr/blog/20060626/cpp.algo.pdf https://www.spinellis.gr/blog/20060626/cpp.algo.pdf to work from.
- tandr 7y agoWalter, I think your Zortech compiler was one of the first (if not The First) to introduce register calling convention. I don't remember much, but how did you get around varargs implementation?
- WalterBright 7y agoSimply, varargs functions didn't use the register calling convention. Varargs is rarely used outside of printf, and so isn't worth the effort optimizing. Indeed, the Linux 64 bit ABI for varargs is hopelessly klunky, but it doesn't really matter.
- tandr 7y agoThank you.
- saagarjha 7y ago> Varargs is rarely used outside of printf, and so isn't worth the effort optimizing. open(2) is also called relatively frequently and AFAIK requires varargs if it’s implemented in C.
- im3w1l 7y agoIt's relatively frequent to see it in source code, but those calls are cold code. People aren't calling open(2) millions of times per second in their inner loop.
- tandr 7y agoFor the reference folks - this is coming from the guy who's wikipedia page [0] says "He was hired by Facebook to write a fast C/C++ preprocessor in D." Not that I have had any doubts about how hard it is... [0] https://en.wikipedia.org/wiki/Walter_Bright https://en.wikipedia.org/wiki/Walter_Bright
- WalterBright 7y agoHere's the source code: https://github.com/facebookarchive/warp https://github.com/facebookarchive/warp and the one for the C compiler I wrote long ago: https://github.com/DigitalMars/Compiler/blob/master/dm/src/dmc/dblklst.d https://github.com/DigitalMars/Compiler/blob/master/dm/src/d... which is integrated with the C lexer for speed reasons.
- snazz 7y agoHe also wrote D (or is credited with creating it). I’m also interested in how one would write a C preprocessor in a day. It’s got so many tricky edge cases and whatnot.
- WalterBright 7y ago> or is credited with creating it Full disclosure: I looked into the future with my Chronoscope, and copied the most popular language!
- rui314 7y agohttps://www.spinellis.gr/blog/20060626/cpp.algo.pdf https://www.spinellis.gr/blog/20060626/cpp.algo.pdf was actually very useful. With that doc, I could implement a preprocessor without thinking too much about how it should work in details. In that sense, even after I implemented a C preprocessor, I don't feel I fully understand how it works.