Could LFortran work as an embedded scripting language in a compiled program

Plugging Julia in a Fortran process is also possible, and thanks to fpm, it is easy also.

Example

program main
use julia
use, intrinsic :: iso_c_binding
implicit none
real(kind=c_double), allocatable :: mat(:,:)

! Load Julia into Fortran process
call julia_init()

! Run Julia script.jl and save the return value into mat
call julia_run("script.jl", mat)

! Fortran can now use the values computed in Julia
print *, 'shape:', shape(mat)
print *, 'sum:', sum(mat)

! Bye
call julia_destroy()

end program

where script.jl is a Julia program,

using LinearAlgebra
# write code here...
# and return a matrix
return [1.1 2.2; 4.4 5.5]

This is in experimental status, but works (compute and access numbers, vectors, matrices and strings from Julia). The restriction is currently the GC, that is disabled. Need to translate Julia macros JL_GC_PUSH, JL_GC_POP “inline” in Fortran. I don’t know if this is possible and I welcome contributions under the MIT license!.

8 Likes