Loading...
Searching...
No Matches
For

Definition

Fortran Preprocessor (fpx) – compile-time loop expansion support.

This module implements the non-standard #for / #endfor directive pair used by fpx to generate repeated source code from a list of values.

Features:

  • Simple iteration over explicit lists: #for T in [integer, real, complex]
  • Iteration over macro-expanded lists: #define NUMERICS [integer, real, complex] #for T in NUMERICS
  • Arbitrary nesting of #for blocks
  • Integration with the normal macro expansion engine
  • Deferred body collection until matching #endfor
  • Automatic cleanup of loop-local variables

During parsing, loop bodies are stored internally and emitted only when the matching #endfor is encountered. Each iteration temporarily defines the loop variable as a macro whose value is substituted into the collected body before output.

Examples

  1. Basic iteration:
    #for T in [integer, real, complex]
    type(T) :: value
    #endfor
    ! Expands to:
    type(integer) :: value
    type(real) :: value
    type(complex) :: value
  2. Using a macro list:
    #define NUMERICS [integer, real, complex]
    #for T in NUMERICS
    type(T) :: value
    #endfor
  3. Nested loops:
    #define CONCAT(a,b) a##b
    #for T in [integer, real]
    #for R in [32,64]
    type(CONCAT(T,R)) :: value
    #endfor
    #endfor
  4. Generic procedure generation:
    #define CONCAT(a,b) a##b
    #define NUMERICS [integer, real, complex]
    #for T in NUMERICS
    module procedure concat(add_,t)
    #endfor

Loop variables behave like temporary macros and participate in normal macro expansion rules.

Methods

◆ add_to_loop()

subroutine, public add_to_loop ( character(*), intent(in) line)

Append a source line to the currently active loop body.

Lines are stored verbatim and expanded only when the matching #endfor directive is encountered.

Parameters
[in]lineSource line to store

Remarks

Definition at line 346 of file loop.f90.

◆ handle_endfor()

subroutine, public handle_endfor ( type(context), intent(inout) ctx,
integer, intent(in) ounit,
type(c_funptr), intent(in) p,
type(macro), dimension(:), intent(in) macros,
character(*), intent(in) token )

Finalize a loop and emit all expanded iterations.

The collected loop body is expanded once for every value contained in the loop variable parameter list. Nested loops are handled recursively by forwarding generated lines to the enclosing loop body when present.

When the outermost loop terminates, all temporary loop state is released automatically.

Parameters
[in]ctxCurrent parsing context
[in]ounitOutput unit
[in]ppreprocessor function pointer
[in,out]macrosActive macro table
[in]tokenDirective keyword (endfor)

Remarks

Definition at line 251 of file loop.f90.

◆ handle_for()

subroutine, public handle_for ( type(context), intent(in) ctx,
type(macro), dimension(:), intent(inout), allocatable macros,
character(*), intent(in) token )

Process a #for directive and initialize a new loop context.

Parses directives of the form:

#for variable in [item1, item2, ...]

or

#for variable in MACRO_NAME

where MACRO_NAME expands to a bracketed list.

A temporary macro representing the loop variable is created and the iteration values are stored internally until the matching #endfor is reached.

Parameters
[in]ctxCurrent parsing context
[in,out]macrosActive macro table
[in]tokenDirective keyword (for)

Remarks

Definition at line 128 of file loop.f90.

◆ is_in_forloop()

logical function, public is_in_forloop

Query whether parsing is currently inside a #for block.

Returns
.true. when one or more loop contexts are active, .false. otherwise.

Remarks

Definition at line 359 of file loop.f90.