src/Pure/POLY.ML
author clasohm
Thu May 19 13:45:50 1994 +0200 (1994-05-19 ago)
changeset 378 85ff48546a05
parent 100 e95b98536b3d
child 396 18c9c28d0f7e
permissions -rw-r--r--
added use_string: string -> unit to execute ML commands passed in a string
clasohm@0
     1
(*  Title:      Pure/POLY
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1991  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Compatibility file for Poly/ML (AHL release 1.88)
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
open PolyML ExtendedIO;
clasohm@0
    10
clasohm@0
    11
clasohm@0
    12
(*A conditional timing function: applies f to () and, if the flag is true,
clasohm@0
    13
  prints its runtime.*)
clasohm@0
    14
clasohm@0
    15
fun cond_timeit flag f =
clasohm@0
    16
  if flag then
clasohm@0
    17
    let val before = System.processtime()
clasohm@0
    18
        val result = f()
clasohm@0
    19
        val both = real(System.processtime() - before) / 10.0
clasohm@0
    20
    in  output(std_out, "Process time (incl GC): "^
clasohm@0
    21
                         makestring both ^ " secs\n");
clasohm@0
    22
        result
clasohm@0
    23
    end
clasohm@0
    24
  else f();
clasohm@0
    25
clasohm@0
    26
clasohm@0
    27
(*Save function: in Poly/ML, ignores filename and commits to present file*)
clasohm@0
    28
clasohm@0
    29
fun xML filename banner = commit();
clasohm@0
    30
clasohm@0
    31
clasohm@0
    32
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
clasohm@0
    33
clasohm@0
    34
fun make_pp _ pprint (str, blk, brk, en) obj =
wenzelm@19
    35
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
wenzelm@19
    36
    fn () => brk (99999, 0), en);
clasohm@0
    37
clasohm@0
    38
lcp@66
    39
(*** File handling ***)
clasohm@0
    40
clasohm@100
    41
local
clasohm@100
    42
clasohm@100
    43
(*These are functions from library.ML *)
clasohm@100
    44
fun take_suffix _ [] = ([], [])
clasohm@100
    45
  | take_suffix pred (x :: xs) =
clasohm@100
    46
      (case take_suffix pred xs of
clasohm@100
    47
         ([], sffx) => if pred x then ([], x :: sffx)
clasohm@100
    48
                                 else ([x], sffx)
clasohm@100
    49
       | (prfx, sffx) => (x :: prfx, sffx));
clasohm@100
    50
clasohm@100
    51
fun apr (f,y) x = f(x,y);
clasohm@100
    52
clasohm@100
    53
in
clasohm@100
    54
clasohm@58
    55
(*Get a string containing the time of last modification, attributes, owner
clasohm@58
    56
  and size of file (but not the path) *)
clasohm@0
    57
fun file_info "" = ""
clasohm@0
    58
  | file_info name =
clasohm@58
    59
      let
clasohm@58
    60
          val (is, os) = ExtendedIO.execute ("ls -l " ^ name)
clasohm@58
    61
          val (result, _) = take_suffix (apr(op<>," ")) 
clasohm@58
    62
                              (explode (ExtendedIO.input_line is))
lcp@66
    63
             (*Remove the part after the last " " (i.e. the path/filename) *)
clasohm@58
    64
        in close_in is;
clasohm@58
    65
           close_out os;
clasohm@58
    66
           implode result
clasohm@58
    67
        end;
lcp@66
    68
clasohm@73
    69
(*Delete a file *)
clasohm@73
    70
fun delete_file name =
clasohm@100
    71
  let val (is, os) = ExtendedIO.execute ("rm " ^ name)
clasohm@100
    72
  in close_in is;
clasohm@100
    73
     close_out os
clasohm@100
    74
  end;
clasohm@100
    75
clasohm@100
    76
(*Get pathname of current working directory *)
clasohm@100
    77
fun pwd () =
clasohm@100
    78
  let val (is, os) = ExtendedIO.execute ("pwd")
clasohm@100
    79
      val (path, _) = take_suffix (apr(op=,"\n"))
clasohm@100
    80
                       (explode (ExtendedIO.input_line is)) (*remove newline *)
clasohm@100
    81
  in close_in is;
clasohm@100
    82
     close_out os;
clasohm@100
    83
     implode path
clasohm@100
    84
  end;
clasohm@100
    85
clasohm@100
    86
end;
clasohm@100
    87
clasohm@378
    88
clasohm@378
    89
(*** ML command execution ***)
clasohm@378
    90
clasohm@378
    91
fun use_string commands =
clasohm@378
    92
  let val firstLine = ref true;
clasohm@378
    93
      fun getLine () =
clasohm@378
    94
        if !firstLine
clasohm@378
    95
        then (firstLine := false; commands ^ ";\n")
clasohm@378
    96
        else raise Io "execML: buffer exhausted"
clasohm@378
    97
  in PolyML.compiler (getLine, fn s => output (std_out, s)) () end;