aradi / fypp

Python powered Fortran preprocessor
http://fypp.readthedocs.io
BSD 2-Clause "Simplified" License
192 stars 30 forks source link

expanding macro in include directive #23

Open ajshephard opened 2 years ago

ajshephard commented 2 years ago

I wish to expand a macro in an include directive (to determine the file name). I have something like

#:set FILENAMES = ['file', 'anotherfile']
#:for FILE in FILENAMES
    ! some code
    ! now include a file
    #:include "${FILE}$.fypp"
    ! more code
#:endfor

This does not work, and fypp returns "include file '${FILE}$.fypp' not found". Is it possible to accomplish this? Thanks.

aradi commented 2 years ago

Indeed, currently that is not possible. Include events are handled during parsing, before any evaluation of variables or even conditionals. For example

#:if defined("INCLUDE_ME")
  #:include "me"
#:endif

would always try to read the file me, even if INCLUDE_ME is not defined. But in that, case, the included content would not be evaluated.

In theory, one could change this behavior, but would need some coding, as the evaluation of the #:include directive would have to be shifted.