src/Pure/ML/ml_compiler0.ML
author wenzelm
Sat Apr 02 20:23:51 2016 +0200 (2016-04-02)
changeset 62817 744bfd770123
parent 62716 d80b9f4990e4
child 62819 d3ff367a16a0
permissions -rw-r--r--
clarified modules;
wenzelm@62508
     1
(*  Title:      Pure/ML/ml_compiler0.ML
wenzelm@62817
     2
    Author:     Makarius
wenzelm@31312
     3
wenzelm@62817
     4
Runtime compilation and evaluation for bootstrap.
wenzelm@62817
     5
Initial ML use operations.
wenzelm@31312
     6
*)
wenzelm@31312
     7
wenzelm@62494
     8
signature ML_COMPILER0 =
wenzelm@62494
     9
sig
wenzelm@62494
    10
  type context =
wenzelm@62494
    11
   {name_space: ML_Name_Space.T,
wenzelm@62716
    12
    print_depth: int option,
wenzelm@62494
    13
    here: int -> string -> string,
wenzelm@62494
    14
    print: string -> unit,
wenzelm@62494
    15
    error: string -> unit}
wenzelm@62817
    16
  val make_context: ML_Name_Space.T -> context
wenzelm@62494
    17
  val use_text: context -> {debug: bool, file: string, line: int, verbose: bool} -> string -> unit
wenzelm@62494
    18
  val use_file: context -> {debug: bool, verbose: bool} -> string -> unit
wenzelm@62494
    19
  val debug_option: bool option -> bool
wenzelm@62494
    20
  val use_operations: (bool option -> string -> unit) ->
wenzelm@62494
    21
    {use: string -> unit, use_debug: string -> unit, use_no_debug: string -> unit}
wenzelm@62494
    22
end;
wenzelm@62494
    23
wenzelm@62494
    24
structure ML_Compiler0: ML_COMPILER0 =
wenzelm@62494
    25
struct
wenzelm@62494
    26
wenzelm@62817
    27
(* global options *)
wenzelm@62817
    28
wenzelm@62817
    29
val _ = PolyML.Compiler.reportUnreferencedIds := true;
wenzelm@62817
    30
val _ = PolyML.Compiler.reportExhaustiveHandlers := true;
wenzelm@62817
    31
val _ = PolyML.Compiler.printInAlphabeticalOrder := false;
wenzelm@62817
    32
val _ = PolyML.Compiler.maxInlineSize := 80;
wenzelm@62817
    33
wenzelm@62817
    34
wenzelm@62817
    35
(* context *)
wenzelm@62817
    36
wenzelm@62494
    37
type context =
wenzelm@62490
    38
 {name_space: ML_Name_Space.T,
wenzelm@62716
    39
  print_depth: int option,
wenzelm@62493
    40
  here: int -> string -> string,
wenzelm@62490
    41
  print: string -> unit,
wenzelm@62490
    42
  error: string -> unit};
wenzelm@62490
    43
wenzelm@62817
    44
fun make_context name_space : context =
wenzelm@62817
    45
 {name_space = name_space,
wenzelm@62817
    46
  print_depth = NONE,
wenzelm@62817
    47
  here = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")",
wenzelm@62817
    48
  print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut),
wenzelm@62817
    49
  error = fn s => error s};
wenzelm@62817
    50
wenzelm@62817
    51
wenzelm@62817
    52
(* use operations *)
wenzelm@62817
    53
wenzelm@62817
    54
local
wenzelm@62817
    55
wenzelm@31312
    56
fun drop_newline s =
wenzelm@31312
    57
  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
wenzelm@31312
    58
  else s;
wenzelm@31312
    59
wenzelm@62529
    60
fun ml_input start_line name txt =
wenzelm@62504
    61
  let
wenzelm@62662
    62
    fun input line (#"@" :: #"{" :: #"h" :: #"e" :: #"r" :: #"e" :: #"}" :: cs) res =
wenzelm@62504
    63
          let val s = "(Position.line_file_only " ^ Int.toString line ^ " \"" ^ name ^ "\")"
wenzelm@62662
    64
          in input line cs (s :: res) end
wenzelm@62662
    65
      | input line (#"@" :: #"{" :: #"m" :: #"a" :: #"k" :: #"e" :: #"_" ::
wenzelm@62662
    66
            #"s" :: #"t" :: #"r" :: #"i" :: #"n" :: #"g" :: #"}" :: cs) res =
wenzelm@62662
    67
          input line cs (ML_Pretty.make_string_fn "" :: res)
wenzelm@62662
    68
      | input line (#"\\" :: #"<" :: cs) res = input line cs ("\\\\<" :: res)
wenzelm@62662
    69
      | input line (#"\n" :: cs) res = input (line + 1) cs ("\n" :: res)
wenzelm@62662
    70
      | input line (c :: cs) res = input line cs (str c :: res)
wenzelm@62662
    71
      | input _ [] res = rev res;
wenzelm@62662
    72
  in String.concat (input start_line (String.explode txt) []) end;
wenzelm@62504
    73
wenzelm@62817
    74
in
wenzelm@62817
    75
wenzelm@62716
    76
fun use_text ({name_space, print_depth, here, print, error, ...}: context)
wenzelm@62716
    77
    {line, file, verbose, debug} text =
wenzelm@31312
    78
  let
wenzelm@60955
    79
    val current_line = Unsynchronized.ref line;
wenzelm@62529
    80
    val in_buffer = Unsynchronized.ref (String.explode (ml_input line file text));
wenzelm@32738
    81
    val out_buffer = Unsynchronized.ref ([]: string list);
wenzelm@31312
    82
    fun output () = drop_newline (implode (rev (! out_buffer)));
wenzelm@31312
    83
wenzelm@31312
    84
    fun get () =
wenzelm@31312
    85
      (case ! in_buffer of
wenzelm@31312
    86
        [] => NONE
wenzelm@60955
    87
      | c :: cs =>
wenzelm@60955
    88
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
wenzelm@31312
    89
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@60955
    90
    fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} =
wenzelm@31312
    91
     (put (if hard then "Error: " else "Warning: ");
wenzelm@31312
    92
      PolyML.prettyPrint (put, 76) msg1;
wenzelm@31312
    93
      (case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2);
wenzelm@62493
    94
      put ("At" ^ here (FixedInt.toInt message_line) file ^ "\n"));
wenzelm@31312
    95
wenzelm@31312
    96
    val parameters =
wenzelm@31312
    97
     [PolyML.Compiler.CPOutStream put,
wenzelm@31471
    98
      PolyML.Compiler.CPNameSpace name_space,
wenzelm@31312
    99
      PolyML.Compiler.CPErrorMessageProc put_message,
wenzelm@60955
   100
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm@60955
   101
      PolyML.Compiler.CPFileName file,
wenzelm@62501
   102
      PolyML.Compiler.CPPrintInAlphabeticalOrder false,
wenzelm@62716
   103
      PolyML.Compiler.CPDebug debug] @
wenzelm@62716
   104
     (case print_depth of NONE => [] | SOME d => [PolyML.Compiler.CPPrintDepth (fn () => d)]);
wenzelm@31312
   105
    val _ =
wenzelm@31312
   106
      (while not (List.null (! in_buffer)) do
wenzelm@31312
   107
        PolyML.compiler (get, parameters) ())
wenzelm@31312
   108
      handle exn =>
wenzelm@62505
   109
        if Exn.is_interrupt exn then Exn.reraise exn
wenzelm@39242
   110
        else
wenzelm@39242
   111
         (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@62505
   112
          error (output ()); Exn.reraise exn);
wenzelm@31312
   113
  in if verbose then print (output ()) else () end;
wenzelm@31312
   114
wenzelm@62817
   115
end;
wenzelm@62817
   116
wenzelm@60956
   117
fun use_file context {verbose, debug} file =
wenzelm@31312
   118
  let
wenzelm@60955
   119
    val instream = TextIO.openIn file;
wenzelm@60956
   120
    val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@60956
   121
  in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end;
wenzelm@31312
   122
wenzelm@62817
   123
fun use_operations (use_ : bool option -> string -> unit) =
wenzelm@62817
   124
  {use = use_ NONE, use_debug = use_ (SOME true), use_no_debug = use_ (SOME false)};
wenzelm@62817
   125
wenzelm@62490
   126
wenzelm@62494
   127
fun debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true"
wenzelm@62494
   128
  | debug_option (SOME debug) = debug;
wenzelm@62490
   129
wenzelm@62817
   130
end;
wenzelm@62489
   131
wenzelm@62817
   132
wenzelm@62817
   133
(* initial use operations *)
wenzelm@62494
   134
wenzelm@62493
   135
val {use, use_debug, use_no_debug} =
wenzelm@62817
   136
  let val context = ML_Compiler0.make_context ML_Name_Space.global in
wenzelm@62494
   137
    ML_Compiler0.use_operations (fn opt_debug => fn file =>
wenzelm@62494
   138
      ML_Compiler0.use_file context
wenzelm@62494
   139
        {verbose = true, debug = ML_Compiler0.debug_option opt_debug} file
wenzelm@62494
   140
      handle ERROR msg => (#print context msg; raise error "ML error"))
wenzelm@62494
   141
  end;