Commits

arkdro committed 0b6cb8a

use option type for intermediate data and result

  • Participants
  • Parent commits 5f6051a

Comments (0)

Files changed (1)

File ocaml/src/odec_port.ml

 
 let make_num = function
   | Some (0, mant, pow) ->
-    compose_num mant pow
+    Some (compose_num mant pow)
   | Some (1, mant, pow) ->
-    Num.minus_num (compose_num mant pow)
+    Some (Num.minus_num (compose_num mant pow))
   | None ->
-    Num.Big_int (Big_int.big_int_of_int (-2))
+    None
 
-let t_mult2 (ET_List list) =
+let mult_list list =
   let list2 = List.map (
     fun x ->
       let res = make_tuple x in
       res
   ) list in
   let list3 = List.map make_num list2 in
-  let f acc x =
-    let res = Num.mult_num acc x in
-    res
+  let f acc = function
+    | Some x -> Num.mult_num acc x
+    | None -> acc
   in
   let res = List.fold_left f (Num.Int 1) list3 in
   Printf.eprintf "tm2, res: %s\n%!" (Num.string_of_num res);
   ET_Bignum res
 
+let t_mult2 = function
+  | ET_List [] ->
+    None
+  | ET_List [x] ->
+    Some x
+  | ET_List list ->
+    Some (mult_list list)
+
 let get_info = function
   | ET_Tuple _ -> ET_Atom "tuple"
   (* erlang list [256,2,3] is a list *)
     let res = get_info t in
     Some t, res
   | ET_Tuple [ET_Atom "ms"; (ET_List _) as t] ->
-    let (ET_Bignum r) = t_mult2 t in
-    let res2 = create_decimal r in
-    Some t, res2
-  | ET_Tuple [ET_Atom "mt"; (ET_Tuple _) as t] ->
-    Some t, t
-  | ET_Tuple [ET_Atom "mult"; items] ->
-    Some items, ET_String "stored after mult"
+    match t_mult2 t with
+    | Some (ET_Bignum r) ->
+      let res2 = create_decimal r in
+      Some t, ET_Tuple [ET_Atom "ok"; res2]
+    | None ->
+      Some t, ET_Tuple [ET_Atom "error"; ET_Atom "no_data"]
   | _ -> raise (Failure "Unknown command")
   ;;