src/Pure/ML-Systems/polyml.ML
author wenzelm
Mon, 24 Mar 2008 18:35:43 +0100
changeset 26379 a01a05cdd3b8
parent 26215 94d32a7cd0fb
child 26385 ae7564661e76
permissions -rw-r--r--
updated use_text/file for 5.2;

(*  Title:      Pure/ML-Systems/polyml.ML
    ID:         $Id$

Compatibility wrapper for Poly/ML (after 5.1).
*)

use "ML-Systems/polyml_common.ML";
use "ML-Systems/multithreading_polyml.ML";

val pointer_eq = PolyML.pointerEq;


(* single-threaded profiling *)

local val profile_orig = profile in

fun profile 0 f x = f x
  | profile n f x = NAMED_CRITICAL "profile" (fn () => profile_orig n f x);

end;


(* improved versions of use_text/file *)

fun use_text (tune: string -> string) name (print, err) verbose txt =
  let
    val in_buffer = ref (String.explode (tune txt));
    val out_buffer = ref ([]: string list);
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));

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

    val _ =
      (while not (List.null (! in_buffer)) do
        PolyML.compiler
          {getChar = get, putString = put, lineNumber = line, fileName = name,
            nameSpace = PolyML.globalNameSpace} ())
      handle exn => (err (output ()); raise exn);
  in
    if verbose then print (output ()) else ()
  end;

fun use_file tune output verbose name =
  let
    val instream = TextIO.openIn name;
    val txt = TextIO.inputAll instream before TextIO.closeIn instream;
  in use_text tune name output verbose txt end;