Programming tools / The preprocessor |
A simple macro is identified by its name and body.
&define identifier body
After substitution, the macro definition is replaced with blank lines.
As the preprocessor scans the text, it substitutes the macro body for the name identifier.
The following example show macro substitution with 2 simple macros:
&define MAX_TEST 12 &define HW "Hello world" MAIN DEFINE i INTEGER FOR i=1 TO MAX_TEST DISPLAY HW END FOR END MAIN
& 1 "A" MAIN DEFINE i INTEGER FOR i=1 TO 12 DISPLAY "Hello world" END FOR END MAIN
The macro definition can be continued on multiple lines, but when the macro is expanded, it is joined to a single line as follows:
&define TABLE_VALUES 1, \ 2, \ 3 DISPLAY TABLE_VALUES
& 1 "A" DISPLAY 1, 2, 3
The source file is processed sequentially, so a macro takes effect at the place it has been written:
DISPLAY X &define X "Hello" DISPLAY X
& 1 "A" DISPLAY X DISPLAY "Hello"
The macro body is expanded only when the macro is applied:
&define AA BB &define BB 12 DISPLAY AA
& 1 "A" DISPLAY 12
In order to prevent infinite recursion, a macro cannot be expanded recursively.
&define A B &define B A &define C C A C
& 1 "A" A C