author | wenzelm |
Fri, 21 May 2004 21:21:38 +0200 | |
changeset 14781 | 2be804d1dda9 |
parent 14656 | 765badface6a |
child 14981 | e73f8140af78 |
permissions | -rw-r--r-- |
4403 | 1 |
(* Title: Pure/ML-Systems/smlnj.ML |
2 |
ID: $Id$ |
|
3 |
Author: Carsten Clasohm and Markus Wenzel, TU Muenchen |
|
12874
368966ceafe5
ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents:
12581
diff
changeset
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
4403 | 5 |
|
5708 | 6 |
Compatibility file for Standard ML of New Jersey 110 or later. |
4403 | 7 |
*) |
8 |
||
12874
368966ceafe5
ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents:
12581
diff
changeset
|
9 |
(case #version_id (Compiler.version) of |
368966ceafe5
ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents:
12581
diff
changeset
|
10 |
[110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else () |
368966ceafe5
ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents:
12581
diff
changeset
|
11 |
| _ => ()); |
368966ceafe5
ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents:
12581
diff
changeset
|
12 |
|
4403 | 13 |
(** ML system related **) |
14 |
||
15 |
(* restore old-style character / string functions *) |
|
16 |
||
10725 | 17 |
val ord = SML90.ord; |
18 |
val chr = SML90.chr; |
|
19 |
val explode = SML90.explode; |
|
20 |
val implode = SML90.implode; |
|
4403 | 21 |
|
22 |
||
23 |
(* New Jersey ML parameters *) |
|
24 |
||
25 |
val _ = |
|
26 |
(Compiler.Control.Print.printLength := 1000; |
|
27 |
Compiler.Control.Print.printDepth := 350; |
|
28 |
Compiler.Control.Print.stringDepth := 250; |
|
12581 | 29 |
Compiler.Control.Print.signatures := 2; |
30 |
Compiler.Control.MC.matchRedundantError := false); |
|
4403 | 31 |
|
32 |
||
33 |
(* Poly/ML emulation *) |
|
34 |
||
35 |
fun quit () = exit 0; |
|
36 |
||
37 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*) |
|
38 |
fun print_depth n = |
|
39 |
(Compiler.Control.Print.printDepth := n div 2; |
|
40 |
Compiler.Control.Print.printLength := n); |
|
41 |
||
42 |
||
5816 | 43 |
(* compiler-independent timing functions *) |
4403 | 44 |
|
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
45 |
(case #version_id (Compiler.version) of |
14656 | 46 |
[110, x] => if x >= 44 |
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
47 |
then use "ML-Systems/cpu-timer-basis.ML" |
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
48 |
else use "ML-Systems/cpu-timer-gc.ML" |
14520 | 49 |
| _ => use "ML-Systems/cpu-timer-gc.ML"); |
4403 | 50 |
|
51 |
||
4977 | 52 |
(* prompts *) |
53 |
||
54 |
fun ml_prompts p1 p2 = |
|
55 |
(Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2); |
|
56 |
||
57 |
||
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
58 |
(case #version_id (Compiler.version) of |
14656 | 59 |
[110, x] => if x >= 44 |
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
60 |
then use "ML-Systems/smlnj-basis-compat.ML" |
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
61 |
else () |
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
62 |
| _ => ()); |
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
63 |
|
4403 | 64 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *) |
65 |
||
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
66 |
(case #version_id (Compiler.version) of |
14656 | 67 |
[110, x] => if x >= 44 |
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
68 |
then use "ML-Systems/smlnj-pp-new.ML" |
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12990
diff
changeset
|
69 |
else use "ML-Systems/smlnj-pp-old.ML" |
14520 | 70 |
| _ => use "ML-Systems/smlnj-pp-old.ML"); |
4403 | 71 |
|
72 |
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp; |
|
73 |
||
74 |
||
75 |
(* ML command execution *) |
|
76 |
||
10914 | 77 |
fun use_text (print, err) verbose txt = |
5090 | 78 |
let |
79 |
val ref out_orig = Compiler.Control.Print.out; |
|
80 |
||
81 |
val out_buffer = ref ([]: string list); |
|
82 |
val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())}; |
|
10914 | 83 |
fun output () = |
7890 | 84 |
let val str = implode (rev (! out_buffer)) |
10914 | 85 |
in String.substring (str, 0, Int.max (0, size str - 1)) end; |
5090 | 86 |
in |
87 |
Compiler.Control.Print.out := out; |
|
88 |
Compiler.Interact.useStream (TextIO.openString txt) handle exn => |
|
10914 | 89 |
(Compiler.Control.Print.out := out_orig; err (output ()); raise exn); |
5090 | 90 |
Compiler.Control.Print.out := out_orig; |
10914 | 91 |
if verbose then print (output ()) else () |
5090 | 92 |
end; |
4403 | 93 |
|
94 |
||
95 |
||
5816 | 96 |
(** interrupts **) |
97 |
||
12990 | 98 |
exception Interrupt; |
99 |
||
5816 | 100 |
local |
101 |
||
12990 | 102 |
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn; |
5816 | 103 |
|
12990 | 104 |
fun release NONE = () |
105 |
| release (SOME exn) = raise exn; |
|
5816 | 106 |
|
107 |
in |
|
108 |
||
12990 | 109 |
fun ignore_interrupt f x = |
5816 | 110 |
let |
12990 | 111 |
val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE); |
112 |
val result = capture f x; |
|
113 |
val _ = Signals.setHandler (Signals.sigINT, old_handler); |
|
114 |
in release result end; |
|
5816 | 115 |
|
12990 | 116 |
fun raise_interrupt f x = |
117 |
let |
|
5816 | 118 |
val interrupted = ref false; |
12990 | 119 |
val result = ref NONE; |
120 |
val old_handler = Signals.inqHandler Signals.sigINT; |
|
5816 | 121 |
in |
12990 | 122 |
SMLofNJ.Cont.callcc (fn cont => |
123 |
(Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont))); |
|
124 |
result := capture f x)); |
|
125 |
Signals.setHandler (Signals.sigINT, old_handler); |
|
126 |
if ! interrupted then raise Interrupt else release (! result) |
|
127 |
end; |
|
5816 | 128 |
|
129 |
end; |
|
130 |
||
131 |
||
132 |
||
4403 | 133 |
(** OS related **) |
134 |
||
135 |
(* system command execution *) |
|
136 |
||
137 |
(*execute Unix command which doesn't take any input from stdin and |
|
138 |
sends its output to stdout; could be done more easily by Unix.execute, |
|
139 |
but that function doesn't use the PATH*) |
|
140 |
fun execute command = |
|
141 |
let |
|
142 |
val tmp_name = OS.FileSys.tmpName (); |
|
143 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
144 |
val result = TextIO.inputAll is; |
|
145 |
in |
|
146 |
TextIO.closeIn is; |
|
147 |
OS.FileSys.remove tmp_name; |
|
148 |
result |
|
149 |
end; |
|
150 |
||
7855 | 151 |
(*plain version; with return code*) |
12990 | 152 |
val system = OS.Process.system: string -> int; |
7855 | 153 |
|
4403 | 154 |
|
155 |
(* file handling *) |
|
156 |
||
6227 | 157 |
(*get time of last modification*) |
158 |
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => ""; |
|
4403 | 159 |
|
160 |
||
161 |
(* getenv *) |
|
162 |
||
163 |
fun getenv var = |
|
164 |
(case OS.Process.getEnv var of |
|
165 |
NONE => "" |
|
166 |
| SOME txt => txt); |