src/Pure/ML-Systems/polyml.ML
author wenzelm
Thu Sep 04 16:03:47 2008 +0200 (2008-09-04)
changeset 28124 10a1f1f4c6ae
parent 26883 ae6ae88f9240
child 28125 e99427bcf7f3
permissions -rw-r--r--
moved Multithreading.task/schedule to Concurrent/schedule.ML;
     1 (*  Title:      Pure/ML-Systems/polyml.ML
     2     ID:         $Id$
     3 
     4 Compatibility wrapper for Poly/ML (after 5.1).
     5 *)
     6 
     7 use "ML-Systems/polyml_common.ML";
     8 use "ML-Systems/multithreading_polyml.ML";
     9 
    10 val pointer_eq = PolyML.pointerEq;
    11 
    12 
    13 (* runtime compilation *)
    14 
    15 local
    16 
    17 fun drop_newline s =
    18   if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
    19   else s;
    20 
    21 in
    22 
    23 fun use_text (tune: string -> string) str_of_pos (start_line, name) (print, err) verbose txt =
    24   let
    25     val current_line = ref start_line;
    26     val in_buffer = ref (String.explode (tune txt));
    27     val out_buffer = ref ([]: string list);
    28     fun output () = drop_newline (implode (rev (! out_buffer)));
    29 
    30     fun get () =
    31       (case ! in_buffer of
    32         [] => NONE
    33       | c :: cs =>
    34           (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
    35     fun put s = out_buffer := s :: ! out_buffer;
    36     fun message (msg, is_err, line) =
    37       (if is_err then "Error: " else "Warning: ") ^ drop_newline msg ^ str_of_pos line name ^ "\n";
    38 
    39     val parameters =
    40      [PolyML.Compiler.CPOutStream put,
    41       PolyML.Compiler.CPLineNo (fn () => ! current_line),
    42       PolyML.Compiler.CPErrorMessageProc (put o message)];
    43     val _ =
    44       (while not (List.null (! in_buffer)) do
    45         PolyML.compiler (get, parameters) ())
    46       handle exn =>
    47        (put ("Exception- " ^ General.exnMessage exn ^ " raised");
    48         err (output ()); raise exn);
    49   in if verbose then print (output ()) else () end;
    50 
    51 fun use_file tune str_of_pos output verbose name =
    52   let
    53     val instream = TextIO.openIn name;
    54     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
    55   in use_text tune str_of_pos (1, name) output verbose txt end;
    56 
    57 end;
    58