I have been working on transpilers to and from Fortran. When transpiling from Fortran you notice that there are many ways to write the same program, so it makes sense to transpile general Fortran to “normalized” Fortran and then transpile that. Here is a partial list of how Fortran code can be normalized. Suggestions are welcome.
- fixed source form can be converted to free
- code can indented with specified rules and made lower case
- either single or double quotes can be made the default for quoting character strings
- implicit typing can be made explicit
printcan be replaced bywrite- specifiers can be named, writing
write (unit=, fmt=*)instead ofwrite(*,*),real(kind=dp)instead ofreal(dp)etc. - one-line
ifstatements can be replaced with anifblock . I have found that one-line if statements can be hard for transpilers to handle. real, dimension(n) :: xcan be replaced byreal :: x(n)::can be added to any declaration without it- each variable can be declared on a separate line, allocated on a separate line, and deallocated on a separate line
real function f(x)can be replaced withfunction(x) result(y). There are many other ways to declare the same function- in a procedure,
real :: n = 0can be replaced byreal, save :: n = 0This should be done regardless of whether code will be transpiled - padded character variables in an array constructor
[”a “, “ab “, “abc”]can be replaced by[character (len=3) :: “a”, “ab”, “abc”] - lines with
;to have multiple statements can be broken up - A bare
endcan be replaced withend function foo,end subroutine foo,end module foo, orend program foo.
More substantively, common can be replaced by module variables. A little-tested script for this is here.
I am not saying that code should be written in the ways described above. Some of the replacements make code more verbose. But for translation, automatic generation of Fortran code, and comparing code, a tool to normalize code can be useful.