diff options
Diffstat (limited to 'projet.ml')
-rw-r--r-- | projet.ml | 60 |
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";; | ||
2 | open List;; | ||
3 | (*line number*) | ||
4 | type line = int | ||
5 | (*register index*) | ||
6 | type regidx = int | ||
7 | (*register value*) | ||
8 | type regval = int | ||
9 | (*register*) | ||
10 | type reg = Reg of regidx * regval | ||
11 | (*URM instruction*) | ||
12 | type urmcmd = | ||
13 | |Copy of regidx * regidx | ||
14 | |Jump of regidx * regidx * line | ||
15 | |Succ of regidx | ||
16 | |Zero of regidx | ||
17 | (*instruction pointer*) | ||
18 | type instptr = InstPtr of (line * urmcmd) list * (line * urmcmd) list | ||
19 | (*URM*) | ||
20 | type urm = {instptr : instptr; regs : reg list} | ||
21 | |||
22 | exception Syntax_error | ||
23 | |||
24 | let 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 | |||
31 | let 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 | |||
40 | let 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 | |||
44 | let 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 | |||
51 | let instptr_move_up = function | ||
52 | | InstPtr(list1,[])-> InstPtr(list1,[]) | ||
53 | | InstPtr(list1,instr::list2) -> InstPtr(instr::list1, list2) | ||
54 | |||
55 | let 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 | ||