Git Product home page Git Product logo

fypp's Introduction

Fypp โ€” Python powered Fortran metaprogramming

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

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.

The project is hosted on github.

Detailed DOCUMENTATION is available on readthedocs.org.

Fypp is released under the BSD 2-clause license.

Main features

  • Definition, evaluation and removal of variables:

    #:if DEBUG > 0
      print *, "Some debug information"
    #:endif
    
    #:set LOGLEVEL = 2
    print *, "LOGLEVEL: ${LOGLEVEL}$"
    
    #:del LOGLEVEL
    
  • Macro definitions and macro calls:

    #:def assertTrue(cond)
    #:if DEBUG > 0
    if (.not. ${cond}$) then
      print *, "Assert failed in file ${_FILE_}$, line ${_LINE_}$"
      error stop
    end if
    #:endif
    #:enddef assertTrue
    
    ! Invoked via direct call (argument needs no quotation)
    @:assertTrue(size(myArray) > 0)
    
    ! Invoked as Python expression (argument 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
    #: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"
    #:call repeat_code(repeat=3)
    this will be repeated 3 times
    #: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" 1
    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

Installing

Fypp needs a working Python interpreter. It is compatible with Python 2 (version 2.6 and above) and Python 3 (all versions).

Automatic install

Use Pythons command line installer pip in order to download the stable release from the Fypp page on PyPI and install it on your system:

pip install fypp

This installs both, the command line tool fypp and the Python module fypp.py. Latter you can import if you want to access the functionality of Fypp directly from within your Python scripts.

Manual install

For a manual install, you can download the source code of the stable releases from the Fypp project website.

If you wish to obtain the latest development version, clone the projects repository:

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

and check out the master branch.

The command line tool is a single stand-alone script. You can run it directly from the source folder

FYPP_SOURCE_FOLDER/bin/fypp

or after copying it from the bin folder to any location listed in your PATH environment variable, by just issuing

fypp

The python module fypp.py can be found in FYP_SOURCE_FOLDER/src.

Running

The Fypp command line tool reads a file, preprocesses it and writes it to another file, so you would typically invoke it like:

fypp source.fpp source.f90

which would process source.fpp and write the result to source.f90. If input and output files are not specified, information is read from stdin and written to stdout.

The behavior of Fypp can be influenced with various command line options. A summary of all command line options can be obtained by:

fypp -h

fypp's People

Contributors

baradi09 avatar aradi avatar haraldkl avatar oschuett avatar

Watchers

Yang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.