caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: blue storm <bluestorm.dylc@gmail.com>
To: Mykola Stryebkov <nick@mykola.org>,
	David Allsopp <dra-news@metastack.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Generation of Java code from OCaml
Date: Thu, 24 Sep 2009 11:45:30 +0200	[thread overview]
Message-ID: <527cf6bc0909240245g11ce241cr16ac482003bb25f2@mail.gmail.com> (raw)
In-Reply-To: <001a01ca3ced$8ef58050$ace080f0$@metastack.com>

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


  reply	other threads:[~2009-09-24  9:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-23 18:15 Mykola Stryebkov
2009-09-23 19:57 ` [Caml-list] " Richard Jones
2009-09-23 22:54   ` Mykola Stryebkov
2009-09-24  8:03     ` David Allsopp
2009-09-24  9:45       ` blue storm [this message]
2009-09-24 11:18         ` Martin Jambon
2009-09-24 12:02           ` blue storm
2009-09-24 12:19             ` Martin Jambon
2009-10-03 12:16               ` Anil Madhavapeddy
2009-10-03 17:27                 ` blue storm
2009-10-03 18:29                   ` Anil Madhavapeddy
2009-09-26  7:37             ` ygrek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=527cf6bc0909240245g11ce241cr16ac482003bb25f2@mail.gmail.com \
    --to=bluestorm.dylc@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=dra-news@metastack.com \
    --cc=nick@mykola.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).