Syntax of interactive Fortran

Right, on a technical level all of these can be done. The question is more “what do we want”?

For now, LFortran is conservative and only does the minimal change to regular Fortran. Regular Fortran has a global scope that allows a program, modules, submodules, functions, subroutines (and also a common block and I believe a data block). LFortran extends this to also allow:

  • Statements (use statement, declarations, regular statements that you can use in the body of a function such as assignment, subroutine call, open statement, etc.)
  • Expressions (such as 2 or 2+3 or a function call)

I think that’s it. This simple change suddenly allows to use Fortran interactively. If you leave an expression at the end of a cell, it will get printed. This is also the minimal change. If you don’t allow expressions, it will be quite limited, as you can’t use it as a calculator. If you don’t allow statements, it’s going to be tough also (how do you declare a variable or assign to it?). So you at least have to do that. By only doing that, we are not closing any doors, we can always do more in the future without breaking people’s interactive notebooks.

However, this can be extended in many ways:

  • Infer the type of a variable from the RHS
  • Potentially use implicit typing, but that has issues, such as dimensions and other attributes that can be inferred from RHS
  • Not require call
  • Shorten the Fortran syntax in any possible way. For example, I was going to experiment with keeping the exact Fortran semantics, but totally change the syntax, such as using Rust style syntax. It’s doable. The question is, do we want this?

Now, being an ahead of time compiler means LFortran will parse this to AST and transform AST to ASR. Once in ASR, it is indistinguishable from regular Fortran (except the loose statements and expressions at global scope), so it is indeed possible to transform back to regular Fortran. Even the loose statements and expressions can be put into subroutines.

In practice, that can look as easy as:

%%show_fortran
a = 5.d0

And LFortran will print as the cell output the transformed code:

subroutine interactive_prompt_1()
integer, parameter :: dp = kind(0.d0)
real(dp) :: a
a = 5._dp
end subroutine

Or something like that. Then you can take it and do something with a.

So that can all be done. I welcome any suggestions. For now, we are just doing the minimal approach. Note that even the minimal approach is not too bad at all — even for interactive use. I quite like it. And as I said above, we can still do a lot more. Just keep brainstorming.

5 Likes