Clicky

Fortran Wiki
Fypp (Rev #9, changes)

Skip the Navigation Links | Home Page | All Pages | Recently Revised | Authors | Feeds | Export |

Showing changes from revision #8 to #9: (追記) Added (追記ここまで) | (削除) Removed (削除ここまで) | (削除) Chan (削除ここまで)(追記) ged (追記ここまで)

Fypp is a Python powered preprocessor. It can be used for any programming languages but its primary aim is to offer a Fortran preprocessor, which helps to extend Fortran with condititional compiling and template metaprogramming capabilities. Instead of introducing its own expression syntax, it uses Python expressions in its preprocessor directives, offering the consistency and versatility of Python when formulating metaprogramming tasks. It puts strong emphasis on robustness and on neat integration into developing toolchains.

Detailed documentation: http://fypp.readthedocs.org

Project homepage: https://bitbucket.org/aradi/fypp

Fypp is released under the BSD 2-clause license.

Main features

Definition, evaluation and removal of preprocessor variables:

#:if DEBUG > 0
 print *, "Some debug information"
#:endif
#:set LOGLEVEL = 2
#:del LOGLEVEL

Macro defintions and macro calls:

#:def assertTrue(cond)
if (.not. ${cond}$) then
 print *, "Assert failed in file ${_FILE_},ドル line ${_LINE_}$"
 error stop
end if
#:enddef assertTrue
! Invoked via direct call (needs no quotation)
@:assertTrue(size(myArray) > 0)
! Invoked as Python expression (needs quotation)
$:assertTrue('size(myArray) > 0')

Conditional output:

program test
#:if defined('WITH_MPI')
 use mpi
#:elif defined('WITH_OPENMP')
 use openmp
#:else
 use serial
#:endif

Iterated output (e.g. for generating Fortran templates):

interface myfunc
#:for dtype in [ 'real', 'dreal', 'complex', 'dcomplex' ]
 module procedure myfunc_${dtype}$
#:endfor
end interface myfunc

Inline directives:

logical, parameter :: hasMpi = #{if defined('MPI')}#.true.#{else}#.false.#{endif}#

Insertion of arbitrary Python expressions:

character(*), parameter :: comp_date = "${time.strftime('%Y-%m-%d')}$"

Inclusion of files during preprocessing:

#:include "macrodefs.fypp"

Using Fortran-style continutation lines in preprocessor directives:

#:if var1 > var2 &
 & or var2 > var4
print *, "Doing something here"
#:endif

Passing (unquoted) multiline string arguments to callables:

#(追記) ! Callable needs only string argument (追記ここまで)(追記) 
# (追記ここまで):def debug_code(code)
 #:if DEBUG > 0
 $:code
 #:endif
#:enddef debug_code
#(追記) ! Pass code block as first positional argument (追記ここまで)(追記) 
# (追記ここまで):call debug_code
 if (size(array) > 100) then
 print *, "DEBUG: spuriously large array"
 end if
#:endcall debug_code
#(追記) ! Callable needs also non-string argument types (追記ここまで)(追記) 
# (追記ここまで):(削除) call (削除ここまで)(追記) def (追記ここまで) (削除) lambda (削除ここまで)(追記) repeat_code (追記ここまで)(削除)  (削除ここまで)(追記) ( (追記ここまで)(削除) s (削除ここまで)(追記) code (追記ここまで)(追記) , (追記ここまで)(追記)  (追記ここまで)(追記) repeat (追記ここまで)(追記) ) (追記ここまで)(追記) 
 # (追記ここまで):(削除)  (削除ここまで)(追記) for (追記ここまで)(削除) s.upper (削除ここまで)(追記)  (追記ここまで)(削除) () (削除ここまで)(追記) ind (追記ここまで)(削除) 
 (削除ここまで)(追記) in (追記ここまで)(追記)  (追記ここまで)(追記) range (追記ここまで)(追記) ( (追記ここまで)(追記) repeat (追記ここまで)(追記) ) (追記ここまで)(追記) 
 $ (追記ここまで)(追記) : (追記ここまで)(追記) code (追記ここまで)(追記) 
 # (追記ここまで)(追記) : (追記ここまで)(追記) endfor (追記ここまで)(追記) 
# (追記ここまで)(追記) : (追記ここまで)(追記) enddef (追記ここまで)(追記)  (追記ここまで)(追記) repeat_code (追記ここまで)(追記) 
# (追記ここまで)(追記) ! Pass code block as positional argument and 3 as keyword argument "repeat" (追記ここまで)(追記) 
# (追記ここまで)(追記) : (追記ここまで)(追記) call (追記ここまで)(追記)  (追記ここまで)(追記) repeat_code (追記ここまで)(追記) ( (追記ここまで)(追記) repeat (追記ここまで)(追記) = (追記ここまで)(追記) 3 (追記ここまで)(追記) ) (追記ここまで)(追記) 
 (追記ここまで)this will be (削除) converted (削除ここまで)(追記) repeated (追記ここまで) (削除) to (削除ここまで)(追記) 3 (追記ここまで) (削除) upper (削除ここまで)(追記) times (追記ここまで)(追記) 
 (追記ここまで)(追記) # (追記ここまで)(削除) case (削除ここまで)(削除) 
# (削除ここまで):endcall(追記)  (追記ここまで)(追記) repeat_code (追記ここまで)

Preprocessor comments:

#! This will not show up in the output
#! Also the newline characters at the end of the lines will be suppressed

Suppressing the preprocessor output in selected regions:

#! Definitions are read, but no output (e.g. newlines) will be produced
#:mute
#:include "macrodefs.fypp"
#:endmute

Explicit request for stopping the preprocessor:

#:if DEBUGLEVEL < 0
 #:stop 'Negative debug level not allowed!'
#:endif

Easy check for macro parameter sanity:

#:def mymacro(RANK)
 #! Macro only works for RANK 1 and above
 #:assert RANK > 0
 :
#:enddef mymacro

Line numbering directives in output:

program test
#:if defined('MPI')
use mpi
#:endif
:

transformed to

# 1 "test.fypp"
program test
# 3 "test.fypp"
use mpi
# 5 "test.fypp"
:

when variable MPI is defined and Fypp was instructed to generate line markers.

(追記) (追記ここまで)(追記)

Automatic folding of generated lines exceeding line length limit.

(追記ここまで)

See also

Revision from March 7, 2017 08:00:33 by Balint Aradi?
Forward in time (to current) | Back in time (8 more) | See current | Hide changes | History | Rollback | View: Source | Linked from: Preprocessors

AltStyle によって変換されたページ (->オリジナル) /