src/Pure/ML-Systems/compiler_polyml-5.3.ML
author huffman
Sat, 20 Aug 2011 13:07:00 -0700
changeset 44348 40101794c52f
parent 41717 8a1ab91df301
permissions -rw-r--r--
move lemma add_eq_0_iff to Groups.thy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML-Systems/compiler_polyml-5.3.ML
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     2
41717
8a1ab91df301 tuned headers;
wenzelm
parents: 39242
diff changeset
     3
Runtime compilation for Poly/ML 5.3.0 and 5.4.0.
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     4
*)
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     5
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     6
local
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     7
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     8
fun drop_newline s =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     9
  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    10
  else s;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    11
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    12
in
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    13
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    14
fun use_text ({tune_source, name_space, str_of_pos, print, error, ...}: use_context)
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    15
    (start_line, name) verbose txt =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    16
  let
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31480
diff changeset
    17
    val line = Unsynchronized.ref start_line;
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31480
diff changeset
    18
    val in_buffer = Unsynchronized.ref (String.explode (tune_source txt));
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31480
diff changeset
    19
    val out_buffer = Unsynchronized.ref ([]: string list);
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    20
    fun output () = drop_newline (implode (rev (! out_buffer)));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    21
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    22
    fun get () =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    23
      (case ! in_buffer of
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    24
        [] => NONE
31471
e3987b32e401 use_text: pass file name to compiler, tuned;
wenzelm
parents: 31433
diff changeset
    25
      | c :: cs => (in_buffer := cs; if c = #"\n" then line := ! line + 1 else (); SOME c));
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    26
    fun put s = out_buffer := s :: ! out_buffer;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    27
    fun put_message {message = msg1, hard, location = {startLine = line, ...}, context} =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    28
     (put (if hard then "Error: " else "Warning: ");
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    29
      PolyML.prettyPrint (put, 76) msg1;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    30
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    31
      put ("At" ^ str_of_pos line name ^ "\n"));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    32
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    33
    val parameters =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    34
     [PolyML.Compiler.CPOutStream put,
31471
e3987b32e401 use_text: pass file name to compiler, tuned;
wenzelm
parents: 31433
diff changeset
    35
      PolyML.Compiler.CPNameSpace name_space,
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    36
      PolyML.Compiler.CPErrorMessageProc put_message,
31471
e3987b32e401 use_text: pass file name to compiler, tuned;
wenzelm
parents: 31433
diff changeset
    37
      PolyML.Compiler.CPLineNo (fn () => ! line),
e3987b32e401 use_text: pass file name to compiler, tuned;
wenzelm
parents: 31433
diff changeset
    38
      PolyML.Compiler.CPFileName name,
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    39
      PolyML.Compiler.CPPrintInAlphabeticalOrder false];
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    40
    val _ =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    41
      (while not (List.null (! in_buffer)) do
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    42
        PolyML.compiler (get, parameters) ())
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    43
      handle exn =>
39242
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    44
        if Exn.is_interrupt exn then reraise exn
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    45
        else
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    46
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
28d3809ff91f primitive use_text: let interrupts pass unhindered;
wenzelm
parents: 38470
diff changeset
    47
          error (output ()); reraise exn);
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    48
  in if verbose then print (output ()) else () end;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    49
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    50
fun use_file context verbose name =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    51
  let
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    52
    val instream = TextIO.openIn name;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    53
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    54
  in use_text context (1, name) verbose txt end;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    55
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    56
end;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    57