src/Pure/ML-Systems/compiler_polyml-5.0.ML
author wenzelm
Sun, 31 May 2009 14:51:21 +0200
changeset 31312 1c00e4ff3c99
child 31329 c8821a6297f9
permissions -rw-r--r--
more modular setup of runtime compilation;
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
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     3
Runtime compilation -- for PolyML.compilerEx in version 5.0 and 5.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
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
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     8
    val in_buffer = ref (explode (tune_source txt));
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
     9
    val out_buffer = ref ([]: string list);
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
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    12
    val current_line = ref line;
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
1c00e4ff3c99 more modular setup of runtime compilation;
wenzelm
parents:
diff changeset
    24
    exec () handle exn => (error (output ()); raise exn);
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;