Skip to content

Commit

Permalink
Docs generated from: feat: Adds flag to always output C id with heade…
Browse files Browse the repository at this point in the history
…rparse (#1353)

Adds -c|--emitcname flag to headerparse to always emit the C identifier
in `register` definitions (useful when the register will be in a
module).

Fixes a bug where the kebab case flag would not output C identifiers
making the emitted C identifiers not match with the ones in the headers.

Adds docs entry about headerparse in CInterop doc.

Makes headerparse emit `CChar` instead of `Char` when encountering a
signature containing `char`.
  • Loading branch information
carp-lang committed Nov 3, 2021
1 parent 706cbfb commit 66099e8
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions CInterop.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This is an extension of what is covered in the [Language Guide](./LanguageGuide.
- [`preproc`](#unsafe-preproc)
- [Registering Types](#register-types)
- [Callbacks](#callbacks)
- [Headerparse](#headerparse)


## How Carp generates identifiers
Expand Down Expand Up @@ -558,3 +559,39 @@ it is the responsibility of the caller to ensure the operation is safe. It is
also important to ensure the lifetime of the `Ptr` doesn't not exceed the
lifetime of the function/env it represents.

## Headerparse

`headerparse` is a Haskell script to aid in writing C bindings by parsing a C
header and generating `register` and `register-type` for you. It resides in the
`./headersparse` folder in Carp source repo and can be used in the following
way:

```sh
stack runhaskell ./headerparse/Main.hs -- ../path/to/c/header.h
```

The script accepts the following flags:

* `[-p|--prefixtoremove thePrefix]` Removes a prefix from the C identifiers
* `[-f|--kebabcase]` Converts identifiers to kebab-case
* `[-c|--emitcname]` Always emit the C identifier name after the binding

### Example

Invoking the script on this C header:

```sh
stack runhaskell ./headerparse/Main.hs -- -p "MyModule_" -f ../path/to/aheader.h
```

```c
// aheader.h
bool MyModule_runThisFile(const char *file);
```
Will output the following:
```clojure
(register run-this-file (λ [(Ptr CChar)] Bool) "MyModule_runThisFile")
```

0 comments on commit 66099e8

Please sign in to comment.