X3J3/96-072 Date: April 24, 1996 To: X3J3 From: David Epstein Subject: The Rest of the Full CoCo Definition Here are the introduction, rationale, and examples sections of the Full CoCo Definition paper X3J3/96-068. The language definition section was sent out with the paper distribution. --------------------------------------------------------------------- CONDITIONAL COMPILATION IN FORTRAN ISO/IEC 1539-3 : 1996 {Auxiliary to ISO/IEC 1539 : 1996 "Programming Language Fortran"} CONTENTS 1. Introduction 2. General 3. The Conditional Compilation Language Definition Annex A : Examples INTRODUCTION This part of ISO/IEC 1539 has been prepared by ISO/IEC JTC1/SC22/WG5, the technical working group for the Fortran language. This part of ISO/IEC 1539 is an auxiliary standard to ISO/IEC 1539 : 1996, which defines the latest revision of the Fortran language, and is the first part of the multipart Fortran family of standards; this part of ISO/IEC 1539 is the third part. The revised language defined by the above standard is informally known as Fortran 95. This part of ISO/IEC 1539 defines a conditional compilation language definition. References to Part 1 refer to "ISO/IEC 1539 : 1996, Information technology--Programming Languages--Fortran." GENERAL Frequently Fortran programmers need to maintain more than one version of a code, or to run the code in various environments. The easiest solution for the programmer is to keep a single source file that has all the code variations interleaved within it so that any version can be easily extracted. This way, modifications that apply to all versions need only be made once. Conditional compilation permits the programmer to define special variables and logical constucts that conditionally control which source lines in the file are passed on to the compiler and which lines are skipped over. There are many uses for conditional compilation. Writing portable code is one of the most popular uses of conditional compilation. Examples in Annex A show some other uses for conditional compilation. 3. THE CONDITIONAL COMPILATION LANGUAGE DEFINITION {{{ Supplied in the paper distribution }}} Annex A : EXAMPLES This annex includes two examples illustrating the use of facilities conformant with this part of ISO/IEC 1539. The first example uses conditional compilation to facilitate the editing of a large block comment. The second example uses conditional compilation to provide debugging information upon entering and exiting procedures. Note, the conditional compilation directives in this example could be automatically generated. Each example contains a conditional compilation program and an output file that results from conditional compilation processing. --- initial text ------------------------------------------ ! EXAMPLE 1 shows the Shift file for output ?? logical :: modifying_header_comment = .false. ?? if (.not. modifying_header_comment) then One convenient use of conditional compilation is the ability to write large comments that span across many lines without requiring each line to start with a "!". Since conditional compilation specifies blocks of lines to be skipped over by the compiler, this whole paragraph can be written and modified without the overhead of making sure that each line is a Fortran comment. One can imagine this use of conditional compilation for header comments preceding Fortran programs, modules and procedures. ?? endif --- text output from conditional compilation processing --- ! EXAMPLE 1 shows the Shift file for output ?? logical :: modifying_header_comment = .false. ?? if (.not. modifying_header_comment) then ?> ?>One convenient use of conditional compilation is the ?>ability to write large comments that span across many ?>lines without requiring each line to start with a "!". ?>Since conditional compilation specifies blocks of lines ?>to be skipped over by the compiler, this whole paragraph ?>can be written and modified without the overhead of ?>making sure that each line is a Fortran comment. ?> ?>One can imagine this use of conditional compilation for ?>header comments preceding Fortran programs, modules and ?>procedures. ?> ?? endif --- initial text ------------------------------------------ ! EXAMPLE 2 shows the Short file for output ?? logical :: debug_proc_name = .false. ?? logical :: debug_proc_args = .false. ?? ! Make sure to debug the procedure name if debugging the arguments ?? debug_proc_name = debug_proc_name .or. debug_proc_args subroutine IntSwap (left, right) integer, intent(inout) :: left, right integer :: wrong ?? if (debug_proc_name) then print *, "Entering IntSwap" ?? endif ?? if (debug_proc_args) then print *, " IntSwap(in):left = ", left print *, " IntSwap(in):right = ", right ?? endif wrong = right left = right right = wrong ?? if (debug_proc_args) then print *, " IntSwap(out):left = ", left print *, " IntSwap(out):right = ", right ?? endif ?? if (debug_proc_name) then print *, "Exiting IntSwap" ?? endif endsubroutine IntSwap --- text output from conditional compilation processing --- ! EXAMPLE 2 shows the Short file for output subroutine IntSwap (left, right) integer, intent(inout) :: left, right integer :: wrong wrong = right left = right right = wrong endsubroutine IntSwap