beef331 / website

Code for the official Nim programming language website
https://nim-lang.org
19 stars 1 forks source link

This Month With Nim: c2nim upgrade #73

Closed elcritch closed 1 year ago

elcritch commented 1 year ago

Name: c2nim upgrades

Author: elcritch

Posting:

c2nim is the standard goto for many when wrapping C projects for Nim. However, after using it for several projects I noticed I was doing the same things every time I wrapped a C project. So I dug into c2nim and have made a number of improvements and features to automated things. I've also fixed a number of issues and c2nim can now parse the entirety of various system headers and handles things like function pointers with function pointers.

One of my favorite is the --reorderComments option. It takes the common C pattern of putting docs before a function and converts it to Nim's comment style. This alone means that 90% of imported docs will become proper Nim docs associated to the right function! Well mostly. ;) Another favorite is --stdints that converts modern C's int32_t to the proper Nim equivalent.

Note that all options should now be available from the CLI and the c2nim pragma's. Also, I've added support for using directives in the form of #pragma c2nim <option>. This let's you add c2nim annotations directly in normal C code and they'll be ignored by your compiler.

If others have ideas for improvements, create an issue. I'm doing this mainly for my own projects, but I like the idea of making c2nim basically be capable of automatically wrapping all C code. C++ has also gotten much better as well, but remains tricky. I've also got another project in the works to automate running c2nim on larger projects!

Here's an (incomplete list) of new options (some newer, some older):