src/Pure/ML-Systems/compiler_polyml-5.3.ML
changeset 47986 ca7104aebb74
parent 47985 22846a7cf66e
parent 47980 c81801f881b3
child 47987 4e9df6ffcc6e
equal deleted inserted replaced
47985:22846a7cf66e 47986:ca7104aebb74
     1 (*  Title:      Pure/ML-Systems/compiler_polyml-5.3.ML
       
     2 
       
     3 Runtime compilation for Poly/ML 5.3.0 and 5.4.0.
       
     4 *)
       
     5 
       
     6 local
       
     7 
       
     8 fun drop_newline s =
       
     9   if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
       
    10   else s;
       
    11 
       
    12 in
       
    13 
       
    14 fun use_text ({tune_source, name_space, str_of_pos, print, error, ...}: use_context)
       
    15     (start_line, name) verbose txt =
       
    16   let
       
    17     val line = Unsynchronized.ref start_line;
       
    18     val in_buffer = Unsynchronized.ref (String.explode (tune_source txt));
       
    19     val out_buffer = Unsynchronized.ref ([]: string list);
       
    20     fun output () = drop_newline (implode (rev (! out_buffer)));
       
    21 
       
    22     fun get () =
       
    23       (case ! in_buffer of
       
    24         [] => NONE
       
    25       | c :: cs => (in_buffer := cs; if c = #"\n" then line := ! line + 1 else (); SOME c));
       
    26     fun put s = out_buffer := s :: ! out_buffer;
       
    27     fun put_message {message = msg1, hard, location = {startLine = line, ...}, context} =
       
    28      (put (if hard then "Error: " else "Warning: ");
       
    29       PolyML.prettyPrint (put, 76) msg1;
       
    30       (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
       
    31       put ("At" ^ str_of_pos line name ^ "\n"));
       
    32 
       
    33     val parameters =
       
    34      [PolyML.Compiler.CPOutStream put,
       
    35       PolyML.Compiler.CPNameSpace name_space,
       
    36       PolyML.Compiler.CPErrorMessageProc put_message,
       
    37       PolyML.Compiler.CPLineNo (fn () => ! line),
       
    38       PolyML.Compiler.CPFileName name,
       
    39       PolyML.Compiler.CPPrintInAlphabeticalOrder false];
       
    40     val _ =
       
    41       (while not (List.null (! in_buffer)) do
       
    42         PolyML.compiler (get, parameters) ())
       
    43       handle exn =>
       
    44         if Exn.is_interrupt exn then reraise exn
       
    45         else
       
    46          (put ("Exception- " ^ General.exnMessage exn ^ " raised");
       
    47           error (output ()); reraise exn);
       
    48   in if verbose then print (output ()) else () end;
       
    49 
       
    50 fun use_file context verbose name =
       
    51   let
       
    52     val instream = TextIO.openIn name;
       
    53     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
       
    54   in use_text context (1, name) verbose txt end;
       
    55 
       
    56 end;
       
    57