src/Pure/ML/ml_compiler_polyml-5.3.ML
author wenzelm
Mon, 22 Jun 2009 22:49:44 +0200
changeset 31756 178621145f98
parent 31495 a153d2de112c
child 32490 6a48db3e627c
permissions -rw-r--r--
eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML/ml_compiler_polyml-5.3.ML
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     3
31495
a153d2de112c updated version;
wenzelm
parents: 31480
diff changeset
     4
Advanced runtime compilation for Poly/ML 5.3 (SVN 773).
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     5
*)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     6
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     7
signature ML_COMPILER =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     8
sig
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
     9
  val exn_position: exn -> Position.T
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
    10
  val exn_message: exn -> string
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    11
  val eval: bool -> Position.T -> ML_Lex.token list -> unit
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    12
end
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    13
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    14
structure ML_Compiler: ML_COMPILER =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    15
struct
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    16
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    17
(* source locations *)
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    18
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    19
fun position_of (loc: PolyML.location) =
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    20
  let
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    21
    val {file = text, startLine = line, startPosition = offset,
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    22
      endLine = end_line, endPosition = end_offset} = loc;
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    23
    val loc_props =
31434
1f9b6a5dc8cc more robust treatment of bootstrap source positions;
wenzelm
parents: 31429
diff changeset
    24
      (case YXML.parse text of
1f9b6a5dc8cc more robust treatment of bootstrap source positions;
wenzelm
parents: 31429
diff changeset
    25
        XML.Elem (e, atts, _) => if e = Markup.positionN then atts else []
1f9b6a5dc8cc more robust treatment of bootstrap source positions;
wenzelm
parents: 31429
diff changeset
    26
      | XML.Text s => Position.file_name s);
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    27
  in
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    28
    Position.value Markup.lineN line @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    29
    Position.value Markup.offsetN offset @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    30
    Position.value Markup.end_lineN end_line @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    31
    Position.value Markup.end_offsetN end_offset @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    32
    loc_props
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    33
  end |> Position.of_properties;
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    34
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
    35
fun exn_position exn =
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    36
  (case PolyML.exceptionLocation exn of
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    37
    NONE => Position.none
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    38
  | SOME loc => position_of loc);
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    39
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
    40
val exn_message = Runtime.exn_message exn_position;
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
    41
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    42
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    43
(* parse trees *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    44
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    45
fun report_parse_tree depth space =
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    46
  let
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    47
    fun report_decl markup loc decl =
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    48
      Position.report_text Markup.ML_ref (position_of loc)
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    49
        (Markup.markup (Markup.properties (Position.properties_of (position_of decl)) markup) "");
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    50
    fun report loc (PolyML.PTtype types) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    51
          PolyML.NameSpace.displayTypeExpression (types, depth, space)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    52
          |> pretty_ml |> Pretty.from_ML |> Pretty.string_of
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    53
          |> Position.report_text Markup.ML_typing (position_of loc)
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    54
      | report loc (PolyML.PTdeclaredAt decl) = report_decl Markup.ML_def loc decl
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    55
      | report loc (PolyML.PTopenedAt decl) = report_decl Markup.ML_open loc decl
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    56
      | report loc (PolyML.PTstructureAt decl) = report_decl Markup.ML_struct loc decl
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    57
      | report _ (PolyML.PTnextSibling tree) = report_tree (tree ())
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    58
      | report _ (PolyML.PTfirstChild tree) = report_tree (tree ())
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    59
      | report _ _ = ()
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    60
    and report_tree (loc, props) = List.app (report loc) props;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    61
  in report_tree end;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    62
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    63
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    64
(* eval ML source tokens *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    65
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    66
val offset_of = the_default 0 o Position.offset_of;
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    67
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    68
fun eval verbose pos toks =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    69
  let
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    70
    val _ = Secure.secure_mltext ();
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    71
    val {name_space = space, print, error = err, ...} = ML_Env.local_context;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    72
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    73
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    74
    (* input *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    75
31437
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    76
    val location_props =
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    77
      Markup.markup (Markup.position |> Markup.properties
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    78
          (filter (member (op =) [Markup.idN, Markup.fileN] o #1) (Position.properties_of pos))) "";
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    79
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    80
    val input = toks |> maps (fn tok =>
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    81
      let
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    82
        val syms = Symbol.explode (ML_Lex.check_content_of tok);
31437
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    83
        val (ps, _) = fold_map (fn s => fn p => (p, Position.advance s p)) syms
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    84
          (Position.reset_range (ML_Lex.pos_of tok));
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    85
      in
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    86
        (ps ~~ syms) |> maps (fn (p, sym) =>
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    87
          map (pair (offset_of p)) (String.explode (Symbol.esc sym)))
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    88
      end);
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    89
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    90
    val end_pos =
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    91
      if null toks then Position.none
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    92
      else ML_Lex.end_pos_of (List.last toks);
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    93
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    94
    val input_buffer = ref (input @ [(offset_of end_pos, #"\n")]);
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    95
    val line = ref (the_default 1 (Position.line_of pos));
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    96
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    97
    fun get_offset () = (case ! input_buffer of [] => 0 | (i, _) :: _ => i);
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    98
    fun get () =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    99
      (case ! input_buffer of
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   100
        [] => NONE
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
   101
      | (_, c) :: rest =>
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
   102
          (input_buffer := rest;
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
   103
           if c = #"\n" then line := ! line + 1 else ();
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   104
           SOME c));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   105
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   106
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   107
    (* output *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   108
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   109
    val output_buffer = ref Buffer.empty;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   110
    fun output () = Buffer.content (! output_buffer);
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   111
    fun put s = change output_buffer (Buffer.add s);
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   112
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   113
    fun put_message {message, hard, location, context = _} =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   114
     (put (if hard then "Error: " else "Warning: ");
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   115
      put (Pretty.string_of (Pretty.from_ML (pretty_ml message)));
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
   116
      put (Position.str_of (position_of location) ^ "\n"));
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   117
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   118
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   119
    (* results *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   120
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   121
    val depth = get_print_depth ();
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   122
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   123
    fun apply_result {fixes, types, signatures, structures, functors, values} =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   124
      let
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   125
        fun display disp x =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   126
          if depth > 0 then
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   127
            (disp x |> pretty_ml |> Pretty.from_ML |> Pretty.string_of |> put; put "\n")
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   128
          else ();
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   129
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   130
        fun apply_fix (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   131
          (display PolyML.NameSpace.displayFix (a, b); #enterFix space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   132
        fun apply_type (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   133
          (display PolyML.NameSpace.displayType (b, depth, space); #enterType space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   134
        fun apply_sig (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   135
          (display PolyML.NameSpace.displaySig (b, depth, space); #enterSig space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   136
        fun apply_struct (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   137
          (display PolyML.NameSpace.displayStruct (b, depth, space); #enterStruct space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   138
        fun apply_funct (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   139
          (display PolyML.NameSpace.displayFunct (b, depth, space); #enterFunct space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   140
        fun apply_val (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   141
          (display PolyML.NameSpace.displayVal (b, depth, space); #enterVal space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   142
      in
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   143
        List.app apply_fix fixes;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   144
        List.app apply_type types;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   145
        List.app apply_sig signatures;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   146
        List.app apply_struct structures;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   147
        List.app apply_funct functors;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   148
        List.app apply_val values
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   149
      end;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   150
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   151
    fun result_fun (phase1, phase2) () =
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   152
     ((case phase1 of
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   153
        NONE => ()
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   154
      | SOME parse_tree => report_parse_tree depth space parse_tree);
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   155
      (case phase2 of
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   156
        NONE => err "Static Errors"
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   157
      | SOME code =>
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   158
          apply_result ((code |> Runtime.debugging |> Runtime.toplevel_error exn_message) ())));
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   159
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   160
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   161
    (* compiler invocation *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   162
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   163
    val parameters =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   164
     [PolyML.Compiler.CPOutStream put,
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   165
      PolyML.Compiler.CPNameSpace space,
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   166
      PolyML.Compiler.CPErrorMessageProc put_message,
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
   167
      PolyML.Compiler.CPLineNo (fn () => ! line),
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
   168
      PolyML.Compiler.CPLineOffset get_offset,
31437
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
   169
      PolyML.Compiler.CPFileName location_props,
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
   170
      PolyML.Compiler.CPCompilerResultFun result_fun,
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
   171
      PolyML.Compiler.CPPrintInAlphabeticalOrder false];
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   172
    val _ =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   173
      (while not (List.null (! input_buffer)) do
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   174
        PolyML.compiler (get, parameters) ())
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   175
      handle exn =>
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   176
       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
31480
05937d6aafb5 reraise exceptions to preserve position information;
wenzelm
parents: 31477
diff changeset
   177
        err (output ()); reraise exn);
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   178
  in if verbose then print (output ()) else () end;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   179
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   180
end;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   181