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