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