next up previous
Next: 5. Text Manipulations Up: A Tutorial on the Previous: 3. Hierarchical Macro Calls

4. Compound Macro Statements

 

With compound macro statements, several macro answers and/or commands can be combined into a single macro line. In the simplest case, this can be useful to reduce the number of lines in a long macro and to make it more readable by grouping answers that logically form an entity. The following lines, which are used to specify a matrix which is written into,

    mf4      / specify impedance matrix
    yes
    upqau
    auto impedances scen. %s%
    yes
    0
can be replaced by the single compound macro command
    ~+|mf4|y|upqau|auto impedances scen. %s%|y|0 /specify impedance matrix
In this case the character | was used as separator, but any other printable character could also be used for this purpose, as long as it does not appear anywhere else in the command.

The compound command is particularly useful after conditionals (~?...). Remember that the next line following a conditional will be executed or skipped, depending whether the condition holds or not. By using a compound statement following the conditional, the condition can be applied to several logical lines. The following example shows a test which verifies if the macro was called in module 0.00 (the main menu). If not, an error message is displayed and the macro jumps to the end:

    ~?m>000
    ~+|~/Error: macro must be called from main menu!|~$end

As seen in the example above, a compound command may contain any combination of macro commands, the only exceptions being other compound commands (~+...)or macro labels (~:...). Look at the following example, which sets the register x to the first macro parameter, if the latter has a non-zero value, or to 1 otherwise.

    ~+|~x=%1%|~?x=0|~x=1

Care must be taken when using substitutions within compound commands. The substitution is done once when the compound command is read as a whole. The maximum length of 128 characters applies both before and after this substitution. Just before it is executed, each sub-command is then again checked for substitutions. This is particularly important if registers are modifed within the compound command. What is the output of the following sequence?

    ~+|~x=1|~y=5
    ~+|~x+1|~x*%y%|~/x=%x%
(If you guessed the output is ``x=10'', you were wrong! Try it out!) If it is necessary to avoid substitution at the level of the compound statements, use %%%...%%% instead of %...%. This way, in the first substitution, the %%% are replaced by % and the actual substitution takes only place before the sub-command is executed.

Another special feature of the compound command is the possibility of branching back to the beginning of the compound command, in other words, starting it all over. This is done by executing an empty branch (without any label) as part of the compound command. This type of branching is particularly efficient, since, in contrast to branching to a label, it does not imply any file I/O. Can you see what the following ``one-liner'' does?

    ~+:~o|6:~*[q=%%%q%%%]:~?!e:~$:~/[error %%%e%%%]:~$



Exercise 4.1:
Write a small macro which implements an empty loop counting from 1 to a number specified as the first macro parameter. Compare implementations with and without compound macro statements.


next up previous
Next: 5. Text Manipulations Up: A Tutorial on the Previous: 3. Hierarchical Macro Calls

Heinz Spiess, EMME/2 Support Center
Fri Sep 27 13:04:28 MET DST 1996