Peter Stuifzand

A code generator

At the company, where I’m doing my internship, I needed a code generator for some C code. There was some C code needed. So I started of writing it. After some lines, there started to show some patterns, which could be easily handled by some description language.

The first description language was a line based format. First a line with the name of the object. And after that the names of the functions. This format is easy to parse and easy to use. The output was done with print statements.

For code generation I always used this method. But why should the webapplications have all the template fun? So I downloaded Template (which is the Template Toolkit), from the CPAN and I tried some things with it. It was really easy to generate code like this. The code was also much cleaner and shorter.

All nice and easy, but not good enough, because it should be possible to add C code to the functions. I almost started to try and create regular expressions for this task, but then the laziness started to kick in again. I remembered something about a Text::Balanced, which is written by Damian Conway. I can extract delimeted text from strings. Which was exactly what I wanted to do.

The code is again much smaller, and writing the regexen would be the wrong way to go. In this case Text::Balanced is the middle ground between regexen and some parser written with something like Parse::RecDescent. Laziness + CPAN is a good thing.

© 2023 Peter Stuifzand