X-Git-Url: https://scm.cri.mines-paristech.fr/git/Faustine.git/blobdiff_plain/f59ac471aa5a080301d70e7d7e1e81f34151ba2f..a3490abb2f472827f39b061c55029949ebb3eba5:/interpretor/basic.ml?ds=inline diff --git a/interpretor/basic.ml b/interpretor/basic.ml index 3131231..ae096af 100644 --- a/interpretor/basic.ml +++ b/interpretor/basic.ml @@ -85,14 +85,31 @@ let basic_to_float_array : basic -> float array = |_ -> [| (basic_to_float v)|];; -let basic_to_string : basic -> string = +let rec basic_to_string : basic -> string = fun (v : basic) -> match v with - |N i1 -> "N " ^ (string_of_int i1) - |R f1 -> "R " ^ (string_of_float f1) - |Vec vec -> "Vec" - |Zero -> "Zero" - |Error -> "Error";; + |N i1 -> string_of_int i1 + |R f1 -> string_of_float f1 + |Vec vec -> + let basics : basic array = + Array.init vec#size vec#nth in + let strings = Array.to_list + (Array.map basic_to_string basics) in + String.concat "," strings + |Zero -> "0" + |Error -> "0";; + +let basic_of_float : float -> basic = fun f -> R f;; + +let rec basic_of_float_array : float array -> basic = + fun (data : float array) -> + let n = Array.length data in + if n = 0 then + raise (Convert_Error "basic_of_float_array : empty.") + else if n = 1 then basic_of_float data.(0) + else + let vec = Array.get (Array.map basic_of_float data) in + Vec (new vector n vec);; (* VALUE OPERATIONS *) @@ -381,3 +398,39 @@ let basic_smaller : basic -> basic -> basic = basic_larger_than_zero (b2 -~ b1);; +let basic_max : basic -> basic -> basic = + fun b1 -> + fun b2 -> + let compare = basic_larger_than_zero (b1 -~ b2) in + match compare with + | N i -> + if i = 1 then b1 + else if i = 0 then b2 + else raise (Basic_operation "compare result not bool.") + | Vec vec -> + let basics = Array.init vec#size vec#nth in + let sum = basic_to_int (Array.fold_left basic_add Zero basics) in + if sum = vec#size then b1 + else if sum = 0 then b2 + else Error + | Error -> Error + | _ -> raise (Basic_operation "compare result not bool.");; + + +let basic_min : basic -> basic -> basic = + fun b1 -> + fun b2 -> + let compare = basic_larger_than_zero (b1 -~ b2) in + match compare with + | N i -> + if i = 1 then b2 + else if i = 0 then b1 + else raise (Basic_operation "compare result not bool.") + | Vec vec -> + let basics = Array.init vec#size vec#nth in + let sum = basic_to_int (Array.fold_left basic_add Zero basics) in + if sum = vec#size then b2 + else if sum = 0 then b1 + else Error + | Error -> Error + | _ -> raise (Basic_operation "compare result not bool.");;