src/Pure/ML-Systems/polyml.ML
author wenzelm
Sat Jun 05 13:06:39 2004 +0200 (2004-06-05)
changeset 14870 c5cf7c001313
parent 14850 393a7be73160
child 15028 f6a22afe0134
permissions -rw-r--r--
tuned comments;
wenzelm@2341
     1
(*  Title:      Pure/ML-Systems/polyml.ML
wenzelm@2341
     2
    ID:         $Id$
wenzelm@2341
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@2341
     4
    Copyright   1991  University of Cambridge
wenzelm@2341
     5
wenzelm@14870
     6
Compatibility file for Poly/ML (version 4.0, 4.1, 4.1.x).
wenzelm@2341
     7
*)
wenzelm@2341
     8
wenzelm@11078
     9
(** ML system related **)
wenzelm@2341
    10
wenzelm@11078
    11
(* old Poly/ML emulation *)
wenzelm@2341
    12
wenzelm@11078
    13
local
wenzelm@11078
    14
  val orig_exit = exit;
wenzelm@11078
    15
in
wenzelm@11078
    16
  open PolyML;
wenzelm@11078
    17
  val exit = orig_exit;
wenzelm@11078
    18
  fun quit () = exit 0;
wenzelm@7148
    19
end;
paulson@6042
    20
wenzelm@2341
    21
wenzelm@11078
    22
(* restore old-style character / string functions *)
wenzelm@3588
    23
wenzelm@11078
    24
val ord = SML90.ord;
wenzelm@11078
    25
val chr = SML90.chr;
wenzelm@11078
    26
val explode = SML90.explode;
wenzelm@11078
    27
val implode = SML90.implode;
wenzelm@11078
    28
wenzelm@14870
    29
skalberg@14519
    30
(* compiler-independent timing functions *)
wenzelm@2341
    31
skalberg@14519
    32
use "ML-Systems/cpu-timer-basis.ML";
wenzelm@2341
    33
wenzelm@14870
    34
webertj@14850
    35
(* bounded time execution *)
webertj@14850
    36
webertj@14850
    37
use "ML-Systems/polyml-time-limit.ML";
webertj@14850
    38
wenzelm@14870
    39
wenzelm@4977
    40
(* prompts *)
wenzelm@4977
    41
wenzelm@4984
    42
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
wenzelm@4977
    43
wenzelm@4977
    44
wenzelm@3588
    45
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@2341
    46
wenzelm@11078
    47
fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
wenzelm@2341
    48
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
wenzelm@2341
    49
    fn () => brk (99999, 0), en);
wenzelm@2341
    50
wenzelm@2341
    51
wenzelm@3588
    52
(* ML command execution -- 'eval' *)
wenzelm@3588
    53
wenzelm@7890
    54
local
wenzelm@7890
    55
wenzelm@7890
    56
fun drop_last [] = []
wenzelm@7890
    57
  | drop_last [x] = []
wenzelm@7890
    58
  | drop_last (x :: xs) = x :: drop_last xs;
wenzelm@7890
    59
wenzelm@7890
    60
val drop_last_char = implode o drop_last o explode;
wenzelm@7890
    61
wenzelm@7890
    62
in
wenzelm@7890
    63
wenzelm@10914
    64
fun use_text (print, err) verbose txt =
wenzelm@3588
    65
  let
wenzelm@5090
    66
    val in_buffer = ref (explode txt);
wenzelm@5090
    67
    val out_buffer = ref ([]: string list);
wenzelm@10914
    68
    fun output () = drop_last_char (implode (rev (! out_buffer)));
wenzelm@5090
    69
wenzelm@5038
    70
    fun get () =
wenzelm@5090
    71
      (case ! in_buffer of
wenzelm@5038
    72
        [] => ""
wenzelm@5090
    73
      | c :: cs => (in_buffer := cs; c));
wenzelm@5090
    74
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@5090
    75
wenzelm@5038
    76
    fun exec () =
wenzelm@5090
    77
      (case ! in_buffer of
wenzelm@5038
    78
        [] => ()
wenzelm@5090
    79
      | _ => (PolyML.compiler (get, put) (); exec ()));
wenzelm@5090
    80
  in
wenzelm@10914
    81
    exec () handle exn => (err (output ()); raise exn);
wenzelm@10914
    82
    if verbose then print (output ()) else ()
wenzelm@5090
    83
  end;
wenzelm@3588
    84
wenzelm@11078
    85
end;
wenzelm@11078
    86
wenzelm@11078
    87
wenzelm@11078
    88
wenzelm@11078
    89
(** interrupts **)
wenzelm@11078
    90
wenzelm@11078
    91
exception Interrupt = SML90.Interrupt;
wenzelm@11078
    92
wenzelm@11078
    93
local
wenzelm@11078
    94
wenzelm@12989
    95
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
wenzelm@11078
    96
wenzelm@12989
    97
fun release NONE = ()
wenzelm@12989
    98
  | release (SOME exn) = raise exn;
wenzelm@3588
    99
wenzelm@11078
   100
val sig_int = 2;
wenzelm@11078
   101
wenzelm@11078
   102
fun change_signal new_handler f x =
wenzelm@11078
   103
  let
wenzelm@11078
   104
    (*RACE wrt. other signals*)
wenzelm@11078
   105
    val old_handler = Signal.signal (sig_int, new_handler);
wenzelm@11078
   106
    val result = capture f x;
wenzelm@11078
   107
    val _ = Signal.signal (sig_int, old_handler);
wenzelm@11078
   108
  in release result end;
wenzelm@5813
   109
wenzelm@11078
   110
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
wenzelm@11078
   111
wenzelm@11078
   112
in
wenzelm@11078
   113
wenzelm@11078
   114
val _ = Signal.signal (sig_int, default_handler);
wenzelm@11078
   115
wenzelm@12989
   116
fun ignore_interrupt f = change_signal Signal.SIG_IGN f;
wenzelm@12989
   117
fun raise_interrupt f = change_signal default_handler f;
wenzelm@11078
   118
wenzelm@11078
   119
end;
wenzelm@5813
   120
wenzelm@5813
   121
wenzelm@5813
   122
wenzelm@3588
   123
(** OS related **)
wenzelm@2341
   124
wenzelm@3588
   125
(* system command execution *)
wenzelm@2341
   126
wenzelm@3588
   127
(*execute Unix command which doesn't take any input from stdin and
wenzelm@11078
   128
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@11078
   129
  but that function doesn't use the PATH*)
wenzelm@3588
   130
fun execute command =
wenzelm@3588
   131
  let
wenzelm@11078
   132
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@11078
   133
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@11078
   134
    val result = TextIO.inputAll is;
wenzelm@11078
   135
  in
wenzelm@11078
   136
    TextIO.closeIn is;
wenzelm@11078
   137
    OS.FileSys.remove tmp_name;
wenzelm@11078
   138
    result
wenzelm@11078
   139
  end;
wenzelm@7855
   140
wenzelm@11078
   141
(*plain version; with return code*)
wenzelm@11078
   142
fun system cmd =
wenzelm@11078
   143
  if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1;
wenzelm@2341
   144
wenzelm@2341
   145
wenzelm@3588
   146
(* file handling *)
wenzelm@2341
   147
wenzelm@6227
   148
(*get time of last modification*)
wenzelm@11078
   149
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
wenzelm@3588
   150
wenzelm@3588
   151
wenzelm@3588
   152
(* getenv *)
wenzelm@3588
   153
wenzelm@11078
   154
fun getenv var =
wenzelm@11078
   155
  (case OS.Process.getEnv var of
wenzelm@11078
   156
    NONE => ""
wenzelm@11078
   157
  | SOME txt => txt);