src/Pure/ML-Systems/compiler_polyml-5.0.ML
author wenzelm
Thu, 04 Mar 2010 21:02:21 +0100
changeset 35567 309e75c58af2
parent 32738 15bb09ca0378
permissions -rw-r--r--
point to http://hginit.com/
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.0.ML
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     2
31329
c8821a6297f9 tuned comments;
wenzelm
parents: 31312
diff changeset
     3
Runtime compilation for Poly/ML 5.0 and 5.1.
31312
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
fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     7
  let
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31480
diff changeset
     8
    val in_buffer = Unsynchronized.ref (explode (tune_source txt));
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31480
diff changeset
     9
    val out_buffer = Unsynchronized.ref ([]: string list);
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    10
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    11
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31480
diff changeset
    12
    val current_line = Unsynchronized.ref line;
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    13
    fun get () =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    14
      (case ! in_buffer of
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    15
        [] => ""
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    16
      | c :: cs => (in_buffer := cs; if c = "\n" then current_line := ! current_line + 1 else (); c));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    17
    fun put s = out_buffer := s :: ! out_buffer;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    18
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    19
    fun exec () =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    20
      (case ! in_buffer of
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    21
        [] => ()
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    22
      | _ => (PolyML.compilerEx (get, put, fn () => ! current_line, name) (); exec ()));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    23
  in
31480
05937d6aafb5 reraise exceptions to preserve position information;
wenzelm
parents: 31329
diff changeset
    24
    exec () handle exn => (error (output ()); reraise exn);
31312
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    25
    if verbose then print (output ()) else ()
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    26
  end;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    27
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    28
fun use_file context verbose name =
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    29
  let
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    30
    val instream = TextIO.openIn name;
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    31
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    32
  in use_text context (1, name) verbose txt end;