author | wenzelm |
Tue, 21 Mar 2006 12:18:13 +0100 | |
changeset 19304 | 15f001295a7b |
parent 18814 | 1a904aebfef0 |
child 21281 | 0767e7dad549 |
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 |
||
18814 | 65 |
(* FIXME proper implementation available? *) |
18760 | 66 |
fun interrupt_timeout time f x = |
67 |
f x; |
|
68 |
||
18814 | 69 |
(* |
16374 | 70 |
unless_cygwin |
18760 | 71 |
use "ML-Systems/polyml-interrupt-timeout.ML"; |
18814 | 72 |
*) |
14850 | 73 |
|
14870 | 74 |
|
4977 | 75 |
(* prompts *) |
76 |
||
4984 | 77 |
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2); |
4977 | 78 |
|
79 |
||
3588 | 80 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *) |
2341 | 81 |
|
11078 | 82 |
fun make_pp _ pprint (str, blk, brk, en) _ _ obj = |
2341 | 83 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0), |
84 |
fn () => brk (99999, 0), en); |
|
85 |
||
86 |
||
3588 | 87 |
(* ML command execution -- 'eval' *) |
88 |
||
7890 | 89 |
local |
90 |
||
91 |
fun drop_last [] = [] |
|
92 |
| drop_last [x] = [] |
|
93 |
| drop_last (x :: xs) = x :: drop_last xs; |
|
94 |
||
95 |
in |
|
96 |
||
10914 | 97 |
fun use_text (print, err) verbose txt = |
3588 | 98 |
let |
5090 | 99 |
val in_buffer = ref (explode txt); |
100 |
val out_buffer = ref ([]: string list); |
|
15832 | 101 |
fun output () = implode (drop_last (rev (! out_buffer))); |
5090 | 102 |
|
5038 | 103 |
fun get () = |
5090 | 104 |
(case ! in_buffer of |
5038 | 105 |
[] => "" |
5090 | 106 |
| c :: cs => (in_buffer := cs; c)); |
107 |
fun put s = out_buffer := s :: ! out_buffer; |
|
108 |
||
5038 | 109 |
fun exec () = |
5090 | 110 |
(case ! in_buffer of |
5038 | 111 |
[] => () |
5090 | 112 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
113 |
in |
|
10914 | 114 |
exec () handle exn => (err (output ()); raise exn); |
115 |
if verbose then print (output ()) else () |
|
5090 | 116 |
end; |
3588 | 117 |
|
11078 | 118 |
end; |
119 |
||
120 |
||
15832 | 121 |
(*eval command line arguments*) |
122 |
local |
|
123 |
fun println s = |
|
124 |
(TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut); |
|
15851 | 125 |
val eval = use_text (println, println) true; |
15832 | 126 |
in |
127 |
val _ = PolyML.onEntry (fn () => app eval (CommandLine.arguments ())); |
|
128 |
end; |
|
129 |
||
130 |
||
11078 | 131 |
|
132 |
(** interrupts **) |
|
133 |
||
134 |
exception Interrupt = SML90.Interrupt; |
|
135 |
||
136 |
local |
|
137 |
||
12989 | 138 |
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn; |
11078 | 139 |
|
12989 | 140 |
fun release NONE = () |
141 |
| release (SOME exn) = raise exn; |
|
3588 | 142 |
|
11078 | 143 |
val sig_int = 2; |
144 |
||
145 |
fun change_signal new_handler f x = |
|
146 |
let |
|
147 |
(*RACE wrt. other signals*) |
|
148 |
val old_handler = Signal.signal (sig_int, new_handler); |
|
149 |
val result = capture f x; |
|
150 |
val _ = Signal.signal (sig_int, old_handler); |
|
151 |
in release result end; |
|
5813 | 152 |
|
11078 | 153 |
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ()); |
154 |
||
155 |
in |
|
156 |
||
157 |
val _ = Signal.signal (sig_int, default_handler); |
|
158 |
||
12989 | 159 |
fun ignore_interrupt f = change_signal Signal.SIG_IGN f; |
160 |
fun raise_interrupt f = change_signal default_handler f; |
|
11078 | 161 |
|
162 |
end; |
|
5813 | 163 |
|
164 |
||
165 |
||
3588 | 166 |
(** OS related **) |
2341 | 167 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
168 |
unless_cygwin |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
169 |
use "ML-Systems/polyml-posix.ML"; |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
170 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
171 |
|
3588 | 172 |
(* system command execution *) |
2341 | 173 |
|
3588 | 174 |
(*execute Unix command which doesn't take any input from stdin and |
11078 | 175 |
sends its output to stdout; could be done more easily by Unix.execute, |
176 |
but that function doesn't use the PATH*) |
|
3588 | 177 |
fun execute command = |
178 |
let |
|
11078 | 179 |
val tmp_name = OS.FileSys.tmpName (); |
180 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
181 |
val result = TextIO.inputAll is; |
|
182 |
in |
|
183 |
TextIO.closeIn is; |
|
184 |
OS.FileSys.remove tmp_name; |
|
185 |
result |
|
186 |
end; |
|
7855 | 187 |
|
11078 | 188 |
(*plain version; with return code*) |
189 |
fun system cmd = |
|
190 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1; |
|
2341 | 191 |
|
192 |
||
17824 | 193 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
194 |
fun string_of_pid pid = |
|
195 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid)); |
|
196 |
||
197 |
||
3588 | 198 |
(* getenv *) |
199 |
||
11078 | 200 |
fun getenv var = |
201 |
(case OS.Process.getEnv var of |
|
202 |
NONE => "" |
|
203 |
| SOME txt => txt); |
|
15028 | 204 |
|
205 |
||
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
206 |
(* profile execution *) |
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 |
local |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
209 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
210 |
fun no_profile () = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
211 |
RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0; |
15028 | 212 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
213 |
in |
15699
7d91dd712ff8
fixing an incompatibility with Posix.IO.mkTextReader
paulson
parents:
15028
diff
changeset
|
214 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
215 |
fun profile 0 f x = f x |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
216 |
| profile n f x = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
217 |
(RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n; |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
218 |
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
|
219 |
in no_profile (); y end); |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
220 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
221 |
end; |