src/Pure/ML-Systems/smlnj.ML
author wenzelm
Mon Feb 11 17:30:58 2002 +0100 (2002-02-11)
changeset 12874 368966ceafe5
parent 12581 dceea9dbdedd
child 12990 c11adf2b1c1e
permissions -rw-r--r--
ML-Systems/smlnj-compiler.ML compatibility tweak;
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2     ID:         $Id$
     3     Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Compatibility file for Standard ML of New Jersey 110 or later.
     7 *)
     8 
     9 (case #version_id (Compiler.version) of
    10   [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
    11 | _ => ());
    12 
    13 
    14 (** ML system related **)
    15 
    16 (* restore old-style character / string functions *)
    17 
    18 val ord     = SML90.ord;
    19 val chr     = SML90.chr;
    20 val explode = SML90.explode;
    21 val implode = SML90.implode;
    22 
    23 
    24 (* New Jersey ML parameters *)
    25 
    26 val _ =
    27  (Compiler.Control.Print.printLength := 1000;
    28   Compiler.Control.Print.printDepth := 350;
    29   Compiler.Control.Print.stringDepth := 250;
    30   Compiler.Control.Print.signatures := 2;
    31   Compiler.Control.MC.matchRedundantError := false);
    32 
    33 
    34 (* Poly/ML emulation *)
    35 
    36 fun quit () = exit 0;
    37 
    38 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    39 fun print_depth n =
    40  (Compiler.Control.Print.printDepth := n div 2;
    41   Compiler.Control.Print.printLength := n);
    42 
    43 
    44 (* compiler-independent timing functions *)
    45 
    46 (*Note start point for timing*)
    47 fun startTiming() =
    48   let val CPUtimer = Timer.startCPUTimer();
    49       val time = Timer.checkCPUTimer(CPUtimer)
    50   in  (CPUtimer,time)  end;
    51 
    52 (*Finish timing and return string*)
    53 fun endTiming (CPUtimer, {gc,sys,usr}) =
    54   let open Time  (*...for Time.toString, Time.+ and Time.- *)
    55       val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
    56   in  "User " ^ toString (usr2-usr) ^
    57       "  GC " ^ toString (gc2-gc) ^
    58       "  All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
    59       " secs"
    60       handle Time => ""
    61   end;
    62 
    63 
    64 (* prompts *)
    65 
    66 fun ml_prompts p1 p2 =
    67   (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
    68 
    69 
    70 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    71 
    72 fun make_pp path pprint =
    73   let
    74     open Compiler.PrettyPrint;
    75 
    76     fun pp pps obj =
    77       pprint obj
    78         (add_string pps, begin_block pps INCONSISTENT,
    79           fn wd => add_break pps (wd, 0), fn () => add_newline pps,
    80           fn () => end_block pps);
    81   in
    82     (path, pp)
    83   end;
    84 
    85 fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
    86 
    87 
    88 (* ML command execution *)
    89 
    90 fun use_text (print, err) verbose txt =
    91   let
    92     val ref out_orig = Compiler.Control.Print.out;
    93 
    94     val out_buffer = ref ([]: string list);
    95     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    96     fun output () =
    97       let val str = implode (rev (! out_buffer))
    98       in String.substring (str, 0, Int.max (0, size str - 1)) end;
    99   in
   100     Compiler.Control.Print.out := out;
   101     Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
   102       (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
   103     Compiler.Control.Print.out := out_orig;
   104     if verbose then print (output ()) else ()
   105   end;
   106 
   107 
   108 
   109 (** interrupts **)
   110 
   111 local
   112 
   113 datatype 'a result =
   114   Result of 'a |
   115   Exn of exn;
   116 
   117 fun capture f x = Result (f x) handle exn => Exn exn;
   118 
   119 fun release (Result x) = x
   120   | release (Exn exn) = raise exn;
   121 
   122 
   123 val sig_int = Signals.sigINT;
   124 val sig_int_mask = Signals.MASK [Signals.sigINT];
   125 
   126 fun interruptible () =
   127   (case Signals.masked () of
   128     Signals.MASKALL => false
   129   | Signals.MASK sigs => List.all (fn s => s <> sig_int) sigs);
   130 
   131 val mask_signals = Signals.maskSignals;
   132 val unmask_signals = Signals.unmaskSignals;
   133 
   134 fun change_mask ok change unchange f x =
   135   if ok () then f x
   136   else
   137     let
   138       val _ = change sig_int_mask;
   139       val result = capture f x;
   140       val _ = unchange sig_int_mask;
   141     in release result end;
   142 
   143 in
   144 
   145 
   146 (* mask / unmask interrupt *)
   147 
   148 fun mask_interrupt f = change_mask (not o interruptible) mask_signals unmask_signals f;
   149 fun unmask_interrupt f = change_mask interruptible unmask_signals mask_signals f;
   150 
   151 
   152 (* exhibit interrupt (via exception) *)
   153 
   154 exception Interrupt;
   155 
   156 fun exhibit_interrupt f x =
   157   let
   158     val orig_handler = Signals.inqHandler sig_int;
   159     fun reset_handler () = (Signals.setHandler (sig_int, orig_handler); ());
   160 
   161     val interrupted = ref false;
   162 
   163     fun set_handler cont =
   164       Signals.setHandler (sig_int, Signals.HANDLER (fn _ => (interrupted := true; cont)));
   165 
   166     fun proceed cont =
   167       let
   168         val _ = set_handler cont;
   169         val result = unmask_interrupt (capture f) x;
   170         val _ = reset_handler ();
   171       in release result end;
   172   in
   173     SMLofNJ.Cont.callcc proceed;
   174     reset_handler ();
   175     if ! interrupted then raise Interrupt else ()
   176    end;
   177 
   178 end;
   179 
   180 
   181 
   182 (** OS related **)
   183 
   184 (* system command execution *)
   185 
   186 (*execute Unix command which doesn't take any input from stdin and
   187   sends its output to stdout; could be done more easily by Unix.execute,
   188   but that function doesn't use the PATH*)
   189 fun execute command =
   190   let
   191     val tmp_name = OS.FileSys.tmpName ();
   192     val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   193     val result = TextIO.inputAll is;
   194   in
   195     TextIO.closeIn is;
   196     OS.FileSys.remove tmp_name;
   197     result
   198   end;
   199 
   200 (*plain version; with return code*)
   201 val system = OS.Process.system: string -> int; 
   202 
   203 
   204 (* file handling *)
   205 
   206 (*get time of last modification*)
   207 fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
   208 
   209 
   210 (* getenv *)
   211 
   212 fun getenv var =
   213   (case OS.Process.getEnv var of
   214     NONE => ""
   215   | SOME txt => txt);