Nimterop is a Nim package that aims to make C/C++ interop seamless
Find a file
Ganesh Viswanathan d8c75a43f7 Plugin support
2019-01-27 22:05:54 -06:00
build fix gitignore (#64) 2019-01-24 22:15:24 -06:00
nimterop Plugin support 2019-01-27 22:05:54 -06:00
tests Plugin support 2019-01-27 22:05:54 -06:00
.gitignore properly ignore all files wo extension 2019-01-25 14:14:32 -06:00
.travis.yml Plugin support 2019-01-27 22:05:54 -06:00
appveyor.yml Plugin support 2019-01-27 22:05:54 -06:00
config.nims Mac support 2018-11-21 17:13:36 -06:00
LICENSE Initial version 2018-11-20 03:01:04 -06:00
nimterop.nimble fix gitignore (#64) 2019-01-24 22:15:24 -06:00
README.md Fix #56 - override and skip, html links 2019-01-23 19:40:01 -06:00
toast.nim Plugin support 2019-01-27 22:05:54 -06:00

Chat on Gitter Build status Build Status

Nimterop is a Nim package that aims to make C/C++ interop seamless

Nim has one of the best FFI you can find - importing C/C++ is supported out of the box. All you need to provide is type and proc definitions for Nim to interop with C/C++ binaries. Generation of these wrappers is easy for simple libraries but quickly gets out of hand. c2nim greatly helps here by parsing and converting C/C++ into Nim but is limited due to the complex and constantly evolving C/C++ grammar. nimgen mainly focuses on automating the wrapping process and fills some holes but is again limited to c2nim's capabilities.

The goal of nimterop is to leverage the tree-sitter engine to parse C/C++ code and then convert relevant portions of the AST into Nim definitions. tree-sitter is a Github sponsored project that can parse a variety of languages into an AST which is then leveraged by the Atom editor for syntax highlighting and code folding. The advantages of this approach are multifold:

  • Benefit from the tree-sitter community's investment into language parsing
  • Avoid depending on Nim compiler API which is evolving constantly and makes backwards compatibility a bit challenging

Most of the functionality is contained within the toast binary that is built when nimterop is installed and can be used standalone similar to how c2nim can be used today. In addition, nimterop also offers an API to pull in the generated Nim content directly into an application.

The nimterop feature set is still limited to C but is expanding rapidly. C++ support will be added once most popular C libraries can be wrapped seamlessly.

Given the simplicity and success of this approach so far, it seems feasible to continue on for more complex code. The goal is to make interop seamless so nimterop will focus on wrapping headers and not the outright conversion of C/C++ implementation.

Installation

Nimterop can be installed via Nimble:

nimble install nimterop -y

or:

git clone http://github.com/genotrance/nimterop && cd nimterop
nimble install -y

This will download and install nimterop in the standard Nimble package location, typically ~/.nimble. Once installed, it can be imported into any Nim program. Note that the ~/.nimble/bin directory needs to be added to the PATH for nimterop to work.

Usage

import nimterop/cimport

cDebug()
cDefine("HAS_ABC")
cDefine("HAS_ABC", "DEF")
cIncludeDir("clib/include")
cImport("clib.h")

cCompile("clib/src/*.c")

Refer to the tests directory for examples on how the library can be used.

The toast binary can also be used directly on the CLI. The --help flag provides more details.

Detailed documentation is available for cimport and git.

Implementation Details

In order to use the tree-sitter C library, it has to be compiled into a separate binary called toast (to AST) since the Nim VM doesn't yet support FFI. toast takes a C/C++ file and runs it through the tree-sitter API which returns an AST data structure. This can then be printed out to stdout in a Lisp S-Expression format or the relevant Nim wrapper output. This content can be saved to a .nim file and imported if so desired.

Alternatively, the cImport() macro allows easier creation of wrappers in code. It runs toast on the specified header file and injects the generated wrapper content into the application at compile time. A few other helper procs are provided to influence this process.

toast can also be used to run the header through the preprocessor which cleans up the code considerably. Along with the recursion capability which runs through all #include files, one large simpler header file can be created which can then be processed with c2nim if so desired.

The tree-sitter library is limited as well - it may fail on some advanced language constructs but is designed to handle them gracefully since it is expected to have bad code while actively typing in an editor. When an error is detected, tree-sitter includes an ERROR node at that location in the AST. At this time, cImport() will complain and continue if it encounters any errors. Depending on how severe the errors are, compilation may succeed or fail. Glaring issues will be communicated to the tree-sitter team but their goals may not always align with those of this project.

Credits

Nimterop depends on tree-sitter and all licensing terms of tree-sitter apply to the usage of this package. Interestingly, the tree-sitter functionality is wrapped using c2nim and nimgen at this time. Depending on the success of this project, those could perhaps be bootstrapped using nimterop eventually.

Feedback

Nimterop is a work in progress and any feedback or suggestions are welcome. It is hosted on GitHub with an MIT license so issues, forks and PRs are most appreciated.