hn.today

Fun Extending the C Preprocessor

davidpriver.com5 points0 comments
Screenshot of Fun Extending the C Preprocessor

David Priver describes extensions he built into his C preprocessor while developing a personal C2Y compiler/REPL. He emphasizes that the preprocessor is a source-to-token translator rather than a simple text filter, then shows practical extensions that make macro development easier. Two debugging builtins, print(...) and where(...), send macro expansions and definition locations to stderr so developers can inspect macro behavior without dumping the entire translation unit. To avoid the classic, ugly macro-recursion tricks blocked by token "blue painting," he implemented a builtin map(macro, ...) that linearly applies a macro to every variadic argument with no fixed limit, demonstrated by mapping a PRINTINT macro across numeric and identifier arguments.

He also added VA_COUNT to return the number of variadic arguments directly, simplifying patterns like passing a count plus an array to functions (example: defining sum(...) as sum(VA_COUNT, (int[]){VA_ARGS})). These builtins eliminate complex preprocessor pumping, improve performance and readability, and reduce reliance on fragile macro hacks. All example code is public domain, and additional, deeper extensions - such as mutable preprocessor state - are hinted at for future exploration.

Read on davidpriver.com0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.