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):
importDefines import C defines as procs or vars with {.importc.}
importFuncDefines import C define funcs as procs with {.importc.}
reorderComments reorder C comments to match Nim's postfix style
mangle:PEG=FORMAT extra PEG expression to mangle identifiers, for example --mangle:'{u?}int{\d+}_t=$1int$2' to convert C to Nim equivalents
stdints Mangle C stdint's into Nim style int's
delete:IDENT option to delete nodes with matching idents for procs, types, or vars
mergeBlocks merge similar adjacent blocks like two let sections
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 intoc2nim
and have made a number of improvements and features to automated things. I've also fixed a number of issues andc2nim
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'sint32_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):
{.importc.}
{.importc.}
--mangle:'{u?}int{\d+}_t=$1int$2'
to convert C