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