C2nim: A tool to translate ANSI C code to Nim
github.com
github.com
proc csetvbuf(f: File,
buf: pointer,
mode: cint,
size: csize):
cint {.importc: "setvbuf",
header: "stdio.h".}
# ...intervening code...
discard csetvbuf(stdout, nil,
0, 16384)
c2nim is more helpful for larger C APIs with a lot of C structs and a wide set of entrypoints/API calls. For simple enough C code it can be used as a "starting point" to port the whole thing, but you will probably at least want to change things like a1, a2, etc. parameter names ("a" for "argument"). Nim has named arguments (like Python, f(x=2)) so such names are a bit more important. (Not that all code meant to be read by a human should not have good param names.)As long as extensions are careful to stay "syntactically isolated", the complexity of parsing C need not explode. E.g., at least on Linux where glibc headers are very gcc-tilted, the only extension tcc really needs is that __attribute__(()) stuff (maybe 1 or 2 other "just ignore this stuff" things).
While there can always be edge cases that are broken, I use tcc daily on Linux without issue. So, I would encourage people to not view C as an untamable complexity monster or be captive to prevailing implementations. { C++ is, of course, a more unfortunate story in this and so many other regards :-) }
Rust will have Rust/WinRT on Windows, Nim can also go down that path for example.
Anyway, the world of C extensions has mostly seemed syntactically judicious/surgical. That's more just cultural, not guaranteed by anything, but it makes me suggest (without knowing) that MS extensions would probably not be hard to support without complexity explosion. Apple, on the other hand, doing their own chips now and Swift and just generally being even more of a closed system..They seem most likely to make that hard someday if they ever think they can make more money by not supporting POSIX-like programs. :-( { EDIT - there will likely always be some demand for/a way to create C callable wrappers/headers for system services, though. So, I don't think that it's crazy to hold out hope that this will remain possible/not too bad for the foreseeable future. }
In what concerns macOS, UNIX certification is anyway only for CLI applications and daemons like software, stuff like graphics isn't part of UNIX trademark.
But if you wanted to port C to a "pure Nim" for whatever reasons (e.g. to also work at compile-time in the Nim VM or the Javascript backend) c2nim also works and can help. It may not work so well on fancier code, e.g. using a lot of cpp macros, etc. That tends to turn up more in .c files than .h files, but the boundary is obviously somewhat arbitrary.