-
Notifications
You must be signed in to change notification settings - Fork 21
Example usage as a tool
The bundled executable fortran-src
exposes tools for working with
Fortran source code, including inspecting analysis results (such as the program
basic blocks or inferred variable and function types) and code reformatting.
In the following examples, the file main.f90
is a Fortran
90-compatible program with the following content:
program main
implicit none
real :: r, area
r = 1.0
area = area_of_circle(r)
print *, area
contains
function area_of_circle(r) result(area)
real, parameter :: pi = 3.14
real, intent(in) :: r
real :: area
area = r * r * pi
end function
end program
The fortran-src
binary must be on your system path in order to
invoke it. Alternatively, if you use Stack to build the project, you
may replace the fortran-src
prefix with stack run --
and
invoke it directly in the project directory.
Invocations follow the common syntax fortran-src <FILE> <OPTIONS>
. You select
the command you wish to run using the relevant option. Run fortran-src --help
to view a built-in description of the options available.
The extension of the input file determines which Fortran version the file is parsed as. This may be overriden by explicitly requesting a specific version:
fortran-src main.f90 --fortranVersion=90 <COMMAND>
Running fortran-src
with no arguments displays the included help, which includes
an enumeration of the Fortran versions supported.
Parse a file and view the typechecker output: fortran-src main.f90 --typecheck
4:12 r Real 4 Variable
4:15 area Real 4 Variable
11:4 area_of_circle - Function
12:27 pi Real 4 Parameter
13:28 r Real 4 Variable
14:16 area Real 4 Variable
A file can be parsed and then pretty-printed back using the
fortran-src printing algorithm: fortran-src main.f90 --reprint
program main
implicit none
real :: r, area
r = 1.0e0
area = area_of_circle(r)
print *, area
contains
function area_of_circle(r) result(area)
real, parameter :: pi = 3.14e0
real, intent(in) :: r
real :: area
area = ((r * r) * pi)
end function area_of_circle
end program main
Note the printing functionality has added the additional information of the program unit name
on the end
lines here.
Fortran code is printed with its corresponding source form i.e. FORTRAN 77 code is printed using fixed source form, while Fortran 90 and above use free source form.