I forget an important feature. The commentaries.
TNT's macro language support the same multiline comment style of C (but not the single line comment style), enclosing comments within /* and */. Comments has no effect on the script, but are very useful to document the code!
/* This is a comment */
Comments are really important, no only to made code legible to other people, but also to our own understanding (specially, if it is code written long time away!).
Comments are valid after the macro mode is open (using macro =).
Following a well knew tradition I put this simple macro ;)
/* My first TNT macro */
quote Hello world! ;
Now, we can enter on the subject of variables...
Variables are objects that the program manipulates by an specific objective given by the user. Variables store values, that can be modified or read. Usually we are interested in that value. In other circumstances the variable value is useful for controlling the program flow. Also they are used to store values given by the user.
When macro mode is open, TNT sets a default number of variables (1000). They can be accessed by a number, starting with 0. It is possible to change the amount of variables using macro* N K ; where N is the number of loops, and K the number of variables.
Using numbers to access variables can be useful for small scripts, but with more complex macros, the management of code can be very difficult. Then, is a good practice to use names for variables. Variables can be named in any part of the code. It is a good practice naming it just after starting the macro's file (just after macro = ). Variables names are declared using the keyword var.
There are two ways to name variables, the first is explicit, that is useful to backward compatibility with scripts written in old versions of TNT.
5 variable 3
In this format, using var =, each variable is declared using the internal number of TNT. The declaration finish with a semi colon. The principal problem with this style, is that it is an invitation to use the numbers instead of the names. This can be dangerous when parts of the code change (for example, adding more variables).
A more elegant way, and more secure (protects against usage of unnamed variables), is declaring directly the names, using var : instead of var =
Here, I always use this style, that produce a more legible code.
TNT variables can be simple, that is, just with one value, or can be arrays, a vector of several consecutive values. TNT arrays are static, that is, they cannot be resized in running time. Then declare them only after you know the size of the array. For example, just after reading the data, and we known the number of nodes in a tree.
An array declaration is simple, just as a normal variable, but dimensions inside brackets (), if it is a multidimensional matrix, use a comma (,) to separate the values, in a similar way that arrays are declared in pascal.
aMatrix [3, 4]
Variables in loops
In TNT loop variables are independent from general variables. This variables are managed by TNT and not directly by the programmer. As general variables they can be named, o accessed using a number that start from 1, and increase in each nesting level. I generally use numbers. Maybe a better practice is to use names, as that freed the loop from its nesting level, an that's the only way to access some variables (for example, if you access it from another file).
Loop variables are declared using an equal before the name:
loop =cicle 1 10
Variables in the command line
When a macro is called, it is possible to assign some initial values from the command line associated with the script. This variables are sometimes known as arguments or parameters. For example, a macro called dojac can receive as parameters the number of replicates and the number of iterations per replicate
dojac 1000 20 ;
Then, dojac receives 1000 replicates and each replicate with 20 iterations.
As TNT's scripts are not interactive, this is the only way to the final user to modify the behavior of the scripts. Then, if the idea is to distribute the macro, calling the macro without parameters shows a help screen explaining the objective, the parameters, and the conditions necessary for using the macro in a correct way.
The function argnumber returns the number of arguments used when calling the macro.