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