src/Pure/ML-Systems/polyml_old_compiler5.ML
author wenzelm
Wed May 14 11:05:45 2008 +0200 (2008-05-14)
changeset 26884 67c54c53da28
parent 26504 6e87c0a60104
child 26885 cfd5eb167706
permissions -rw-r--r--
use_text/file: ignore str_of_pos argument;
wenzelm@26383
     1
(*  Title:      Pure/ML-Systems/polyml_old_compiler5.ML
wenzelm@26383
     2
    ID:         $Id$
wenzelm@26383
     3
wenzelm@26385
     4
Runtime compilation -- for old PolyML.compilerEx (version 5.0, 5.1).
wenzelm@26383
     5
*)
wenzelm@26383
     6
wenzelm@26884
     7
fun use_text (tune: string -> string) _ (line, name) (print, err) verbose txt =
wenzelm@26383
     8
  let
wenzelm@26383
     9
    val in_buffer = ref (explode (tune txt));
wenzelm@26383
    10
    val out_buffer = ref ([]: string list);
wenzelm@26383
    11
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
wenzelm@26383
    12
wenzelm@26385
    13
    val current_line = ref line;
wenzelm@26383
    14
    fun get () =
wenzelm@26383
    15
      (case ! in_buffer of
wenzelm@26383
    16
        [] => ""
wenzelm@26385
    17
      | c :: cs => (in_buffer := cs; if c = "\n" then current_line := ! current_line + 1 else (); c));
wenzelm@26383
    18
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@26383
    19
wenzelm@26383
    20
    fun exec () =
wenzelm@26383
    21
      (case ! in_buffer of
wenzelm@26383
    22
        [] => ()
wenzelm@26385
    23
      | _ => (PolyML.compilerEx (get, put, fn () => ! current_line, name) (); exec ()));
wenzelm@26383
    24
  in
wenzelm@26383
    25
    exec () handle exn => (err (output ()); raise exn);
wenzelm@26383
    26
    if verbose then print (output ()) else ()
wenzelm@26383
    27
  end;
wenzelm@26383
    28
wenzelm@26884
    29
fun use_file tune _ output verbose name =
wenzelm@26383
    30
  let
wenzelm@26383
    31
    val instream = TextIO.openIn name;
wenzelm@26504
    32
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@26385
    33
  in use_text tune (1, name) output verbose txt end;
wenzelm@26383
    34