src/Pure/ML-Systems/smlnj.ML
author wenzelm
Sat Mar 21 15:08:00 2009 +0100 (2009-03-21)
changeset 30619 0226c07352db
parent 30187 b92b3375e919
child 30626 248de8dd839e
permissions -rw-r--r--
added generic ML_Pretty interface;
     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 
     8 use "ML-Systems/proper_int.ML";
     9 use "ML-Systems/overloading_smlnj.ML";
    10 use "ML-Systems/exn.ML";
    11 use "ML-Systems/universal.ML";
    12 use "ML-Systems/thread_dummy.ML";
    13 use "ML-Systems/multithreading.ML";
    14 use "ML-Systems/system_shell.ML";
    15 use "ML-Systems/ml_name_space.ML";
    16 use "ML-Systems/ml_pretty.ML";
    17 
    18 
    19 (*low-level pointer equality*)
    20 
    21 CM.autoload "$smlnj/init/init.cmi";
    22 val pointer_eq = InlineT.ptreql;
    23 
    24 
    25 (* restore old-style character / string functions *)
    26 
    27 val ord     = mk_int o SML90.ord;
    28 val chr     = SML90.chr o dest_int;
    29 val explode = SML90.explode;
    30 val implode = SML90.implode;
    31 
    32 
    33 (* New Jersey ML parameters *)
    34 
    35 val _ =
    36  (Control.Print.printLength := 1000;
    37   Control.Print.printDepth := 350;
    38   Control.Print.stringDepth := 250;
    39   Control.Print.signatures := 2;
    40   Control.MC.matchRedundantError := false);
    41 
    42 
    43 (* Poly/ML emulation *)
    44 
    45 val exit = exit o dest_int;
    46 fun quit () = exit 0;
    47 
    48 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    49 local
    50   val depth = ref (10: int);
    51 in
    52   fun get_print_depth () = ! depth;
    53   fun print_depth n =
    54    (depth := n;
    55     Control.Print.printDepth := dest_int n div 2;
    56     Control.Print.printLength := dest_int n);
    57 end;
    58 
    59 
    60 (* compiler-independent timing functions *)
    61 
    62 fun start_timing () =
    63   let
    64     val timer = Timer.startCPUTimer ();
    65     val time = Timer.checkCPUTimer timer;
    66   in (timer, time) end;
    67 
    68 fun end_timing (timer, {sys, usr}) =
    69   let
    70     open Time;  (*...for Time.toString, Time.+ and Time.- *)
    71     val {sys = sys2, usr = usr2} = Timer.checkCPUTimer timer;
    72     val user = usr2 - usr;
    73     val all = user + sys2 - sys;
    74     val message = "User " ^ toString user ^ "  All "^ toString all ^ " secs" handle Time => "";
    75   in {message = message, user = user, all = all} end;
    76 
    77 fun check_timer timer =
    78   let
    79     val {sys, usr} = Timer.checkCPUTimer timer;
    80     val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
    81   in (sys, usr, gc) end;
    82 
    83 
    84 (*prompts*)
    85 fun ml_prompts p1 p2 =
    86   (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
    87 
    88 (*dummy implementation*)
    89 fun profile (n: int) f x = f x;
    90 
    91 (*dummy implementation*)
    92 fun exception_trace f = f ();
    93 
    94 (*dummy implementation*)
    95 fun print x = x;
    96 
    97 (*dummy implementation*)
    98 fun makestring x = "dummy string for SML New Jersey";
    99 
   100 
   101 (* toplevel pretty printing (see also Pure/pure_setup.ML) *)
   102 
   103 fun make_pp path pprint =
   104   let
   105     open PrettyPrint;
   106 
   107     fun pp pps obj =
   108       pprint obj
   109         (string pps, openHOVBox pps o Rel o dest_int,
   110           fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps,
   111           fn () => closeBox pps);
   112   in (path, pp) end;
   113 
   114 fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
   115 
   116 
   117 (* ML command execution *)
   118 
   119 fun use_text (tune: string -> string) _ _ (line: int, name) (print, err) verbose txt =
   120   let
   121     val ref out_orig = Control.Print.out;
   122 
   123     val out_buffer = ref ([]: string list);
   124     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
   125     fun output () =
   126       let val str = implode (rev (! out_buffer))
   127       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   128   in
   129     Control.Print.out := out;
   130     Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn =>
   131       (Control.Print.out := out_orig;
   132         err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
   133     Control.Print.out := out_orig;
   134     if verbose then print (output ()) else ()
   135   end;
   136 
   137 fun use_file tune str_of_pos name_space output verbose name =
   138   let
   139     val instream = TextIO.openIn name;
   140     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
   141   in use_text tune str_of_pos name_space (1, name) output verbose txt end;
   142 
   143 fun forget_structure name =
   144   use_text (fn x => x) (fn _ => "") () (1, "ML") (TextIO.print, fn s => raise Fail s) false
   145     ("structure " ^ name ^ " = struct end");
   146 
   147 
   148 
   149 (** interrupts **)
   150 
   151 local
   152 
   153 fun change_signal new_handler f x =
   154   let
   155     val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
   156     val result = Exn.capture (f old_handler) x;
   157     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   158   in Exn.release result end;
   159 
   160 in
   161 
   162 fun interruptible (f: 'a -> 'b) x =
   163   let
   164     val result = ref (Exn.Exn Interrupt: 'b Exn.result);
   165     val old_handler = Signals.inqHandler Signals.sigINT;
   166   in
   167     SMLofNJ.Cont.callcc (fn cont =>
   168       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
   169         result := Exn.capture f x));
   170     Signals.setHandler (Signals.sigINT, old_handler);
   171     Exn.release (! result)
   172   end;
   173 
   174 fun uninterruptible f =
   175   change_signal Signals.IGNORE
   176     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   177 
   178 end;
   179 
   180 
   181 (* basis library fixes *)
   182 
   183 structure TextIO =
   184 struct
   185   open TextIO;
   186   fun inputLine is = TextIO.inputLine is
   187     handle IO.Io _ => raise Interrupt;
   188 end;
   189 
   190 
   191 
   192 (** OS related **)
   193 
   194 (* current directory *)
   195 
   196 val cd = OS.FileSys.chDir;
   197 val pwd = OS.FileSys.getDir;
   198 
   199 
   200 (* system command execution *)
   201 
   202 val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
   203 
   204 
   205 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   206 fun process_id pid =
   207   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())));
   208 
   209 
   210 (* getenv *)
   211 
   212 fun getenv var =
   213   (case OS.Process.getEnv var of
   214     NONE => ""
   215   | SOME txt => txt);