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