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