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