From 3f25710a891dfcd17f597c16dfedf5499bc1bbd6 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 20 Apr 2018 23:32:38 +0200 Subject: Modularize everything --- parser.ml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 parser.ml (limited to 'parser.ml') diff --git a/parser.ml b/parser.ml new file mode 100644 index 0000000..e01208f --- /dev/null +++ b/parser.ml @@ -0,0 +1,24 @@ +(* + * UPEM / L3 / Functional programming / Project: URM + * Pacien TRAN-GIRARD, Adam NAILI + *) + +open Common + +let rec string_of_file f = + try + let str = input_line f + in str ^ " " ^ (string_of_file f) + with End_of_file -> "" + +let rec program_of_lex = function + | [] -> [] + | "zero" :: arg_1 :: tail -> (Zero (int_of_string arg_1)) :: (program_of_lex tail) + | "succ" :: arg_1 :: tail -> (Succ (int_of_string arg_1)) :: (program_of_lex tail) + | "copy" :: arg_1 :: arg_2 :: tail -> (Copy ((int_of_string arg_1), (int_of_string arg_2))) :: (program_of_lex tail) + | "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) + | _ -> raise Syntax_error + +let program_of_string str = + let lex = Str.split (Str.regexp "[\t\n(),]+") str + in List.iter (fun s -> print_string s; print_newline ()) lex; program_of_lex lex -- cgit v1.2.3