src/Pure/ML-Systems/compiler_polyml.ML
changeset 47980 c81801f881b3
parent 41717 8a1ab91df301
child 50910 54f06ba192ef
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/ML-Systems/compiler_polyml.ML	Thu May 24 15:33:45 2012 +0200
     1.3 @@ -0,0 +1,59 @@
     1.4 +(*  Title:      Pure/ML-Systems/compiler_polyml.ML
     1.5 +
     1.6 +Runtime compilation for Poly/ML 5.3.0 or later.
     1.7 +
     1.8 +See also Pure/ML/ml_compiler_polyml.ML for advanced version.
     1.9 +*)
    1.10 +
    1.11 +local
    1.12 +
    1.13 +fun drop_newline s =
    1.14 +  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
    1.15 +  else s;
    1.16 +
    1.17 +in
    1.18 +
    1.19 +fun use_text ({tune_source, name_space, str_of_pos, print, error, ...}: use_context)
    1.20 +    (start_line, name) verbose txt =
    1.21 +  let
    1.22 +    val line = Unsynchronized.ref start_line;
    1.23 +    val in_buffer = Unsynchronized.ref (String.explode (tune_source txt));
    1.24 +    val out_buffer = Unsynchronized.ref ([]: string list);
    1.25 +    fun output () = drop_newline (implode (rev (! out_buffer)));
    1.26 +
    1.27 +    fun get () =
    1.28 +      (case ! in_buffer of
    1.29 +        [] => NONE
    1.30 +      | c :: cs => (in_buffer := cs; if c = #"\n" then line := ! line + 1 else (); SOME c));
    1.31 +    fun put s = out_buffer := s :: ! out_buffer;
    1.32 +    fun put_message {message = msg1, hard, location = {startLine = line, ...}, context} =
    1.33 +     (put (if hard then "Error: " else "Warning: ");
    1.34 +      PolyML.prettyPrint (put, 76) msg1;
    1.35 +      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
    1.36 +      put ("At" ^ str_of_pos line name ^ "\n"));
    1.37 +
    1.38 +    val parameters =
    1.39 +     [PolyML.Compiler.CPOutStream put,
    1.40 +      PolyML.Compiler.CPNameSpace name_space,
    1.41 +      PolyML.Compiler.CPErrorMessageProc put_message,
    1.42 +      PolyML.Compiler.CPLineNo (fn () => ! line),
    1.43 +      PolyML.Compiler.CPFileName name,
    1.44 +      PolyML.Compiler.CPPrintInAlphabeticalOrder false];
    1.45 +    val _ =
    1.46 +      (while not (List.null (! in_buffer)) do
    1.47 +        PolyML.compiler (get, parameters) ())
    1.48 +      handle exn =>
    1.49 +        if Exn.is_interrupt exn then reraise exn
    1.50 +        else
    1.51 +         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
    1.52 +          error (output ()); reraise exn);
    1.53 +  in if verbose then print (output ()) else () end;
    1.54 +
    1.55 +fun use_file context verbose name =
    1.56 +  let
    1.57 +    val instream = TextIO.openIn name;
    1.58 +    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
    1.59 +  in use_text context (1, name) verbose txt end;
    1.60 +
    1.61 +end;
    1.62 +