Precompiler for Fortran 202y Templates?

Hallo,

while there now exists a draft for Fortran templates it wont become part of the language standard for several years.

So I ask myself whether a program that takes Fortran 202y template code as an input and generates Fortan 2008/2018-comforming code would in principle be a sensible idea.

I know that it would not fully replace a implementation in a compiler as there are difficulties how to manage the processed and unprocessed code, but maybe it would be useful for experimenting (It might not be very useful to use templates in important productive code before their specification is fixed).

I will likely not be able to write such a preprocessor (at least not one that supports al features of Fortran templates) nor do I expect anybody else to write such code, but I am interested about your opinion, whether such a preprocessor would useful until there are compilers supporting Fortran templates or whether you think that such an approach would rather cause problems than promote Fortran.

2 Likes

I think it might be possible, but I suspect you’re likely to end up implementing a whole parser and AST to be able to do it well. At which point, you’ve practically implemented a compiler, so… maybe spend the effort implementing it in an open-source compiler?

3 Likes

If you are talking about Generics, LFortran is in the process of implementing them, see PR Initial implementation of generics by certik · Pull Request #39 · lfortran/lfortran · GitHub, so you can at least expect LFortran to be able to compile to binary such code in the next couple of years (hopefully way sooner).

That is one of the core ideas behind LFortran, rapid prototyping of potential additions to the Standard to help the community understand how useful a certain feature would be.

10 Likes

@Jweber yes, not only is this possible, that is exactly our plan with LFortran. The main branch now supports a small subset of the templates. Over the coming months we’ll expand it. We’ll then write transformations that can produce regular Fortran 2018 code (where all the templates will be instantiated), so that you could use other compilers as well, not only LFortran.

8 Likes

Hallo Certik,
That is really great news.
Thank you for your great work.

2 Likes