author | haftmann |
Sat, 19 May 2007 11:33:30 +0200 | |
changeset 23024 | 70435ffe077d |
parent 22144 | c33450acd873 |
child 23139 | aa899bce7c3b |
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 |
||
22144 | 93 |
fun use_text name (print, err) verbose txt = |
3588 | 94 |
let |
5090 | 95 |
val in_buffer = ref (explode txt); |
96 |
val out_buffer = ref ([]: string list); |
|
21715 | 97 |
fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs)); |
5090 | 98 |
|
5038 | 99 |
fun get () = |
5090 | 100 |
(case ! in_buffer of |
5038 | 101 |
[] => "" |
5090 | 102 |
| c :: cs => (in_buffer := cs; c)); |
103 |
fun put s = out_buffer := s :: ! out_buffer; |
|
104 |
||
5038 | 105 |
fun exec () = |
5090 | 106 |
(case ! in_buffer of |
5038 | 107 |
[] => () |
5090 | 108 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
109 |
in |
|
22144 | 110 |
exec () handle exn => |
111 |
(err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn); |
|
10914 | 112 |
if verbose then print (output ()) else () |
5090 | 113 |
end; |
3588 | 114 |
|
21770 | 115 |
fun use_file _ _ name = use name; |
116 |
||
11078 | 117 |
|
15832 | 118 |
(*eval command line arguments*) |
119 |
local |
|
120 |
fun println s = |
|
121 |
(TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut); |
|
21715 | 122 |
fun eval "-q" = () |
22144 | 123 |
| eval txt = use_text "" (println, println) false txt; |
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 |
|
21281
0767e7dad549
no special treatment for cygwin (this is supposed to be actual cygwin, not win32 polyml within cygwin);
wenzelm
parents:
18814
diff
changeset
|
166 |
use "ML-Systems/polyml-posix.ML"; |
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
167 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
168 |
|
3588 | 169 |
(* system command execution *) |
2341 | 170 |
|
3588 | 171 |
(*execute Unix command which doesn't take any input from stdin and |
11078 | 172 |
sends its output to stdout; could be done more easily by Unix.execute, |
173 |
but that function doesn't use the PATH*) |
|
3588 | 174 |
fun execute command = |
175 |
let |
|
11078 | 176 |
val tmp_name = OS.FileSys.tmpName (); |
177 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
178 |
val result = TextIO.inputAll is; |
|
179 |
in |
|
180 |
TextIO.closeIn is; |
|
181 |
OS.FileSys.remove tmp_name; |
|
182 |
result |
|
183 |
end; |
|
7855 | 184 |
|
11078 | 185 |
(*plain version; with return code*) |
186 |
fun system cmd = |
|
187 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1; |
|
2341 | 188 |
|
189 |
||
17824 | 190 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
191 |
fun string_of_pid pid = |
|
192 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid)); |
|
193 |
||
194 |
||
3588 | 195 |
(* getenv *) |
196 |
||
11078 | 197 |
fun getenv var = |
198 |
(case OS.Process.getEnv var of |
|
199 |
NONE => "" |
|
200 |
| SOME txt => txt); |
|
15028 | 201 |
|
202 |
||
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
203 |
(* profile execution *) |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
204 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
205 |
local |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
206 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
207 |
fun no_profile () = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
208 |
RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0; |
15028 | 209 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
210 |
in |
15699
7d91dd712ff8
fixing an incompatibility with Posix.IO.mkTextReader
paulson
parents:
15028
diff
changeset
|
211 |
|
16659
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
212 |
fun profile 0 f x = f x |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
213 |
| profile n f x = |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
214 |
(RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n; |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
215 |
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
|
216 |
in no_profile (); y end); |
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
217 |
|
1cf39eba29fe
added profiler interface, keep 'profiling' of PolyML structure;
wenzelm
parents:
16516
diff
changeset
|
218 |
end; |