Skip to content

Commit

Permalink
feat: empty function parameters become unit param (#13)
Browse files Browse the repository at this point in the history
Directly match out an empty list rather than using empty string "" for
label.
  • Loading branch information
omnisci3nce authored Mar 9, 2024
1 parent 75887e2 commit 37c3a69
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
21 changes: 14 additions & 7 deletions bindgen/caml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ let rec core_type_from_ir typ =
| Ir.Prim Char -> Typ.constr (lid "char") []
| Ir.Prim Void -> Typ.constr (lid "unit") []
| Ir.Ptr t -> core_type_from_ir t
| Ir.Func { fn_ret; fn_params } ->
List.fold_left
(fun acc (name, typ) ->
let label = Asttypes.Labelled name in
let typ = core_type_from_ir typ in
Typ.arrow label typ acc)
(core_type_from_ir fn_ret) fn_params
| Ir.Func { fn_ret; fn_params } -> (
match fn_params with
| [] ->
(* If the C function declaration has no parameters we must introduce a `unit` param *)
Typ.arrow Asttypes.Nolabel
(core_type_from_ir (Ir.Prim Void))
(core_type_from_ir fn_ret)
| params ->
List.fold_left
(fun acc (name, typ) ->
let label = Asttypes.Labelled name in
let typ = core_type_from_ir typ in
Typ.arrow label typ acc)
(core_type_from_ir fn_ret) params)

let type_from_ir typ =
match typ with
Expand Down
6 changes: 6 additions & 0 deletions examples/caml_doggo.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ void caml_eleven_out_of_ten_majestic_af(value caml_pupper) {
CAMLreturn0;
}

void caml_no_input_no_output() {
CAMLparam0();
no_input_no_output();
CAMLreturn0;
}


3 changes: 3 additions & 0 deletions examples/doggo.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ void eleven_out_of_ten_majestic_af(Doggo* pupper) {
printf("doggo is a %s\n", BreedToString[pupper->breed]);
}

void no_input_no_output(void) {
printf("We are doing nothing (of importance)\n");
}
2 changes: 2 additions & 0 deletions examples/doggo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ typedef struct Doggo {
} Doggo;

void eleven_out_of_ten_majestic_af(Doggo* pupper);

void no_input_no_output(void);
1 change: 1 addition & 0 deletions examples/doggo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ type nonrec doggo = {
wow: char }
external eleven_out_of_ten_majestic_af :
pupper:doggo -> unit = "caml_eleven_out_of_ten_majestic_af"
external no_input_no_output : unit -> unit = "caml_no_input_no_output"

0 comments on commit 37c3a69

Please sign in to comment.