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 program called ShowSubstitution that contains the following line:

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

If the commands shown below were used to execute 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 #<apples>) - you must employ the backslash (\-escape character) to indicate that the number sign is to be take 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. For #<1> and #<apples> to be taken literally, the output command would read

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

Nested Macros

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

The software 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 one.

Example

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

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

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

The values of the macros becomes

<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>'