src/Pure/ML-Systems/polyml-5.1.ML
author wenzelm
Thu Oct 04 21:11:06 2007 +0200 (2007-10-04)
changeset 24852 30da58e0a483
parent 24600 5877b88f262c
permissions -rw-r--r--
single-threaded profiling;
haftmann@24584
     1
(*  Title:      Pure/ML-Systems/polyml-5.1.ML
wenzelm@23943
     2
    ID:         $Id$
wenzelm@23943
     3
wenzelm@23943
     4
Compatibility wrapper for Poly/ML 5.1.
wenzelm@23943
     5
*)
wenzelm@23943
     6
wenzelm@23943
     7
use "ML-Systems/polyml.ML";
wenzelm@23966
     8
use "ML-Systems/multithreading_polyml.ML";
wenzelm@23943
     9
wenzelm@23943
    10
val pointer_eq = PolyML.pointerEq;
wenzelm@23943
    11
wenzelm@23943
    12
wenzelm@24852
    13
(* single-threaded profiling *)
wenzelm@24852
    14
wenzelm@24852
    15
local val profile_orig = profile in
wenzelm@24852
    16
wenzelm@24852
    17
fun profile 0 f x = f x
wenzelm@24852
    18
  | profile n f x = NAMED_CRITICAL "profile" (fn () => profile_orig n f x);
wenzelm@24852
    19
wenzelm@24852
    20
end;
wenzelm@24852
    21
wenzelm@24852
    22
wenzelm@23943
    23
(* improved versions of use_text/file *)
wenzelm@23943
    24
wenzelm@24600
    25
fun use_text (tune: string -> string) name (print, err) verbose txt =
wenzelm@23943
    26
  let
wenzelm@24600
    27
    val in_buffer = ref (explode (tune txt));
wenzelm@23943
    28
    val out_buffer = ref ([]: string list);
wenzelm@23943
    29
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
wenzelm@23943
    30
wenzelm@23943
    31
    val line_no = ref 1;
wenzelm@23943
    32
    fun line () = ! line_no;
wenzelm@23943
    33
    fun get () =
wenzelm@23943
    34
      (case ! in_buffer of
wenzelm@23943
    35
        [] => ""
wenzelm@23943
    36
      | c :: cs => (in_buffer := cs; if c = "\n" then line_no := ! line_no + 1 else (); c));
wenzelm@23943
    37
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@23943
    38
wenzelm@23943
    39
    fun exec () =
wenzelm@23943
    40
      (case ! in_buffer of
wenzelm@23943
    41
        [] => ()
wenzelm@23943
    42
      | _ => (PolyML.compilerEx (get, put, line, name) (); exec ()));
wenzelm@23943
    43
  in
wenzelm@23943
    44
    exec () handle exn => (err (output ()); raise exn);
wenzelm@23943
    45
    if verbose then print (output ()) else ()
wenzelm@23943
    46
  end;
wenzelm@23943
    47
wenzelm@24600
    48
fun use_file tune output verbose name =
wenzelm@23943
    49
  let
wenzelm@23943
    50
    val instream = TextIO.openIn name;
wenzelm@23943
    51
    val txt = TextIO.inputAll instream before TextIO.closeIn instream;
wenzelm@24600
    52
  in use_text tune name output verbose txt end;