X-Git-Url: https://svn.cri.ensmp.fr/git/Faustine.git/blobdiff_plain/bad540d5d41694d5d6f347756273474b2a67195e..d1f93175cc683d72876cb56b4a8f566578296bb9:/interpretor/basic.ml?ds=inline diff --git a/interpretor/basic.ml b/interpretor/basic.ml index cda0c6b..1e56142 100644 --- a/interpretor/basic.ml +++ b/interpretor/basic.ml @@ -28,17 +28,17 @@ let fun_ternary oper f g h = fun x -> oper (f x) (g x) (h x);; let memorize : int -> (index -> basic) -> (index -> basic) = fun size -> fun vec -> - let memory_array = Array.create size Error in - let index_array = Array.create size false in + let memory = Array.create size Error in + let filled = Array.create size false in let vec_mem : index -> basic = fun i -> if i >= 0 && i < size then ( - if index_array.(i) then - memory_array.(i) + if filled.(i) then + memory.(i) else let result = vec i in - let () = memory_array.(i) <- result in - let () = index_array.(i) <- true in + let () = memory.(i) <- result in + let () = filled.(i) <- true in result) else raise (Invalid_argument "vector overflow.") in vec_mem;; @@ -79,20 +79,37 @@ let basic_to_float_array : basic -> float array = fun v -> match v with |Vec vec -> - let result : basic array = + let basics : basic array = Array.init vec#size vec#nth in - Array.map basic_to_float result + Array.map basic_to_float basics |_ -> [| (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 *)