Edit and run a Fortran program

Arjen Markus (26 may 2014) Typically, when programming in Fortran (or C or C++ or ...) you first edit the source file, store it, run it through the compiler and linker and then you are ready (modulo all the compile and link errors you may encounter) to run the program itself. This cycle makes experimenting with unfamiliar subroutines functions and features or writing small one-off programs a bit more cumbersome than if you were to use an interpreter like you can with Tcl - tclsh or wish just allow you to type commands and see the results.

Well, while the Tcl program below:

  • is far from complete
  • is not an interpreter by any meaningful definition
  • has a clumsy user-interface which lacks just about anything
  • took me about an hour to put together and test,

it does demonstrate that you can shorten the development cycle considerably if you limit yourself to smallish programs and have a fast enough compiler/linker.

I had some fancy ideas when I started writing it, like shifting subprogram definitions into the right location, allowing shortcut statements, like for printing the result of a variable etc. But actually, this little program works quite satisfactorily, at least to me.

One caveat: I extend the PATH environment variable with the path to my gfortran installation. That may not be the same as on your system, should you want to try it out.

One remark: While I present it as a way to speed up writing small Fortran programs, you can actually use this with other compiled languages as well, as the GUI is almost entirely language-agnostic.

# fint.tcl --
#     Mockup of a Fortran "interpreter". The idea is that
#     it should be possible to run small Fortran programs
#     in a way that resembles the interpreter loop.
#
#     This is a mere experiment to see if i thas a chance
#     to work
#
grid [::ttk::label .lout -text "Output:"] - -sticky w
grid [text  .tout -width 70 -height 20] - -sticky news
grid [::ttk::label .linp -text "Program:"] [::ttk::button .brun -text "Run" -command runProgram] -sticky w
grid [text  .tinp -width 70 -height 20] - -stick news

set env(PATH) "c:\\mingw\\bin;$env(PATH)"

proc runProgram {} {
    .tout delete 1.0 end

    set program [.tinp get 1.0 end]
    set outfile [open "fint.f90" w]
    puts $outfile $program
    close $outfile

    exec gfortran -o fint fint.f90
    set output [exec fint]

    .tout insert 1.0 $output
}