src/Pure/ML-Systems/smlnj.ML
changeset 30672 beaadd5af500
parent 30626 248de8dd839e
child 31321 fe786d4633b9
equal deleted inserted replaced
30671:2f64540707d6 30672:beaadd5af500
    12 use "ML-Systems/thread_dummy.ML";
    12 use "ML-Systems/thread_dummy.ML";
    13 use "ML-Systems/multithreading.ML";
    13 use "ML-Systems/multithreading.ML";
    14 use "ML-Systems/system_shell.ML";
    14 use "ML-Systems/system_shell.ML";
    15 use "ML-Systems/ml_name_space.ML";
    15 use "ML-Systems/ml_name_space.ML";
    16 use "ML-Systems/ml_pretty.ML";
    16 use "ML-Systems/ml_pretty.ML";
       
    17 use "ML-Systems/use_context.ML";
    17 
    18 
    18 
    19 
    19 (*low-level pointer equality*)
    20 (*low-level pointer equality*)
    20 
    21 
    21 CM.autoload "$smlnj/init/init.cmi";
    22 CM.autoload "$smlnj/init/init.cmi";
    98 fun makestring x = "dummy string for SML New Jersey";
    99 fun makestring x = "dummy string for SML New Jersey";
    99 
   100 
   100 
   101 
   101 (* ML command execution *)
   102 (* ML command execution *)
   102 
   103 
   103 fun use_text (tune: string -> string) _ _ (line: int, name) (print, err) verbose txt =
   104 fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
   104   let
   105   let
   105     val ref out_orig = Control.Print.out;
   106     val ref out_orig = Control.Print.out;
   106 
   107 
   107     val out_buffer = ref ([]: string list);
   108     val out_buffer = ref ([]: string list);
   108     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
   109     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
   109     fun output () =
   110     fun output () =
   110       let val str = implode (rev (! out_buffer))
   111       let val str = implode (rev (! out_buffer))
   111       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   112       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   112   in
   113   in
   113     Control.Print.out := out;
   114     Control.Print.out := out;
   114     Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn =>
   115     Backend.Interact.useStream (TextIO.openString (tune_source txt)) handle exn =>
   115       (Control.Print.out := out_orig;
   116       (Control.Print.out := out_orig;
   116         err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
   117         error ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
   117     Control.Print.out := out_orig;
   118     Control.Print.out := out_orig;
   118     if verbose then print (output ()) else ()
   119     if verbose then print (output ()) else ()
   119   end;
   120   end;
   120 
   121 
   121 fun use_file tune str_of_pos name_space output verbose name =
   122 fun use_file context verbose name =
   122   let
   123   let
   123     val instream = TextIO.openIn name;
   124     val instream = TextIO.openIn name;
   124     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
   125     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
   125   in use_text tune str_of_pos name_space (1, name) output verbose txt end;
   126   in use_text context (1, name) verbose txt end;
   126 
   127 
   127 fun forget_structure name =
   128 fun forget_structure _ = ();
   128   use_text (fn x => x) (fn _ => "") () (1, "ML") (TextIO.print, fn s => raise Fail s) false
       
   129     ("structure " ^ name ^ " = struct end");
       
   130 
   129 
   131 
   130 
   132 (* toplevel pretty printing *)
   131 (* toplevel pretty printing *)
   133 
   132 
   134 fun ml_pprint pps =
   133 fun ml_pprint pps =
   141       | pprint (ML_Pretty.String (s, _)) = str s
   140       | pprint (ML_Pretty.String (s, _)) = str s
   142       | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
   141       | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
   143       | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps;
   142       | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps;
   144   in pprint end;
   143   in pprint end;
   145 
   144 
   146 fun toplevel_pp tune str_of_pos output path pp =
   145 fun toplevel_pp context path pp =
   147   use_text tune str_of_pos ML_NameSpace.global (1, "pp") output false
   146   use_text context (1, "pp") false
   148     ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
   147     ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
   149       "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
   148       "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
   150 
   149 
   151 
   150 
   152 
   151