aboutsummaryrefslogtreecommitdiff
path: root/src/instptr.ml
diff options
context:
space:
mode:
Diffstat (limited to 'src/instptr.ml')
-rw-r--r--src/instptr.ml43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/instptr.ml b/src/instptr.ml
new file mode 100644
index 0000000..0311a00
--- /dev/null
+++ b/src/instptr.ml
@@ -0,0 +1,43 @@
1(*
2 * UPEM / L3 / Functional programming / Project: URM
3 * Pacien TRAN-GIRARD, Adam NAILI
4 *)
5
6open Common
7
8let instptr_mk urmcmd_list =
9 let rec aux urmcmd_list count acc = match urmcmd_list with
10 | [] -> acc
11 | instr :: tail -> aux tail (count + 1) ((count, instr) :: acc)
12 in InstPtr([], List.rev (aux urmcmd_list 0 []))
13
14let instptr_move_up = function
15 | InstPtr(instr :: list1, list2) -> InstPtr(list1, instr :: list2)
16 | x -> x
17
18let instptr_move_down = function
19 | InstPtr(list1, instr :: list2) -> InstPtr(instr :: list1, list2)
20 | x -> x
21
22let instptr_get = function
23 | InstPtr(_, x :: _) -> x
24 | InstPtr(_, []) -> failwith "No instruction left"
25
26let instptr_string instptr =
27 let string_of_inst = function
28 | Zero(a) -> "Zero " ^ (string_of_int a)
29 | Succ(a) -> "Succ " ^ (string_of_int a)
30 | Copy(a, b) -> "Copy " ^ (string_of_int a) ^ " " ^ (string_of_int b)
31 | Jump(a, b, c) -> "Jump " ^ (string_of_int a) ^ " " ^ (string_of_int b) ^ " " ^ (string_of_int c)
32 in let string_of_instptr (l, inst) = (string_of_int l) ^ ": " ^ string_of_inst inst
33 in try string_of_instptr (instptr_get instptr) with _ -> "null"
34
35let instptr_end = function
36 | InstPtr(_, []) -> true
37 | _ -> false
38
39let rec instptr_jump ptr offset = match offset with
40 | 0 -> ptr
41 | _ when offset > 0 -> instptr_jump (instptr_move_up ptr) (offset - 1)
42 | _ -> instptr_jump (instptr_move_down ptr) (offset + 1)
43