Macro Substitution

Macro Substitution

Welcome to our Knowledge Base

Documentation | Blog | Demos | Support

< All Topics
Print

Macro Substitution

A macro can appear anywhere and can be repeated any number of times in an application program.

Each time the software encounters a call to the macro, it substitutes the current character string value of the macro. Substitution takes place one command at a time, before each command is executed. To view these substitutions as they occur, issue a SET LEXTRACE ON command.

Consider a macroprogram called ShowSubstitution that contains the following line:

list #<1> Employees #<2> LastName = '#<3>' #<SortOrder>

If the commands shown below were used to execute the macroprogram ShowSubstitution

let <SortOrder> = 'Sorted by DeptNum'
ShowSubstitution "2" "where" "Smith"

then the LIST command shown above would be interpreted as

list 2 Employees where LastName = 'Smith' Sorted by DeptNum

Avoiding Macro Substitution

To use a character literal that could be mistaken for a macro – that is, the character literal contains the number sign and the macro name delimiters (#<1> or #) – you must employ the backslash (“\”, the escape character) to indicate that the number sign is to be taken literally.

For example, in the command

output 'The #<apples> in the box #<1> is:'

#<apples> and #<1> are recognized as macros. If they are not intended to be macros and therefore have not been previously defined, they are automatically replaced with the null string. Thus the command becomes

output 'The  in box  is:'

If the escape character precedes the number sign, the number sign is taken as a literal character as in:

output 'The \#<apples> in box \#<1> is:'

Nested Macros

The character string that a macro represents can contain references to other macros.

ZIM automatically performs multiple rounds of substitution when macros are nested outside quoted strings. If a nested macro appears inside a quoted string, macro substitution occurs only once.

Example

Consider two macros #<mac1> and #<mac2>. The following command could  be used to assign character strings to these macros:

let <mac1> = 'put' 
let <mac2> = 'out\#<mac1>'

Note the use of the backslash (“\”) to avoid macro substitution in the assignment to <mac2>

<mac1> put
<mac2> out\#<mac1>

With this arrangement, the command

#<mac2> 'Hello'

becomes

out#<mac1> 'Hello'

which in turn becomes

output 'Hello'

On the other hand, the command

output '#<mac2>'

is processed as

output 'out#<mac1>'
Was this article helpful?
5 out of 5 stars

1 rating

5 Stars 100%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
5
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

pt_BRPortuguese