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