diff options
author | pacien | 2018-04-22 20:54:32 +0200 |
---|---|---|
committer | pacien | 2018-04-22 20:54:32 +0200 |
commit | db9de5bc717be46f0ca2dc1aa975c75adca6264d (patch) | |
tree | e426a885ab866cce9c5972c56ddadd72734f695a | |
parent | 27fadcf4521b0459975d6b9b02a68181c44b791f (diff) | |
download | urm-db9de5bc717be46f0ca2dc1aa975c75adca6264d.tar.gz |
Simplifications
-rw-r--r-- | instptr.ml | 36 | ||||
-rw-r--r-- | reg.ml | 9 | ||||
-rw-r--r-- | urm.ml | 1 |
3 files changed, 20 insertions, 26 deletions
@@ -5,42 +5,33 @@ | |||
5 | 5 | ||
6 | open Common | 6 | open Common |
7 | 7 | ||
8 | (* Creates a pointer of instruction from an urm command list *) | ||
9 | let instptr_mk urmcmd_list = | 8 | let instptr_mk urmcmd_list = |
10 | let rec aux urmcmd_list count acc = | 9 | let rec aux urmcmd_list count acc = match urmcmd_list with |
11 | match urmcmd_list with | ||
12 | | [] -> acc | 10 | | [] -> acc |
13 | | instr :: tail -> aux tail (count + 1) ((count, instr) :: acc) | 11 | | instr :: tail -> aux tail (count + 1) ((count, instr) :: acc) |
14 | in InstPtr([], List.rev (aux urmcmd_list 0 [])) | 12 | in InstPtr([], List.rev (aux urmcmd_list 0 [])) |
15 | 13 | ||
16 | (* Moves the pointer to the previous instruction *) | ||
17 | let instptr_move_up = function | 14 | let instptr_move_up = function |
18 | | InstPtr([], list2) -> InstPtr([], list2) | ||
19 | | InstPtr(instr :: list1, list2) -> InstPtr(list1, instr :: list2) | 15 | | InstPtr(instr :: list1, list2) -> InstPtr(list1, instr :: list2) |
16 | | x -> x | ||
20 | 17 | ||
21 | (* Moves the pointer to the next instruction *) | ||
22 | let instptr_move_down = function | 18 | let instptr_move_down = function |
23 | | InstPtr(list1, []) -> InstPtr(list1, []) | ||
24 | | InstPtr(list1, instr :: list2) -> InstPtr(instr :: list1, list2) | 19 | | InstPtr(list1, instr :: list2) -> InstPtr(instr :: list1, list2) |
20 | | x -> x | ||
25 | 21 | ||
26 | (* Returns the couple from the current pointer position : (line, instruction) where instruction is an urm command or fails if there is no instruction pointed *) | ||
27 | let instptr_get = function | 22 | let instptr_get = function |
28 | | InstPtr(list1, (l, Zero(a)) :: tail)-> (l, Zero(a)) | 23 | | InstPtr(_, x :: _) -> x |
29 | | InstPtr(list1, (l, Succ(a)) :: tail) -> (l, Succ(a)) | 24 | | InstPtr(_, []) -> failwith "No instruction left" |
30 | | InstPtr(list1, (l, Copy(a, b)) :: tail) -> (l, Copy(a, b)) | ||
31 | | InstPtr(list1, (l, Jump(a, b, c)) :: tail) -> (l, Jump(a, b, c)) | ||
32 | | InstPtr(_, [])-> failwith "No instruction left" | ||
33 | 25 | ||
34 | (* Converts the current instruction pointed into a string (line and instruction formatted). If there is no instruction, returns "null" *) | ||
35 | let instptr_string instptr = | 26 | let instptr_string instptr = |
36 | let aux = function | 27 | let string_of_inst = function |
37 | | l, Zero(a) -> (string_of_int l) ^ ": Zero " ^ (string_of_int a) | 28 | | Zero(a) -> "Zero " ^ (string_of_int a) |
38 | | l, Succ(a) -> (string_of_int l) ^ ": Succ " ^ (string_of_int a) | 29 | | Succ(a) -> "Succ " ^ (string_of_int a) |
39 | | l, Copy(a, b) -> (string_of_int l) ^ ": Copy " ^ (string_of_int a) ^ " " ^ (string_of_int b) | 30 | | Copy(a, b) -> "Copy " ^ (string_of_int a) ^ " " ^ (string_of_int b) |
40 | | l, Jump(a, b, c) -> (string_of_int l) ^ ": Jump " ^ (string_of_int a) ^ " " ^ (string_of_int b) ^ " " ^ (string_of_int c) | 31 | | Jump(a, b, c) -> "Jump " ^ (string_of_int a) ^ " " ^ (string_of_int b) ^ " " ^ (string_of_int c) |
41 | in try aux (instptr_get instptr) with _ -> "null" | 32 | in let string_of_instptr (l, inst) = (string_of_int l) ^ ": " ^ string_of_inst inst |
42 | 33 | in try string_of_instptr (instptr_get instptr) with _ -> "null" | |
43 | (* Returns true if the instruction pointer is not pointing on any instruction (end of the instruction list) *) | 34 | |
44 | let instptr_end = function | 35 | let instptr_end = function |
45 | | InstPtr(_, []) -> true | 36 | | InstPtr(_, []) -> true |
46 | | _ -> false | 37 | | _ -> false |
@@ -49,3 +40,4 @@ let rec instptr_jump ptr offset = match offset with | |||
49 | | 0 -> ptr | 40 | | 0 -> ptr |
50 | | _ when offset > 0 -> instptr_jump (instptr_move_up ptr) (offset - 1) | 41 | | _ when offset > 0 -> instptr_jump (instptr_move_up ptr) (offset - 1) |
51 | | _ -> instptr_jump (instptr_move_down ptr) (offset + 1) | 42 | | _ -> instptr_jump (instptr_move_down ptr) (offset + 1) |
43 | |||
@@ -7,10 +7,11 @@ open Common | |||
7 | 7 | ||
8 | let reg_idx (Reg(idx, _)) = idx | 8 | let reg_idx (Reg(idx, _)) = idx |
9 | let reg_val (Reg(_, value)) = value | 9 | let reg_val (Reg(_, value)) = value |
10 | let reg_compar reg1 reg2 = (reg_val reg1) - (reg_val reg2) | 10 | let reg_compar l r = (reg_val l) - (reg_val r) |
11 | 11 | ||
12 | let regs_get reglist idx = | 12 | let regs_get reglist index = |
13 | List.find (fun (Reg(x,v)) -> x = idx) reglist |> reg_val | 13 | List.find (fun (Reg(idx, _)) -> idx = index) reglist |> reg_val |
14 | 14 | ||
15 | let regs_set reglist index value = | 15 | let regs_set reglist index value = |
16 | Reg(index, value) :: List.filter (fun (Reg(x, v)) -> x != index) reglist | 16 | Reg(index, value) :: List.filter (fun (Reg(idx, _)) -> idx != index) reglist |
17 | |||
@@ -32,3 +32,4 @@ let urm_run_trace = urm_run (* TODO *) | |||
32 | 32 | ||
33 | (* Creates an URM from a command list and a register list *) | 33 | (* Creates an URM from a command list and a register list *) |
34 | let urm_mk cmd_list reg_list = { instptr = (instptr_mk cmd_list) ; regs = reg_list } | 34 | let urm_mk cmd_list reg_list = { instptr = (instptr_mk cmd_list) ; regs = reg_list } |
35 | |||