Skip to content

Commit 97bd254

Browse files
committed
Start on the C api
1 parent a2b2bc0 commit 97bd254

File tree

6 files changed

+441
-0
lines changed

6 files changed

+441
-0
lines changed

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@
482482
inputsFrom = [ (mkCraneArtifacts { inherit rust; profile = "dev"; }).cargoArtifactsDeps ];
483483

484484
buildInputs = [
485+
pkgs.cargo-c
485486
pkgs.cargo-flamegraph
486487
pkgs.cargo-insta
487488
pkgs.cargo-nextest

nickel/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ repository.workspace = true
1010
homepage.workspace = true
1111
readme.workspace = true
1212

13+
[features]
14+
capi = []
15+
1316
[dependencies]
1417
codespan-reporting.workspace = true
1518
indexmap.workspace = true

nickel/capi-tests/run_tests.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stddef.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
6+
#include "nickel_lang.h"
7+
8+
const char* EXAMPLE = " \
9+
{ \
10+
foo = 1, \
11+
} \
12+
";
13+
14+
int main() {
15+
Context *ctx = nickel_context_new();
16+
Expr *expr = nickel_expr_alloc();
17+
Error *error = NULL;
18+
nickel_context_eval_deep(ctx, EXAMPLE, expr, &error);
19+
20+
printf("%p %p\n", expr, error);
21+
22+
printf("is record: %d\n", nickel_expr_is_record(expr));
23+
Record const *rec = nickel_expr_as_record(expr);
24+
printf("record len: %ld\n", nickel_record_len(rec));
25+
26+
char const* key;
27+
uintptr_t len;
28+
Expr *val = nickel_expr_alloc();
29+
nickel_record_key_value_by_index(rec, 0, &key, &len, val);
30+
31+
char *key_str = malloc(len + 1);
32+
memcpy(key_str, key, len);
33+
key_str[len] = '\0';
34+
35+
printf("key %s, value number: %d\n", key_str, nickel_expr_is_number(val));
36+
37+
nickel_expr_free(val);
38+
nickel_expr_free(expr);
39+
nickel_context_free(ctx);
40+
}

nickel/cbindgen.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
header = "// SPDX-License-Identifier: MIT"
2+
sys_includes = ["stdint.h"]
3+
no_includes = true
4+
include_guard = "NICKEL_LANG_H"
5+
tab_width = 4
6+
style = "Type"
7+
language = "C"
8+
cpp_compat = true
9+
10+
[export]
11+
12+
[const]
13+
allow_static_const = false
14+
allow_constexpr = false

0 commit comments

Comments
 (0)