4 let print_type name ocaml =
6 printf "type %s = %s\n" name ocaml
8 let print_let prefix name body =
9 printf "let %s_%s = %s\n" prefix name body
11 type map = (string*string) list
21 let regist name ~low ~high ~funs =
24 types = ["low",low;"high",high];
31 let cpool name ~high ~entry =
32 regist name ~low:"int" ~high
34 "byte" => "BytesOut.u30";
35 "read" => "BytesIn.u30";
36 "const" => sprintf "fun x -> Some ((%s x) :> Cpool.entry)" entry;
37 "arg" => sprintf "fun ctx x -> Cpool.index ctx#cpool (%s x)" entry;
43 regist name ~low:"int" ~high:"int"
45 "byte" => sprintf "BytesOut.%s" name;
46 "read" => sprintf "BytesIn.%s" name;
48 "arg" => "fun _ -> id";
54 regist "method_" ~low:"int" ~high:"method_"
56 "byte" => "BytesOut.u30";
57 "read" => "BytesIn.u30";
59 "method" => "fun x -> Some x";
61 "arg" => "fun ctx x -> index x ctx#methods"
64 regist "class_" ~low:"int" ~high:"class_"
66 "byte" => "BytesOut.u30";
67 "read" => "BytesIn.u30";
70 "class" => "fun x -> Some x";
71 "arg" => "fun ctx x -> index x ctx#classes"
76 regist "label" ~low:"(Label.t,int) either" ~high:"Label.t"
79 Left label -> label_ref label
80 | Right address -> s24 address";
81 "read" => "fun s -> Right (BytesIn.s24 s)";
85 "arg" => "fun _ x -> Left x"
88 regist "label_def" ~low:"Label.t" ~high:"Label.t"
90 "byte" => "fun l ->label l";
91 "read" => "fun _ -> Label.make()";
95 "arg" => "fun _ -> id"
98 cpool "c_int" ~high:"int" ~entry:"`Int";;
99 cpool "c_uint" ~high:"int" ~entry:"`UInt";;
100 cpool "c_string" ~high:"string" ~entry:"`String";;
101 cpool "c_float" ~high:"float" ~entry:"`Double";;
102 cpool "namespace" ~high:"Cpool.namespace" ~entry:"";;
103 cpool "multiname" ~high:"Cpool.multiname" ~entry:"";;
105 let print_field t fs =
106 ListLabels.iter !map ~f:begin fun {name;types;funs}->
107 print_type name @@ List.assoc t types;
108 List.iter (fun f -> print_let f name @@ List.assoc f funs) fs
112 match Sys.argv.(1) with
114 print_field "low" ["byte";"read"]
116 print_field "high" ["const";
121 failwith "usage: gen_typemap TYPE"