src/Pure/ML-Systems/compiler_polyml-5.2.ML
author haftmann
Mon, 08 Feb 2010 17:12:22 +0100
changeset 35045 a77d200e6503
parent 32738 15bb09ca0378
child 39242 28d3809ff91f
permissions -rw-r--r--
using code antiquotation
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.2.ML
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     2
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     3
Runtime compilation for Poly/ML 5.2 and 5.2.1.
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 current_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
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    25
      | c :: cs =>
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    26
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    27
    fun put s = out_buffer := s :: ! out_buffer;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    28
    fun message (msg, is_err, line) =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    29
      (if is_err then "Error: " else "Warning: ") ^ drop_newline msg ^ str_of_pos line name ^ "\n";
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    30
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    31
    val parameters =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    32
     [PolyML.Compiler.CPOutStream put,
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    33
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    34
      PolyML.Compiler.CPErrorMessageProc (put o message),
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    35
      PolyML.Compiler.CPNameSpace name_space];
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    36
    val _ =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    37
      (while not (List.null (! in_buffer)) do
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    38
        PolyML.compiler (get, parameters) ())
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    39
      handle exn =>
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    40
       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
31480
05937d6aafb5 reraise exceptions to preserve position information;
wenzelm
parents: 31312
diff changeset
    41
        error (output ()); reraise exn);
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    42
  in if verbose then print (output ()) else () end;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    43
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    44
fun use_file context verbose name =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    45
  let
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    46
    val instream = TextIO.openIn name;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    47
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    48
  in use_text context (1, name) verbose txt 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
end;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    51