caml-urm

A OCaml module for manipulating unlimited register machines

Commit
fb90b46df7265d58895069aea06872e6e4d5522b
Parent
f741008ce2c1e3d2a1c0fdf99a57f30703f4556f
Author
Pablo <pablo-escobar@riseup.net>
Date

Added support for integers separated by multiple spaces in REGISTERS command-line argument

Also formatted a function

Diffstat

1 file changed, 15 insertions, 13 deletions

Status File Name N° Changes Insertions Deletions
Modified main.ml 28 15 13
diff --git a/main.ml b/main.ml
@@ -8,9 +8,9 @@ let default_max_iters = 1000
 
 (** An equivalent to Haskell's [sequence] *)
 let seq (xs : 'a option list) : 'a list option =
-    let f x acc = 
-        Option.join (Option.map (fun y -> Option.map (fun ys -> y :: ys) acc) x)
-    in List.fold_right f xs (Some [])
+  let f x acc = 
+    Option.join (Option.map (fun y -> Option.map (fun ys -> y :: ys) acc) x)
+  in List.fold_right f xs (Some [])
 
 
 (** Execute the program from [file] with initial register values given by
@@ -73,16 +73,18 @@ let (iters, regs_str, filepath) : int * string * string =
 (** Parse the initial values of the registers as a list of ints separated by
     spaces *)
 let regs =
-  match seq (map int_of_string_opt (String.split_on_char ' ' regs_str)) with
-  | Some regs ->
-    regs
-  | None ->
-    Printf.eprintf 
-      "ERROR: invalid initial values for the registers provided: '%s'\n"
-      regs_str;
-    Printf.eprintf "Expected a list of integers separated by spaces\n";
-    usage ();
-    exit 1
+  let regs = List.filter ((<>) "") (String.split_on_char ' ' regs_str)
+  in
+    match seq (List.map int_of_string_opt regs) with
+    | Some regs ->
+      regs
+    | None ->
+      Printf.eprintf 
+        "ERROR: invalid initial values for the registers provided: '%s'\n"
+        regs_str;
+      Printf.eprintf "Expected a list of integers separated by spaces\n";
+      usage ();
+      exit 1
 
 (** Try to open the file with the contents of the program *)
 let file =