src/Pure/RAW/compiler_polyml.ML
author wenzelm
Tue, 01 Mar 2016 19:42:59 +0100
changeset 62490 39d01eaf5292
parent 62489 36f11bc393a2
child 62493 dd154240a53c
permissions -rw-r--r--
ML debugger support in Pure (again, see 3565c9f407ec);

(*  Title:      Pure/RAW/compiler_polyml.ML

Basic runtime compilation for Poly/ML (cf. Pure/ML/ml_compiler_polyml.ML).
*)

type use_context =
 {name_space: ML_Name_Space.T,
  str_of_pos: int -> string -> string,
  print: string -> unit,
  error: string -> unit};

local

val boot_context: use_context =
 {name_space = ML_Name_Space.global,
  str_of_pos = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")",
  print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut),
  error = fn s => raise Fail s};

fun drop_newline s =
  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
  else s;

in

fun use_text ({name_space, str_of_pos, print, error, ...}: use_context)
    {line, file, verbose, debug} text =
  let
    val current_line = Unsynchronized.ref line;
    val in_buffer = Unsynchronized.ref (String.explode (ml_positions line file text));
    val out_buffer = Unsynchronized.ref ([]: string list);
    fun output () = drop_newline (implode (rev (! out_buffer)));

    fun get () =
      (case ! in_buffer of
        [] => NONE
      | c :: cs =>
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
    fun put s = out_buffer := s :: ! out_buffer;
    fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} =
     (put (if hard then "Error: " else "Warning: ");
      PolyML.prettyPrint (put, 76) msg1;
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
      put ("At" ^ str_of_pos (FixedInt.toInt message_line) file ^ "\n"));

    val parameters =
     [PolyML.Compiler.CPOutStream put,
      PolyML.Compiler.CPNameSpace name_space,
      PolyML.Compiler.CPErrorMessageProc put_message,
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
      PolyML.Compiler.CPFileName file,
      PolyML.Compiler.CPPrintInAlphabeticalOrder false] @
      ML_Compiler_Parameters.debug debug;
    val _ =
      (while not (List.null (! in_buffer)) do
        PolyML.compiler (get, parameters) ())
      handle exn =>
        if Exn.is_interrupt exn then reraise exn
        else
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
          error (output ()); reraise exn);
  in if verbose then print (output ()) else () end;

fun use_file context {verbose, debug} file =
  let
    val instream = TextIO.openIn file;
    val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
  in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end;


fun use_debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true"
  | use_debug_option (SOME debug) = debug;

local

fun use_ opt_debug file =
  use_file boot_context {verbose = true, debug = use_debug_option opt_debug} file
    handle Fail msg => (#print boot_context msg; raise Fail "ML error");

in

val use = use_ NONE;
val use_debug = use_ (SOME true);
val use_no_debug = use_ (SOME false);

end;

end;