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