type args = { debug : bool; file_name : string option } let parse_args () : args = let usage_msg = "mlox [--debug] []" in let debug = ref false in let anon = ref [] in let anon_fun s = anon := s :: !anon in let spec = [ ("--debug", Arg.Set debug, "Debug mode") ] in let () = Arg.parse spec anon_fun usage_msg in let debug = !debug in let file_and_args = List.rev !anon in let file_name = List.nth_opt file_and_args 0 in { debug; file_name } let () = let { debug; file_name } = parse_args () in match file_name with | None -> Lox.runRepl ~debug () | Some file_name -> ( (* Printf.printf "Running script %s\n" path; *) let ic = open_in file_name in let source = In_channel.input_all ic in match Lox.run ~debug source with | Error e -> Lox.Error.print_error e; exit 1 | Ok () -> exit 0)