2014-10-22 17:42:57 +02:00
|
|
|
open Format
|
|
|
|
open Filename
|
|
|
|
open CtrlNbac
|
|
|
|
open Compiler_utils
|
|
|
|
open Compiler_options
|
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
|
|
|
|
|
|
let report_msgs ?filename =
|
|
|
|
let report_msg = Parser.Reporting.report_msg ?filename err_formatter in
|
|
|
|
List.iter begin function
|
|
|
|
| #CtrlNbac.Parser.msg as msg -> report_msg msg
|
|
|
|
| `TError info -> report_msg (`MError info)
|
|
|
|
end
|
|
|
|
|
|
|
|
let abort ?filename n msgs =
|
|
|
|
report_msgs ?filename msgs;
|
|
|
|
error "Aborting due to errors in %s" n;
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
|
|
|
|
|
|
(** File extensions officially understood by the tool, with associated input
|
|
|
|
types. *)
|
|
|
|
let ityps_alist = [
|
|
|
|
"ctrlf", `Ctrlf; "cf", `Ctrlf;
|
2014-10-28 16:34:58 +01:00
|
|
|
"ctrls", `Ctrlf; "cs", `Ctrlf; (* No need to discriminate between weaved and
|
|
|
|
split functions (for now). *)
|
2014-10-22 17:42:57 +02:00
|
|
|
]
|
|
|
|
|
2014-10-28 16:34:58 +01:00
|
|
|
(** name of official input types as understood by the tool. *)
|
2014-10-22 17:42:57 +02:00
|
|
|
let ityps = List.map fst ityps_alist
|
|
|
|
|
|
|
|
let set_input_type r t =
|
|
|
|
try r := Some (List.assoc t ityps_alist) with
|
|
|
|
| Not_found -> raise (Arg.Bad (asprintf "Unknown input file type: `%s'" t))
|
|
|
|
|
|
|
|
let inputs = ref []
|
|
|
|
let output = ref ""
|
|
|
|
let input_type = ref None
|
2014-10-28 16:34:58 +01:00
|
|
|
let node = ref ""
|
2014-10-22 17:42:57 +02:00
|
|
|
|
2014-10-28 16:34:58 +01:00
|
|
|
exception Help
|
|
|
|
let usage = "Usage: ctrl2ept [options] { [-i] <filename> | -n <node> } \
|
|
|
|
[ -- { <filename> } ]"
|
|
|
|
let print_vers () =
|
|
|
|
fprintf err_formatter "ctrl2ept, version %s (compiled on %s)@." version date;
|
|
|
|
exit 0
|
2014-10-22 17:42:57 +02:00
|
|
|
let anon x = inputs := x :: !inputs
|
2014-10-28 16:34:58 +01:00
|
|
|
let it = Arg.Symbol (ityps, set_input_type input_type)
|
|
|
|
let options = Arg.align
|
2014-10-22 17:42:57 +02:00
|
|
|
[
|
2014-10-28 16:34:58 +01:00
|
|
|
"-i", Arg.String anon, "<file> Input file (`-' means standard input)";
|
|
|
|
"-input-type", it, " Input file type";
|
|
|
|
"--input-type", it, "";
|
|
|
|
"-o", Arg.Set_string output, "<file> Select output file (`-' means \
|
|
|
|
standard output)";
|
|
|
|
"-n", Arg.Set_string node, "<node> Select base input node";
|
|
|
|
"--", Arg.Rest anon, " Treat all remaining arguments as input files";
|
|
|
|
"-where", Arg.Unit locate_stdlib, doc_locate_stdlib;
|
|
|
|
"-stdlib", Arg.String set_stdlib, doc_stdlib;
|
|
|
|
"-v",Arg.Set verbose, " Set verbose mode";
|
|
|
|
"-version", Arg.Unit print_vers, " Print the version of the compiler";
|
|
|
|
"--version", Arg.Unit print_vers, "";
|
|
|
|
"-h", Arg.Unit (fun _ -> raise Help), "";
|
2014-10-22 17:42:57 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
|
|
|
|
|
|
type out =
|
|
|
|
{
|
|
|
|
out_mult: bool; (* Are multiple calls to `out_exec' allowed? *)
|
|
|
|
out_exec: string -> out_channel * (unit -> unit); (* oc * close *)
|
|
|
|
}
|
|
|
|
|
|
|
|
(* --- *)
|
|
|
|
|
|
|
|
let mk_oc basename =
|
|
|
|
{
|
|
|
|
out_exec = (fun ext ->
|
2014-10-28 16:34:58 +01:00
|
|
|
let filename = asprintf "%s%s" basename ext in
|
2014-10-22 17:42:57 +02:00
|
|
|
let oc = open_out filename in
|
|
|
|
info "Outputting into `%s'…" filename;
|
|
|
|
oc, (fun () -> flush oc; close_out oc));
|
|
|
|
out_mult = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mk_oc' filename =
|
|
|
|
{
|
|
|
|
out_exec = (fun _ ->
|
|
|
|
let oc = open_out filename in
|
|
|
|
info "Outputting into `%s'…" filename;
|
|
|
|
oc, (fun () -> flush oc; close_out oc));
|
|
|
|
out_mult = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mk_std_oc =
|
|
|
|
{
|
|
|
|
out_exec = (fun _ ->
|
|
|
|
info "Outputting onto standard output…";
|
|
|
|
stdout, (fun () -> flush stdout));
|
|
|
|
out_mult = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
(* --- *)
|
|
|
|
|
|
|
|
(** Parses the given input file. *)
|
|
|
|
let parse_input ?filename (parse: ?filename:string -> _) =
|
|
|
|
try
|
|
|
|
CtrlNbac.Symb.reset ();
|
|
|
|
let s, n, msgs = parse ?filename () in
|
|
|
|
report_msgs ?filename msgs;
|
|
|
|
s, n
|
|
|
|
with
|
|
|
|
| CtrlNbac.Parser.Error (n, msgs) -> abort ?filename n msgs
|
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
|
|
|
2014-10-28 16:34:58 +01:00
|
|
|
exception Error of string
|
|
|
|
|
|
|
|
(* let hack_filter_inputs = let open AST in function *)
|
|
|
|
(* | `Desc ({ fn_decls = decls } as f) -> *)
|
|
|
|
(* (\* TODO: we should actually _substitute_ these variables with ff in the *)
|
|
|
|
(* definitions; yet I think they are unlikely to appear anywhere in the *)
|
|
|
|
(* controller. *\) *)
|
|
|
|
(* let init_symb = Symb.of_string Ctrln_utils.init_cond_str *)
|
|
|
|
(* and sink_symb = Symb.of_string Ctrln_utils.sink_state_str in *)
|
|
|
|
(* let decls = SMap.remove init_symb decls in *)
|
|
|
|
(* let decls = SMap.remove sink_symb decls in *)
|
|
|
|
(* `Desc { f with fn_decls = decls } *)
|
|
|
|
(* | _ -> failwith "should be given an unchecked function!" *)
|
|
|
|
|
|
|
|
let parse_n_gen_ept_node ?filename ?node_name ?node_sig () =
|
2014-10-22 17:42:57 +02:00
|
|
|
let name, func = parse_input ?filename CtrlNbac.Parser.Unsafe.parse_func in
|
2014-10-28 16:34:58 +01:00
|
|
|
let node_name = match node_name with Some n -> n
|
|
|
|
| None -> match name with None -> assert false
|
|
|
|
| Some n -> Names.local_qn (n ^ "_ctrlr")
|
|
|
|
in
|
|
|
|
(* let name = match name with None -> "ctrlr" | Some n -> n ^"_ctrlr" in *)
|
|
|
|
(* let func = hack_filter_inputs func in *)
|
|
|
|
name, CtrlNbacAsEpt.gen_func ~node_name ?node_sig func
|
|
|
|
|
|
|
|
let handle_ctrlf ?filename mk_oc =
|
|
|
|
let _, (node, typs) = parse_n_gen_ept_node ?filename () in
|
|
|
|
let prog = CtrlNbacAsEpt.create_prog Names.LocalModule in (* don't care? *)
|
|
|
|
let prog = List.fold_right CtrlNbacAsEpt.add_to_prog typs prog in
|
|
|
|
let prog = CtrlNbacAsEpt.add_to_prog node prog in
|
2014-10-22 17:42:57 +02:00
|
|
|
let oc, close = mk_oc.out_exec "ept" in
|
|
|
|
Hept_printer.print oc prog;
|
|
|
|
close ()
|
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
|
|
|
2014-10-28 16:34:58 +01:00
|
|
|
let parse_nodename nn = try Names.qualname_of_string nn with
|
|
|
|
| Exit -> raise (Error (sprintf "Invalid node name: `%s'" nn))
|
|
|
|
|
|
|
|
let output_prog prog modul =
|
|
|
|
Modules.select modul;
|
|
|
|
let filename = String.uncapitalize (Names.modul_to_string modul) ^ ".ept" in
|
|
|
|
let oc = open_out filename in
|
|
|
|
info "Outputting into `%s'…" filename;
|
|
|
|
Hept_printer.print oc prog;
|
|
|
|
close_out oc
|
|
|
|
|
|
|
|
let input_function prog filename node_name node_sig =
|
|
|
|
info "Reading function from `%s'…" filename;
|
|
|
|
let res = parse_n_gen_ept_node ~filename ~node_name ~node_sig () in
|
|
|
|
let node, typs = snd res in
|
|
|
|
let prog = List.fold_right CtrlNbacAsEpt.add_to_prog typs prog in
|
|
|
|
let prog = CtrlNbacAsEpt.add_to_prog node prog in
|
|
|
|
prog
|
|
|
|
|
|
|
|
let try_ctrlf nn prog =
|
|
|
|
let node_name = Ctrln_utils.controller_node nn in
|
|
|
|
if Modules.check_value node_name then
|
|
|
|
let filename = Ctrln_utils.ctrlf_for_node nn in
|
|
|
|
let node_sig = Modules.find_value node_name in
|
|
|
|
input_function prog filename node_name node_sig
|
|
|
|
else
|
|
|
|
raise (Error "Unable to load any controller function.")
|
|
|
|
|
|
|
|
let try_ctrls nn prog =
|
|
|
|
let rec try_ctrls num prog =
|
|
|
|
let node_name = Ctrln_utils.controller_node ~num nn in
|
|
|
|
if Modules.check_value node_name then
|
|
|
|
let filename = Ctrln_utils.ctrls_for_node nn num in
|
|
|
|
let node_sig = Modules.find_value node_name in
|
|
|
|
let prog = input_function prog filename node_name node_sig in
|
|
|
|
try_ctrls (succ num) prog
|
|
|
|
else
|
|
|
|
prog
|
|
|
|
in
|
|
|
|
try_ctrls 0 prog
|
|
|
|
|
|
|
|
let handle_node arg =
|
|
|
|
let nn = parse_nodename arg in
|
|
|
|
|
|
|
|
let mo = Names.modul nn in
|
|
|
|
if mo = Names.Pervasives || mo = Names.LocalModule then
|
|
|
|
raise (Error (sprintf "Invalid node specification: `%s'." arg));
|
|
|
|
|
|
|
|
Initial.initialize Names.Pervasives;
|
|
|
|
info "Loading module of controllers for node %s…" (Names.fullname nn);
|
|
|
|
let om = Ctrln_utils.controller_modul mo in
|
|
|
|
Modules.open_module om;
|
|
|
|
let prog = CtrlNbacAsEpt.create_prog om in
|
|
|
|
let prog = try_ctrls nn prog in
|
|
|
|
let prog = if prog.Heptagon.p_desc = [] then try_ctrlf nn prog else prog in
|
|
|
|
output_prog prog om
|
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
|
|
|
2014-10-22 17:42:57 +02:00
|
|
|
let ityp_name_n_handle = function
|
|
|
|
(* | `Ctrln -> "node", handle_ctrln *)
|
|
|
|
| `Ctrlf -> "function", handle_ctrlf
|
|
|
|
(* | `Ctrlr -> "predicate", handle_ctrlr *)
|
|
|
|
|
|
|
|
let guesstyp_n_output filename =
|
|
|
|
try
|
|
|
|
let typ = match !input_type with
|
|
|
|
| Some t -> t
|
|
|
|
| None -> snd (List.find (fun (suff, _) -> check_suffix filename suff)
|
|
|
|
ityps_alist)
|
|
|
|
in
|
|
|
|
let basename_extra = match typ with
|
|
|
|
| `Ctrlf -> "_ctrlr"
|
|
|
|
in
|
|
|
|
typ,
|
|
|
|
(match !output with
|
|
|
|
| "-" -> mk_std_oc
|
|
|
|
| "" -> (try chop_extension filename ^ basename_extra |> mk_oc with
|
|
|
|
| Invalid_argument _ when filename <> "" -> mk_oc filename
|
|
|
|
| Invalid_argument _ -> mk_std_oc)
|
|
|
|
| o -> mk_oc' o)
|
|
|
|
with
|
|
|
|
| Not_found ->
|
|
|
|
raise (Arg.Bad (sprintf "Cannot guess input type of `%s'" filename))
|
|
|
|
|
2014-10-28 16:34:58 +01:00
|
|
|
let handle_input_file filename =
|
|
|
|
let ityp, mk_oc = guesstyp_n_output filename in
|
2014-10-22 17:42:57 +02:00
|
|
|
let itypname, handle = ityp_name_n_handle ityp in
|
|
|
|
info "Reading %s from `%s'…" itypname filename;
|
|
|
|
handle ~filename mk_oc
|
|
|
|
|
|
|
|
let handle_input_stream = function
|
|
|
|
| None ->
|
|
|
|
info "Reading function from standard input…";
|
|
|
|
handle_ctrlf mk_std_oc
|
|
|
|
| Some ityp ->
|
|
|
|
let itypname, handle = ityp_name_n_handle ityp in
|
|
|
|
info "Reading %s from standard input…" itypname;
|
|
|
|
handle mk_std_oc
|
|
|
|
|
|
|
|
(** [main] function to be launched *)
|
|
|
|
let main () =
|
2014-10-28 16:34:58 +01:00
|
|
|
Arg.parse options anon usage;
|
2014-10-22 17:42:57 +02:00
|
|
|
match List.rev !inputs with
|
2014-10-28 16:34:58 +01:00
|
|
|
| [] when !node <> "" -> handle_node !node
|
2014-10-22 17:42:57 +02:00
|
|
|
| [] -> handle_input_stream !input_type
|
2014-10-28 16:34:58 +01:00
|
|
|
| lst -> (if !node <> "" then handle_node !node;
|
|
|
|
List.iter handle_input_file lst)
|
2014-10-22 17:42:57 +02:00
|
|
|
|
|
|
|
(* -------------------------------------------------------------------------- *)
|
|
|
|
(** Launch the [main] *)
|
|
|
|
let _ =
|
|
|
|
try
|
|
|
|
main ()
|
|
|
|
with
|
2014-10-28 16:34:58 +01:00
|
|
|
| Help -> Arg.usage options usage
|
2014-10-22 17:42:57 +02:00
|
|
|
| Errors.Error -> error "aborted."; exit 2
|
2014-10-28 16:34:58 +01:00
|
|
|
| Error s | Arg.Bad s | Sys_error s -> error "%s" s; exit 2
|