Source

ocaml-bert / bert.ml

Diff from to
       print_endline ""
   in
     print_ 0;;
+
+
+(* encode to BERT binary: erlterm -> string
+   In future, this should be in C code *)
+let encode term = function 
+  | Int(i) when i < 256 -> (* SMALL_INTEGER_EXT *)
+      Printf.sprintf "a%.2c" (char_of_int i);
+  | Int(i) when i < 0xFFFFFFFF -> (* SMALL_INTEGER_EXT *)
+      Printf.sprintf "b%d" i;
+  | Float(f) -> (* FLOAT_EXT *)
+      "c";
+  | Atom(atom) when String.length atom > 255 -> (* ATOM_EXT *) "d";
+  | Tuple(t) when List.length t < 256 -> (* SMALL_TUPLE_EXT *)
+      "h";
+  | Tuple(t) -> (* LARGE_TUPLE_EXT *)
+      "i";
+  | String(str)-> (* STRING_EXT *) "k";
+  | List(list)->  (* LIST_EXT *) "l";
+  | Binary(bin)-> (* BINARY_EXT *) "m";
+  | Int(i) -> (* SMALL_BIG_EXT *) "n"; (* LARGE_BIG_EXT -> "o" *)
+  | Atom(atom) -> (* SMALL_ATOM_EXT *) "q";
+  | _ -> raise Unknown_type;;
+