aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam NAILI2018-03-30 15:16:24 +0200
committerAdam NAILI2018-03-30 15:16:24 +0200
commitfe155b6f7bb8df70bb9823a5f35736ac6f7eddff (patch)
tree5f8f4529bbabadcc9f2540ea781232cb534a1804
parent747271151ceeb184163ddfecfc90b31af8013be6 (diff)
downloadurm-fe155b6f7bb8df70bb9823a5f35736ac6f7eddff.tar.gz
Beginning of the project
-rw-r--r--projet.ml60
1 files changed, 60 insertions, 0 deletions
diff --git a/projet.ml b/projet.ml
new file mode 100644
index 0000000..2a4c051
--- /dev/null
+++ b/projet.ml
@@ -0,0 +1,60 @@
1#load "str.cma";;
2open List;;
3(*line number*)
4type line = int
5(*register index*)
6type regidx = int
7(*register value*)
8type regval = int
9(*register*)
10type reg = Reg of regidx * regval
11(*URM instruction*)
12type urmcmd =
13|Copy of regidx * regidx
14|Jump of regidx * regidx * line
15|Succ of regidx
16|Zero of regidx
17(*instruction pointer*)
18type instptr = InstPtr of (line * urmcmd) list * (line * urmcmd) list
19(*URM*)
20type urm = {instptr : instptr; regs : reg list}
21
22exception Syntax_error
23
24let rec string_of_file f =
25 try
26 let str = input_line f
27 in str ^ " " ^ (string_of_file f)
28 with
29 | End_of_file -> ""
30
31let rec program_of_lex lex =
32 match lex with
33 |[] -> []
34 |"zero" :: arg_1 :: tail ->(Zero (int_of_string arg_1)) :: (program_of_lex tail)
35 |"succ" :: arg_1 :: tail -> (Succ (int_of_string arg_1)) :: (program_of_lex tail)
36 |"copy" :: arg_1 :: arg_2 :: tail -> (Copy ((int_of_string arg_1), (int_of_string arg_2))):: (program_of_lex tail)
37 |"jump" :: arg_1 :: arg_2 :: arg_3 :: tail ->(Jump ((int_of_string arg_1), (int_of_string arg_2),(int_of_string arg_3))):: (program_of_lex tail)
38 |_ -> raise Syntax_error
39
40let program_of_string str =
41 let lex = Str.split (Str.regexp "[\t\n(),]+") str
42 in List.iter (fun s -> print_string s; print_newline ()) lex; program_of_lex lex
43
44let instptr_mk urmcmd_list =
45 let rec aux urmcmd_list count acc =
46 match urmcmd_list with
47 | [] -> acc
48 | instruction::reste -> aux reste (count + 1) ((count,instruction)::acc)
49 in InstPtr([],rev (aux urmcmd_list 0 []))
50
51let instptr_move_up = function
52 | InstPtr(list1,[])-> InstPtr(list1,[])
53 | InstPtr(list1,instr::list2) -> InstPtr(instr::list1, list2)
54
55let instptr_get = function
56 | _,Zero(_) -> Zero(_)
57 | _,Succ(_) -> Succ(_)
58 | _,Copy(_,_) -> Copy(_,_)
59 | _,Jump(_,_,_) -> Jump(_,_,_)
60 | _ -> raise Syntax_error \ No newline at end of file