Translate fortran to julia

I have a Fortran code and would prefer if it was a julia code instead :slight_smile: . The code heavily uses common blocks. Is there a semi automatic way to translate this into (non idiomatic, but executable) julia?

There are around 100 functions and almost 13000 lines of code, so I guess even some semi-automatic conversion will yield a lot of work and testing anyways :wink:

I think a better approach would be writing a Julia wrapper for it, adding tests and then replace the functions one by one (if you really want to replace everything).

3 Likes

The trouble with wrapping this is, that there is no API. There is the main function and all other functions are kind of private. Main reads tons of parameters via stdin and textfiles, calculates and writes to stdout and text files.
Instead of a wrapper, you need to implement a string generator + parser in order to use it.
I did this with similar codes before. It is also a tedious and buggy process as the string formats are not precisely specified and meant for humans instead of machines.
So before going through this again, I thought maybe I could translate the code and skip the intermediate string form.

I translated a couple of Fortran 90 packages using some hacky regex. It’s not pretty but it definitely saved me a lot of time.

You will probably need to edit it a bunch to suit the fortran version and coding style, and you’ll have to go through and deal with all the GOTOs manually if your FORTRAN has them.

I’m also not sure it runs on 1.0, its from the 0.6 era.

4 Likes

FYI, GitHub - JuliaCN/Py2Jl.jl: Python to Julia transpiler.

You can map fortran ASTs into the Julia’s. I’m not familiar with fortran but I’ve heard of that its language features are covered by Julia.

1 Like

I looked into this too, but came to the conclusion that if your goal is to eventually produce readable, idiomatic Julia code, for any one project tweaking some regex is far simpler and easier to customise than using a transpiler.

And where you don’t want to edit the code, you may as well just call the Fortran directly.

Yes, and I think there might be some Julia package providing fortran FFI like PyCall and RCall…

It’s actually built-in with ccall(). Probably even faster than calling Fortran functions from Fortran.

ccall is not that handy, and IMO it might make it difficult to compose your programs and debugging.

Yeah, composability is the main reason I rewrite in Julia. But if you don’t need composability, ccall() is easier than any kind of rewrite, as annoying as it is for the 50 argument functions that I have to deal with…

Transpiling wont get you composition either, you need an actual programmers brain to achieve that… I just think regex is a more flexible tool for the average programmer to speed up this process than a transpiler.

1 Like

Mhh in order to use your packates, would I have to write a FORTRAN parser?

Yes, however I’m quite enthusiastic about parsers, and feels like to help you with this if you can give me the FORTRAN grammar! Wouldn’t take much time but have to introduce a dependency called MLStyle…

2 Likes

I once spent around 2 months doing something like this, with the result turning out to be extremely fragile. A quick glance at that zip file does not show any tests, so unless you are convinced that the code is

  1. of good quality (tested, reasonably bug-free), and
  2. you cannot understand and reimplement it in Julia,

maybe you should think about just rewriting it in Julia. I recognize that this is an expensive and time-consuming process, but the alternatives may be worse.

1 Like

@thautwarm I am stunned by your offer. I think a fortran parser + transpiler would be a useful thing to have in julia. I found this grammar. It looks quite long and complex, but also systematic and clean.
I must also admit, that I am by no means a fortran programmer, let alone a language lawyer. So given a parser, all I would attempt is create a buggy transpiler for a small subset that I actually need.

Yeah, there are no automatic tests unfortunatelly. But at least the code is quite battle tested and I am confident that it is reasonably bug free. I don’t understand the physics well enough to attempt a rewrite in julia.

Thanks! I’ve browsed that source and this seems to be quite a big and hard task:

Total 172 terminal symbols: 98 keywords

I’ll try to make a prototype in 2 weeks, not to be full-featured but shall work for a subset.

1 Like

Again I am stunned by your will to help. However I cannot promise to write a transpiler on top of your parser. And even if I do it will be a hacky one. It is even possible that I decide against moving to julia altogether and use another approach (e.g. the hacky input generation + output parsing).

That’s okay because I live on this sort of stuffs. If you’re not very urgent of your works, I can make it for you(I cannot promise a fast implementation but always I am). Have you posted your codes at GitHub?

You mean code related to this subject? I do have some hacky string parsing + generation code, if you want to take a look, I can send you via email.

I want to have a knowledge about which features you’ve used in your fortran codes, and I don’t care about the others.

1 Like