• Aucun résultat trouvé

Free Source Form

Dans le document The Fortran 2003 Handbook (Page 59-63)

3.3 Source Form

3.3.1 Free Source Form

In free source form, there are no restrictions limiting statements to specific positions on a Fortran line. The blank character is significant and may be required to separate lexi-cal tokens.

Rules and restrictions:

1. Blank characters are significant everywhere except that a sequence of blank charac-ters outside a character context is treated as a single blank character. They may be used freely between tokens and delimiters to improve the readability of the source text. For example, the two statements:

SUM=SUM+A(I) SUM = SUM + A (I) are the same.

52 Chapter 3 2. Each line may contain from 0 to 132 characters, provided that they are of default character kind. If any character is of a nondefault character kind, the processor may limit the number of characters to fewer than 132 characters. For example, a line such as

TEXT = GREEK_’This line has 132 characters and contains ’ may use exactly 132 graphic characters, but the implementation may require more space to represent this source line than 132 Fortran characters. The processor may thus limit how many graphic characters may be used on a line if any of them are of nondefault character kind.

3. The exclamation mark (!), not in character context, is used to indicate the beginning of a comment that ends with the end of the line. A line may contain nothing but a comment. Comments, including the !, are ignored and do not alter the interpreta-tion of Fortran statements in any way. There is no language limit on the number of comments in a program unit, although the processor may impose such a limit. A line whose first nonblank character is an exclamation mark is called a comment line. An example of a Fortran statement with a trailing comment is:

ITER = ITER + 1 ! Begin the next iteration.

An example of a comment line is:

! Begin the next iteration.

A comment may appear before or after a program unit, but the standard does not indicate which program unit it belongs to if it is between program units.

A line with only blank characters or with no characters is treated as a comment line.

4. The ampersand (&) is used as the continuation symbol in free source form. If it is the last nonblank character after any comments are deleted and it is not in a char-acter context, the statement is continued on the next line that does not begin with a comment. If the first nonblank character on the continuing line is an ampersand, the statement continues after the ampersand; otherwise, the statement continues with the first position of the line. The ampersand or ampersands used as the con-tinuation symbols are not considered part of the statement. For example, the fol-lowing statement takes two lines (one continuation line):

STOKES_LAW_VELOCITY = 2 * GRAVITY * RADIUS ** 2 * &

(DENSITY_1 - DENSITY_2) / (9*COEFF_OF_VISCOSITY)

The leading blanks on the continued line are included in the statement and are al-lowed in this case because they are between lexical tokens.

No more than 255 continuation lines are allowed in a Fortran statement. No line may contain an ampersand as the only nonblank character before an exclamation

α

Language Elements and Source Form 53 mark. Comment lines cannot be continued; that is, the ampersand as the last char-acter in a comment is part of the comment and does not indicate continuation.

The double-ampersand convention must be used to continue a name, a character constant, or a lexical token consisting of more than one character split across lines.

The following statement is the same statement as in the previous example:

STOKES_LAW_VELOCITY = 2 * GRAVITY * RADIUS * 2 * (DEN&

&SITY_1 - DENSITY_2) / (9 * COEFF_OF_VISCOSITY)

However, splitting names across lines makes the code difficult to read and is not recommended.

Ampersands may be included in a character constant. Only the last ampersand on the line is the continuation symbol, as illustrated in the following example:

LAWYERS = "Jones & Clay & &

&Davis"

The value of this constant is ʺJones & Clay & Davisʺ. The first two ampersands are in character context; they are part of the value of the character string.

5. More than one statement may appear on a line. The statement separator is the semicolon (;), provided it is not in a character context; multiple successive semico-lons on a line with or without blanks intervening are considered as a single separa-tor. The end of a line is also a statement separator, and any number of semicolons at the end of the line have no effect. For example:

! The semicolon is a statement separator.

X = 1.0; Y = 2.0

! However, the semicolon below at the end of a line

! is not treated as a separator and is ignored.

Z = 3.0;

! Also, consecutive semicolons are treated as one

! semicolon, even if blanks intervene.

Z = 3.0; ; W = 4.0

! Continuation lines and statement separators may be mixed.

A = &

B; C = D; E &

= D

A semicolon must not be the first nonblank character on a line. Thus, the following is illegal:

A = B &

; C = D

but the following is legal:

54 Chapter 3 A = B &

&; C = D

This rule does not seem to make much sense, but that is what the standard says.

6. A label may appear before a statement, provided it is not part of another state-ment, but it must be separated from the statement by at least one blank. For exam-ple:

10 FORMAT(10X,2I5) ! 10 is a label.

IF (X == 0.0) 200 Y = SQRT(X) ! Label 200 is not allowed.

7. Any graphic character in the processor character set may be used in character liter-al constants (4.3.5.5) and character string edit descriptors (10.2.3). Note that this ex-cludes control characters; it is recommended that the implementor’s manual be consulted for the specific details.

3.3.1.1 Blanks as Separators

Blanks in free source form may not appear within tokens, such as names or symbols consisting of more than one character, except that blanks may be freely used in format specifications. For instance, blanks may not appear between the characters of multi-character operators such as ∗∗ and

.

NE

.

Format specifications are an exception be-cause blanks may appear within edit descriptors such as BN, SS, or TR in format specifications. On the other hand, a blank must be used to separate a statement key-word, name, constant, or label from an adjacent name, constant, or label. For example, the blanks in the following statements are required.

INTEGER SIZE PRINT 10,N DO I=1,N

Adjacent keywords require a blank separator in some cases (for example, CASE DEFAULT) whereas in other cases two adjacent keywords may be written either with or without intervening blanks (for example, BLOCK DATA); The following list gives the situations where blank separators are optional.

BLOCK DATA

DOUBLE PRECISION ELSE IF

ELSE WHERE END ASSOCIATE END BLOCK DATA END DO

END ENUM END FILE END FORALL END FUNCTION END IF

Language Elements and Source Form 55 END INTERFACE

END MODULE END PROGRAM END SELECT END SUBROUTINE END TYPE

END WHERE GO TO IN OUT SELECT CASE SELECT TYPE

Thus both of the following statements are legal:

END IF ENDIF

Despite these rules, blank separators between statement keywords make the source text more readable and clarify the statements. In general, if common rules of English text are followed, everything will be correct. For example, blank separators in the fol-lowing statements make them quite readable, even though the blanks between the key-words DOUBLE and PRECISION and between END and FUNCTION are not required.

RECURSIVE PURE FUNCTION F(X) DOUBLE PRECISION X

END FUNCTION F

3.3.1.2 Sample Program, Free Source Form A sample program in free source form is:

123456789...

---|PROGRAM LEFT_RIGHT

| REAL X(5), Y(5)

| ! Print arrays X and Y

| PRINT 100, X, Y

| 100 FORMAT (F10.1, F10.2, F10.3, F10.4, &

| F10.5)

| . . .

|END

Dans le document The Fortran 2003 Handbook (Page 59-63)