src/Pure/ML-Systems/smlnj.ML
author wenzelm
Fri Apr 12 14:54:14 2013 +0200 (2013-04-12)
changeset 51700 c8f2bad67dbb
parent 50911 ee7fe4230642
child 53709 84522727f9d3
permissions -rw-r--r--
tuned signature;
tuned comments;
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2 
     3 Compatibility file for Standard ML of New Jersey.
     4 *)
     5 
     6 use "ML-Systems/proper_int.ML";
     7 
     8 exception Interrupt;
     9 fun reraise exn = raise exn;
    10 
    11 use "ML-Systems/overloading_smlnj.ML";
    12 use "General/exn.ML";
    13 use "ML-Systems/single_assignment.ML";
    14 use "ML-Systems/universal.ML";
    15 use "ML-Systems/thread_dummy.ML";
    16 use "ML-Systems/multithreading.ML";
    17 use "ML-Systems/ml_name_space.ML";
    18 use "ML-Systems/ml_pretty.ML";
    19 structure PolyML = struct end;
    20 use "ML-Systems/pp_dummy.ML";
    21 use "ML-Systems/use_context.ML";
    22 
    23 
    24 val seconds = Time.fromReal;
    25 
    26 (*low-level pointer equality*)
    27 CM.autoload "$smlnj/init/init.cmi";
    28 val pointer_eq = InlineT.ptreql;
    29 
    30 
    31 (* restore old-style character / string functions *)
    32 
    33 val ord = mk_int o SML90.ord;
    34 val chr = SML90.chr o dest_int;
    35 val raw_explode = SML90.explode;
    36 val implode = SML90.implode;
    37 
    38 
    39 (* New Jersey ML parameters *)
    40 
    41 val _ =
    42  (Control.Print.printLength := 1000;
    43   Control.Print.printDepth := 350;
    44   Control.Print.stringDepth := 250;
    45   Control.Print.signatures := 2;
    46   Control.MC.matchRedundantError := false);
    47 
    48 
    49 (* Poly/ML emulation *)
    50 
    51 val exit = exit o dest_int;
    52 fun quit () = exit 0;
    53 
    54 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    55 local
    56   val depth = ref (10: int);
    57 in
    58   fun get_print_depth () = ! depth;
    59   fun print_depth n =
    60    (depth := n;
    61     Control.Print.printDepth := dest_int n div 2;
    62     Control.Print.printLength := dest_int n);
    63 end;
    64 
    65 val ml_make_string = "(fn _ => \"?\")";
    66 
    67 
    68 (*prompts*)
    69 fun ml_prompts p1 p2 =
    70   (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
    71 
    72 (*dummy implementation*)
    73 fun profile (n: int) f x = f x;
    74 
    75 (*dummy implementation*)
    76 fun exception_trace f = f ();
    77 
    78 
    79 (* ML command execution *)
    80 
    81 fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
    82   let
    83     val ref out_orig = Control.Print.out;
    84 
    85     val out_buffer = ref ([]: string list);
    86     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    87     fun output () =
    88       let val str = implode (rev (! out_buffer))
    89       in String.substring (str, 0, Int.max (0, size str - 1)) end;
    90   in
    91     Control.Print.out := out;
    92     Backend.Interact.useStream (TextIO.openString (tune_source txt)) handle exn =>
    93       (Control.Print.out := out_orig;
    94         error ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
    95     Control.Print.out := out_orig;
    96     if verbose then print (output ()) else ()
    97   end;
    98 
    99 fun use_file context verbose name =
   100   let
   101     val instream = TextIO.openIn name;
   102     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
   103   in use_text context (1, name) verbose txt end;
   104 
   105 
   106 (* toplevel pretty printing *)
   107 
   108 fun ml_pprint pps =
   109   let
   110     fun str "" = ()
   111       | str s = PrettyPrint.string pps s;
   112     fun pprint (ML_Pretty.Block ((bg, en), prts, ind)) =
   113           (str bg; PrettyPrint.openHOVBox pps (PrettyPrint.Rel (dest_int ind));
   114             List.app pprint prts; PrettyPrint.closeBox pps; str en)
   115       | pprint (ML_Pretty.String (s, _)) = str s
   116       | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
   117       | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps;
   118   in pprint end;
   119 
   120 fun toplevel_pp context path pp =
   121   use_text context (1, "pp") false
   122     ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
   123       "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
   124 
   125 
   126 
   127 (** interrupts **)
   128 
   129 local
   130 
   131 fun change_signal new_handler f x =
   132   let
   133     val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
   134     val result = Exn.capture (f old_handler) x;
   135     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   136   in Exn.release result end;
   137 
   138 in
   139 
   140 fun interruptible (f: 'a -> 'b) x =
   141   let
   142     val result = ref (Exn.interrupt_exn: 'b Exn.result);
   143     val old_handler = Signals.inqHandler Signals.sigINT;
   144   in
   145     SMLofNJ.Cont.callcc (fn cont =>
   146       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
   147         result := Exn.capture f x));
   148     Signals.setHandler (Signals.sigINT, old_handler);
   149     Exn.release (! result)
   150   end;
   151 
   152 fun uninterruptible f =
   153   change_signal Signals.IGNORE
   154     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   155 
   156 end;
   157 
   158 
   159 use "ML-Systems/unsynchronized.ML";
   160 
   161 
   162 (* ML system operations *)
   163 
   164 use "ML-Systems/ml_system.ML";
   165 
   166 structure ML_System =
   167 struct
   168 
   169 open ML_System;
   170 
   171 fun save_state name =
   172   if SMLofNJ.exportML name then ()
   173   else OS.FileSys.rename {old = name ^ "." ^ platform, new = name};
   174 
   175 end;
   176