(* wrappi * Copyright (C) 2011 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) (* 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 Camlp4.PreCast open Syntax open Ast 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$ >> (* Convert a _loc to an AST. *) let expr_of_loc _loc loc = let file_name, start_line, start_bol, start_off, stop_line, stop_bol, stop_off, ghost = Loc.to_tuple loc in <:expr< Camlp4.PreCast.Loc.of_tuple ($str:file_name$, $`int:start_line$, $`int:start_bol$, $`int:start_off$, $`int:stop_line$, $`int:stop_bol$, $`int:stop_off$, $`bool:ghost$) >> let add_entry_point _loc name parameters return_type code = 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 let loc = expr_of_loc _loc _loc in <:str_item< let ep = { Wrappi_types.ep_loc = $loc$; 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; (* Default quotation expander (<< .. >>) should be C code ("c"). *) Quotation.default := "c" ;; (* Extend the regular OCaml grammar. *) EXTEND Gram GLOBAL: str_item; (* A parameter or return type. *) any_type: [ [ "int32" -> <:expr< Wrappi_types.TInt32 >> ] | [ "int64" -> <:expr< Wrappi_types.TInt64 >> ] | [ t = LIDENT -> <:expr< Wrappi_types.Type $str:t$ >> ] ]; (* A return type. *) return_type: [ [ "err" -> <:expr< Wrappi_types.RErr >> ] | [ t = any_type -> <:expr< Wrappi_types.Return $t$ >> ] ]; (* A single function parameter. *) parameter: [[ t = any_type; name = LIDENT -> (name, t) ]]; str_item: LEVEL "top" [ [ "entry_point"; return_type = return_type; name = LIDENT; "("; parameters = LIST0 parameter SEP ","; ")"; code = OPT [ code = expr -> code ] -> add_entry_point _loc name parameters return_type code ] ]; END