+let pp_times fname bench_mode rc big_bang =
+ if bench_mode then
+ begin
+ let { Unix.tms_utime = u ; Unix.tms_stime = s} = Unix.times () in
+ let r = Unix.gettimeofday () -. big_bang in
+ let extra = try Sys.getenv "BENCH_EXTRA_TEXT" with Not_found -> "" in
+ let cc =
+ if Str.string_match (Str.regexp ".*opt$") Sys.argv.(0) 0 then
+ "matitac.opt"
+ else
+ "matitac"
+ in
+ let rc = if rc then "\e[0;32mOK\e[0m" else "\e[0;31mFAIL\e[0m" in
+ let times =
+ let fmt t =
+ let seconds = int_of_float t in
+ let cents = int_of_float ((t -. floor t) *. 100.0) in
+ let minutes = seconds / 60 in
+ let seconds = seconds mod 60 in
+ Printf.sprintf "%dm%02d.%02ds" minutes seconds cents
+ in
+ Printf.sprintf "%s %s %s" (fmt r) (fmt u) (fmt s)
+ in
+ let fname =
+ match MatitamakeLib.development_for_dir (Filename.dirname fname) with
+ | None -> fname
+ | Some d ->
+ let rootlen = String.length(MatitamakeLib.root_for_development d)in
+ let fnamelen = String.length fname in
+ assert (fnamelen > rootlen);
+ String.sub fname rootlen (fnamelen - rootlen)
+ in
+ let fname =
+ if fname.[0] = '/' then
+ String.sub fname 1 (String.length fname - 1)
+ else
+ fname
+ in
+ let s = Printf.sprintf "%s %-35s %-4s %s %s" cc fname rc times extra in
+ print_endline s;
+ flush stdout
+ end
+;;
+
+let rec compiler_loop fname big_bang mode buf =
+ let include_paths =
+ Helm_registry.get_list Helm_registry.string "matita.includes" in
+ let clean_baseuri = not (Helm_registry.get_bool "matita.preserve") in
+ let matita_debug = Helm_registry.get_bool "matita.debug" in
+ let bench_mode = Helm_registry.get_bool "matita.bench" in
+ try
+ run_script buf
+ (MatitaEngine.eval_from_stream ~first_statement_only:false ~include_paths
+ ~clean_baseuri)
+ with
+ | End_of_file -> ()
+ | Sys.Break as exn ->
+ if matita_debug then raise exn;
+ HLog.error "user break!";
+ pp_times fname bench_mode false big_bang;
+ if mode = `COMPILER then
+ clean_exit (Some ~-1)
+ else
+ pp_ocaml_mode ()
+ | GrafiteEngine.Drop ->
+ if mode = `COMPILER then
+ begin
+ pp_times fname bench_mode false big_bang;
+ clean_exit (Some 1)
+ end
+ else
+ pp_ocaml_mode ()
+ | GrafiteEngine.Macro (floc, f) ->
+ begin match f (get_macro_context !grafite_status) with
+ | _, GrafiteAst.Inline (_, style, suri, prefix) ->
+ let str =
+ ApplyTransformation.txt_of_inline_macro style suri prefix
+ ~map_unicode_to_tex:(Helm_registry.get_bool
+ "matita.paste_unicode_as_tex") in
+ !out str;
+ compiler_loop fname big_bang mode buf
+ | _ ->
+ let x, y = HExtlib.loc_of_floc floc in
+ HLog.error (sprintf "A macro has been found in a script at %d-%d" x y);
+ if mode = `COMPILER then
+ begin
+ pp_times fname bench_mode false big_bang;
+ clean_exit (Some 1)
+ end
+ else
+ pp_ocaml_mode ()
+ end
+ | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
+ let (x, y) = HExtlib.loc_of_floc floc in
+ HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
+ if mode = `COMPILER then
+ begin
+ pp_times fname bench_mode false big_bang;
+ clean_exit (Some 1)
+ end
+ else
+ pp_ocaml_mode ()
+ | exn ->
+ if matita_debug then raise exn;
+ if mode = `COMPILER then
+ begin
+ pp_times fname bench_mode false big_bang;
+ clean_exit (Some 3)
+ end
+ else
+ pp_ocaml_mode ()
+