RFortran: a WIP Fortran compiler and interpreter in Rust

On Oct 9, while updating my list of Fortran tools, I tried the GitHub project f90c of SirPigari, a “Single executable Fortran 90 compiler written in Rust”, finding that it compiled some basic programs but failed on others. It generates native object code using Cranelift. By giving Codex and Claude programs to compile and language features to add, I have created a cross-platform compiler that implements much of Fortran 2023, excluding coarrays, summarized here. Implemented Fortran 2023 additions include conditional expressions and arguments, SIMPLE procedures, enumeration and enum types, TYPEOF/CLASSOF declarations, and DO CONCURRENT reductions (currently executed sequentially). It also supports SPLIT, TOKENIZE, degree and half-revolution trigonometric intrinsics, and the AT edit descriptor for trimmed character output. Parameterized derived types have partial support, including KIND and LEN parameters, constructors, inheritance, and selected forms of runtime and deferred-length parameters. It runs on at least Windows x64, Linux x86-64 and macOS Apple Silicon.

On a few kernels it was about 2 times slower than gfortran. It has many warning and debugging options. It has interpreter and REPL modes you can try here. It’s an experiment to see if LLMs can write a Fortran compiler in Rust and is far from being a production compiler, but it may be useful in checking code with its debugging options and for trying new language features. Work is ongoing, and bug reports are welcome. It is GPL-3.0 licensed overall since f90c is, but 85% of the code is new and MIT licensed.

11 Likes

I am always excited to see a Fortran compiler able to detect uninitialized variables. This is pretty amazing.

Is it possible to use the Rfortran playground in my browser by specifying a filename for the Fortran code, instead of cut-and-paste? I notice your python to Fortran translator has an upload file button, but the Rfortran playground does not. Also, if it runs in my browser, what is the need for the limitations on code size and execution time?

1 Like

Those are now configurable under Execution limits, and the Open source button lets you upload code from your computer or the web. Glad you like the compiler.

Very interesting! What do you see as the key advantages of Cranelift for Fortran?

RFortran uses Cranelift because the parent project f90c did. Cranelift being in Rust is convenient when the rest of the project is. I am not qualified to compare compiler backends, but I posed your question to GPT 6.1 Sol, which concluded

For an alpha compiler intended to make Fortran features easy to experiment with, Cranelift is a useful choice. For peak HPC performance, LLVM or GCC has the stronger starting position. An additional optimizing backend could eventually complement Cranelift if benchmarks justify the development effort.

The full answer is here.

In another thread @urbanjost asked “Can we at least ban direct use of numeric kind type parameters?”

RFortran is unlikely to ever match the speed of other compilers, but I have added many warning options, including -Wliteral-kind. In the RFortran playground, with -Weverything enabled, for the program

implicit none
integer, parameter :: rkind = 8, ikind = 4
real(8) :: x
integer(4) :: i
real(rkind) :: y
integer(ikind) :: j
print*,huge(x), huge(y), huge(i), huge(j)
end

you get the warnings

warning: kind number in `real(8)` is processor-dependent [literal-kind]
  ┌─ /program.f90:3:1
  │
3 │ real(8) :: x
  │ ^^^^^^^ use real(real64)
  │
  = the names come from `use iso_fortran_env, only: real64`
  = a named constant such as `integer, parameter :: dp = kind(1.0d0)` or `selected_real_kind(15, 307)` also works
  = -Wno-literal-kind disables this warning

warning: kind number in `integer(4)` is processor-dependent [literal-kind]
  ┌─ /program.f90:4:1
  │
4 │ integer(4) :: i
  │ ^^^^^^^^^^ use integer(int32)
  │
  = the names come from `use iso_fortran_env, only: int32`
  = a named constant such as `integer, parameter :: dp = kind(1.0d0)` or `selected_real_kind(15, 307)` also works
  = -Wno-literal-kind disables this warning

warning: `rkind` is used as a REAL kind but is set to the kind number 8, which is processor-dependent [literal-kind]
  ┌─ /program.f90:2:31
  │
2 │ integer, parameter :: rkind = 8, ikind = 4
  │                               ^ kind number
  ·
5 │ real(rkind) :: y
  │ ----------- used as a kind here
  │
  = suggestion: integer, parameter :: rkind = real64
  = the name comes from `use iso_fortran_env, only: real64`
  = `kind(1.0d0)` or `selected_real_kind(15, 307)` also work
  = -Wno-literal-kind disables this warning

warning: `ikind` is used as an INTEGER kind but is set to the kind number 4, which is processor-dependent [literal-kind]
  ┌─ /program.f90:2:42
  │
2 │ integer, parameter :: rkind = 8, ikind = 4
  │                                          ^ kind number
  ·
6 │ integer(ikind) :: j
  │ -------------- used as a kind here
  │
  = suggestion: integer, parameter :: ikind = int32
  = the name comes from `use iso_fortran_env, only: int32`
  = -Wno-literal-kind disables this warning

and with -Werror you can turn warnings into errors.

Not to be confused with this RFortran.

https://doi.org/10.1016/j.envsoft.2010.05.007

I vote for a new name. How can it not be “Rustran”.

1 Like