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