aboutsummaryrefslogtreecommitdiff
path: root/src/parser.ml
diff options
context:
space:
mode:
authorpacien2018-04-29 20:24:38 +0200
committerpacien2018-04-29 20:24:38 +0200
commit0647f37eebbefb8446fc8abfc533a23952fbb8be (patch)
treefc9d69f32bd6c04de27c3795f6d54ed150bd4958 /src/parser.ml
parent80d7f0f204aacefa768d34f6db30108cb430cede (diff)
downloadurm-0647f37eebbefb8446fc8abfc533a23952fbb8be.tar.gz
Move sources to dedicated directory
Diffstat (limited to 'src/parser.ml')
-rw-r--r--src/parser.ml32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/parser.ml b/src/parser.ml
new file mode 100644
index 0000000..1f367d1
--- /dev/null
+++ b/src/parser.ml
@@ -0,0 +1,32 @@
1(*
2 * UPEM / L3 / Functional programming / Project: URM
3 * Pacien TRAN-GIRARD, Adam NAILI
4 *)
5
6open Common
7
8let rec string_of_file f =
9 try
10 let str = input_line f
11 in str ^ " " ^ (string_of_file f)
12 with End_of_file -> ""
13
14let rec program_of_lex = function
15 | [] -> []
16 | instr :: tail -> match (String.lowercase_ascii instr) :: tail with
17 | "zero" :: arg_1 :: tail -> (Zero (int_of_string arg_1)) :: (program_of_lex tail)
18 | "succ" :: arg_1 :: tail -> (Succ (int_of_string arg_1)) :: (program_of_lex tail)
19 | "copy" :: arg_1 :: arg_2 :: tail -> (Copy ((int_of_string arg_1), (int_of_string arg_2))) :: (program_of_lex tail)
20 | "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)
21 | _ -> raise Syntax_error
22
23(* FIXME: reject multiple definition of a single register *)
24let rec regs_of_lex = function
25 | [] -> []
26 | regnum :: regvalue :: tail -> Reg (int_of_string regnum, int_of_string regvalue) :: (regs_of_lex tail)
27 | _ -> raise Syntax_error
28
29let seq_from_string lexer_func str = Str.split (Str.regexp "[\t\n(), ]+") str |> lexer_func
30let program_of_string = seq_from_string program_of_lex
31let regs_of_string = seq_from_string regs_of_lex
32