Some uses I've found for it:
* Generate long literal lists, with less tedium and fewer bugs:
snprintf(buf, sizeof buf,
"%f %f %f %f\n"
"%f %f %f %f\n"
"%f %f %f %f\n"
"%f %f %f %f\n"
"%f %f %f %f\n",
/* [[[cog
for i in range(20):
cog.outl(f"matrix[{i}]" + (',' if i != 19 else ''))
]]] */
matrix[0],
matrix[1],
(...)
* Automatically instantiate classes from a set of files: /* [[[cog
for path in glob('src/editor/Panel*.cc'):
name = stem(path)
instance = name[0].lower() + name[1:]
outl(f'{name} {instance};')
]]] */
PanelInspector inspector;
PanelGame game;
PanelTextures textures;
PanelModules modules;
PanelProject project;
PanelInput input;
PanelPerformance performance;
// [[[end]]]
* Given a list of Python strings, generate a C++ enum, and functions to convert it to and from a string.* Parse a C++ source file for struct definitions, and generate code to debug-dump them, inspect them in a GUI, save/load them... alright, Clang might be a better tool for this, but Cog will do in a pinch.
All this is stuff that a more expressive language could do without external tools, but even in such a language, you won't usually see what a macro is being expanded to without some sort of tooling. Cog puts it right there in the file for you, which I often appreciate.