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