src/Pure/RAW/compiler_polyml.ML
author wenzelm
Tue Mar 01 19:42:59 2016 +0100 (2016-03-01)
changeset 62490 39d01eaf5292
parent 62489 36f11bc393a2
child 62493 dd154240a53c
permissions -rw-r--r--
ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm@61925
     1
(*  Title:      Pure/RAW/compiler_polyml.ML
wenzelm@31312
     2
wenzelm@50910
     3
Basic runtime compilation for Poly/ML (cf. Pure/ML/ml_compiler_polyml.ML).
wenzelm@31312
     4
*)
wenzelm@31312
     5
wenzelm@62490
     6
type use_context =
wenzelm@62490
     7
 {name_space: ML_Name_Space.T,
wenzelm@62490
     8
  str_of_pos: int -> string -> string,
wenzelm@62490
     9
  print: string -> unit,
wenzelm@62490
    10
  error: string -> unit};
wenzelm@62490
    11
wenzelm@31312
    12
local
wenzelm@31312
    13
wenzelm@62490
    14
val boot_context: use_context =
wenzelm@62490
    15
 {name_space = ML_Name_Space.global,
wenzelm@62490
    16
  str_of_pos = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")",
wenzelm@62490
    17
  print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut),
wenzelm@62490
    18
  error = fn s => raise Fail s};
wenzelm@62490
    19
wenzelm@31312
    20
fun drop_newline s =
wenzelm@31312
    21
  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
wenzelm@31312
    22
  else s;
wenzelm@31312
    23
wenzelm@31312
    24
in
wenzelm@31312
    25
wenzelm@62354
    26
fun use_text ({name_space, str_of_pos, print, error, ...}: use_context)
wenzelm@60956
    27
    {line, file, verbose, debug} text =
wenzelm@31312
    28
  let
wenzelm@60955
    29
    val current_line = Unsynchronized.ref line;
wenzelm@62354
    30
    val in_buffer = Unsynchronized.ref (String.explode (ml_positions line file text));
wenzelm@32738
    31
    val out_buffer = Unsynchronized.ref ([]: string list);
wenzelm@31312
    32
    fun output () = drop_newline (implode (rev (! out_buffer)));
wenzelm@31312
    33
wenzelm@31312
    34
    fun get () =
wenzelm@31312
    35
      (case ! in_buffer of
wenzelm@31312
    36
        [] => NONE
wenzelm@60955
    37
      | c :: cs =>
wenzelm@60955
    38
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
wenzelm@31312
    39
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@60955
    40
    fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} =
wenzelm@31312
    41
     (put (if hard then "Error: " else "Warning: ");
wenzelm@31312
    42
      PolyML.prettyPrint (put, 76) msg1;
wenzelm@31312
    43
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
wenzelm@62387
    44
      put ("At" ^ str_of_pos (FixedInt.toInt message_line) file ^ "\n"));
wenzelm@31312
    45
wenzelm@31312
    46
    val parameters =
wenzelm@31312
    47
     [PolyML.Compiler.CPOutStream put,
wenzelm@31471
    48
      PolyML.Compiler.CPNameSpace name_space,
wenzelm@31312
    49
      PolyML.Compiler.CPErrorMessageProc put_message,
wenzelm@60955
    50
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm@60955
    51
      PolyML.Compiler.CPFileName file,
wenzelm@60956
    52
      PolyML.Compiler.CPPrintInAlphabeticalOrder false] @
wenzelm@60956
    53
      ML_Compiler_Parameters.debug debug;
wenzelm@31312
    54
    val _ =
wenzelm@31312
    55
      (while not (List.null (! in_buffer)) do
wenzelm@31312
    56
        PolyML.compiler (get, parameters) ())
wenzelm@31312
    57
      handle exn =>
wenzelm@39242
    58
        if Exn.is_interrupt exn then reraise exn
wenzelm@39242
    59
        else
wenzelm@39242
    60
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@39242
    61
          error (output ()); reraise exn);
wenzelm@31312
    62
  in if verbose then print (output ()) else () end;
wenzelm@31312
    63
wenzelm@60956
    64
fun use_file context {verbose, debug} file =
wenzelm@31312
    65
  let
wenzelm@60955
    66
    val instream = TextIO.openIn file;
wenzelm@60956
    67
    val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@60956
    68
  in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end;
wenzelm@31312
    69
wenzelm@62490
    70
wenzelm@62490
    71
fun use_debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true"
wenzelm@62490
    72
  | use_debug_option (SOME debug) = debug;
wenzelm@62490
    73
wenzelm@62490
    74
local
wenzelm@62490
    75
wenzelm@62490
    76
fun use_ opt_debug file =
wenzelm@62490
    77
  use_file boot_context {verbose = true, debug = use_debug_option opt_debug} file
wenzelm@62489
    78
    handle Fail msg => (#print boot_context msg; raise Fail "ML error");
wenzelm@62489
    79
wenzelm@62490
    80
in
wenzelm@62490
    81
wenzelm@62490
    82
val use = use_ NONE;
wenzelm@62490
    83
val use_debug = use_ (SOME true);
wenzelm@62490
    84
val use_no_debug = use_ (SOME false);
wenzelm@62490
    85
wenzelm@31312
    86
end;
wenzelm@62490
    87
wenzelm@62490
    88
end;