2024-08-26 01:58:03 +02:00
|
|
|
type args = { debug : bool; file_name : string option }
|
|
|
|
|
|
|
|
|
|
let parse_args () : args =
|
|
|
|
|
let usage_msg = "mlox [--debug] [<file>]" 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 }
|
2024-08-02 00:10:48 +02:00
|
|
|
|
|
|
|
|
let () =
|
2024-08-26 01:58:03 +02:00
|
|
|
let { debug; file_name } = parse_args () in
|
|
|
|
|
match file_name with
|
|
|
|
|
| None -> Lox.runRepl ~debug ()
|
|
|
|
|
| Some file_name -> (
|
2024-08-03 02:44:47 +02:00
|
|
|
(* Printf.printf "Running script %s\n" path; *)
|
2024-08-26 01:58:03 +02:00
|
|
|
let ic = open_in file_name in
|
2024-08-02 00:10:48 +02:00
|
|
|
let source = In_channel.input_all ic in
|
2024-08-26 01:58:03 +02:00
|
|
|
match Lox.run ~debug source with
|
2024-08-03 02:44:47 +02:00
|
|
|
| Error e ->
|
|
|
|
|
Lox.Error.print_error e;
|
|
|
|
|
exit 1
|
|
|
|
|
| Ok () -> exit 0)
|