author | wenzelm |
Thu, 28 Feb 2002 21:35:07 +0100 | |
changeset 12990 | c11adf2b1c1e |
parent 12874 | 368966ceafe5 |
child 14519 | 4ca3608fdf4f |
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 |
|
368966ceafe5
ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents:
12581
diff
changeset
|
13 |
|
4403 | 14 |
(** ML system related **) |
15 |
||
16 |
(* restore old-style character / string functions *) |
|
17 |
||
10725 | 18 |
val ord = SML90.ord; |
19 |
val chr = SML90.chr; |
|
20 |
val explode = SML90.explode; |
|
21 |
val implode = SML90.implode; |
|
4403 | 22 |
|
23 |
||
24 |
(* New Jersey ML parameters *) |
|
25 |
||
26 |
val _ = |
|
27 |
(Compiler.Control.Print.printLength := 1000; |
|
28 |
Compiler.Control.Print.printDepth := 350; |
|
29 |
Compiler.Control.Print.stringDepth := 250; |
|
12581 | 30 |
Compiler.Control.Print.signatures := 2; |
31 |
Compiler.Control.MC.matchRedundantError := false); |
|
4403 | 32 |
|
33 |
||
34 |
(* Poly/ML emulation *) |
|
35 |
||
36 |
fun quit () = exit 0; |
|
37 |
||
38 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*) |
|
39 |
fun print_depth n = |
|
40 |
(Compiler.Control.Print.printDepth := n div 2; |
|
41 |
Compiler.Control.Print.printLength := n); |
|
42 |
||
43 |
||
5816 | 44 |
(* compiler-independent timing functions *) |
4403 | 45 |
|
46 |
(*Note start point for timing*) |
|
5816 | 47 |
fun startTiming() = |
4403 | 48 |
let val CPUtimer = Timer.startCPUTimer(); |
49 |
val time = Timer.checkCPUTimer(CPUtimer) |
|
50 |
in (CPUtimer,time) end; |
|
51 |
||
52 |
(*Finish timing and return string*) |
|
53 |
fun endTiming (CPUtimer, {gc,sys,usr}) = |
|
54 |
let open Time (*...for Time.toString, Time.+ and Time.- *) |
|
55 |
val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer) |
|
56 |
in "User " ^ toString (usr2-usr) ^ |
|
57 |
" GC " ^ toString (gc2-gc) ^ |
|
58 |
" All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^ |
|
59 |
" secs" |
|
60 |
handle Time => "" |
|
61 |
end; |
|
62 |
||
63 |
||
4977 | 64 |
(* prompts *) |
65 |
||
66 |
fun ml_prompts p1 p2 = |
|
67 |
(Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2); |
|
68 |
||
69 |
||
4403 | 70 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *) |
71 |
||
72 |
fun make_pp path pprint = |
|
73 |
let |
|
74 |
open Compiler.PrettyPrint; |
|
75 |
||
76 |
fun pp pps obj = |
|
77 |
pprint obj |
|
78 |
(add_string pps, begin_block pps INCONSISTENT, |
|
79 |
fn wd => add_break pps (wd, 0), fn () => add_newline pps, |
|
80 |
fn () => end_block pps); |
|
81 |
in |
|
82 |
(path, pp) |
|
83 |
end; |
|
84 |
||
85 |
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp; |
|
86 |
||
87 |
||
88 |
(* ML command execution *) |
|
89 |
||
10914 | 90 |
fun use_text (print, err) verbose txt = |
5090 | 91 |
let |
92 |
val ref out_orig = Compiler.Control.Print.out; |
|
93 |
||
94 |
val out_buffer = ref ([]: string list); |
|
95 |
val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())}; |
|
10914 | 96 |
fun output () = |
7890 | 97 |
let val str = implode (rev (! out_buffer)) |
10914 | 98 |
in String.substring (str, 0, Int.max (0, size str - 1)) end; |
5090 | 99 |
in |
100 |
Compiler.Control.Print.out := out; |
|
101 |
Compiler.Interact.useStream (TextIO.openString txt) handle exn => |
|
10914 | 102 |
(Compiler.Control.Print.out := out_orig; err (output ()); raise exn); |
5090 | 103 |
Compiler.Control.Print.out := out_orig; |
10914 | 104 |
if verbose then print (output ()) else () |
5090 | 105 |
end; |
4403 | 106 |
|
107 |
||
108 |
||
5816 | 109 |
(** interrupts **) |
110 |
||
12990 | 111 |
exception Interrupt; |
112 |
||
5816 | 113 |
local |
114 |
||
12990 | 115 |
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn; |
5816 | 116 |
|
12990 | 117 |
fun release NONE = () |
118 |
| release (SOME exn) = raise exn; |
|
5816 | 119 |
|
120 |
in |
|
121 |
||
12990 | 122 |
fun ignore_interrupt f x = |
5816 | 123 |
let |
12990 | 124 |
val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE); |
125 |
val result = capture f x; |
|
126 |
val _ = Signals.setHandler (Signals.sigINT, old_handler); |
|
127 |
in release result end; |
|
5816 | 128 |
|
12990 | 129 |
fun raise_interrupt f x = |
130 |
let |
|
5816 | 131 |
val interrupted = ref false; |
12990 | 132 |
val result = ref NONE; |
133 |
val old_handler = Signals.inqHandler Signals.sigINT; |
|
5816 | 134 |
in |
12990 | 135 |
SMLofNJ.Cont.callcc (fn cont => |
136 |
(Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont))); |
|
137 |
result := capture f x)); |
|
138 |
Signals.setHandler (Signals.sigINT, old_handler); |
|
139 |
if ! interrupted then raise Interrupt else release (! result) |
|
140 |
end; |
|
5816 | 141 |
|
142 |
end; |
|
143 |
||
144 |
||
145 |
||
4403 | 146 |
(** OS related **) |
147 |
||
148 |
(* system command execution *) |
|
149 |
||
150 |
(*execute Unix command which doesn't take any input from stdin and |
|
151 |
sends its output to stdout; could be done more easily by Unix.execute, |
|
152 |
but that function doesn't use the PATH*) |
|
153 |
fun execute command = |
|
154 |
let |
|
155 |
val tmp_name = OS.FileSys.tmpName (); |
|
156 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
157 |
val result = TextIO.inputAll is; |
|
158 |
in |
|
159 |
TextIO.closeIn is; |
|
160 |
OS.FileSys.remove tmp_name; |
|
161 |
result |
|
162 |
end; |
|
163 |
||
7855 | 164 |
(*plain version; with return code*) |
12990 | 165 |
val system = OS.Process.system: string -> int; |
7855 | 166 |
|
4403 | 167 |
|
168 |
(* file handling *) |
|
169 |
||
6227 | 170 |
(*get time of last modification*) |
171 |
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => ""; |
|
4403 | 172 |
|
173 |
||
174 |
(* getenv *) |
|
175 |
||
176 |
fun getenv var = |
|
177 |
(case OS.Process.getEnv var of |
|
178 |
NONE => "" |
|
179 |
| SOME txt => txt); |