Tabitha Programming Guide

Metaprogramming

Some parts of programmingg are really tedious. Depending on the functionality of the language, and how high or low level it is, some operations require a heck of a lot of typing. To solve this, a lot of languages emply some idea of metaproramming. Simply put, metaprogramming is when one writes code to aid the creation of other code.

A weak example is the idea of a preprocessor macro in C. This allows us to #define pieces of code corresponding to some mnemonic. I say this is a weak example, for macros are quite limited in what they can really do. There are no such things as loops in C macros (though a macro can be defined to contain a C loop).

Another example of metaprogramming would be the C++ template system. This is a more featureful version of metaprogramming, however it is quite infamous for its difficulty. This is particularly true with regard to the often cryptic error messages which arise from its usage.

Tabitha has a quite versatile form of metaprogramming which takes place in the preprocessor. In Tabitha we can create blocks of arbitrary text which is fed into a temporary file in your workspace. A user-defined command is then run and the contents of another file is insertd into your source where this block was originally.

Inserting a Metaprogramming Block

This concept is best-illustrated by means of an example.

collection type Quad {
##python3 tabic_pree.src > tabic_pre.dst##
for i in range(4): 
    print("Float x{}\nFloat y{}".format(i+1, i+1))
##
}

See that we have a set of three double-hashed ##. Two important pieces of text are sandwiched in this set. Between the second two ##, is some text which during preprocessing is written to tabic_pre.src. Betwen the first two ###, is a command executed during the preprocessing stage. After this command is run, the preprocessor replaces the entire block with the contents of the file tabic_pre.dst` in-memory. The end result is that the source code is by the parser is acutally:

collection type Quad {
Float x1
Float y1
Float x2
Float y2
Float x3
Float y3
Float x4
Float y4
}

The beauty of Tabitha metaprogramming, is that the block test and the commands are arbitrary. Through this mechanism, you can use any command line tool and any language to generate Tabitha code.

The main drawback of this technique, is that it potentially hurts cross-compatibility; one may be using tools specific to a particular system, and should hence be wary of this if they hope to port their programs to other environments.