aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--projet.ml61
1 files 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 =
42 in List.iter (fun s -> print_string s; print_newline ()) lex; program_of_lex lex 42 in List.iter (fun s -> print_string s; print_newline ()) lex; program_of_lex lex
43 43
44let instptr_mk urmcmd_list = 44let instptr_mk urmcmd_list =
45 let rec aux urmcmd_list count acc = 45 let rec aux urmcmd_list count acc =
46 match urmcmd_list with 46 match urmcmd_list with
47 | [] -> acc 47 | [] -> acc
48 | instruction::reste -> aux reste (count + 1) ((count,instruction)::acc) 48 | instruction::reste -> aux reste (count + 1) ((count,instruction)::acc)
49 in InstPtr([],rev (aux urmcmd_list 0 [])) 49 in InstPtr([],rev (aux urmcmd_list 0 []))
50 50
51let instptr_move_up = function 51let instptr_move_up = function
52 | InstPtr([],list2)-> InstPtr([],list2)
53 | InstPtr(instr::list1,list2) -> InstPtr(list1, instr::list2)
54
55let instptr_move_down = function
52 | InstPtr(list1,[])-> InstPtr(list1,[]) 56 | InstPtr(list1,[])-> InstPtr(list1,[])
53 | InstPtr(list1,instr::list2) -> InstPtr(instr::list1, list2) 57 | InstPtr(list1,instr::list2) -> InstPtr(instr::list1, list2)
54 58
55let instptr_get = function 59let instptr_get = function
56 | _,Zero(_) -> Zero(_) 60 | InstPtr(list1,(l,Zero(a))::tail)-> (l,Zero(a))
57 | _,Succ(_) -> Succ(_) 61 | InstPtr(list1,(l,Succ(a))::tail) -> (l,Succ(a))
58 | _,Copy(_,_) -> Copy(_,_) 62 | InstPtr(list1,(l,Copy(a,b))::tail) -> (l,Copy(a,b))
59 | _,Jump(_,_,_) -> Jump(_,_,_) 63 | InstPtr(list1,(l,Jump(a,b,c))::tail) -> (l,Jump(a,b,c))
60 | _ -> raise Syntax_error \ No newline at end of file 64 | InstPtr(_,[])-> failwith "No instruction left"
65
66let instptr_string instptr =
67 let aux = function
68 | l,Zero(a) -> (string_of_int l)^": Zero "^(string_of_int a)
69 | l,Succ(a) -> (string_of_int l)^": Succ "^(string_of_int a)
70 | l,Copy(a,b) -> (string_of_int l)^": Copy "^(string_of_int a)^" "^(string_of_int b)
71 | l,Jump(a,b,c) -> (string_of_int l)^": Jump "^(string_of_int a)^" "^(string_of_int b)^" "^(string_of_int c)
72 in try aux (instptr_get instptr) with
73 | _ -> "null"
74
75
76let reg_idx = function
77 | Reg(a,b) -> a
78
79let reg_val = function
80 | Reg(a,b) -> b
81
82let reg_compar reg1 reg2 = (reg_val reg1) - (reg_val reg2)
83
84let regs_get reglist idx =
85 let rec aux = function
86 | [] -> failwith "Register not found"
87 | Reg(i,v)::tail when i = idx -> v
88 | Reg(_,_)::tail -> aux tail
89 in aux reglist
90
91let regs_exists regs idx = List.exists (fun (Reg(x,_)) -> x = idx) regs
92
93(*TODO: Function of register manipulation: create a register or modify an existent register (remove + create?)*)
94
95
96let urm_apply urm =
97 let aux = function
98 | _,Zero(a) -> urm.regs
99 in aux (instptr_get urm.instptr)
100
101let urm_run urm =
102 match urm with
103 | {instptr = InstPtr(_,[]); regs = reg_list } -> reg_list
104
105let urm_mk cmd_list reg_list = {instptr = (instptr_mk cmd_list) ; regs = reg_list}