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