src/Pure/ML-Systems/smlnj.ML
author wenzelm
Wed Sep 30 11:36:12 2009 +0200 (2009-09-30)
changeset 32776 1504f9c2d060
parent 32737 76fa673eee8b
child 33060 e66b41782cb5
permissions -rw-r--r--
more uniform treatment of structure Unsynchronized in ML bootstrap phase;
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2 
     3 Compatibility file for Standard ML of New Jersey 110 or later.
     4 *)
     5 
     6 exception Interrupt;
     7 fun reraise exn = raise exn;
     8 
     9 use "ML-Systems/proper_int.ML";
    10 use "ML-Systems/unsynchronized.ML";
    11 use "ML-Systems/overloading_smlnj.ML";
    12 use "ML-Systems/exn.ML";
    13 use "ML-Systems/universal.ML";
    14 use "ML-Systems/thread_dummy.ML";
    15 use "ML-Systems/multithreading.ML";
    16 use "ML-Systems/timing.ML";
    17 use "ML-Systems/system_shell.ML";
    18 use "ML-Systems/ml_name_space.ML";
    19 use "ML-Systems/ml_pretty.ML";
    20 use "ML-Systems/use_context.ML";
    21 
    22 
    23 (*low-level pointer equality*)
    24 
    25 CM.autoload "$smlnj/init/init.cmi";
    26 val pointer_eq = InlineT.ptreql;
    27 
    28 
    29 (* restore old-style character / string functions *)
    30 
    31 val ord     = mk_int o SML90.ord;
    32 val chr     = SML90.chr o dest_int;
    33 val explode = SML90.explode;
    34 val implode = SML90.implode;
    35 
    36 
    37 (* New Jersey ML parameters *)
    38 
    39 val _ =
    40  (Control.Print.printLength := 1000;
    41   Control.Print.printDepth := 350;
    42   Control.Print.stringDepth := 250;
    43   Control.Print.signatures := 2;
    44   Control.MC.matchRedundantError := false);
    45 
    46 
    47 (* Poly/ML emulation *)
    48 
    49 val exit = exit o dest_int;
    50 fun quit () = exit 0;
    51 
    52 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    53 local
    54   val depth = ref (10: int);
    55 in
    56   fun get_print_depth () = ! depth;
    57   fun print_depth n =
    58    (depth := n;
    59     Control.Print.printDepth := dest_int n div 2;
    60     Control.Print.printLength := dest_int n);
    61 end;
    62 
    63 
    64 (*prompts*)
    65 fun ml_prompts p1 p2 =
    66   (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
    67 
    68 (*dummy implementation*)
    69 fun profile (n: int) f x = f x;
    70 
    71 (*dummy implementation*)
    72 fun exception_trace f = f ();
    73 
    74 
    75 (* ML command execution *)
    76 
    77 fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
    78   let
    79     val ref out_orig = Control.Print.out;
    80 
    81     val out_buffer = ref ([]: string list);
    82     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    83     fun output () =
    84       let val str = implode (rev (! out_buffer))
    85       in String.substring (str, 0, Int.max (0, size str - 1)) end;
    86   in
    87     Control.Print.out := out;
    88     Backend.Interact.useStream (TextIO.openString (tune_source txt)) handle exn =>
    89       (Control.Print.out := out_orig;
    90         error ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
    91     Control.Print.out := out_orig;
    92     if verbose then print (output ()) else ()
    93   end;
    94 
    95 fun use_file context verbose name =
    96   let
    97     val instream = TextIO.openIn name;
    98     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
    99   in use_text context (1, name) verbose txt end;
   100 
   101 fun forget_structure _ = ();
   102 
   103 
   104 (* toplevel pretty printing *)
   105 
   106 fun ml_pprint pps =
   107   let
   108     fun str "" = ()
   109       | str s = PrettyPrint.string pps s;
   110     fun pprint (ML_Pretty.Block ((bg, en), prts, ind)) =
   111           (str bg; PrettyPrint.openHOVBox pps (PrettyPrint.Rel (dest_int ind));
   112             List.app pprint prts; PrettyPrint.closeBox pps; str en)
   113       | pprint (ML_Pretty.String (s, _)) = str s
   114       | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
   115       | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps;
   116   in pprint end;
   117 
   118 fun toplevel_pp context path pp =
   119   use_text context (1, "pp") false
   120     ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
   121       "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
   122 
   123 
   124 
   125 (** interrupts **)
   126 
   127 local
   128 
   129 fun change_signal new_handler f x =
   130   let
   131     val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
   132     val result = Exn.capture (f old_handler) x;
   133     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   134   in Exn.release result end;
   135 
   136 in
   137 
   138 fun interruptible (f: 'a -> 'b) x =
   139   let
   140     val result = ref (Exn.Exn Interrupt: 'b Exn.result);
   141     val old_handler = Signals.inqHandler Signals.sigINT;
   142   in
   143     SMLofNJ.Cont.callcc (fn cont =>
   144       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
   145         result := Exn.capture f x));
   146     Signals.setHandler (Signals.sigINT, old_handler);
   147     Exn.release (! result)
   148   end;
   149 
   150 fun uninterruptible f =
   151   change_signal Signals.IGNORE
   152     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   153 
   154 end;
   155 
   156 
   157 (* basis library fixes *)
   158 
   159 structure TextIO =
   160 struct
   161   open TextIO;
   162   fun inputLine is = TextIO.inputLine is
   163     handle IO.Io _ => raise Interrupt;
   164 end;
   165 
   166 
   167 
   168 (** OS related **)
   169 
   170 (* current directory *)
   171 
   172 val cd = OS.FileSys.chDir;
   173 val pwd = OS.FileSys.getDir;
   174 
   175 
   176 (* system command execution *)
   177 
   178 val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
   179 
   180 
   181 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   182 fun process_id pid =
   183   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())));
   184 
   185 
   186 (* getenv *)
   187 
   188 fun getenv var =
   189   (case OS.Process.getEnv var of
   190     NONE => ""
   191   | SOME txt => txt);