Saturday, September 04, 2004

Bourne Shell

Ned's reference to the Spirit parser generator which uses a combination of C++ templates and operator overloading to allow you to create parsers reminded me of the Bourne Shell and the evils of C macros.

The Bourne shell has a syntax consciously modeled on Algol-68 syntax (e.g. do .. od, if .. fi). That's because its author, Steve Bourne, was involved with Algol-68 before he wrote the shell.

But it's not just the shell that uses Algol-68 syntax, its source code does too. Steve made heavy use of C macros to make C look like Algol-68. For example, look at the source code from V7 Unix here. Yuck. The macros that he used are defined here. This is probably one of the best examples of someone going too far with C macros.

Another example that comes to mind was a Math package that was written by someone at a startup I worked for years ago. The author went crazy with deeply nested macros to do all sorts of numeric operations: cross products, affine transforms, etc. In the end, the code was probably not any faster than what a decent compiler would generate and it was nearly impossible to debug the generated code