src/Pure/ML-Systems/compiler_polyml-5.2.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32738 15bb09ca0378
child 39242 28d3809ff91f
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
wenzelm@31312
     1
(*  Title:      Pure/ML-Systems/compiler_polyml-5.2.ML
wenzelm@31312
     2
wenzelm@31312
     3
Runtime compilation for Poly/ML 5.2 and 5.2.1.
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 current_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@31312
    25
      | c :: cs =>
wenzelm@31312
    26
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
wenzelm@31312
    27
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@31312
    28
    fun message (msg, is_err, line) =
wenzelm@31312
    29
      (if is_err then "Error: " else "Warning: ") ^ drop_newline msg ^ str_of_pos line name ^ "\n";
wenzelm@31312
    30
wenzelm@31312
    31
    val parameters =
wenzelm@31312
    32
     [PolyML.Compiler.CPOutStream put,
wenzelm@31312
    33
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm@31312
    34
      PolyML.Compiler.CPErrorMessageProc (put o message),
wenzelm@31312
    35
      PolyML.Compiler.CPNameSpace name_space];
wenzelm@31312
    36
    val _ =
wenzelm@31312
    37
      (while not (List.null (! in_buffer)) do
wenzelm@31312
    38
        PolyML.compiler (get, parameters) ())
wenzelm@31312
    39
      handle exn =>
wenzelm@31312
    40
       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@31480
    41
        error (output ()); reraise exn);
wenzelm@31312
    42
  in if verbose then print (output ()) else () end;
wenzelm@31312
    43
wenzelm@31312
    44
fun use_file context verbose name =
wenzelm@31312
    45
  let
wenzelm@31312
    46
    val instream = TextIO.openIn name;
wenzelm@31312
    47
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@31312
    48
  in use_text context (1, name) verbose txt end;
wenzelm@31312
    49
wenzelm@31312
    50
end;
wenzelm@31312
    51