src/Pure/ML-Systems/smlnj.ML
author wenzelm
Wed Oct 21 14:05:49 1998 +0200 (1998-10-21)
changeset 5708 fb09ab6a447f
parent 5090 75ac9b451ae0
child 5816 6f3cb53502fa
permissions -rw-r--r--
dropped support for SML/NJ 109.x;
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@4403
     4
wenzelm@5708
     5
Compatibility file for Standard ML of New Jersey 110 or later.
wenzelm@4403
     6
*)
wenzelm@4403
     7
wenzelm@4403
     8
(** ML system related **)
wenzelm@4403
     9
wenzelm@4403
    10
(* restore old-style character / string functions *)
wenzelm@4403
    11
wenzelm@4403
    12
fun ord s = Char.ord (String.sub (s, 0));
wenzelm@4403
    13
val chr = str o Char.chr;
wenzelm@4403
    14
val explode = (map str) o String.explode;
wenzelm@4403
    15
val implode = String.concat;
wenzelm@4403
    16
wenzelm@4403
    17
wenzelm@4403
    18
(* New Jersey ML parameters *)
wenzelm@4403
    19
wenzelm@4403
    20
val _ =
wenzelm@4403
    21
 (Compiler.Control.Print.printLength := 1000;
wenzelm@4403
    22
  Compiler.Control.Print.printDepth := 350;
wenzelm@4403
    23
  Compiler.Control.Print.stringDepth := 250;
wenzelm@4403
    24
  Compiler.Control.Print.signatures := 2);
wenzelm@4403
    25
wenzelm@4403
    26
wenzelm@4403
    27
(* Poly/ML emulation *)
wenzelm@4403
    28
wenzelm@4403
    29
fun quit () = exit 0;
wenzelm@4403
    30
wenzelm@4403
    31
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
wenzelm@4403
    32
fun print_depth n =
wenzelm@4403
    33
 (Compiler.Control.Print.printDepth := n div 2;
wenzelm@4403
    34
  Compiler.Control.Print.printLength := n);
wenzelm@4403
    35
wenzelm@4403
    36
wenzelm@4403
    37
wenzelm@4403
    38
(** Compiler-independent timing functions **)
wenzelm@4403
    39
wenzelm@4403
    40
(*Note start point for timing*)
wenzelm@4403
    41
fun startTiming() = 
wenzelm@4403
    42
  let val CPUtimer = Timer.startCPUTimer();
wenzelm@4403
    43
      val time = Timer.checkCPUTimer(CPUtimer)
wenzelm@4403
    44
  in  (CPUtimer,time)  end;
wenzelm@4403
    45
wenzelm@4403
    46
(*Finish timing and return string*)
wenzelm@4403
    47
fun endTiming (CPUtimer, {gc,sys,usr}) =
wenzelm@4403
    48
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
wenzelm@4403
    49
      val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
wenzelm@4403
    50
  in  "User " ^ toString (usr2-usr) ^
wenzelm@4403
    51
      "  GC " ^ toString (gc2-gc) ^
wenzelm@4403
    52
      "  All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
wenzelm@4403
    53
      " secs"
wenzelm@4403
    54
      handle Time => ""
wenzelm@4403
    55
  end;
wenzelm@4403
    56
wenzelm@4403
    57
wenzelm@4977
    58
(* prompts *)
wenzelm@4977
    59
wenzelm@4977
    60
fun ml_prompts p1 p2 =
wenzelm@4977
    61
  (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
wenzelm@4977
    62
wenzelm@4977
    63
wenzelm@4403
    64
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@4403
    65
wenzelm@4403
    66
fun make_pp path pprint =
wenzelm@4403
    67
  let
wenzelm@4403
    68
    open Compiler.PrettyPrint;
wenzelm@4403
    69
wenzelm@4403
    70
    fun pp pps obj =
wenzelm@4403
    71
      pprint obj
wenzelm@4403
    72
        (add_string pps, begin_block pps INCONSISTENT,
wenzelm@4403
    73
          fn wd => add_break pps (wd, 0), fn () => add_newline pps,
wenzelm@4403
    74
          fn () => end_block pps);
wenzelm@4403
    75
  in
wenzelm@4403
    76
    (path, pp)
wenzelm@4403
    77
  end;
wenzelm@4403
    78
wenzelm@4403
    79
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
wenzelm@4403
    80
wenzelm@4403
    81
wenzelm@4403
    82
(* ML command execution *)
wenzelm@4403
    83
wenzelm@5090
    84
fun use_text verbose txt =
wenzelm@5090
    85
  let
wenzelm@5090
    86
    val ref out_orig = Compiler.Control.Print.out;
wenzelm@5090
    87
wenzelm@5090
    88
    val out_buffer = ref ([]: string list);
wenzelm@5090
    89
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@5090
    90
wenzelm@5090
    91
    fun show_output () = print (implode (rev (! out_buffer)));
wenzelm@5090
    92
  in
wenzelm@5090
    93
    Compiler.Control.Print.out := out;
wenzelm@5090
    94
    Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
wenzelm@5090
    95
      (Compiler.Control.Print.out := out_orig; show_output (); raise exn);
wenzelm@5090
    96
    Compiler.Control.Print.out := out_orig;
wenzelm@5090
    97
    if verbose then show_output () else ()
wenzelm@5090
    98
  end;
wenzelm@4403
    99
wenzelm@4403
   100
wenzelm@4403
   101
wenzelm@4403
   102
(** OS related **)
wenzelm@4403
   103
wenzelm@4403
   104
(* system command execution *)
wenzelm@4403
   105
wenzelm@4403
   106
(*execute Unix command which doesn't take any input from stdin and
wenzelm@4403
   107
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@4403
   108
  but that function doesn't use the PATH*)
wenzelm@4403
   109
fun execute command =
wenzelm@4403
   110
  let
wenzelm@4403
   111
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@4403
   112
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@4403
   113
    val result = TextIO.inputAll is;
wenzelm@4403
   114
  in
wenzelm@4403
   115
    TextIO.closeIn is;
wenzelm@4403
   116
    OS.FileSys.remove tmp_name;
wenzelm@4403
   117
    result
wenzelm@4403
   118
  end;
wenzelm@4403
   119
wenzelm@4403
   120
wenzelm@4403
   121
(* file handling *)
wenzelm@4403
   122
wenzelm@4403
   123
(*get time of last modification; if file doesn't exist return an empty string*)
wenzelm@4403
   124
fun file_info "" = ""		(* FIXME !? *)
wenzelm@4403
   125
  | file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
wenzelm@4403
   126
wenzelm@4403
   127
wenzelm@4403
   128
(* getenv *)
wenzelm@4403
   129
wenzelm@4403
   130
fun getenv var =
wenzelm@4403
   131
  (case OS.Process.getEnv var of
wenzelm@4403
   132
    NONE => ""
wenzelm@4403
   133
  | SOME txt => txt);
wenzelm@4403
   134
wenzelm@4403
   135
wenzelm@4428
   136
(* non-ASCII input (see also Thy/use.ML) *)
wenzelm@4403
   137
wenzelm@4403
   138
val needs_filtered_use = false;