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