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