Version française
Home     About     Download     Resources     Contact us    
Browse thread
Generation of Java code from OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: blue storm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] Generation of Java code from OCaml
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