aboutsummaryrefslogtreecommitdiff
path: root/instptr.ml
diff options
context:
space:
mode:
Diffstat (limited to 'instptr.ml')
-rw-r--r--instptr.ml51
1 files changed, 51 insertions, 0 deletions
diff --git a/instptr.ml b/instptr.ml
new file mode 100644
index 0000000..9e472c4
--- /dev/null
+++ b/instptr.ml
@@ -0,0 +1,51 @@
1(*
2 * UPEM / L3 / Functional programming / Project: URM
3 * Pacien TRAN-GIRARD, Adam NAILI
4 *)
5
6open Common
7
8(* Creates a pointer of instruction from an urm command list *)
9let instptr_mk urmcmd_list =
10 let rec aux urmcmd_list count acc =
11 match urmcmd_list with
12 | [] -> acc
13 | instr :: tail -> aux tail (count + 1) ((count, instr) :: acc)
14 in InstPtr([], List.rev (aux urmcmd_list 0 []))
15
16(* Moves the pointer to the previous instruction *)
17let instptr_move_up = function
18 | InstPtr([], list2) -> InstPtr([], list2)
19 | InstPtr(instr :: list1, list2) -> InstPtr(list1, instr :: list2)
20
21(* Moves the pointer to the next instruction *)
22let instptr_move_down = function
23 | InstPtr(list1, []) -> InstPtr(list1, [])
24 | InstPtr(list1, instr :: list2) -> InstPtr(instr :: list1, list2)
25
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
28 | InstPtr(list1, (l, Zero(a)) :: tail)-> (l, Zero(a))
29 | InstPtr(list1, (l, Succ(a)) :: tail) -> (l, Succ(a))
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
34(* Converts the current instruction pointed into a string (line and instruction formatted). If there is no instruction, returns "null" *)
35let instptr_string instptr =
36 let aux = function
37 | l, Zero(a) -> (string_of_int l) ^ ": Zero " ^ (string_of_int a)
38 | l, Succ(a) -> (string_of_int l) ^ ": Succ " ^ (string_of_int a)
39 | l, Copy(a, b) -> (string_of_int l) ^ ": 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)
41 in try aux (instptr_get instptr) with _ -> "null"
42
43(* Returns true if the instruction pointer is not pointing on any instruction (end of the instruction list) *)
44let instptr_end = function
45 | InstPtr(_, []) -> true
46 | _ -> false
47
48let rec instptr_jump ptr offset = match offset with
49 | 0 -> ptr
50 | _ when offset > 0 -> instptr_jump (instptr_move_up ptr) (offset - 1)
51 | _ -> instptr_jump (instptr_move_down ptr) (offset + 1)