author | webertj |
Mon, 23 Jan 2006 17:29:52 +0100 | |
changeset 18760 | 97aaecb84afe |
parent 18504 | 6574d62fe76b |
child 18814 | 1a904aebfef0 |
permissions | -rw-r--r-- |
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 |
||
14870 | 6 |
Compatibility file for Poly/ML (version 4.0, 4.1, 4.1.x). |
2341 | 7 |
*) |
8 |
||
16374 | 9 |
(** ML system and platform related **) |
10 |
||
17077 | 11 |
(* String compatibility *) |
12 |
||
13 |
structure String = |
|
14 |
struct |
|
15 |
fun isSuffix s1 s2 = |
|
16 |
let val n1 = size s1 and n2 = size s2 |
|
17 |
in if n1 = n2 then s1 = s2 else n1 <= n2 andalso String.substring (s2, n2 - n1, n1) = s1 end; |
|
18 |
open String; |
|
19 |
end; |
|
20 |
||
18504 | 21 |
structure Substring = |
22 |
struct |
|
23 |
open Substring; |
|
24 |
val full = all; |
|
25 |
end; |
|
26 |
||
17077 | 27 |
|
16374 | 28 |
(* cygwin *) |
29 |
||
17077 | 30 |
val cygwin_platform = String.isSuffix "cygwin" ml_platform; |
16374 | 31 |
fun if_cygwin f x = if cygwin_platform then f x else (); |
32 |
fun unless_cygwin f x = if not cygwin_platform then f x else (); |
|
33 |
||
2341 | 34 |
|
16516 | 35 |
(*low-level pointer equality*) |
36 |
val pointer_eq = Address.wordEq; |
|
16502 | 37 |
|
38 |
||
11078 | 39 |
(* old Poly/ML emulation *) |
2341 | 40 |
|
11078 | 41 |
local |
42 |
val orig_exit = exit; |
|
43 |
in |
|
44 |
open PolyML; |
|
45 |
val exit = orig_exit; |
|
46 |
fun quit () = exit 0; |
|
7148 | 47 |
end; |
6042 | 48 |
|
2341 | 49 |
|
11078 | 50 |
(* restore old-style character / string functions *) |
3588 | 51 |
|
11078 | 52 |
val ord = SML90.ord; |
53 |
val chr = SML90.chr; |
|
54 |
val explode = SML90.explode; |
|
55 |
val implode = SML90.implode; |
|
56 |
||
14870 | 57 |
|
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12989
diff
changeset
|
58 |
(* compiler-independent timing functions *) |
2341 | 59 |
|
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12989
diff
changeset
|
60 |
use "ML-Systems/cpu-timer-basis.ML"; |
2341 | 61 |
|
14870 | 62 |
|
14850 | 63 |
(* bounded time execution *) |
64 |
||
18760 | 65 |
(* FIXME proper implementation for Cygwin available? *) |
66 |
fun interrupt_timeout time f x = |
|
67 |
f x; |
|
68 |
||
16374 | 69 |
unless_cygwin |
18760 | 70 |
use "ML-Systems/polyml-interrupt-timeout.ML"; |
14850 | 71 |
|
14870 | 72 |
|
4977 | 73 |
(* prompts *) |
74 |
||
4984 | 75 |
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2); |
4977 | 76 |
|
77 |
||
3588 | 78 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *) |
2341 | 79 |
|
11078 | 80 |
fun make_pp _ pprint (str, blk, brk, en) _ _ obj = |
2341 | 81 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0), |
82 |
fn () => brk (99999, 0), en); |
|
83 |
||
84 |
||
3588 | 85 |
(* ML command execution -- 'eval' *) |
86 |
||
7890 | 87 |
local |
88 |
||
89 |
fun drop_last [] = [] |
|
90 |
| drop_last [x] = [] |
|
91 |
| drop_last (x :: xs) = x :: drop_last xs; |
|
92 |
||
93 |
in |
|
94 |
||
10914 | 95 |
fun use_text (print, err) verbose txt = |
3588 | 96 |
let |
5090 | 97 |
val in_buffer = ref (explode txt); |
98 |
val out_buffer = ref ([]: string list); |
|
15832 | 99 |
fun output () = implode (drop_last (rev (! out_buffer))); |
5090 | 100 |
|
5038 | 101 |
fun get () = |
5090 | 102 |
(case ! in_buffer of |
5038 | 103 |
[] => "" |
5090 | 104 |
| c :: cs => (in_buffer := cs; c)); |
105 |
fun put s = out_buffer := s :: ! out_buffer; |
|
106 |
||
5038 | 107 |
fun exec () = |
5090 | 108 |
(case ! in_buffer of |
5038 | 109 |
[] => () |
5090 | 110 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
111 |
in |
|
10914 | 112 |
exec () handle exn => (err (output ()); raise exn); |
113 |
if verbose then print (output ()) else () |
|
5090 | 114 |
end; |
3588 | 115 |
|
11078 | 116 |
end; |
117 |
||
118 |
||
15832 | 119 |
(*eval command line arguments*) |
120 |
local |
|
121 |
fun println s = |
|
122 |
(TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut); |
|
15851 | 123 |
val eval = use_text (println, println) true; |
15832 | 124 |
in |
125 |
val _ = PolyML.onEntry (fn () => app eval (CommandLine.arguments ())); |
|
126 |
end; |
|
127 |
||
128 |
||
11078 | 129 |
|
130 |
(** interrupts **) |
|
131 |
||
132 |
exception Interrupt = SML90.Interrupt; |
|
133 |
||
134 |
local |
|
135 |
||
12989 | 136 |
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn; |
11078 | 137 |
|
12989 | 138 |
fun release NONE = () |
139 |
| release (SOME exn) = raise exn; |
|
3588 | 140 |
|
11078 | 141 |
val sig_int = 2; |
142 |
||
143 |
fun change_signal new_handler f x = |
|
144 |
let |
|
145 |
(*RACE wrt. other signals*) |
|
146 |
val old_handler = Signal.signal (sig_int, new_handler); |
|
147 |
val result = capture f x; |
|
148 |
val _ = Signal.signal (sig_int, old_handler); |
|
149 |
in release result end; |
|
5813 | 150 |
|
11078 | 151 |
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ()); |
152 |
||
153 |
in |
|
154 |
||
155 |
val _ = Signal.signal (sig_int, default_handler); |
|
156 |
||
12989 | 157 |
fun ignore_interrupt f = change_signal Signal.SIG_IGN f; |
158 |
fun raise_interrupt f = change_signal default_handler f; |
|
11078 | 159 |
|
160 |
end; |
|
5813 | 161 |
|
162 |
||
163 |
||
3588 | 164 |
(** OS related **) |
2341 | 165 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
166 |
unless_cygwin |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
167 |
use "ML-Systems/polyml-posix.ML"; |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
168 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
169 |
|
3588 | 170 |
(* system command execution *) |
2341 | 171 |
|
3588 | 172 |
(*execute Unix command which doesn't take any input from stdin and |
11078 | 173 |
sends its output to stdout; could be done more easily by Unix.execute, |
174 |
but that function doesn't use the PATH*) |
|
3588 | 175 |
fun execute command = |
176 |
let |
|
11078 | 177 |
val tmp_name = OS.FileSys.tmpName (); |
178 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
179 |
val result = TextIO.inputAll is; |
|
180 |
in |
|
181 |
TextIO.closeIn is; |
|
182 |
OS.FileSys.remove tmp_name; |
|
183 |
result |
|
184 |
end; |
|
7855 | 185 |
|
11078 | 186 |
(*plain version; with return code*) |
187 |
fun system cmd = |
|
188 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1; |
|
2341 | 189 |
|
190 |
||
17824 | 191 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
192 |
fun string_of_pid pid = |
|
193 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid)); |
|
194 |
||
195 |
||
3588 | 196 |
(* getenv *) |
197 |
||
11078 | 198 |
fun getenv var = |
199 |
(case OS.Process.getEnv var of |
|
200 |
NONE => "" |
|
201 |
| SOME txt => txt); |
|
15028 | 202 |
|
203 |
||
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
204 |
(* profile execution *) |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
205 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
206 |
local |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
207 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
208 |
fun no_profile () = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
209 |
RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0; |
15028 | 210 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
211 |
in |
15699
7d91dd712ff8
fixing an incompatibility with Posix.IO.mkTextReader
paulson
parents:
15028
diff
changeset
|
212 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
213 |
fun profile 0 f x = f x |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
214 |
| profile n f x = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
215 |
(RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n; |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
216 |
let val y = f x handle exn => (no_profile (); raise exn) |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
217 |
in no_profile (); y end); |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
218 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
219 |
end; |