Fortran interface to Tcl's regex engine

This is interface is self-contained. It has a copy of the relevant C files (unmodified) from Tcl9.1 along with a shim and the Fortran interface.

This has been a back-burner project of mine for the last ten years but with the advent of Claude I realized it might actually be possible to do it. I wrote this with extensive help from Claude 4.6 who identified the C-files to copy, wrote the shim and wrote the initial interface. I specified the directory structure, added the object interface, refactored into a submodule and added the example. After uploading to github I got Claude 4.7 to look at it and it identified some memory leak issues which I then fixed. It seems that 4.7’s Fortran knowledge is discernibly better than that of 4.6.

The code builds with gfortran 15.2.0 and ifx 2025.2.1 (gcc 15.2.0 for the C code).

The code can be found at
https://github.com/sgeard/regex_f

To read more about Tcl’s regex engine try
https://wiki.tcl-lang.org/page/Regular+Expression+Examples

6 Likes

Nice!

The more high-quality Fortran code we write and make publicly available as open source, the more code AI has to learn on and the better it gets at writing it.

If you want some tips to further improve your project:

  • Add more examples and tests
  • Add an example usage into the README
  • You can use Claude to create documentation, then iterate with AI on it until you are happy
  • Use fpm as the build system (you can keep your Makefile as well with the same directory structure that fpm would use). You can ask Claude to do it.

I agree with all those. I did have the example in the README but removed it 'cos it seems to be pointless but maybe if that’s all people look at in the first instance I should add it. I can also try to build with lfortran and flang and record the results. AI is definitely the way to go with fpm - it’s how I did it the last couple of times.

1 Like

I’ve looked some more, I can now build with both flang and lfortran and I’ve pasted the example into the README.
As far as I can see the project can’t be built with fpm because there is no mechanism to exclude some of the c files from building - it just compiles everything it finds.

1 Like

As far as I can see the project can’t be built with fpm because there is no mechanism to exclude some of the c files from building - it just compiles everything it finds.

We might need to implement this in fpm. Why do you need to skip some C files?

It’s because some of the c files are just in #include lines and not designed to be compiled directly. I could change the code so that those files have a different extension but my design for this project is that the tcl files are used as they are.

I often suggest that people add topic tags to their github repos, such as regex fortran tcl for this project, as explained at Classifying your repository with topics - GitHub Docs .

Would your design allow you to move the included files into the include/ directory? The files in include/ are used by #include directives and INCLUDE statements but otherwise not compiled.

2 Likes

Thanks, I didn’t know about that. I’ve added some topics now as you suggested.

I did think about that but don’t want to do it. Currently the re_engine and utf8proc directories contain just the necessary subset of files copied from the tcl source. There is a compat subdirectory for all other c code. This way it’ll be very easy for me to check for any future modifications. I also tend to shy away from making changes to projects just to accommodate tools. I don’t mind moving programs into ‘test’ and source code down to ‘src’ but changing the relative structure just for fpm is a bit too much.

On fpm, I’ve now updated the directory structure as much as I can to accommodate fpm. If it gains the ability to filter out files by name then it should all just work.

1 Like

I created an issue for this at Allow fpm to skip C files from compiling in src/ · Issue #1279 · fortran-lang/fpm · GitHub.

2 Likes