(* For general information about camlp4, see:
* http://brion.inria.fr/gallium/index.php/Camlp4
+ *
* For information about quotations, see:
* http://brion.inria.fr/gallium/index.php/Quotation
*)
open Wrappi_types
+(* Convert a plain list into a list AST. *)
+let rec expr_of_list _loc = function
+ | [] -> <:expr< [] >>
+ | h::t -> let t = expr_of_list _loc t in <:expr< $h$ :: $t$ >>
+
+(* Convert an optional type into an option AST. *)
+let expr_of_option _loc = function
+ | None -> <:expr< None >>
+ | Some x -> <:expr< Some $x$ >>
+
let add_entry_point _loc name parameters return_type code =
- (* XXX *)
- <:str_item< >>
+ let parameters = List.map (
+ fun (name, t) -> <:expr< ($str:name$, $t$) >>
+ ) parameters in
+ let parameters = expr_of_list _loc parameters in
+
+ let code = expr_of_option _loc code in
+
+ <:str_item<
+ let ep = { Wrappi_types.ep_name = $str:name$;
+ ep_params = $parameters$;
+ ep_return = $return_type$;
+ ep_code = $code$ } in
+ Wrappi_globals.add_entry_point ep
+ >>
let () =
(* Quotation expander for C code. *)
let c_quotation_expander _loc _ code =
(* XXX Expand %- or $- expressions in code. *)
+ (* XXX Escape >> in code. *)
ExStr (_loc, code)
in
Quotation.add "c" Quotation.DynAst.expr_tag c_quotation_expander;
(* A parameter or return type. *)
any_type: [
- [ "int32" -> TInt32 ]
- | [ "int64" -> TInt64 ]
- | [ t = LIDENT -> Type t ]
+ [ "int32" -> <:expr< Wrappi_types.TInt32 >> ]
+ | [ "int64" -> <:expr< Wrappi_types.TInt64 >> ]
+ | [ t = LIDENT -> <:expr< Wrappi_types.Type $str:t$ >> ]
];
(* A return type. *)
return_type: [
- [ "err" -> RErr ]
- | [ t = any_type -> Return t ]
+ [ "err" -> <:expr< Wrappi_types.RErr >> ]
+ | [ t = any_type -> <:expr< Wrappi_types.Return $t$ >> ]
];
(* A single function parameter. *)
- parameter: [[ t = any_type; name = LIDENT -> (t, name) ]];
+ parameter: [[ t = any_type; name = LIDENT -> (name, t) ]];
str_item: LEVEL "top" [
[ "entry_point";