From c2a0688087a91dba700e8a6a1cdd1c52b8b8397e Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Fri, 20 Apr 2018 19:44:04 +0200 Subject: urm_run working + a lot of subfunctionnalities --- projet.ml | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/projet.ml b/projet.ml index 6fd13ea..d2b66ae 100644 --- a/projet.ml +++ b/projet.ml @@ -72,34 +72,42 @@ let instptr_string instptr = in try aux (instptr_get instptr) with | _ -> "null" +let instptr_end = function + | InstPtr(_,[]) -> true + | _ -> false -let reg_idx = function - | Reg(a,b) -> a +(*Jump ne marche pas dans le cas ou il jump trop loin après la fin*) +let rec instptr_jump ptr offset = +if offset = 0 then ptr else + if offset > 0 then instptr_jump (instptr_move_up ptr) (offset-1) + else instptr_jump (instptr_move_down ptr) (offset+1) -let reg_val = function - | Reg(a,b) -> b -let reg_compar reg1 reg2 = (reg_val reg1) - (reg_val reg2) +let reg_idx (Reg(idx, _)) = idx + +let reg_val (Reg(_, value)) = value -let regs_get reglist idx = - let rec aux = function - | [] -> failwith "Register not found" - | Reg(i,v)::tail when i = idx -> v - | Reg(_,_)::tail -> aux tail - in aux reglist +let reg_compar reg1 reg2 = (reg_val reg1) - (reg_val reg2) -let regs_exists regs idx = List.exists (fun (Reg(x,_)) -> x = idx) regs +let regs_get reglist idx = List.find (fun (Reg(x,v)) -> x=idx) reglist |> reg_val -(*TODO: Function of register manipulation: create a register or modify an existent register (remove + create?)*) +let regs_set reglist index value = Reg(index,value)::(List.filter (fun (Reg(x,v))-> x!=index) reglist) +let urm_move_down urm = {instptr = (instptr_move_down urm.instptr); regs = urm.regs} +(*TODO: Verifier pour JUMP que a et b sont deux registres initialisés*) let urm_apply urm = let aux = function - | _,Zero(a) -> urm.regs - in aux (instptr_get urm.instptr) - -let urm_run urm = - match urm with + | _,Zero(a) -> {instptr = urm.instptr ; regs = regs_set (urm.regs) a 0} |> urm_move_down + | _,Copy(a,b) when a!=b -> {instptr = urm.instptr ; regs = regs_set urm.regs a (regs_get urm.regs b)} |> urm_move_down + | _,Copy(a,b) -> failwith "Copy from one register to itself" + | _,Succ(a) -> {instptr = urm.instptr ; regs = regs_set urm.regs a ((regs_get urm.regs a)+1)} |> urm_move_down + | _,Jump(a,b,c) when (regs_get urm.regs a)=(regs_get urm.regs b)-> {instptr = (instptr_jump urm.instptr (fst (instptr_get urm.instptr) - c)); regs = urm.regs} + | _,_-> {instptr = urm.instptr; regs = urm.regs} |> urm_move_down + in if instptr_end urm.instptr then urm else aux (instptr_get urm.instptr) + +let rec urm_run = function | {instptr = InstPtr(_,[]); regs = reg_list } -> reg_list + | urm -> urm_apply urm |> urm_run let urm_mk cmd_list reg_list = {instptr = (instptr_mk cmd_list) ; regs = reg_list} -- cgit v1.2.3