src/Pure/ML-Systems/polyml-experimental.ML
author wenzelm
Mon, 23 Mar 2009 22:57:27 +0100
changeset 30694 bcc63fcbc3ce
parent 30691 edca392a2abb
child 31312 1c00e4ff3c99
permissions -rw-r--r--
tuned;
     1 (*  Title:      Pure/ML-Systems/polyml.ML
     2 
     3 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
     4 *)
     5 
     6 open Thread;
     7 
     8 structure ML_Name_Space =
     9 struct
    10   open PolyML.NameSpace;
    11   type T = PolyML.NameSpace.nameSpace;
    12   val global = PolyML.globalNameSpace;
    13 end;
    14 
    15 use "ML-Systems/polyml_common.ML";
    16 use "ML-Systems/multithreading_polyml.ML";
    17 
    18 val pointer_eq = PolyML.pointerEq;
    19 
    20 fun share_common_data () = PolyML.shareCommonData PolyML.rootFunction;
    21 
    22 
    23 (* runtime compilation *)
    24 
    25 local
    26 
    27 fun drop_newline s =
    28   if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
    29   else s;
    30 
    31 in
    32 
    33 fun use_text ({tune_source, name_space, str_of_pos, print, error, ...}: use_context)
    34     (start_line, name) verbose txt =
    35   let
    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)));
    40 
    41     fun get () =
    42       (case ! in_buffer of
    43         [] => NONE
    44       | c :: cs =>
    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"));
    52 
    53     val parameters =
    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];
    59     val _ =
    60       (while not (List.null (! in_buffer)) do
    61         PolyML.compiler (get, parameters) ())
    62       handle exn =>
    63        (put ("Exception- " ^ General.exnMessage exn ^ " raised");
    64         error (output ()); raise exn);
    65   in if verbose then print (output ()) else () end;
    66 
    67 fun use_file context verbose name =
    68   let
    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;
    72 
    73 end;
    74 
    75 
    76 (* toplevel pretty printing *)
    77 
    78 val pretty_ml =
    79   let
    80     fun convert len (PrettyBlock (ind, _, context, prts)) =
    81           let
    82             fun property name default =
    83               (case List.find (fn ContextProperty (a, _) => name = a | _ => false) context of
    84                 SOME (ContextProperty (_, b)) => b
    85               | NONE => default);
    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));
    94   in convert "" end;
    95 
    96 fun ml_pretty (ML_Pretty.Block ((bg, en), prts, ind)) =
    97       let val context =
    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);
   106 
   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 ^ "))");
   110