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