src/Pure/ML-Systems/compiler_polyml-5.3.ML
author wenzelm
Thu Oct 01 23:27:05 2009 +0200 (2009-10-01)
changeset 32843 c8f5a7c8353f
parent 32738 15bb09ca0378
child 33538 edf497b5b5d2
permissions -rw-r--r--
moved cache_conv to src/Pure/conv.ML, use Thm.cterm_cache;
wenzelm@31312
     1
(*  Title:      Pure/ML-Systems/compiler_polyml-5.3.ML
wenzelm@31312
     2
wenzelm@31433
     3
Runtime compilation for Poly/ML 5.3.
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@31312
    44
       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@31480
    45
        error (output ()); reraise exn);
wenzelm@31312
    46
  in if verbose then print (output ()) else () end;
wenzelm@31312
    47
wenzelm@31312
    48
fun use_file context verbose name =
wenzelm@31312
    49
  let
wenzelm@31312
    50
    val instream = TextIO.openIn name;
wenzelm@31312
    51
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@31312
    52
  in use_text context (1, name) verbose txt end;
wenzelm@31312
    53
wenzelm@31312
    54
end;
wenzelm@31312
    55