Anonymous avatar Anonymous committed 86a4538

tuples and binaries hasn't passed the test yet.

Comments (0)

Files changed (2)

 open Str;;
 
-(*
-class pid = let a in 1;;
-class ref = let a in 1;;
-class port = Not_supported;;
-*)
-
 type erlterm =
     Int of int
   | Float of float
   | List of erlterm list 
   | String of string
   | Binary of string
-  | None;;
+  | Nil;;
 
 type result = 
   | Ok of erlterm * string
    throws exceptions: Not_supported | Unknown_type
    when more binaries  *)
 let decode_binary bin = 
-  let rec decode_list len binary list = 
+  let rec decode_tuple len binary list = 
     if len = 0 then
       Ok(List(list), binary)
     else 
       begin match local_decode binary with
+	| Ok(t, remain) ->  decode_tuple (len-1) remain (t::list);
+	| other -> other;
+      end 
+  and decode_list len binary list = 
+    if len = 0 then
+      begin match local_decode binary with
+	| Ok(Nil, remain) -> 
+	    Ok(List(list), remain);
+	| Ok(term, remain)->
+	    Ok(List( term::list ), remain );
+	| other -> other;
+      end
+    else 
+      begin match local_decode binary with
 	| Ok(t, remain) ->  decode_list (len-1) remain (t::list);
 	| other -> other;
       end
 	  raise Not_supported;
       | 'h' -> (* SMALL_TUPLE_EXT *)
 	  let len = int_of_char (String.get local_bin 1) in
-	    decode_list len (Str.string_after local_bin 2) []; (* is it OK to use list? *)
+	    decode_tuple len (Str.string_after local_bin 2) []; (* is it OK to use list? *)
       | 'i' -> (* LARGE_TUPLE_EXT *)
 	  let len = make_int32 (String.sub local_bin 1 5) in
-	    decode_list len (Str.string_after local_bin 5) []; (* is it OK to use list? *)
+	    decode_tuple len (Str.string_after local_bin 5) []; (* is it OK to use list? *)
       | 'j' -> (* NIL_EXT *)
-	  None;
+	  Ok( Nil, Str.string_after local_bin 1);
       | 'k' -> (* STRING_EXT *)
 	  let len = (int_of_char (String.get local_bin 1))*256 + (int_of_char (String.get local_bin 2)) in
 	    Ok( String( String.sub local_bin 3 len ), Str.string_after local_bin (len+3) );
 	  raise Not_supported;
       | 'F' -> (* NEW_FLOAT_EXT *)
 	  raise Not_supported;
-      | _-> raise Unknown_type;
+      | _-> 
+	  raise Unknown_type;
     end in
     begin match int_of_char (String.get bin 0) with
       | 131 -> (* magic number for BERT term; *)
 	  local_decode (Str.string_after bin 1);
-      | _ -> raise Unknown_type;
+      | _ -> 
+	  raise Unknown_type;
     end;;
 
-let print_binary_string bin = 
-  let rec print_ n =
-    if n < String.length bin then begin 
-      Printf.printf "%.2X " (int_of_char (String.get bin n));
-      print_ (n+1);
-    end else 
-      print_endline ""
-  in
-    print_string "binary: ";
-    print_ 0;;
-
 let print_binary bin = 
   let rec p_bin i =
     if i < (String.length bin) then begin
     end
   and print_term t = 
     begin match t with
+      | Nil ->
+	  print_string "Nil";
       | Int(i)-> Printf.printf "%d" i;
       | Float(f)-> Printf.printf "%f" f;
       | Tuple(tpl)-> begin
 	  print_char '"';
 	  print_string str;
 	  print_char '"';
-      | Binary(bin)-> begin
-	  print_binary bin ;
-	end;
-(*      | None ->
-	  print_string "Nil"; *)
-      | _->
-	  raise Unknown_type;
+      | Binary(bin)->
+	  print_binary bin;
+      | Atom(atom)->
+	  print_string atom;
+(*      | _->
+	  raise Unknown_type; *)
     end in
     print_term term;
     print_char '.';;
+
+let print_binary_string bin = 
+  let width = 16 in
+  let rec print_ n =
+    if n < String.length bin then begin
+      if (n mod width) = 0 then Printf.printf "%.4X: " n;
+      Printf.printf "%.2X " (int_of_char (String.get bin n));
+      if (n mod width) = (width-1) then print_endline "";
+      print_ (n+1);
+    end else 
+      print_endline ""
+  in
+    print_ 0;;
+
+234.
+1324567890.
+234.345.
+1.45e-5.
+manipulator.
+{tuple_test, 23, 354}.
 "hogehoge".
-234.
+"214354yurthgfdwy6][[\]'g".
+["hogehoge", manipulator, 235].
+<<"qwerftafds">>.
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.