Mixing Hand-Written, Generating, and Generated Code with Koog

Some days ago I released a little code generation utility that I have been using for well over a year in cranking out repetitive C++ code. Koog is—for lack of a better established term—a mixed-code generator. It is similar to other tools of its kind (such as Cog), but the only one that I know of that uses the Scheme language for specifying what code to generate.

The concept of mixed-code generation is simple. You generally maintain a source code file by editing it manually, but you wish to generate some part or parts of the code. You mark those code sections with markers recognized by the code generator, and you also include (potentially foreign) code that produces the code to go between those markers. To avoid confusing the host language compiler, you make sure that the markers and the code generation directives have syntax such that they are treated as comments in the host language. These comments are generally retained, so that the code can be regenerated when the code generation directive or data affecting it changes.

A simple if somewhat pointless example of this is the following snippet of C/C++/Java/etc. code:

int x = /***koog (display (+ 1 2)) ***//***end***/;

When Koog is invoked on the above input, it expands to:

int x = /***koog (display (+ 1 2)) ***/3/***end***/;

I largely use the tool interactively under Emacs, with custom libraries for generating code for some of the Symbian C++ idioms.

See the Koog homepage to download the tool, or to find out more about it.

Note (18 Jan 2018): By now there is a more recent version of Koog that supports additional comment formats. You might want to see its homepage instead.