This post is the third in my series discussing automatic source code generation. In this post, I’m going to present a Matlab tool for integrating generated code into your hand written source.
There are many different ways to mix machine and hand generated source code. By far the most common is the C Preprocessor. The preprocessor command
#define does a simple string replacement, while
#include copies a full file into your source. Integrating tools such as the Preprocessor into your code generation tool stack is simple for many languages. I use it quite extensively for generating code when the required data manipulation does not call for a more flexible programming language.
Another common method is to use a template system. C++ has a full template library for compiler supported code generation. There are lots of HTML related systems that use templates too.
I personally do not like using either of these methods when generating code that requires a lot of mathematical manipulations, often required for control systems development. Both of these techniques have the same pair of shortcomings. First, the hand and machine generated source are in completely separate files. This separation breaks up the logical flow of the code. Second, the developer never sees the final combined source. While not insurmountable, this does complicate debugging.
A preferable a system will inject the machine generated code directly into the hand generated source. I’ve developed two different Matlab tools to do this. The first tool, completely of my own creation, worked but there was a large disconnect between the hand generated source and the Matlab code used for the generation. This disconnect made it difficult to track the origin of generated code, and left me generally unsatisfied.
A few months ago, I was lucky enough to stumble upon a Python code generation tool by Ned Batchelder called “Cog”. As Ned describes on the Cog web page:
Cog transforms files in a very simple way: it finds chunks of Python code embedded in them, executes the Python code, and inserts its output back into the original file. The file can contain whatever text you like around the Python code.
The beauty of this system is that it directly integrates the source of the generator language into the source of the generated language. This completely solves the build integration and tag-to-generator disconnect problems. If you’re a Python programmer (or Perl, Ruby or PHP) programmer, I highly recommend you check out the Cog web page.
If you look on my Matlab Tools page, you’ll find my Matlab knockoff of Cog. I’ve used this tool for both C and for LaTeX code generation, and it seems to work quite well. It has a few limitations, but I created it to be source file language independent. It is now my tool of choice for integrating Matlab generated into the source of any of my projects.