src/Pure/RAW/compiler_polyml.ML
author wenzelm
Tue Mar 01 21:10:29 2016 +0100 (2016-03-01)
changeset 62493 dd154240a53c
parent 62490 39d01eaf5292
permissions -rw-r--r--
load secure.ML earlier;
eliminated obsolete ml_parse.ML;
tuned signature;
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@62493
     8
  here: 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@62493
    16
  here = 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@62493
    18
  error = fn s => error 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@62493
    26
fun use_text ({name_space, here, print, error, ...}: use_context)
wenzelm@60956
    27
    {line, file, verbose, debug} text =
wenzelm@31312
    28
  let
wenzelm@62493
    29
    val _ = Secure.deny_ml ();
wenzelm@62493
    30
wenzelm@60955
    31
    val current_line = Unsynchronized.ref line;
wenzelm@62354
    32
    val in_buffer = Unsynchronized.ref (String.explode (ml_positions line file text));
wenzelm@32738
    33
    val out_buffer = Unsynchronized.ref ([]: string list);
wenzelm@31312
    34
    fun output () = drop_newline (implode (rev (! out_buffer)));
wenzelm@31312
    35
wenzelm@31312
    36
    fun get () =
wenzelm@31312
    37
      (case ! in_buffer of
wenzelm@31312
    38
        [] => NONE
wenzelm@60955
    39
      | c :: cs =>
wenzelm@60955
    40
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
wenzelm@31312
    41
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@60955
    42
    fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} =
wenzelm@31312
    43
     (put (if hard then "Error: " else "Warning: ");
wenzelm@31312
    44
      PolyML.prettyPrint (put, 76) msg1;
wenzelm@31312
    45
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
wenzelm@62493
    46
      put ("At" ^ here (FixedInt.toInt message_line) file ^ "\n"));
wenzelm@31312
    47
wenzelm@31312
    48
    val parameters =
wenzelm@31312
    49
     [PolyML.Compiler.CPOutStream put,
wenzelm@31471
    50
      PolyML.Compiler.CPNameSpace name_space,
wenzelm@31312
    51
      PolyML.Compiler.CPErrorMessageProc put_message,
wenzelm@60955
    52
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm@60955
    53
      PolyML.Compiler.CPFileName file,
wenzelm@60956
    54
      PolyML.Compiler.CPPrintInAlphabeticalOrder false] @
wenzelm@60956
    55
      ML_Compiler_Parameters.debug debug;
wenzelm@31312
    56
    val _ =
wenzelm@31312
    57
      (while not (List.null (! in_buffer)) do
wenzelm@31312
    58
        PolyML.compiler (get, parameters) ())
wenzelm@31312
    59
      handle exn =>
wenzelm@39242
    60
        if Exn.is_interrupt exn then reraise exn
wenzelm@39242
    61
        else
wenzelm@39242
    62
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@39242
    63
          error (output ()); reraise exn);
wenzelm@31312
    64
  in if verbose then print (output ()) else () end;
wenzelm@31312
    65
wenzelm@60956
    66
fun use_file context {verbose, debug} file =
wenzelm@31312
    67
  let
wenzelm@60955
    68
    val instream = TextIO.openIn file;
wenzelm@60956
    69
    val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@60956
    70
  in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end;
wenzelm@31312
    71
wenzelm@62490
    72
wenzelm@62490
    73
fun use_debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true"
wenzelm@62490
    74
  | use_debug_option (SOME debug) = debug;
wenzelm@62490
    75
wenzelm@62493
    76
fun use_operations (use_ : bool option -> string -> unit) =
wenzelm@62493
    77
  {use = use_ NONE, use_debug = use_ (SOME true), use_no_debug = use_ (SOME false)};
wenzelm@62489
    78
wenzelm@62493
    79
val {use, use_debug, use_no_debug} =
wenzelm@62493
    80
  use_operations (fn opt_debug => fn file =>
wenzelm@62493
    81
    use_file boot_context {verbose = true, debug = use_debug_option opt_debug} file
wenzelm@62493
    82
      handle ERROR msg => (#print boot_context msg; raise error "ML error"));
wenzelm@62490
    83
wenzelm@31312
    84
end;