Refactored module EvalOpt by removing useless command line options.

This commit is contained in:
Christian Rinderknecht 2019-07-24 14:34:26 +02:00
parent 685c25de9a
commit d2f4d00011
3 changed files with 21 additions and 72 deletions

View File

@ -1,12 +1,9 @@
(* Parsing the command-line option for the Mini-ML compiler/interpreter *)
(* Parsing the command-line option for CameLIGO *)
type options = {
input : string option;
eval : bool;
compile : string option;
libs : string list;
verbose : Utils.String.Set.t;
raw_edits : bool
verbose : Utils.String.Set.t
}
let abort msg =
@ -19,16 +16,12 @@ let sprintf = Printf.sprintf
let help () =
let file = Filename.basename Sys.argv.(0) in
printf "Usage: %s [<option> ...] [<input>.ml | \"-\"]\n" file;
print_endline "where <input>.mml is the Mini-ML source file (default: stdin),";
printf "Usage: %s [<option> ...] [<input>.mligo | \"-\"]\n" file;
print_endline "where <input>.mligo is the CameLIGO source file (default: stdin),";
print_endline "and each <option> (if any) is one of the following:";
print_endline " -I <paths> Library paths (colon-separated)";
print_endline " -c [<file>.ml] Translate to OCaml in <file>.ml";
print_endline " (default: <input>.ml)";
print_endline " -e, --eval Interpret <input>.mml or stdin";
print_endline " --raw-edits Do not optimise translation edits";
print_endline " --verbose=<phases> Colon-separated phases: cmdline, lexer, parser";
print_endline " --version Short commit hash on stdout";
print_endline " --version Send short commit hash to stdout";
print_endline " -h, --help This help";
exit 0
@ -39,16 +32,9 @@ let version () = printf "%s\n" Version.version; exit 0
(* Specifying the command-line options a la GNU *)
let input = ref None
and eval = ref false
and compile = ref None
and verbose = ref Utils.String.Set.empty
and libs = ref []
and raw_edits = ref false
let verb_str = ref ""
let set_opt var err =
Some (fun x -> if !var = None then var := Some x else raise (Getopt.Error err))
and verb_str = ref ""
let split_at_colon = Str.(split (regexp ":"))
@ -62,10 +48,6 @@ let add_verbose d =
let specs =
let open! Getopt in [
'I', nolong, None, Some add_path;
'c', nolong, set compile (Some ""),
set_opt compile "Multiple OCaml outputs";
'e', "eval", set eval true, None;
noshort, "raw-edits", set raw_edits true, None;
noshort, "verbose", None, Some add_verbose;
'h', "help", Some help, None;
noshort, "version", Some version, None
@ -94,9 +76,6 @@ let quote s = Printf.sprintf "\"%s\"" s
let print_opt () =
printf "COMMAND LINE\n";
printf "input = %s\n" (string_of quote !input);
printf "compile = %s\n" (string_of quote !compile);
printf "eval = %B\n" !eval;
printf "raw_edits = %b\n" !raw_edits;
printf "verbose = %s\n" !verb_str;
printf "libs = %s\n" (string_of_path !libs)
@ -106,50 +85,29 @@ let check () =
let input =
match !input with
None | Some "-" ->
if !compile <> None then
abort "An input file is missing (for compilation)."
else !input
None | Some "-" -> !input
| Some file_path ->
if Filename.check_suffix file_path ".mml"
if Filename.check_suffix file_path ".mligo"
then if Sys.file_exists file_path
then Some file_path
else abort "Source file not found."
else abort "Source file lacks the extension .mml." in
let compile =
match !compile with
Some _ when !eval -> abort "Options -e and -c are mutually exclusive."
| None | Some "-" -> !compile
| Some "" ->
(match input with
None | Some "-" -> abort "The target OCaml filename is missing."
| Some file -> Some (Filename.remove_extension file ^ ".ml"))
| Some compile' ->
if Filename.check_suffix compile' ".ml"
then !compile
else abort "The extension of the target OCaml file is not .ml" in
else abort "Source file lacks the extension .mligo." in
(* Exporting remaining options as non-mutable values *)
let eval = !eval
and verbose = !verbose
and libs = !libs
and raw_edits = !raw_edits in
let verbose = !verbose
and libs = !libs in
let () =
if Utils.String.Set.mem "cmdline" verbose then
begin
printf "\nEXPORTED COMMAND LINE\n";
printf "input = %s\n" (string_of quote input);
printf "compile = %s\n" (string_of quote compile);
printf "eval = %B\n" eval;
printf "raw_edits = %B\n" raw_edits;
printf "verbose = %s\n" !verb_str;
printf "I = %s\n" (string_of_path libs)
end
in {input; eval; compile; libs; verbose; raw_edits}
in {input; libs; verbose}
(* Parsing the command-line options *)

View File

@ -1,24 +1,14 @@
(* Command-line options for the Mini-ML compiler/interpreter *)
(* Command-line options for CameLIGO *)
(* If the value of [input] is [Some src], the name of the Mini-ML
source file, with the extension ".mml", is [src]. If [input] is
(* If the value of [input] is [Some src], the name of the CameLIGO
source file, with the extension ".mligo", is [src]. If [input] is
[Some "-"] or [None], the source file is read from standard
input. *)
(* The Mini-ML source file can be processed in two mutually exclusive
manners: if the value [eval] is set to [true], the source is
interpreted; if the value [compile] is not [None], the source is
compiled to OCaml; if [eval] is [false] and [compile] is [None],
nothing is done with the source. Note: if [compile] is [Some "-"],
the compiled code is sent to standard output. *)
type options = {
input : string option;
eval : bool;
compile : string option;
libs : string list;
verbose : Utils.String.Set.t;
raw_edits : bool
}
val read : unit -> options

View File

@ -53,6 +53,7 @@ let rec mk_field_path (rank, tail) =
%start program interactive_expr
%type <AST.t> program
%type <AST.expr> interactive_expr
(*%type <('item,'sep) sep_or_term_list> sep_or_term_list*)
%%
@ -178,7 +179,7 @@ tuple(item):
(* Possibly empty semicolon-separated values between brackets *)
list_of(item):
list(item):
lbracket sep_or_term_list(item,semi) rbracket {
let elements, terminator = $2 in {
opening = LBracket $1;
@ -360,7 +361,7 @@ core_pattern:
| kwd(False) { PFalse $1 }
| string { PString $1 }
| par(ptuple) { PPar $1 }
| reg(list_of(tail)) { PList (Sugar $1) }
| reg(list(tail)) { PList (Sugar $1) }
| reg(constr_pattern) { PConstr $1 }
| reg(record_pattern) { PRecord $1 }
@ -608,7 +609,7 @@ core_expr:
| unit { EUnit $1 }
| kwd(False) { ELogic (BoolExpr (False $1)) }
| kwd(True) { ELogic (BoolExpr (True $1)) }
| reg(list_of(expr)) { EList (List $1) }
| reg(list(expr)) { EList (List $1) }
| par(expr) { EPar $1 }
| reg(sequence) { ESeq $1 }
| reg(record_expr) { ERecord $1 }