src/Pure/ML-Systems/smlnj.ML
author skalberg
Mon Apr 05 13:30:37 2004 +0200 (2004-04-05)
changeset 14520 af9d7fcf873e
parent 14519 4ca3608fdf4f
child 14656 765badface6a
permissions -rw-r--r--
Whoops. Those default cases can be tricky.
     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 (** ML system related **)
    14 
    15 (* restore old-style character / string functions *)
    16 
    17 val ord     = SML90.ord;
    18 val chr     = SML90.chr;
    19 val explode = SML90.explode;
    20 val implode = SML90.implode;
    21 
    22 
    23 (* New Jersey ML parameters *)
    24 
    25 val _ =
    26  (Compiler.Control.Print.printLength := 1000;
    27   Compiler.Control.Print.printDepth := 350;
    28   Compiler.Control.Print.stringDepth := 250;
    29   Compiler.Control.Print.signatures := 2;
    30   Compiler.Control.MC.matchRedundantError := false);
    31 
    32 
    33 (* Poly/ML emulation *)
    34 
    35 fun quit () = exit 0;
    36 
    37 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    38 fun print_depth n =
    39  (Compiler.Control.Print.printDepth := n div 2;
    40   Compiler.Control.Print.printLength := n);
    41 
    42 
    43 (* compiler-independent timing functions *)
    44 
    45 (case #version_id (Compiler.version) of
    46   [110, x] => if x >= 45
    47 	      then use "ML-Systems/cpu-timer-basis.ML"
    48 	      else use "ML-Systems/cpu-timer-gc.ML"
    49 | _ => use "ML-Systems/cpu-timer-gc.ML");
    50 
    51 
    52 (* prompts *)
    53 
    54 fun ml_prompts p1 p2 =
    55   (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
    56 
    57 
    58 (case #version_id (Compiler.version) of
    59   [110, x] => if x >= 45
    60 	      then use "ML-Systems/smlnj-basis-compat.ML"
    61 	      else ()
    62 | _ => ());
    63 
    64 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    65 
    66 (case #version_id (Compiler.version) of
    67   [110, x] => if x >= 45
    68 	      then use "ML-Systems/smlnj-pp-new.ML"
    69 	      else use "ML-Systems/smlnj-pp-old.ML"
    70 | _ => use "ML-Systems/smlnj-pp-old.ML");
    71 
    72 fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
    73 
    74 
    75 (* ML command execution *)
    76 
    77 fun use_text (print, err) verbose txt =
    78   let
    79     val ref out_orig = Compiler.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     Compiler.Control.Print.out := out;
    88     Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
    89       (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
    90     Compiler.Control.Print.out := out_orig;
    91     if verbose then print (output ()) else ()
    92   end;
    93 
    94 
    95 
    96 (** interrupts **)
    97 
    98 exception Interrupt;
    99 
   100 local
   101 
   102 fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
   103 
   104 fun release NONE = ()
   105   | release (SOME exn) = raise exn;
   106 
   107 in
   108 
   109 fun ignore_interrupt f x =
   110   let
   111     val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
   112     val result = capture f x;
   113     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   114   in release result end;
   115 
   116 fun raise_interrupt f x =
   117   let
   118     val interrupted = ref false;
   119     val result = ref NONE;
   120     val old_handler = Signals.inqHandler Signals.sigINT;
   121   in
   122     SMLofNJ.Cont.callcc (fn cont =>
   123       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
   124       result := capture f x));
   125     Signals.setHandler (Signals.sigINT, old_handler);
   126     if ! interrupted then raise Interrupt else release (! result)
   127   end;
   128 
   129 end;
   130 
   131 
   132 
   133 (** OS related **)
   134 
   135 (* system command execution *)
   136 
   137 (*execute Unix command which doesn't take any input from stdin and
   138   sends its output to stdout; could be done more easily by Unix.execute,
   139   but that function doesn't use the PATH*)
   140 fun execute command =
   141   let
   142     val tmp_name = OS.FileSys.tmpName ();
   143     val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   144     val result = TextIO.inputAll is;
   145   in
   146     TextIO.closeIn is;
   147     OS.FileSys.remove tmp_name;
   148     result
   149   end;
   150 
   151 (*plain version; with return code*)
   152 val system = OS.Process.system: string -> int;
   153 
   154 
   155 (* file handling *)
   156 
   157 (*get time of last modification*)
   158 fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
   159 
   160 
   161 (* getenv *)
   162 
   163 fun getenv var =
   164   (case OS.Process.getEnv var of
   165     NONE => ""
   166   | SOME txt => txt);