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