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