arkdro avatar arkdro committed 612599b

adding small_big_ext, id 110

Comments (0)

Files changed (3)

lib/ocamerl/ocamerl/eterm.ml

 let magic_string        = '\107'
 let magic_list          = '\108'
 let magic_binary        = '\109'
+let magic_small_big     = '\110'
 let magic_new_reference = '\114'
 
 
 
 type t =
     | ET_int    of Int32.t
+    | ET_bignum of Num.num
     | ET_float  of float
     | ET_atom   of string
     | ET_bool   of bool
 (* ETerm API *)
 
 let rec to_string t = match t with
+    | ET_bignum n-> Num.string_of_num n
     | ET_int n   -> Int32.to_string n
     | ET_float n -> string_of_float n
     | ET_atom a  -> a
 
 (* TODO can probably be optimized ... at least look at buffer! *)
 let rec _to_chars t = match t with
+    | ET_bignum (Num.Int n) ->
+        magic_large_int :: (Tools.chars_of_int32 (Int32.of_int n) 4)
+    | ET_bignum (Num.Big_int n) ->
+        magic_small_big
+        :: (Tools.chars_of_big_int n)
     | ET_int n when n < 256l ->
         magic_small_int :: [char_of_int (Int32.to_int n)]
     | ET_int n ->
         | n when n = magic_list    -> parse_list
         | n when n = magic_binary    -> parse_binary
         (* TODO small big *)
+        | n when n = magic_small_big -> parse_small_big
         (* TODO large big *)
         (* TODO new cache *)
         (* TODO cached atom *)
             n
         )
 
+and parse_small_big =
+    parser [< n = Tools.eint_n 1; s = Tools.eint_n 1; num = Tools.big_int_n n >] ->
+        match s with
+            | 0 ->
+              ET_bignum (Num.Big_int num)
+            | 1 ->
+              ET_bignum (Num.Big_int (Big_int.minus_big_int num))
+
 and parse_small_int =
     parser [< i = Tools.eint32_n 1 >] ->
         ET_int i

lib/ocamerl/ocamerl/eterm.mli

 (** Ocaml type corresponding to Erlang type.*)
 type t =
     ET_int of Int32.t
+  | ET_bignum of Num.num
   | ET_float of float
   | ET_atom of string
   | ET_bool of bool

lib/ocamerl/ocamerl/tools.ml

         | false ->
             failwith "cannot construct Int32.t from more than 4 chars"
 
+let big_int_of_chars chars =
+    List.fold_left
+        (fun acc c ->
+            Big_int.or_big_int
+                (Big_int.shift_left_big_int acc 8)
+                (Big_int.and_big_int
+                    (Big_int.big_int_of_int (int_of_char c))
+                    (Big_int.big_int_of_int 0xFF)
+                )
+        )
+        Big_int.zero_big_int
+        chars
+
+let chars_of_big_int n = failwith "chars_of_big_int not implemented yet"
+
 let rec _chars_of_int v chars n =
     match n > 0 with
         | true ->
     let chars = next_n n stream in
     int32_of_chars chars
 
+let big_int_n n stream =
+    let chars = next_n n stream in
+    big_int_of_chars chars
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.