m4 all the things

I've been solving problems with m4 lately:

  • Code generation for service definitions
  • Generate boilerplate for database access
  • Generate this blog from a DSL

For quick DSL's, m4 is great. The DSL looks like simple function calls, and is much more succinct than the same DSL in Ruby (the favorite language for DSL's today). It also ships on every *nix, no need for a special runtime, or packages for that runtime (with all the versioning/dependency issues that come with it).

Oh, there are downsides:

  • Errors in quoting cause macros to be evaluated too early or too late (or never), and are hard to debug. This places bounds on your DSL's complexity.
  • Commas inside a macro definition can prove equally vexing, often causing your string to be interpreted as two strings

I'm whipping up a static blog generator for this blog, which is why it's low on features right now. I think there's a case for always authoring this way, even if I want to move back to a dynamic blog engine (e.g. WordPress and friends). That case is portability. It will be trivial to add an m4 script later which transforms my blog posts into SQL 'upsert' statements instead of static pages. I'll always be able to migrate the blog easily to any platform.

I can think of a few other uses:

  • Authoring game content
  • Generating documentation about an API
...and I'm sure you can think of others.