From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: ** X-Spam-Status: No, score=2.2 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 85878BBAF for ; Thu, 24 Sep 2009 11:45:32 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvYBAATcukrRVdK1i2dsb2JhbACRSYh4PwEBAQoLCgcRBakwgTGPcgEDAgSEFwU X-IronPort-AV: E=Sophos;i="4.44,444,1249250400"; d="scan'208";a="47183635" Received: from mail-yx0-f181.google.com ([209.85.210.181]) by mail4-smtp-sop.national.inria.fr with ESMTP; 24 Sep 2009 11:45:32 +0200 Received: by yxe11 with SMTP id 11so1914503yxe.15 for ; Thu, 24 Sep 2009 02:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=xMtTmFq1D5yuAQMFm618zqNRuW0LRRQnwszM+3RlLKo=; b=o8zivNM5QXexMhOs6K+TKDE/VKjaGChu1Nqdjb+IuPv5LxFi/hnKKIsLZgbUlb8FPj R5BxpDmji8M6lRrxEGR5LcyjDWeU4mhxPMfuHu/CuOdOzwWzY9kAu+xv0LPfOFs/VwSo /mfm0uDWn9eC5sCM22GVjynPnYRJ7LLS4bLEk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=AyU5trNupicQ6cZIb2oQBEfwcBgdNmikyP3MIfB9CeIRD2KdBwHNd6hSU/oos5+IyT kmWbJ3zaUDmL/D07Ce0NgDV+6dgjBDoPKNoFdXnUV5KkJRplzaEoY6b0HDLsspxr1C+1 aadawtjx9tkodgGyxwm8xosFZ5Xx84XbR/sAU= MIME-Version: 1.0 Received: by 10.101.95.6 with SMTP id x6mr3930560anl.108.1253785530757; Thu, 24 Sep 2009 02:45:30 -0700 (PDT) In-Reply-To: <001a01ca3ced$8ef58050$ace080f0$@metastack.com> References: <46ACB6F9-FFAE-4C9C-9E46-21CA2E93434C@mykola.org> <20090923195713.GA12236@annexia.org> <001a01ca3ced$8ef58050$ace080f0$@metastack.com> Date: Thu, 24 Sep 2009 11:45:30 +0200 Message-ID: <527cf6bc0909240245g11ce241cr16ac482003bb25f2@mail.gmail.com> Subject: Re: [Caml-list] Generation of Java code from OCaml From: blue storm To: Mykola Stryebkov , David Allsopp Cc: caml-list@yquem.inria.fr Content-Type: text/plain; charset=ISO-8859-1 X-Spam: no; 0.00; ocaml:01 ocaml:01 defs:01 printf:01 sprintf:01 printf:01 sprintf:01 bool:01 bool:01 defs:01 def:01 typedefs:01 typedef:01 typedef:01 storm:98 In case it helps, below is a basic patch against json-static (SVN trunk, 3.10 version). On your example it produces the following code : let javadef_of_create_order_response = "public class Create_order_response {\n\tprivate int order_id;\n\tprivate String order_code;\n\tprivate int order_price;\n\tpublic Create_order_response { ... }\n}" Wich, printed to sdout, gives the following output : public class Create_order_response { private int order_id; private String order_code; private int order_price; public Create_order_response { ... } } I choosed to generate the Java code inside an ocaml string declared inside the program to keep coherency with the other json code generators, but if you want you could easily output the java code to a separate file, and that would remove some of the string escaping in play. --- json-static/pa_json_static.ml.310 2009-09-24 11:40:28.000000000 +0200 +++ json-static-new/pa_json_static.ml.310 2009-09-24 11:39:52.000000000 +0200 @@ -1,3 +1,4 @@ +# 1 "pa_json_static.ml.310" (* Conversion between OCaml types and JSON types as provided by the json-wheel library. @@ -532,6 +533,39 @@ <:str_item< value rec $defs$ >> +let make_javadef _loc l = + let rec print name (_loc, l) = match l with + | Record r -> + let class_name = String.capitalize name in (* TODO : real java naming conventions *) + Printf.sprintf + "public class %s {\\n\ + \\t%s;\\n\ + \\tpublic %s { ... }\\n\ + }" (* TODO : real constructor body *) + class_name + (String.concat ";\\n\\t" (List.map print_field r)) + class_name + | _ -> Loc.raise _loc + (Failure "javadef patch draft : only records are supported") + and print_field f = + Printf.sprintf "private %s %s" + (print_type f.field_type) + f.field_caml_name + and print_type (_loc, t) = match t with + | String -> "String" + | Bool -> "bool" + | Int -> "int" + | Float -> "double" + | _ -> Loc.raise _loc + (Failure "javadef patch draft : only base types are supported") in + let defs = + List.fold_right + (fun ((_loc, name), x) acc -> + let fname = "javadef_of_" ^ name in + <:binding< $lid:fname$ = $str:print name x.def$ and $acc$ >>) + l <:binding<>> in + <:str_item< value $defs$ >> + let expand_typedefs _loc l = check_unique (fun (name, _) -> name) l; let names = @@ -541,7 +575,8 @@ let typedef = make_typedef _loc names l in let ofjson = make_ofjson _loc l in let tojson = make_tojson _loc l in - <:str_item< $typedef$; $ofjson$; $tojson$ >> + let javadef = make_javadef _loc l in + <:str_item< $typedef$; $ofjson$; $tojson$; $javadef$>> let o2b = function None -> false | _ -> true