Skip to main content

Python 驱动的 Fortran 预处理器

项目描述

https://travis-ci.org/aradi/fypp.svg?branch=develop

Fypp 是一个 Python 驱动的预处理器。它可以用于任何编程语言,但其主要目的是提供 Fortran 预处理器,它有助于通过条件编译和模板元编程功能扩展 Fortran。它没有引入自己的表达式语法,而是在其预处理器指令中使用 Python 表达式,在制定元编程任务时提供 Python 的一致性和多功能性。它非常强调健壮性和与开发工具链的巧妙集成。

该项目托管在 github 上

详细文档可在 readthedocs.org上找到。

Fypp 在BSD 2-clause license下发布。

主要特点

  • 变量的定义、评估和删除:

    #:if DEBUG > 0
      print *, "Some debug information"
    #:endif
    
    #:set LOGLEVEL = 2
    print *, "LOGLEVEL: ${LOGLEVEL}$"
    
    #:del LOGLEVEL
  • 宏定义和宏调用:

    #:def ASSERT(cond)
      #:if DEBUG > 0
        if (.not. ${cond}$) then
          print *, "Assert failed in file ${_FILE_}$, line ${_LINE_}$"
          error stop
        end if
      #:endif
    #:enddef ASSERT
    
    ! Invoked via direct call (argument needs no quotation)
    @:ASSERT(size(myArray) > 0)
    
    ! Invoked as Python expression (argument needs quotation)
    $:ASSERT('size(myArray) > 0')
  • 条件输出:

    program test
    #:if defined('WITH_MPI')
      use mpi
    #:elif defined('WITH_OPENMP')
      use openmp
    #:else
      use serial
    #:endif
  • 迭代输出(例如用于生成 Fortran 模板):

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

    logical, parameter :: hasMpi = #{if defined('MPI')}# .true. #{else}# .false. #{endif}#
  • 插入任意 Python 表达式:

    character(*), parameter :: comp_date = "${time.strftime('%Y-%m-%d')}$"
  • 在预处理期间包含文件:

    #:include "macrodefs.fypp"
  • 在预处理器指令中使用 Fortran 风格的延续行:

    #:if var1 > var2 &
        & or var2 > var4
      print *, "Doing something here"
    #:endif
  • 将(不带引号的)多行字符串参数传递给可调用对象:

    #! Callable needs only string argument
    #:def DEBUG_CODE(code)
      #:if DEBUG > 0
        $:code
      #:endif
    #:enddef DEBUG_CODE
    
    #! Pass code block as first positional argument
    #:block DEBUG_CODE
      if (size(array) > 100) then
        print *, "DEBUG: spuriously large array"
      end if
    #:endblock DEBUG_CODE
    
    #! Callable needs also non-string argument types
    #:def REPEAT_CODE(code, repeat)
      #:for ind in range(repeat)
        $:code
      #:endfor
    #:enddef REPEAT_CODE
    
    #! Pass code block as positional argument and 3 as keyword argument "repeat"
    #:block REPEAT_CODE(repeat=3)
    this will be repeated 3 times
    #:endblock REPEAT_CODE
  • 预处理器评论:

    #! This will not show up in the output
    #! Also the newline characters at the end of the lines will be suppressed
  • 抑制选定区域中的预处理器输出:

    #!读取定义,但不会产生输出(例如换行符)
    #:沉默的
    #:include "macrodefs.fypp"
    #:endmute
  • 停止预处理器的显式请求:

    #:if DEBUGLEVEL < 0
      #:stop 'Negative debug level not allowed!'
    #:endif
  • 轻松检查宏参数的健全性:

    #:def mymacro(RANK)
      #! Macro only works for RANK 1 and above
      #:assert RANK > 0
      :
    #:enddef mymacro
  • 输出中的行号指令:

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

    转变为

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

    当定义变量MPI并指示 Fypp 生成线标记时。

  • 超过线长限制的生成线自动折叠

安装

Fypp 需要一个工作的 Python 解释器。它与 Python 2(2.6 及更高版本)和 Python 3(所有版本)兼容。

自动安装

使用 Python 命令行安装程序pip从PyPI 上的 Fypp 页面下载稳定版本并将其安装在您的系统上:

pip install fypp

这将安装命令行工具fypp和 Python 模块 fypp.py。如果您想直接从 Python 脚本中访问 Fypp 的功能,则可以导入后者。

手动安装

对于手动安装,您可以从Fypp 项目网站下载稳定 版本的源代码。

如果您希望获得最新的开发版本,请克隆项目存储库:

git clone https://github.com/aradi/fypp.git

并检查分支。

命令行工具是一个独立的脚本。您可以直接从源文件夹运行它

FYPP_SOURCE_FOLDER/bin/fypp

或者在将它从bin文件夹复制到PATH环境变量中列出的任何位置之后 ,只需发出

fypp

python 模块fypp.py可以在FYP_SOURCE_FOLDER/src中找到。

跑步

Fypp 命令行工具读取文件,对其进行预处理并将其写入另一个文件,因此您通常会像这样调用它:

fypp source.fpp source.f90

这将处理source.fpp并将结果写入source.f90。如果未指定输入和输出文件,则从标准输入读取信息并将其写入标准输出。

Fypp 的行为会受到各种命令行选项的影响。可以通过以下方式获得所有命令行选项的摘要:

fypp -h

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

fypp-3.1.tar.gz (68.2 kB 查看哈希

已上传 source