1 (* Title: Pure/ML-Systems/polyml.ML
3 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
8 structure ML_Name_Space =
10 open PolyML.NameSpace;
11 type T = PolyML.NameSpace.nameSpace;
12 val global = PolyML.globalNameSpace;
15 use "ML-Systems/polyml_common.ML";
16 use "ML-Systems/multithreading_polyml.ML";
18 val pointer_eq = PolyML.pointerEq;
20 fun share_common_data () = PolyML.shareCommonData PolyML.rootFunction;
23 (* runtime compilation *)
28 if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
33 fun use_text ({tune_source, name_space, str_of_pos, print, error, ...}: use_context)
34 (start_line, name) verbose txt =
36 val current_line = ref start_line;
37 val in_buffer = ref (String.explode (tune_source txt));
38 val out_buffer = ref ([]: string list);
39 fun output () = drop_newline (implode (rev (! out_buffer)));
45 (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
46 fun put s = out_buffer := s :: ! out_buffer;
47 fun put_message {message = msg1, hard, location = {startLine = line, ...}, context} =
48 (put (if hard then "Error: " else "Warning: ");
49 PolyML.prettyPrint (put, 76) msg1;
50 (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
51 put ("At" ^ str_of_pos line name ^ "\n"));
54 [PolyML.Compiler.CPOutStream put,
55 PolyML.Compiler.CPLineNo (fn () => ! current_line),
56 PolyML.Compiler.CPErrorMessageProc put_message,
57 PolyML.Compiler.CPNameSpace name_space,
58 PolyML.Compiler.CPPrintInAlphabeticalOrder false];
60 (while not (List.null (! in_buffer)) do
61 PolyML.compiler (get, parameters) ())
63 (put ("Exception- " ^ General.exnMessage exn ^ " raised");
64 error (output ()); raise exn);
65 in if verbose then print (output ()) else () end;
67 fun use_file context verbose name =
69 val instream = TextIO.openIn name;
70 val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
71 in use_text context (1, name) verbose txt end;
76 (* toplevel pretty printing *)
80 fun convert len (PrettyBlock (ind, _, context, prts)) =
82 fun property name default =
83 (case List.find (fn ContextProperty (a, _) => name = a | _ => false) context of
84 SOME (ContextProperty (_, b)) => b
86 val bg = property "begin" "";
87 val en = property "end" "";
88 val len' = property "length" len;
89 in ML_Pretty.Block ((bg, en), map (convert len') prts, ind) end
90 | convert len (PrettyString s) =
91 ML_Pretty.String (s, case Int.fromString len of SOME i => i | NONE => size s)
92 | convert _ (PrettyBreak (wd, _)) =
93 ML_Pretty.Break (if wd < 99999 then (false, wd) else (true, 2));
96 fun ml_pretty (ML_Pretty.Block ((bg, en), prts, ind)) =
98 (if bg = "" then [] else [ContextProperty ("begin", bg)]) @
99 (if en = "" then [] else [ContextProperty ("end", en)])
100 in PrettyBlock (ind, false, context, map ml_pretty prts) end
101 | ml_pretty (ML_Pretty.String (s, len)) =
102 if len = size s then PrettyString s
103 else PrettyBlock (0, false, [ContextProperty ("length", Int.toString len)], [PrettyString s])
104 | ml_pretty (ML_Pretty.Break (false, wd)) = PrettyBreak (wd, 0)
105 | ml_pretty (ML_Pretty.Break (true, _)) = PrettyBreak (99999, 0);
107 fun toplevel_pp context (_: string list) pp =
108 use_text context (1, "pp") false
109 ("addPrettyPrinter (fn _ => fn _ => ml_pretty o Pretty.to_ML o (" ^ pp ^ "))");