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