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