diff options
author | pacien | 2018-04-20 23:32:38 +0200 |
---|---|---|
committer | pacien | 2018-04-20 23:32:38 +0200 |
commit | 3f25710a891dfcd17f597c16dfedf5499bc1bbd6 (patch) | |
tree | 71bec80335de3820994d26c68bb4cc8ca961789a /instptr.ml | |
parent | 2e6e365262b7dc072d7ac35ff867f309e3ff784c (diff) | |
download | urm-3f25710a891dfcd17f597c16dfedf5499bc1bbd6.tar.gz |
Modularize everything
Diffstat (limited to 'instptr.ml')
-rw-r--r-- | instptr.ml | 51 |
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 | |||
6 | open Common | ||
7 | |||
8 | (* Creates a pointer of instruction from an urm command list *) | ||
9 | let 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 *) | ||
17 | let 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 *) | ||
22 | let 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 *) | ||
27 | let 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" *) | ||
35 | let 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) *) | ||
44 | let instptr_end = function | ||
45 | | InstPtr(_, []) -> true | ||
46 | | _ -> false | ||
47 | |||
48 | let 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) | ||