src/Pure/ML-Systems/compiler_polyml.ML
author wenzelm
Mon Aug 17 15:29:30 2015 +0200 (2015-08-17)
changeset 60955 65149ae760a0
parent 56435 28b34e8e4a80
child 60956 10d463883dc2
permissions -rw-r--r--
tuned;
wenzelm@47980
     1
(*  Title:      Pure/ML-Systems/compiler_polyml.ML
wenzelm@31312
     2
wenzelm@50910
     3
Basic runtime compilation for Poly/ML (cf. Pure/ML/ml_compiler_polyml.ML).
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@60955
    15
    (line, file) verbose txt =
wenzelm@31312
    16
  let
wenzelm@60955
    17
    val current_line = Unsynchronized.ref line;
wenzelm@56435
    18
    val in_buffer =
wenzelm@60955
    19
      Unsynchronized.ref (String.explode (tune_source (ml_positions line file txt)));
wenzelm@32738
    20
    val out_buffer = Unsynchronized.ref ([]: string list);
wenzelm@31312
    21
    fun output () = drop_newline (implode (rev (! out_buffer)));
wenzelm@31312
    22
wenzelm@31312
    23
    fun get () =
wenzelm@31312
    24
      (case ! in_buffer of
wenzelm@31312
    25
        [] => NONE
wenzelm@60955
    26
      | c :: cs =>
wenzelm@60955
    27
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
wenzelm@31312
    28
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@60955
    29
    fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} =
wenzelm@31312
    30
     (put (if hard then "Error: " else "Warning: ");
wenzelm@31312
    31
      PolyML.prettyPrint (put, 76) msg1;
wenzelm@31312
    32
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
wenzelm@60955
    33
      put ("At" ^ str_of_pos message_line file ^ "\n"));
wenzelm@31312
    34
wenzelm@31312
    35
    val parameters =
wenzelm@31312
    36
     [PolyML.Compiler.CPOutStream put,
wenzelm@31471
    37
      PolyML.Compiler.CPNameSpace name_space,
wenzelm@31312
    38
      PolyML.Compiler.CPErrorMessageProc put_message,
wenzelm@60955
    39
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm@60955
    40
      PolyML.Compiler.CPFileName file,
wenzelm@31312
    41
      PolyML.Compiler.CPPrintInAlphabeticalOrder false];
wenzelm@31312
    42
    val _ =
wenzelm@31312
    43
      (while not (List.null (! in_buffer)) do
wenzelm@31312
    44
        PolyML.compiler (get, parameters) ())
wenzelm@31312
    45
      handle exn =>
wenzelm@39242
    46
        if Exn.is_interrupt exn then reraise exn
wenzelm@39242
    47
        else
wenzelm@39242
    48
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@39242
    49
          error (output ()); reraise exn);
wenzelm@31312
    50
  in if verbose then print (output ()) else () end;
wenzelm@31312
    51
wenzelm@60955
    52
fun use_file context verbose file =
wenzelm@31312
    53
  let
wenzelm@60955
    54
    val instream = TextIO.openIn file;
wenzelm@31312
    55
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@60955
    56
  in use_text context (1, file) verbose txt end;
wenzelm@31312
    57
wenzelm@31312
    58
end;