src/Pure/ML-Systems/smlnj.ML
author wenzelm
Wed Nov 26 14:35:55 2014 +0100 (2014-11-26 ago)
changeset 59055 5a7157b8e870
parent 56627 cb912b7de3cf
child 59127 723b11f8ffbf
permissions -rw-r--r--
more informative failure of protocol commands, with exception trace;
eliminated obsolete Runtime.TERMINATE (left-over from former 'exit' command);
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2 
     3 Compatibility file for Standard ML of New Jersey.
     4 *)
     5 
     6 val io_buffer_size = 4096;
     7 use "ML-Systems/proper_int.ML";
     8 
     9 exception Interrupt;
    10 fun reraise exn = raise exn;
    11 
    12 fun exit rc = Posix.Process.exit (Word8.fromInt rc);
    13 fun quit () = exit 0;
    14 
    15 use "ML-Systems/overloading_smlnj.ML";
    16 use "General/exn.ML";
    17 use "ML-Systems/single_assignment.ML";
    18 use "ML-Systems/universal.ML";
    19 use "ML-Systems/thread_dummy.ML";
    20 use "ML-Systems/multithreading.ML";
    21 use "ML-Systems/ml_name_space.ML";
    22 use "ML-Systems/ml_pretty.ML";
    23 structure PolyML = struct end;
    24 use "ML-Systems/pp_dummy.ML";
    25 use "ML-Systems/use_context.ML";
    26 use "ML-Systems/ml_positions.ML";
    27 
    28 
    29 val seconds = Time.fromReal;
    30 
    31 (*low-level pointer equality*)
    32 CM.autoload "$smlnj/init/init.cmi";
    33 val pointer_eq = InlineT.ptreql;
    34 
    35 
    36 (* restore old-style character / string functions *)
    37 
    38 val ord = mk_int o SML90.ord;
    39 val chr = SML90.chr o dest_int;
    40 val raw_explode = SML90.explode;
    41 val implode = SML90.implode;
    42 
    43 
    44 (* New Jersey ML parameters *)
    45 
    46 val _ =
    47  (Control.Print.printLength := 1000;
    48   Control.Print.printDepth := 350;
    49   Control.Print.stringDepth := 250;
    50   Control.Print.signatures := 2;
    51   Control.MC.matchRedundantError := false);
    52 
    53 
    54 (* Poly/ML emulation *)
    55 
    56 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    57 local
    58   val depth = ref (10: int);
    59 in
    60   fun get_default_print_depth () = ! depth;
    61   fun default_print_depth n =
    62    (depth := n;
    63     Control.Print.printDepth := dest_int n div 2;
    64     Control.Print.printLength := dest_int n);
    65   val _ = default_print_depth 10;
    66 end;
    67 
    68 val ml_make_string = "(fn _ => \"?\")";
    69 
    70 
    71 (*prompts*)
    72 fun ml_prompts p1 p2 =
    73   (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
    74 
    75 (*dummy implementation*)
    76 fun profile (n: int) f x = f x;
    77 
    78 (*dummy implementation*)
    79 fun print_exception_trace (_: exn -> string) (_: string -> unit) f = f ();
    80 
    81 
    82 (* ML command execution *)
    83 
    84 fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
    85   let
    86     val ref out_orig = Control.Print.out;
    87 
    88     val out_buffer = ref ([]: string list);
    89     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    90     fun output () =
    91       let val str = implode (rev (! out_buffer))
    92       in String.substring (str, 0, Int.max (0, size str - 1)) end;
    93   in
    94     Control.Print.out := out;
    95     Backend.Interact.useStream (TextIO.openString (tune_source (ml_positions line name txt)))
    96       handle exn =>
    97         (Control.Print.out := out_orig;
    98           error ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
    99     Control.Print.out := out_orig;
   100     if verbose then print (output ()) else ()
   101   end;
   102 
   103 fun use_file context verbose name =
   104   let
   105     val instream = TextIO.openIn name;
   106     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
   107   in use_text context (1, name) verbose txt end;
   108 
   109 
   110 (* toplevel pretty printing *)
   111 
   112 fun ml_pprint pps =
   113   let
   114     fun str "" = ()
   115       | str s = PrettyPrint.string pps s;
   116     fun pprint (ML_Pretty.Block ((bg, en), prts, ind)) =
   117           (str bg; PrettyPrint.openHOVBox pps (PrettyPrint.Rel (dest_int ind));
   118             List.app pprint prts; PrettyPrint.closeBox pps; str en)
   119       | pprint (ML_Pretty.String (s, _)) = str s
   120       | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
   121       | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps;
   122   in pprint end;
   123 
   124 fun toplevel_pp context path pp =
   125   use_text context (1, "pp") false
   126     ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
   127       "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
   128 
   129 
   130 
   131 (** interrupts **)
   132 
   133 local
   134 
   135 fun change_signal new_handler f x =
   136   let
   137     val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
   138     val result = Exn.capture (f old_handler) x;
   139     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   140   in Exn.release result end;
   141 
   142 in
   143 
   144 fun interruptible (f: 'a -> 'b) x =
   145   let
   146     val result = ref (Exn.interrupt_exn: 'b Exn.result);
   147     val old_handler = Signals.inqHandler Signals.sigINT;
   148   in
   149     SMLofNJ.Cont.callcc (fn cont =>
   150       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
   151         result := Exn.capture f x));
   152     Signals.setHandler (Signals.sigINT, old_handler);
   153     Exn.release (! result)
   154   end;
   155 
   156 fun uninterruptible f =
   157   change_signal Signals.IGNORE
   158     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   159 
   160 end;
   161 
   162 
   163 use "ML-Systems/unsynchronized.ML";
   164 
   165 
   166 (* ML system operations *)
   167 
   168 use "ML-Systems/ml_system.ML";
   169 
   170 structure ML_System =
   171 struct
   172 
   173 open ML_System;
   174 
   175 fun save_state name =
   176   if SMLofNJ.exportML name then ()
   177   else OS.FileSys.rename {old = name ^ "." ^ platform, new = name};
   178 
   179 end;
   180