2341
|
1 |
(* Title: Pure/ML-Systems/polyml.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
Copyright 1991 University of Cambridge
|
|
5 |
|
|
6 |
Compatibility file for Poly/ML (versions 2.x, 3.x).
|
|
7 |
*)
|
|
8 |
|
|
9 |
open PolyML ExtendedIO;
|
|
10 |
|
|
11 |
use"basis.ML";
|
|
12 |
|
|
13 |
|
|
14 |
(*A conditional timing function: applies f to () and, if the flag is true,
|
|
15 |
prints its runtime.*)
|
|
16 |
|
|
17 |
fun cond_timeit flag f =
|
|
18 |
if flag then
|
|
19 |
let val before = System.processtime()
|
|
20 |
val result = f()
|
|
21 |
val both = real(System.processtime() - before) / 10.0
|
|
22 |
in output(std_out, "Process time (incl GC): "^
|
|
23 |
makestring both ^ " secs\n");
|
|
24 |
result
|
|
25 |
end
|
|
26 |
else f();
|
|
27 |
|
|
28 |
|
|
29 |
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
|
|
30 |
|
|
31 |
fun make_pp _ pprint (str, blk, brk, en) obj =
|
|
32 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
|
|
33 |
fn () => brk (99999, 0), en);
|
|
34 |
|
|
35 |
|
|
36 |
(*** File handling ***)
|
|
37 |
|
|
38 |
local
|
|
39 |
|
|
40 |
(*These are functions from library.ML *)
|
|
41 |
fun take_suffix _ [] = ([], [])
|
|
42 |
| take_suffix pred (x :: xs) =
|
|
43 |
(case take_suffix pred xs of
|
|
44 |
([], sffx) => if pred x then ([], x :: sffx)
|
|
45 |
else ([x], sffx)
|
|
46 |
| (prfx, sffx) => (x :: prfx, sffx));
|
|
47 |
|
|
48 |
fun apr (f,y) x = f(x,y);
|
|
49 |
|
|
50 |
fun seq (proc: 'a -> unit) : 'a list -> unit =
|
|
51 |
let fun seqf [] = ()
|
|
52 |
| seqf (x :: xs) = (proc x; seqf xs)
|
|
53 |
in seqf end;
|
|
54 |
|
|
55 |
fun radixpand num : int list =
|
|
56 |
let fun radix (n, tail) =
|
|
57 |
if n < 10 then n :: tail else radix (n div 10, (n mod 10) :: tail)
|
|
58 |
in radix (num, []) end;
|
|
59 |
|
|
60 |
fun radixstring num =
|
|
61 |
let fun chrof n = chr (ord "0" + n);
|
|
62 |
in implode (map chrof (radixpand num)) end;
|
|
63 |
|
|
64 |
in
|
|
65 |
|
|
66 |
(*Get time of last modification; if file doesn't exist return an empty string*)
|
|
67 |
fun file_info "" = ""
|
|
68 |
| file_info name = radixstring (System.filedate name) handle _ => "";
|
|
69 |
|
|
70 |
|
|
71 |
structure OS =
|
|
72 |
struct
|
|
73 |
structure FileSys =
|
|
74 |
struct
|
|
75 |
val chDir = PolyML.cd
|
|
76 |
|
|
77 |
fun remove name = (*Delete a file *)
|
|
78 |
let val (is, os) = ExtendedIO.execute ("rm " ^ name)
|
|
79 |
in close_in is; close_out os end;
|
|
80 |
|
|
81 |
(*Get pathname of current working directory *)
|
|
82 |
fun getDir () =
|
|
83 |
let val (is, os) = ExtendedIO.execute ("pwd")
|
|
84 |
val (path, _) = take_suffix (apr(op=,"\n")) (*remove newline*)
|
|
85 |
(explode (ExtendedIO.input_line is))
|
|
86 |
in close_in is; close_out os; implode path end;
|
|
87 |
|
|
88 |
end
|
|
89 |
end;
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
(*** ML command execution ***)
|
|
94 |
|
|
95 |
val use_string =
|
|
96 |
let fun exec command =
|
|
97 |
let val firstLine = ref true;
|
|
98 |
|
|
99 |
fun getLine () =
|
|
100 |
if !firstLine
|
|
101 |
then (firstLine := false; command)
|
|
102 |
else raise Io "use_string: buffer exhausted"
|
|
103 |
in PolyML.compiler (getLine, fn s => output (std_out, s)) () end
|
|
104 |
in seq exec end;
|
|
105 |
|
|
106 |
end;
|
|
107 |
|
|
108 |
|
|
109 |
(*** System command execution ***)
|
|
110 |
|
|
111 |
(*Execute an Unix command which doesn't take any input from stdin and
|
|
112 |
sends its output to stdout.*)
|
|
113 |
fun execute command =
|
|
114 |
let val (is, os) = ExtendedIO.execute command;
|
|
115 |
val result = input (is, 999999);
|
|
116 |
in close_out os;
|
|
117 |
close_in is;
|
|
118 |
result
|
|
119 |
end;
|