Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pkhry committed Jun 2, 2021
1 parent d86f0c4 commit 64ad4a1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
8 changes: 4 additions & 4 deletions toplevel/byte/topmain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ let prepare ppf =
Format.fprintf ppf "Uncaught exception: %s\n" (Printexc.to_string x);
false

let file_argument ?(tag=Toploop.File) name =
let file_argument ?(constructor=(fun x -> Toploop.File x)) name =
let ppf = Format.err_formatter in
if Filename.check_suffix name ".cmo" || Filename.check_suffix name ".cma"
then preload_objects := name :: !preload_objects
Expand All @@ -176,7 +176,7 @@ let file_argument ?(tag=Toploop.File) name =
Compenv.readenv ppf Before_link;
Compmisc.read_clflags_from_env ();
let usage =
{Toploop.tag=tag; value=name} in
constructor name in
if prepare ppf && Toploop.run_script ppf usage newargs
then raise (Compenv.Exit_with_status 0)
else raise (Compenv.Exit_with_status 2)
Expand All @@ -191,11 +191,11 @@ let wrap_expand f s =

module Options = Main_args.Make_bytetop_options (struct
include Main_args.Default.Topmain
let _stdin () = file_argument ~tag:Toploop.Stdin ""
let _stdin () = file_argument ~constructor:(fun _ -> Toploop.Stdin) ""
let _args = wrap_expand Arg.read_arg
let _args0 = wrap_expand Arg.read_arg0
let anonymous s = file_argument s
let _eval s = file_argument ~tag:Toploop.String s
let _eval s = file_argument ~constructor:(fun x -> Toploop.String x) s
end)

let () =
Expand Down
8 changes: 4 additions & 4 deletions toplevel/native/topmain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ let prepare ppf =
Format.fprintf ppf "Uncaught exception: %s\n" (Printexc.to_string x);
false

let file_argument ?(tag=Toploop.File) name =
let file_argument ?(constructor=(fun x -> Toploop.File x)) name =
let ppf = Format.err_formatter in
if Filename.check_suffix name ".cmxs"
|| Filename.check_suffix name ".cmx"
Expand All @@ -69,7 +69,7 @@ let file_argument ?(tag=Toploop.File) name =
in
Compmisc.read_clflags_from_env ();
if prepare ppf && Toploop.run_script ppf
{Toploop.tag=tag; value=name} newargs
(constructor name) newargs
then raise (Compenv.Exit_with_status 0)
else raise (Compenv.Exit_with_status 2)
end
Expand All @@ -82,11 +82,11 @@ let wrap_expand f s =

module Options = Main_args.Make_opttop_options (struct
include Main_args.Default.Opttopmain
let _stdin () = file_argument ~tag:Toploop.Stdin ""
let _stdin () = file_argument ~constructor:(fun _ -> Toploop.Stdin) ""
let _args = wrap_expand Arg.read_arg
let _args0 = wrap_expand Arg.read_arg0
let anonymous s = file_argument s
let _eval s = file_argument ~tag:Toploop.String s
let _eval s = file_argument ~constructor:(fun x -> Toploop.String x) s

end);;

Expand Down
4 changes: 2 additions & 2 deletions toplevel/topdirs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ let load_file = Topeval.load_file false
(* Load commands from a file *)

let dir_use ppf name =
let usage = {Toploop.tag = Toploop.File; value= name} in
let usage = File name in
ignore (Toploop.use_file ppf usage)
let dir_use_output ppf name = ignore(Toploop.use_output ppf name)
let dir_mod_use ppf name =
let usage ={Toploop.tag = Toploop.File; value= name} in
let usage = File name in
ignore (Toploop.mod_use_file ppf usage)

let _ = add_directive "use" (Directive_string (dir_use std_out))
Expand Down
32 changes: 15 additions & 17 deletions toplevel/toploop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ include Topcommon
include Topeval

(* Read and execute commands from a file, or from stdin if [name] is "". *)
type usage_types =
| File
type input_type =
| Stdin
| String
type usage = {value: string; tag: usage_types}

| File of string
| String of string

let use_print_results = ref true

Expand Down Expand Up @@ -73,22 +71,22 @@ let use_output ppf command =

let use_file ppf ~wrap_in_module usage =
match usage with
| {tag=Stdin} ->
| Stdin ->
let lexbuf = Lexing.from_channel stdin in
use_channel ppf ~wrap_in_module lexbuf "" "(stdin)"
| {tag=String; value } ->
| String value ->
let lexbuf = Lexing.from_string value in
use_channel ppf ~wrap_in_module lexbuf "" "(stdin)"
| { value; _} ->
match Load_path.find value with
| File name ->
match Load_path.find name with
| filename ->
let ic = open_in_bin filename in
Misc.try_finally ~always:(fun () -> close_in ic)
(fun () ->
let lexbuf = Lexing.from_channel ic in
use_channel ppf ~wrap_in_module lexbuf value filename)
use_channel ppf ~wrap_in_module lexbuf name filename)
| exception Not_found ->
fprintf ppf "Cannot find file %s.@." value;
fprintf ppf "Cannot find file %s.@." name;
false

let mod_use_file ppf name =
Expand All @@ -109,7 +107,7 @@ let run_script ppf name args =
override_sys_argv args;
let filename =
match name with
| {tag=File; value=name} -> name
| File name -> name
| _ -> "" in
Compmisc.init_path ~dir:(Filename.dirname filename) ();
(* Note: would use [Filename.abspath] here, if we had it. *)
Expand All @@ -122,11 +120,11 @@ let run_script ppf name args =
run_hooks After_setup;
let explicit_name =
match name with
| {tag=File; value=name} as x -> (
| File name as filename -> (
(* Prevent use_silently from searching in the path. *)
if name <> "" && Filename.is_implicit name
then {x with value = (Filename.concat Filename.current_dir_name name)}
else x)
then File (Filename.concat Filename.current_dir_name name)
else filename)
| x -> x
in
use_silently ppf explicit_name
Expand Down Expand Up @@ -172,12 +170,12 @@ let load_ocamlinit ppf =
if !Clflags.noinit then ()
else match !Clflags.init_file with
| Some f ->
if Sys.file_exists f then ignore (use_silently ppf {tag= File; value = f} )
if Sys.file_exists f then ignore (use_silently ppf (File f) )
else fprintf ppf "Init file not found: \"%s\".@." f
| None ->
match find_ocamlinit () with
| None -> ()
| Some file -> ignore (use_silently ppf {tag= File; value = file})
| Some file -> ignore (use_silently ppf (File file))

(* The interactive loop *)

Expand Down
15 changes: 7 additions & 8 deletions toplevel/toploop.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
open Format

(* type of toplevel usages *)
type usage_types =
| File
type input_type =
| Stdin
| String
type usage = {value: string; tag: usage_types}
| File of string
| String of string
(* Accessors for the table of toplevel value bindings. These functions
must appear as first and second exported functions in this module.
(See module Translmod.) *)
Expand All @@ -37,7 +36,7 @@ val loop : formatter -> unit

(* Read and execute a script from the given file *)

val run_script : formatter -> usage -> string array -> bool
val run_script : formatter -> input_type -> string array -> bool
(* true if successful, false if error *)

(* Interface with toplevel directives *)
Expand Down Expand Up @@ -88,10 +87,10 @@ val preprocess_phrase :
formatter -> Parsetree.toplevel_phrase -> Parsetree.toplevel_phrase
(* Preprocess the given toplevel phrase using regular and ppx
preprocessors. Return the updated phrase. *)
val use_file : formatter -> usage -> bool
val use_file : formatter -> input_type -> bool
val use_output : formatter -> string -> bool
val use_silently : formatter -> usage -> bool
val mod_use_file : formatter -> usage -> bool
val use_silently : formatter -> input_type -> bool
val mod_use_file : formatter -> input_type -> bool
(* Read and execute commands from a file.
[use_file] prints the types and values of the results.
[use_silently] does not print them.
Expand Down

0 comments on commit 64ad4a1

Please sign in to comment.