From 35b808e5a3ba0a70f72b67c5690af78f86fe0c29 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Thu, 19 Apr 2018 15:16:11 +0200 Subject: Implementing some functions. Urm not working. Need reg manipulation func --- projet.ml | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/projet.ml b/projet.ml index 2a4c051..6fd13ea 100644 --- a/projet.ml +++ b/projet.ml @@ -42,19 +42,64 @@ let program_of_string str = in List.iter (fun s -> print_string s; print_newline ()) lex; program_of_lex lex let instptr_mk urmcmd_list = - let rec aux urmcmd_list count acc = + let rec aux urmcmd_list count acc = match urmcmd_list with - | [] -> acc - | instruction::reste -> aux reste (count + 1) ((count,instruction)::acc) + | [] -> acc + | instruction::reste -> aux reste (count + 1) ((count,instruction)::acc) in InstPtr([],rev (aux urmcmd_list 0 [])) let instptr_move_up = function + | InstPtr([],list2)-> InstPtr([],list2) + | InstPtr(instr::list1,list2) -> InstPtr(list1, instr::list2) + +let instptr_move_down = function | InstPtr(list1,[])-> InstPtr(list1,[]) | InstPtr(list1,instr::list2) -> InstPtr(instr::list1, list2) let instptr_get = function - | _,Zero(_) -> Zero(_) - | _,Succ(_) -> Succ(_) - | _,Copy(_,_) -> Copy(_,_) - | _,Jump(_,_,_) -> Jump(_,_,_) - | _ -> raise Syntax_error \ No newline at end of file + | InstPtr(list1,(l,Zero(a))::tail)-> (l,Zero(a)) + | InstPtr(list1,(l,Succ(a))::tail) -> (l,Succ(a)) + | InstPtr(list1,(l,Copy(a,b))::tail) -> (l,Copy(a,b)) + | InstPtr(list1,(l,Jump(a,b,c))::tail) -> (l,Jump(a,b,c)) + | InstPtr(_,[])-> failwith "No instruction left" + +let instptr_string instptr = + let aux = function + | l,Zero(a) -> (string_of_int l)^": Zero "^(string_of_int a) + | l,Succ(a) -> (string_of_int l)^": Succ "^(string_of_int a) + | l,Copy(a,b) -> (string_of_int l)^": Copy "^(string_of_int a)^" "^(string_of_int b) + | l,Jump(a,b,c) -> (string_of_int l)^": Jump "^(string_of_int a)^" "^(string_of_int b)^" "^(string_of_int c) + in try aux (instptr_get instptr) with + | _ -> "null" + + +let reg_idx = function + | Reg(a,b) -> a + +let reg_val = function + | Reg(a,b) -> b + +let reg_compar reg1 reg2 = (reg_val reg1) - (reg_val reg2) + +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 regs_exists regs idx = List.exists (fun (Reg(x,_)) -> x = idx) regs + +(*TODO: Function of register manipulation: create a register or modify an existent register (remove + create?)*) + + +let urm_apply urm = + let aux = function + | _,Zero(a) -> urm.regs + in aux (instptr_get urm.instptr) + +let urm_run urm = + match urm with + | {instptr = InstPtr(_,[]); regs = reg_list } -> reg_list + +let urm_mk cmd_list reg_list = {instptr = (instptr_mk cmd_list) ; regs = reg_list} -- cgit v1.2.3