src/Pure/ML-Systems/polyml-experimental.ML
author wenzelm
Mon, 23 Mar 2009 22:37:41 +0100
changeset 30676 edca392a2abb
parent 30673 60568c168040
child 30679 bcc63fcbc3ce
permissions -rw-r--r--
pretty_ml/ml_pretty: proper handling of markup and string length;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML-Systems/polyml.ML
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
     2
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
     3
Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
     4
*)
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
     5
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
     6
open Thread;
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
     7
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
     8
structure ML_Name_Space =
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
     9
struct
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    10
  open PolyML.NameSpace;
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    11
  type T = PolyML.NameSpace.nameSpace;
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    12
  val global = PolyML.globalNameSpace;
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    13
end;
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    14
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    15
use "ML-Systems/polyml_common.ML";
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    16
use "ML-Systems/multithreading_polyml.ML";
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    17
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    18
val pointer_eq = PolyML.pointerEq;
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    19
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    20
fun share_common_data () = PolyML.shareCommonData PolyML.rootFunction;
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    21
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    22
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    23
(* runtime compilation *)
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    24
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    25
local
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    26
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    27
fun drop_newline s =
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    28
  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    29
  else s;
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    30
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    31
in
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    32
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    33
fun use_text ({tune_source, name_space, str_of_pos, print, error, ...}: use_context)
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    34
    (start_line, name) verbose txt =
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    35
  let
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    36
    val current_line = ref start_line;
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    37
    val in_buffer = ref (String.explode (tune_source txt));
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    38
    val out_buffer = ref ([]: string list);
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    39
    fun output () = drop_newline (implode (rev (! out_buffer)));
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    40
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    41
    fun get () =
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    42
      (case ! in_buffer of
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    43
        [] => NONE
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    44
      | c :: cs =>
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    45
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    46
    fun put s = out_buffer := s :: ! out_buffer;
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    47
    fun put_message {message = msg1, hard, location = {startLine = line, ...}, context} =
30205
e33ce8d765b4 adapted to lates experimental version;
wenzelm
parents: 29714
diff changeset
    48
     (put (if hard then "Error: " else "Warning: ");
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    49
      PolyML.prettyPrint (put, 76) msg1;
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    50
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    51
      put ("At" ^ str_of_pos line name ^ "\n"));
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    52
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    53
    val parameters =
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    54
     [PolyML.Compiler.CPOutStream put,
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    55
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    56
      PolyML.Compiler.CPErrorMessageProc put_message,
30205
e33ce8d765b4 adapted to lates experimental version;
wenzelm
parents: 29714
diff changeset
    57
      PolyML.Compiler.CPNameSpace name_space,
e33ce8d765b4 adapted to lates experimental version;
wenzelm
parents: 29714
diff changeset
    58
      PolyML.Compiler.CPPrintInAlphabeticalOrder false];
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    59
    val _ =
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    60
      (while not (List.null (! in_buffer)) do
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    61
        PolyML.compiler (get, parameters) ())
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    62
      handle exn =>
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    63
       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    64
        error (output ()); raise exn);
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    65
  in if verbose then print (output ()) else () end;
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    66
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    67
fun use_file context verbose name =
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    68
  let
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    69
    val instream = TextIO.openIn name;
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    70
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
    71
  in use_text context (1, name) verbose txt end;
29714
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    72
6cef6700c841 Compatibility wrapper for experimental versions of Poly/ML after 5.2.1.
wenzelm
parents:
diff changeset
    73
end;
30626
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
    74
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
    75
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
    76
(* toplevel pretty printing *)
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
    77
30676
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    78
val pretty_ml =
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    79
  let
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    80
    fun convert len (PrettyBlock (ind, _, context, prts)) =
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    81
          let
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    82
            fun property name default =
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    83
              (case List.find (fn ContextProperty (a, _) => name = a | _ => false) context of
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    84
                SOME (ContextProperty (_, b)) => b
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    85
              | NONE => default);
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    86
            val bg = property "begin" "";
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    87
            val en = property "end" "";
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    88
            val len' = property "length" len;
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    89
          in ML_Pretty.Block ((bg, en), map (convert len') prts, ind) end
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    90
      | convert len (PrettyString s) =
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    91
          ML_Pretty.String (s, case Int.fromString len of SOME i => i | NONE => size s)
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    92
      | convert _ (PrettyBreak (wd, _)) =
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    93
          ML_Pretty.Break (if wd < 99999 then (false, wd) else (true, 2));
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    94
  in convert "" end;
30638
15cc4ad0e6e9 added pretty_ml;
wenzelm
parents: 30626
diff changeset
    95
30676
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    96
fun ml_pretty (ML_Pretty.Block ((bg, en), prts, ind)) =
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    97
      PrettyBlock (ind, false,
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    98
        [ContextProperty ("begin", bg), ContextProperty ("end", en)], map ml_pretty prts)
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
    99
  | ml_pretty (ML_Pretty.String (s, len)) =
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
   100
      if len = size s then PrettyString s
edca392a2abb pretty_ml/ml_pretty: proper handling of markup and string length;
wenzelm
parents: 30673
diff changeset
   101
      else PrettyBlock (0, false, [ContextProperty ("length", Int.toString len)], [PrettyString s])
30626
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
   102
  | ml_pretty (ML_Pretty.Break (false, wd)) = PrettyBreak (wd, 0)
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
   103
  | ml_pretty (ML_Pretty.Break (true, _)) = PrettyBreak (99999, 0);
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
   104
30673
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
   105
fun toplevel_pp context (_: string list) pp =
60568c168040 more systematic type use_context;
wenzelm
parents: 30638
diff changeset
   106
  use_text context (1, "pp") false
30626
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
   107
    ("addPrettyPrinter (fn _ => fn _ => ml_pretty o Pretty.to_ML o (" ^ pp ^ "))");
248de8dd839e adapted toplevel_pp to ML_Pretty.pretty;
wenzelm
parents: 30619
diff changeset
   108