Allow expressions as lines of code?

In Python and other languages with a REPL, you can enter an expression at the prompt and get its value. If you store your code in a file and run it, the expression will be evaluated, although the value will not be printed. If Fortran is to have a REPL, as in LFortran, will the user always need to preface expressions with print*, or to set the expression to a variable? Should Fortran be extended to allow statements with just an expression? The program would be required to evaluate functions appearing in the expression, which in the case of expressions with non-PURE functions could affect program results. If Fortran were to allow stand-alone expressions, I don’t think their values should be printed, because you want to be able to search a code for PRINT or WRITE and see where output is produced. But maybe compilers could offer an interactive mode as an extension where stand-alone expressions are printed.

I think non-PURE functions should usually be avoided and like that Fortran has subroutines where other languages use non-PURE functions. An argument against allowing stand-alone expressions is that it could encourage bad programming practices.

1 Like

Yes, that is how LFortran works. It evaluates all your expressions (and they can affect your state via non-pure functions) but only the last one is printed.

Will there be any kind of API that lets you call LFortran as an embedded language from existing compilers or do you have to use LFortran and/or other LLVM languages? I can think of circumstances when I need or want to use the GNU or commercial compilers but would like to be able to look at data interactively using Fortran statements, versus compiling up the entire program in LFortran or calling the code as a library from LFortran; especially during the time LFortran is maturing. Have to wait and see whether I can compile up everything from LFortran easily enough (with codes using MKL, MPI, …).

LFortran works as a C++ library, so you can call it from anywhere. One thing that we also work on is being able to “use” GFortran compiled modules. It should work interactively also, so that should allow to use GFortran to compile your whole code, and use LFortran to execute parts interactively. I think we can add support for other compilers also.

2 Likes

Fantastic. So if I call LFortran via the C++ interfaces in python and that can call my compiled Fortran I think I might be getting dizzy thinking about the possible scenarios. That basically covers about everything I was thinking about doing with a recent smaller project with Lfortran providing an entire implementation of Fortran for interactive use instead of a small subset of Fortran/Octave-like commands. Sounds exciting.

2 Likes

Yes, I think it opens up tons of possibilities. I still believe that LFortran itself has to compile all of Fortran eventually, and so that is what our focus has been. Everything else will follow.

2 Likes