src/Pure/ML-Systems/polyml.ML
author wenzelm
Mon Jun 29 10:32:06 1998 +0200 (1998-06-29)
changeset 5090 75ac9b451ae0
parent 5038 301c37df931d
child 5813 4eea84a9427d
permissions -rw-r--r--
use_text: verbose flag;
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@3588
     6
Compatibility file for Poly/ML (versions 2.x and 3.x).
wenzelm@2341
     7
*)
wenzelm@2341
     8
wenzelm@2341
     9
open PolyML ExtendedIO;
wenzelm@2341
    10
wenzelm@3588
    11
(*needs the Basis Library emulation*)
wenzelm@3588
    12
use "basis.ML";
wenzelm@3588
    13
wenzelm@2341
    14
wenzelm@2341
    15
wenzelm@3588
    16
(** ML system related **)
wenzelm@3588
    17
paulson@4326
    18
(** Compiler-independent timing functions **)
wenzelm@3588
    19
paulson@4326
    20
(*Note start point for timing*)
paulson@4326
    21
fun startTiming() = System.processtime ();
wenzelm@2341
    22
paulson@4326
    23
(*Finish timing and return string*)
paulson@4326
    24
fun endTiming before = 
paulson@4326
    25
  "User + GC: " ^ 
paulson@4326
    26
  makestring (real (System.processtime () - before) / 10.0) ^ 
paulson@4326
    27
  " secs";
wenzelm@2341
    28
wenzelm@2341
    29
wenzelm@4977
    30
(* prompts *)
wenzelm@4977
    31
wenzelm@4984
    32
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
wenzelm@4977
    33
wenzelm@4977
    34
wenzelm@3588
    35
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@2341
    36
wenzelm@2341
    37
fun make_pp _ pprint (str, blk, brk, en) obj =
wenzelm@2341
    38
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
wenzelm@2341
    39
    fn () => brk (99999, 0), en);
wenzelm@2341
    40
wenzelm@2341
    41
wenzelm@3588
    42
(* ML command execution -- 'eval' *)
wenzelm@3588
    43
wenzelm@5090
    44
fun use_text verbose txt =
wenzelm@3588
    45
  let
wenzelm@5090
    46
    val in_buffer = ref (explode txt);
wenzelm@5090
    47
    val out_buffer = ref ([]: string list);
wenzelm@5090
    48
wenzelm@5038
    49
    fun get () =
wenzelm@5090
    50
      (case ! in_buffer of
wenzelm@5038
    51
        [] => ""
wenzelm@5090
    52
      | c :: cs => (in_buffer := cs; c));
wenzelm@5090
    53
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@5090
    54
wenzelm@5038
    55
    fun exec () =
wenzelm@5090
    56
      (case ! in_buffer of
wenzelm@5038
    57
        [] => ()
wenzelm@5090
    58
      | _ => (PolyML.compiler (get, put) (); exec ()));
wenzelm@5090
    59
wenzelm@5090
    60
    fun show_output () = print (implode (rev (! out_buffer)));
wenzelm@5090
    61
  in
wenzelm@5090
    62
    exec () handle exn => (show_output (); raise exn);
wenzelm@5090
    63
    if verbose then show_output () else ()
wenzelm@5090
    64
  end;
wenzelm@3588
    65
wenzelm@3588
    66
wenzelm@3588
    67
wenzelm@3588
    68
(** OS related **)
wenzelm@2341
    69
wenzelm@2341
    70
local
wenzelm@2341
    71
wenzelm@3588
    72
fun drop_last [] = []
wenzelm@3588
    73
  | drop_last [x] = []
wenzelm@3588
    74
  | drop_last (x :: xs) = x :: drop_last xs;
wenzelm@2341
    75
wenzelm@3588
    76
val drop_last_char = implode o drop_last o explode;
wenzelm@2341
    77
wenzelm@2341
    78
in
wenzelm@2341
    79
wenzelm@2341
    80
wenzelm@3588
    81
(* system command execution *)
wenzelm@2341
    82
wenzelm@3588
    83
(*execute Unix command which doesn't take any input from stdin and
wenzelm@3588
    84
  sends its output to stdout*)
wenzelm@3588
    85
fun execute command =
wenzelm@3588
    86
  let
wenzelm@3588
    87
    val (is, os) =  ExtendedIO.execute command;
wenzelm@3588
    88
    val result = input (is, 999999);
wenzelm@3588
    89
  in
wenzelm@3588
    90
    close_out os;
wenzelm@3588
    91
    close_in is;
wenzelm@3588
    92
    result
wenzelm@2341
    93
  end;
wenzelm@2341
    94
wenzelm@2341
    95
wenzelm@3588
    96
(* file handling *)
wenzelm@2341
    97
wenzelm@3588
    98
(*get time of last modification; if file doesn't exist return an empty string*)
wenzelm@3588
    99
fun file_info "" = ""		(* FIXME !? *)
wenzelm@3588
   100
  | file_info name = Int.toString (System.filedate name) handle _ => "";
wenzelm@2341
   101
wenzelm@3588
   102
structure OS =
wenzelm@3588
   103
struct
wenzelm@3588
   104
  structure FileSys =
wenzelm@3588
   105
  struct
wenzelm@3588
   106
    val chDir = PolyML.cd;
wenzelm@3588
   107
    fun remove name = (execute ("rm " ^ name); ());
wenzelm@3588
   108
    fun getDir () = drop_last_char (execute "pwd");
wenzelm@3588
   109
  end;
wenzelm@3588
   110
end;
wenzelm@3588
   111
wenzelm@3588
   112
wenzelm@3588
   113
(* getenv *)
wenzelm@3588
   114
wenzelm@3588
   115
fun getenv var = drop_last_char
wenzelm@3588
   116
  (execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
wenzelm@3588
   117
wenzelm@2341
   118
wenzelm@2341
   119
end;
wenzelm@2341
   120
wenzelm@2341
   121
wenzelm@4428
   122
(* non-ASCII input (see also Thy/use.ML) *)
wenzelm@2408
   123
wenzelm@2408
   124
val needs_filtered_use =
wenzelm@2408
   125
  (case explode ml_system of
wenzelm@2408
   126
    "p" :: "o" :: "l" :: "y" :: "m" :: "l" :: "-" :: "3" :: _ => true
wenzelm@2408
   127
  | _ => false);