src/Pure/ML-Systems/compiler_polyml-5.0.ML
author wenzelm
Thu, 12 Aug 2010 15:19:11 +0200
changeset 38363 af7f41a8a0a8
parent 32738 15bb09ca0378
permissions -rw-r--r--
clarified "state" (accumulated data) vs. "exec" (execution that produces data); generic type Document.id (ML) / Document.ID;

(*  Title:      Pure/ML-Systems/compiler_polyml-5.0.ML

Runtime compilation for Poly/ML 5.0 and 5.1.
*)

fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
  let
    val in_buffer = Unsynchronized.ref (explode (tune_source txt));
    val out_buffer = Unsynchronized.ref ([]: string list);
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));

    val current_line = Unsynchronized.ref line;
    fun get () =
      (case ! in_buffer of
        [] => ""
      | c :: cs => (in_buffer := cs; if c = "\n" then current_line := ! current_line + 1 else (); c));
    fun put s = out_buffer := s :: ! out_buffer;

    fun exec () =
      (case ! in_buffer of
        [] => ()
      | _ => (PolyML.compilerEx (get, put, fn () => ! current_line, name) (); exec ()));
  in
    exec () handle exn => (error (output ()); reraise exn);
    if verbose then print (output ()) else ()
  end;

fun use_file context verbose name =
  let
    val instream = TextIO.openIn name;
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
  in use_text context (1, name) verbose txt end;