Skip to content

Comment on Using the C preprocessor as an HTML templating engine

Comments

IIRC the earliest version of the C++ language, before a C++ compiler existed, was implemented with C and the M4 preprocessor.

https://en.wikipedia.org/wiki/M4_(computer_language)

https://www.gnu.org/software/m4/

Thank you, I did not know this. I was about to comment and say: Why not go with m4(1)? I have used it for a personal homepage and while it certainly will try to stab you in the back like any old macro processor will, it is more full-fledged that its C counterpart and will also allow to to smugly state that your homepage is POSIX compliant [1]. There are a few guides out there from around 2000 to get you started, but at some point one needs to read the original paper [2] and let your imagination run wild.

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/m...

[2]: https://maibriz.de/unix/ultrix/etc/m4.pdf

one of the first things i did with html was to attempt to use m4 to simplify my personal home page. not only did that not work, it backfired to an incredible degree, but that's because i was using proprietary vendor m4. gnu m4 has a couple of features that make it significantly less error-prone:

1. built-in macro names that require arguments such as `define' are not "expanded" if you don't follow them with parentheses

2. there's an option to require a magic sigil character to invoke macros, such as % or something; this turns out to have been required in the original gpm and its bell labs clone m6. this helps enormously with complicated macros

however, the fact that the output of your macro is always immediately run as new code (as opposed to m6 and gpm, where this was optional on a per-call basis) makes m4 just ridiculously error-prone. i feel like trauma from debugging m4 macros was a major consideration in the design of the weak-ass c preprocessor

1. built-in macro names that require arguments such as `define' are not "expanded" if you don't follow them with parentheses

This one is pure pain. Thankfully, the only implementation I have encountered that does this is Heirloom's [1]. I doubt POSIX will do it, but I wish they would just define this behaviour out of existence for the built-ins as it is a minefield when trying to write prose (or comments, for that matter).

[1]: https://heirloom.sourceforge.net

As for outputs being interpreted iteratively, there is no way around it that I know of. To make invocation more explicit, I have just settled on prefixing macros with an underscore and it has worked well so far as it is uncommon in English text, HTML, and CSS.

cfront was the actual compiler; it was written in c++, not m4

cfront was a C++ => C transpiler

woah, that's a really fun tidbit.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.