aboutsummaryrefslogtreecommitdiff
path: root/instptr.ml
diff options
context:
space:
mode:
Diffstat (limited to 'instptr.ml')
-rw-r--r--instptr.ml36
1 files changed, 14 insertions, 22 deletions
diff --git a/instptr.ml b/instptr.ml
index 9e472c4..0311a00 100644
--- a/instptr.ml
+++ b/instptr.ml
@@ -5,42 +5,33 @@
5 5
6open Common 6open Common
7 7
8(* Creates a pointer of instruction from an urm command list *)
9let instptr_mk urmcmd_list = 8let 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 *)
17let instptr_move_up = function 14let 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 *)
22let instptr_move_down = function 18let 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 *)
27let instptr_get = function 22let 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" *)
35let instptr_string instptr = 26let 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
44let instptr_end = function 35let 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