4403
|
1 |
(* Title: Pure/ML-Systems/smlnj.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Carsten Clasohm and Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Compatibility file for Standard ML of New Jersey versions 109.27 to
|
4407
|
6 |
109.33, and 110 or later.
|
4403
|
7 |
*)
|
|
8 |
|
|
9 |
(** ML system related **)
|
|
10 |
|
|
11 |
(* restore old-style character / string functions *)
|
|
12 |
|
|
13 |
fun ord s = Char.ord (String.sub (s, 0));
|
|
14 |
val chr = str o Char.chr;
|
|
15 |
val explode = (map str) o String.explode;
|
|
16 |
val implode = String.concat;
|
|
17 |
|
|
18 |
|
|
19 |
(* New Jersey ML parameters *)
|
|
20 |
|
|
21 |
val _ =
|
|
22 |
(Compiler.Control.Print.printLength := 1000;
|
|
23 |
Compiler.Control.Print.printDepth := 350;
|
|
24 |
Compiler.Control.Print.stringDepth := 250;
|
|
25 |
Compiler.Control.Print.signatures := 2);
|
|
26 |
|
|
27 |
|
|
28 |
(* Poly/ML emulation *)
|
|
29 |
|
|
30 |
fun quit () = exit 0;
|
|
31 |
|
|
32 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
|
|
33 |
fun print_depth n =
|
|
34 |
(Compiler.Control.Print.printDepth := n div 2;
|
|
35 |
Compiler.Control.Print.printLength := n);
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
(** Compiler-independent timing functions **)
|
|
40 |
|
|
41 |
(*Note start point for timing*)
|
|
42 |
fun startTiming() =
|
|
43 |
let val CPUtimer = Timer.startCPUTimer();
|
|
44 |
val time = Timer.checkCPUTimer(CPUtimer)
|
|
45 |
in (CPUtimer,time) end;
|
|
46 |
|
|
47 |
(*Finish timing and return string*)
|
|
48 |
fun endTiming (CPUtimer, {gc,sys,usr}) =
|
|
49 |
let open Time (*...for Time.toString, Time.+ and Time.- *)
|
|
50 |
val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
|
|
51 |
in "User " ^ toString (usr2-usr) ^
|
|
52 |
" GC " ^ toString (gc2-gc) ^
|
|
53 |
" All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
|
|
54 |
" secs"
|
|
55 |
handle Time => ""
|
|
56 |
end;
|
|
57 |
|
|
58 |
|
4977
|
59 |
(* prompts *)
|
|
60 |
|
|
61 |
fun ml_prompts p1 p2 =
|
|
62 |
(Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
|
|
63 |
|
|
64 |
|
4403
|
65 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
|
|
66 |
|
|
67 |
fun make_pp path pprint =
|
|
68 |
let
|
|
69 |
open Compiler.PrettyPrint;
|
|
70 |
|
|
71 |
fun pp pps obj =
|
|
72 |
pprint obj
|
|
73 |
(add_string pps, begin_block pps INCONSISTENT,
|
|
74 |
fn wd => add_break pps (wd, 0), fn () => add_newline pps,
|
|
75 |
fn () => end_block pps);
|
|
76 |
in
|
|
77 |
(path, pp)
|
|
78 |
end;
|
|
79 |
|
|
80 |
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
|
|
81 |
|
|
82 |
|
|
83 |
(* ML command execution *)
|
|
84 |
|
5090
|
85 |
fun use_text verbose txt =
|
|
86 |
let
|
|
87 |
val ref out_orig = Compiler.Control.Print.out;
|
|
88 |
|
|
89 |
val out_buffer = ref ([]: string list);
|
|
90 |
val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
|
|
91 |
|
|
92 |
fun show_output () = print (implode (rev (! out_buffer)));
|
|
93 |
in
|
|
94 |
Compiler.Control.Print.out := out;
|
|
95 |
Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
|
|
96 |
(Compiler.Control.Print.out := out_orig; show_output (); raise exn);
|
|
97 |
Compiler.Control.Print.out := out_orig;
|
|
98 |
if verbose then show_output () else ()
|
|
99 |
end;
|
4403
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
(** OS related **)
|
|
104 |
|
|
105 |
(* system command execution *)
|
|
106 |
|
|
107 |
(*execute Unix command which doesn't take any input from stdin and
|
|
108 |
sends its output to stdout; could be done more easily by Unix.execute,
|
|
109 |
but that function doesn't use the PATH*)
|
|
110 |
fun execute command =
|
|
111 |
let
|
|
112 |
val tmp_name = OS.FileSys.tmpName ();
|
|
113 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
|
|
114 |
val result = TextIO.inputAll is;
|
|
115 |
in
|
|
116 |
TextIO.closeIn is;
|
|
117 |
OS.FileSys.remove tmp_name;
|
|
118 |
result
|
|
119 |
end;
|
|
120 |
|
|
121 |
|
|
122 |
(* file handling *)
|
|
123 |
|
|
124 |
(*get time of last modification; if file doesn't exist return an empty string*)
|
|
125 |
fun file_info "" = "" (* FIXME !? *)
|
|
126 |
| file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
|
|
127 |
|
|
128 |
|
|
129 |
(* getenv *)
|
|
130 |
|
|
131 |
fun getenv var =
|
|
132 |
(case OS.Process.getEnv var of
|
|
133 |
NONE => ""
|
|
134 |
| SOME txt => txt);
|
|
135 |
|
|
136 |
|
4428
|
137 |
(* non-ASCII input (see also Thy/use.ML) *)
|
4403
|
138 |
|
|
139 |
val needs_filtered_use = false;
|