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