src/Pure/ML-Systems/compiler_polyml-5.0.ML
author wenzelm
Sun May 31 14:51:21 2009 +0200 (2009-05-31)
changeset 31312 1c00e4ff3c99
child 31329 c8821a6297f9
permissions -rw-r--r--
more modular setup of runtime compilation;
wenzelm@31312
     1
(*  Title:      Pure/ML-Systems/compiler_polyml-5.0.ML
wenzelm@31312
     2
wenzelm@31312
     3
Runtime compilation -- for PolyML.compilerEx in version 5.0 and 5.1.
wenzelm@31312
     4
*)
wenzelm@31312
     5
wenzelm@31312
     6
fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
wenzelm@31312
     7
  let
wenzelm@31312
     8
    val in_buffer = ref (explode (tune_source txt));
wenzelm@31312
     9
    val out_buffer = ref ([]: string list);
wenzelm@31312
    10
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
wenzelm@31312
    11
wenzelm@31312
    12
    val current_line = ref line;
wenzelm@31312
    13
    fun get () =
wenzelm@31312
    14
      (case ! in_buffer of
wenzelm@31312
    15
        [] => ""
wenzelm@31312
    16
      | c :: cs => (in_buffer := cs; if c = "\n" then current_line := ! current_line + 1 else (); c));
wenzelm@31312
    17
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@31312
    18
wenzelm@31312
    19
    fun exec () =
wenzelm@31312
    20
      (case ! in_buffer of
wenzelm@31312
    21
        [] => ()
wenzelm@31312
    22
      | _ => (PolyML.compilerEx (get, put, fn () => ! current_line, name) (); exec ()));
wenzelm@31312
    23
  in
wenzelm@31312
    24
    exec () handle exn => (error (output ()); raise exn);
wenzelm@31312
    25
    if verbose then print (output ()) else ()
wenzelm@31312
    26
  end;
wenzelm@31312
    27
wenzelm@31312
    28
fun use_file context verbose name =
wenzelm@31312
    29
  let
wenzelm@31312
    30
    val instream = TextIO.openIn name;
wenzelm@31312
    31
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@31312
    32
  in use_text context (1, name) verbose txt end;