src/Pure/ML-Systems/polyml_old_compiler5.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29564 f8b933a62151
child 30672 beaadd5af500
permissions -rw-r--r--
use long names for old-style fold combinators;
wenzelm@26383
     1
(*  Title:      Pure/ML-Systems/polyml_old_compiler5.ML
wenzelm@26383
     2
wenzelm@26385
     3
Runtime compilation -- for old PolyML.compilerEx (version 5.0, 5.1).
wenzelm@26383
     4
*)
wenzelm@26383
     5
wenzelm@28268
     6
fun use_text (tune: string -> string) _ _ (line, name) (print, err) verbose txt =
wenzelm@26383
     7
  let
wenzelm@26383
     8
    val in_buffer = ref (explode (tune txt));
wenzelm@26383
     9
    val out_buffer = ref ([]: string list);
wenzelm@26383
    10
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
wenzelm@26383
    11
wenzelm@26385
    12
    val current_line = ref line;
wenzelm@26383
    13
    fun get () =
wenzelm@26383
    14
      (case ! in_buffer of
wenzelm@26383
    15
        [] => ""
wenzelm@26385
    16
      | c :: cs => (in_buffer := cs; if c = "\n" then current_line := ! current_line + 1 else (); c));
wenzelm@26383
    17
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@26383
    18
wenzelm@26383
    19
    fun exec () =
wenzelm@26383
    20
      (case ! in_buffer of
wenzelm@26383
    21
        [] => ()
wenzelm@26385
    22
      | _ => (PolyML.compilerEx (get, put, fn () => ! current_line, name) (); exec ()));
wenzelm@26383
    23
  in
wenzelm@26383
    24
    exec () handle exn => (err (output ()); raise exn);
wenzelm@26383
    25
    if verbose then print (output ()) else ()
wenzelm@26383
    26
  end;
wenzelm@26383
    27
wenzelm@28268
    28
fun use_file tune str_of_pos name_space output verbose name =
wenzelm@26383
    29
  let
wenzelm@26383
    30
    val instream = TextIO.openIn name;
wenzelm@26504
    31
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@28268
    32
  in use_text tune str_of_pos name_space (1, name) output verbose txt end;