Polish version - English version - User manual - Download: tg-0.11.tgz

temgen - universal code generator

Temgen is an universal code generator based on its own template language. It can be used for generating any texts, such as program source in any language, HTML documents, or even texts in natural languages. Possible applications are similar to preprocessors (such as M4) and to PHP-like tools.

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:

  1. Define global variable
  2. Add switch branch
  3. Add description in help text

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)


Polish version - English version - User manual - Download: tg-0.11.tgz