src/Pure/ML/ml_compiler0.ML
author wenzelm
Thu Mar 03 21:59:21 2016 +0100 (2016-03-03)
changeset 62508 d0b68218ea55
parent 62505 src/Pure/RAW/ml_compiler0.ML@9e2a65912111
child 62529 8b7bdfc09f3b
permissions -rw-r--r--
discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm@62508
     1
(*  Title:      Pure/ML/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@62504
    34
fun ml_positions start_line name txt =
wenzelm@62504
    35
  let
wenzelm@62504
    36
    fun positions line (#"@" :: #"{" :: #"h" :: #"e" :: #"r" :: #"e" :: #"}" :: cs) res =
wenzelm@62504
    37
          let val s = "(Position.line_file_only " ^ Int.toString line ^ " \"" ^ name ^ "\")"
wenzelm@62504
    38
          in positions line cs (s :: res) end
wenzelm@62504
    39
      | positions line (c :: cs) res =
wenzelm@62504
    40
          positions (if c = #"\n" then line + 1 else line) cs (str c :: res)
wenzelm@62504
    41
      | positions _ [] res = rev res;
wenzelm@62504
    42
  in String.concat (positions start_line (String.explode txt) []) end;
wenzelm@62504
    43
wenzelm@62494
    44
fun use_text ({name_space, here, print, error, ...}: context) {line, file, verbose, debug} text =
wenzelm@31312
    45
  let
wenzelm@62493
    46
    val _ = Secure.deny_ml ();
wenzelm@62493
    47
wenzelm@60955
    48
    val current_line = Unsynchronized.ref line;
wenzelm@62354
    49
    val in_buffer = Unsynchronized.ref (String.explode (ml_positions line file text));
wenzelm@32738
    50
    val out_buffer = Unsynchronized.ref ([]: string list);
wenzelm@31312
    51
    fun output () = drop_newline (implode (rev (! out_buffer)));
wenzelm@31312
    52
wenzelm@31312
    53
    fun get () =
wenzelm@31312
    54
      (case ! in_buffer of
wenzelm@31312
    55
        [] => NONE
wenzelm@60955
    56
      | c :: cs =>
wenzelm@60955
    57
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
wenzelm@31312
    58
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@60955
    59
    fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} =
wenzelm@31312
    60
     (put (if hard then "Error: " else "Warning: ");
wenzelm@31312
    61
      PolyML.prettyPrint (put, 76) msg1;
wenzelm@31312
    62
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
wenzelm@62493
    63
      put ("At" ^ here (FixedInt.toInt message_line) file ^ "\n"));
wenzelm@31312
    64
wenzelm@31312
    65
    val parameters =
wenzelm@31312
    66
     [PolyML.Compiler.CPOutStream put,
wenzelm@31471
    67
      PolyML.Compiler.CPNameSpace name_space,
wenzelm@31312
    68
      PolyML.Compiler.CPErrorMessageProc put_message,
wenzelm@60955
    69
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm@60955
    70
      PolyML.Compiler.CPFileName file,
wenzelm@62501
    71
      PolyML.Compiler.CPPrintInAlphabeticalOrder false,
wenzelm@62501
    72
      PolyML.Compiler.CPDebug debug];
wenzelm@31312
    73
    val _ =
wenzelm@31312
    74
      (while not (List.null (! in_buffer)) do
wenzelm@31312
    75
        PolyML.compiler (get, parameters) ())
wenzelm@31312
    76
      handle exn =>
wenzelm@62505
    77
        if Exn.is_interrupt exn then Exn.reraise exn
wenzelm@39242
    78
        else
wenzelm@39242
    79
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@62505
    80
          error (output ()); Exn.reraise exn);
wenzelm@31312
    81
  in if verbose then print (output ()) else () end;
wenzelm@31312
    82
wenzelm@60956
    83
fun use_file context {verbose, debug} file =
wenzelm@31312
    84
  let
wenzelm@60955
    85
    val instream = TextIO.openIn file;
wenzelm@60956
    86
    val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@60956
    87
  in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end;
wenzelm@31312
    88
wenzelm@62490
    89
wenzelm@62494
    90
fun debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true"
wenzelm@62494
    91
  | debug_option (SOME debug) = debug;
wenzelm@62490
    92
wenzelm@62493
    93
fun use_operations (use_ : bool option -> string -> unit) =
wenzelm@62493
    94
  {use = use_ NONE, use_debug = use_ (SOME true), use_no_debug = use_ (SOME false)};
wenzelm@62489
    95
wenzelm@62494
    96
end;
wenzelm@62494
    97
wenzelm@62493
    98
val {use, use_debug, use_no_debug} =
wenzelm@62494
    99
  let
wenzelm@62494
   100
    val context: ML_Compiler0.context =
wenzelm@62494
   101
     {name_space = ML_Name_Space.global,
wenzelm@62494
   102
      here = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")",
wenzelm@62494
   103
      print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut),
wenzelm@62494
   104
      error = fn s => error s};
wenzelm@62494
   105
  in
wenzelm@62494
   106
    ML_Compiler0.use_operations (fn opt_debug => fn file =>
wenzelm@62494
   107
      ML_Compiler0.use_file context
wenzelm@62494
   108
        {verbose = true, debug = ML_Compiler0.debug_option opt_debug} file
wenzelm@62494
   109
      handle ERROR msg => (#print context msg; raise error "ML error"))
wenzelm@62494
   110
  end;