caml-urm

A OCaml module for manipulating unlimited register machines

Commit
c880518b3ea9533417583163713f60129c63f8d5
Parent
71d63f0fa53303394acccdac88a195834feb147d
Author
Pablo <pablo-escobar@riseup.net>
Date

Fixed a bug in the parsing

The parser failed to ignore empty lines

Diffstat

1 file changed, 9 insertions, 3 deletions

Status File Name N° Changes Insertions Deletions
Modified urm.ml 12 9 3
diff --git a/urm.ml b/urm.ml
@@ -95,9 +95,15 @@ let parse_instruction (i : token list) : instruction =
       J (r1, r2, i)
   | [ Kwd "J"; Kwd "("; Int _; Kwd ","; Int _; Kwd ","; Int i; Kwd ")" ] ->
       raise (Syntax_error (Printf.sprintf "invalid jump address: %d" i))
-  | _ -> raise (Syntax_error "invalid syntax")
+  | _ -> 
+      raise (Syntax_error "invalid syntax")
 
 let parse (s : string) : instruction array =
-  let f s = parse_instruction (Stream.npeek 8 (lex (Stream.of_string s)))
-  in Array.of_list (List.map f (String.split_on_char '\n' s))
+    let f s = 
+      match String.trim s with
+      | "" -> 
+          None
+      | t -> 
+          Some (parse_instruction (Stream.npeek 8 (lex (Stream.of_string t))))
+    in Array.of_list (List.filter_map f (String.split_on_char '\n' s))