FPP 1L "17 May 2001" "" "Version 3.0.0" [section 9 of 17]

.-3[DEFINITION STATEMENTS]         .-2[CONDITIONAL STATEMENTS]         .-1[EXPRESSIONS]
Top
.+1[MESSAGE OUTPUT STATEMENTS]     .+2[OUTPUT OF fpp]         .+3[PREDEFINED SYMBOLS]


MACRO EXPANSION

fpp supports a reversible argument-free macro expansion capability. This involves pairs of lines, the first a comment line containing the macro references as strings of the form #(constant-expression), and the second a non-comment Fortran statement.

The first line of the pair is always exactly preserved in the output, while the second is replaced by the expansion of the comment, with the first character deleted, to change the comment into a non-comment. The original contents of the second line are preserved as a comment with the prefix C-fpp- in a third output line.

This peculiar input line pairing is necessary to ensure that the expansion is reversible.

The parentheses around the expression serve to distinguish between macros and fpp preprocessor directives in comments, and serendipitously permit the extension from simple names to arbitrary constant expressions that can be evaluated by fpp.

Care must be taken in writing the input to ensure that any expected expansion does not make the line longer than 72 characters; fpp has almost no knowledge of Fortran, and therefore cannot provide correct line wrapping for it. However, it will warn about long lines.

Similarly, macro expansion in a multi-line continued statement should avoided, since it introduces comment lines between continuation lines. While such comments are legal in full Fortran 77, they are illegal in subset Fortran 77, and in older Fortrans, and may cause problems for other tools that process Fortran code.

Here is a small example. Given command-line definitions

FPTYPE='DOUBLE
MAXA=19
MAXB=25

then the input

C     #(FPTYPE) A(#(MAXA)), B(#(MAXB),#(MAXA**2))
      REAL A(100), B(255,10000)

is converted to the output

C     #(FPTYPE) A(#(MAXA)), B(#(MAXB),#(MAXA**2))
      DOUBLE PRECISION A(19), B(25,361)
C-fpp-      REAL A(100), B(255,10000)

Fortran 77 PARAMETER statements can be used to achieve similar effects, but in more restricted circumstances. In particular, fpp permits such expansions to happen in strings:

C10000 FORMAT ('Host operating system = #(OS)')
10000 FORMAT ('Host operating system = UNIX')

This may be awkward to achieve in standard Fortran.


.-3[DEFINITION STATEMENTS]         .-2[CONDITIONAL STATEMENTS]         .-1[EXPRESSIONS]
Top
.+1[MESSAGE OUTPUT STATEMENTS]     .+2[OUTPUT OF fpp]         .+3[PREDEFINED SYMBOLS]