The inspiration was mainly Clarion template language with beautiful "embed/emit" commands. This feature actually mimics human activity during code writing: author (programmer - alive code generator) never writes text linearly, he rather moves over files inserting pieces of code here and there.
This is illustrated in following example, showing what one does when adding new command line option to an application:
Using the template showed below, one can do all the things with one function call.
Adding option | |
---|---|
@function opt( type, var, letter, help ) @emit "variables" $type $var; @emit "switch" case $letter: @if ( $type == "int" ) $var = atoi(argv[ i ]); @else $var = argv[ i ]; @endif break; @emit "help" "$help" @endfunction | 1. Define global variable 2. Add branch in switch command ... depending on type of the variable 3. Add description in help text |
Lines starting with @ control generation flow. Other lines include templates of generated strings.
Template code is clean and easy to read. The @embed/@emit pair is simpler in use then divert/undivert known from M4.
The syntax of the template language follows well known languages (C and PHP). Unlike PHP, Temgen is universal generator. There are no assumptions about generated text. The "escaping" is also simpler, based on two kind of template lines: control lines and data lines. Such a code is easy to read and understand.
It is not a preprocessor, it is rather language interpreter. It has compound data structures (records, hash tables) and classic function call and definition mechanisms.
Temgen is distributed under the terms of GNU public license, with source code included. The author will be pleased with all suggestions and even with critique (if polite ;)
Marek Letowski (mletowski@firma.interia.pl)