aboutsummaryrefslogtreecommitdiff
path: root/parser.ml
diff options
context:
space:
mode:
Diffstat (limited to 'parser.ml')
-rw-r--r--parser.ml22
1 files changed, 15 insertions, 7 deletions
diff --git a/parser.ml b/parser.ml
index e01208f..1f367d1 100644
--- a/parser.ml
+++ b/parser.ml
@@ -13,12 +13,20 @@ let rec string_of_file f =
13 13
14let rec program_of_lex = function 14let rec program_of_lex = function
15 | [] -> [] 15 | [] -> []
16 | "zero" :: arg_1 :: tail -> (Zero (int_of_string arg_1)) :: (program_of_lex tail) 16 | instr :: tail -> match (String.lowercase_ascii instr) :: tail with
17 | "succ" :: arg_1 :: tail -> (Succ (int_of_string arg_1)) :: (program_of_lex tail) 17 | "zero" :: arg_1 :: tail -> (Zero (int_of_string arg_1)) :: (program_of_lex tail)
18 | "copy" :: arg_1 :: arg_2 :: tail -> (Copy ((int_of_string arg_1), (int_of_string arg_2))) :: (program_of_lex tail) 18 | "succ" :: arg_1 :: tail -> (Succ (int_of_string arg_1)) :: (program_of_lex tail)
19 | "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) 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)
20 | _ -> raise Syntax_error 27 | _ -> raise Syntax_error
21 28
22let program_of_string str = 29let seq_from_string lexer_func str = Str.split (Str.regexp "[\t\n(), ]+") str |> lexer_func
23 let lex = Str.split (Str.regexp "[\t\n(),]+") str 30let program_of_string = seq_from_string program_of_lex
24 in List.iter (fun s -> print_string s; print_newline ()) lex; program_of_lex lex 31let regs_of_string = seq_from_string regs_of_lex
32