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