src/Pure/ML-Systems/smlnj.ML
author wenzelm
Sun, 07 Sep 2008 17:46:43 +0200
changeset 28151 61f9c918b410
parent 26885 cfd5eb167706
child 28268 ac8431ecd57e
permissions -rw-r--r--
explicit use of universal.ML and dummy_thread.ML;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML-Systems/smlnj.ML
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     3
5708
fb09ab6a447f dropped support for SML/NJ 109.x;
wenzelm
parents: 5090
diff changeset
     4
Compatibility file for Standard ML of New Jersey 110 or later.
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     5
*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     6
24599
7b0ecf9a9055 use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents: 24329
diff changeset
     7
use "ML-Systems/proper_int.ML";
7b0ecf9a9055 use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents: 24329
diff changeset
     8
use "ML-Systems/overloading_smlnj.ML";
23965
f93e509659c1 ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents: 23921
diff changeset
     9
use "ML-Systems/exn.ML";
25732
308315ee2b6d added ML-Systems/universal.ML;
wenzelm
parents: 24688
diff changeset
    10
use "ML-Systems/universal.ML";
28151
61f9c918b410 explicit use of universal.ML and dummy_thread.ML;
wenzelm
parents: 26885
diff changeset
    11
use "ML-Systems/thread_dummy.ML";
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24599
diff changeset
    12
use "ML-Systems/multithreading.ML";
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents: 26084
diff changeset
    13
use "ML-Systems/system_shell.ML";
23921
947152add153 added compatibility file for ML systems without multithreading;
wenzelm
parents: 23826
diff changeset
    14
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    15
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    16
(*low-level pointer equality*)
16502
5a56e59526a5 added pointer_eq;
wenzelm
parents: 16266
diff changeset
    17
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    18
CM.autoload "$smlnj/init/init.cmi";
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    19
val pointer_eq = InlineT.ptreql;
16528
25a7459d4d4a pointer equality for sml/nj
paulson
parents: 16502
diff changeset
    20
16502
5a56e59526a5 added pointer_eq;
wenzelm
parents: 16266
diff changeset
    21
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    22
(* restore old-style character / string functions *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    23
24145
c6402b61d44a preparations for proper type int;
wenzelm
parents: 23965
diff changeset
    24
val ord     = mk_int o SML90.ord;
c6402b61d44a preparations for proper type int;
wenzelm
parents: 23965
diff changeset
    25
val chr     = SML90.chr o dest_int;
10725
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    26
val explode = SML90.explode;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    27
val implode = SML90.implode;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    28
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    29
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    30
(* New Jersey ML parameters *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    31
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    32
val _ =
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    33
 (Control.Print.printLength := 1000;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    34
  Control.Print.printDepth := 350;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    35
  Control.Print.stringDepth := 250;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    36
  Control.Print.signatures := 2;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    37
  Control.MC.matchRedundantError := false);
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    38
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    39
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    40
(* Poly/ML emulation *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    41
24599
7b0ecf9a9055 use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents: 24329
diff changeset
    42
val exit = exit o dest_int;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    43
fun quit () = exit 0;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    44
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    45
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
24329
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    46
local
24599
7b0ecf9a9055 use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents: 24329
diff changeset
    47
  val depth = ref (10: int);
24329
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    48
in
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    49
  fun get_print_depth () = ! depth;
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    50
  fun print_depth n =
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    51
   (depth := n;
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    52
    Control.Print.printDepth := dest_int n div 2;
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    53
    Control.Print.printLength := dest_int n);
f31594168d27 ML system provides get_print_depth;
wenzelm
parents: 24290
diff changeset
    54
end;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    55
26474
94735cff132c added forget_structure;
wenzelm
parents: 26385
diff changeset
    56
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
    57
(* compiler-independent timing functions *)
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    58
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    59
fun start_timing () =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    60
  let val CPUtimer = Timer.startCPUTimer();
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    61
      val time = Timer.checkCPUTimer(CPUtimer)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    62
  in  (CPUtimer,time)  end;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    63
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    64
fun end_timing (CPUtimer, {sys,usr}) =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    65
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    66
      val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    67
  in  "User " ^ toString (usr2-usr) ^
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    68
      "  All "^ toString (sys2-sys + usr2-usr) ^
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    69
      " secs"
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    70
      handle Time => ""
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    71
  end;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    72
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    73
fun check_timer timer =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    74
  let
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    75
    val {sys, usr} = Timer.checkCPUTimer timer;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    76
    val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    77
  in (sys, usr, gc) end;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    78
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    79
16660
76613dff2c9a added profiler interface (dummy);
wenzelm
parents: 16542
diff changeset
    80
(*prompts*)
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    81
fun ml_prompts p1 p2 =
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    82
  (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    83
17511
wenzelm
parents: 16681
diff changeset
    84
(*dummy implementation*)
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    85
fun profile (n: int) f x = f x;
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    86
17511
wenzelm
parents: 16681
diff changeset
    87
(*dummy implementation*)
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    88
fun exception_trace f = f ();
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    89
18384
fa38cca42913 added dummy 'print' to non-polyml systems
haftmann
parents: 17824
diff changeset
    90
(*dummy implementation*)
fa38cca42913 added dummy 'print' to non-polyml systems
haftmann
parents: 17824
diff changeset
    91
fun print x = x;
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    92
23770
2711e0285072 added dummy makestring function
obua
parents: 23139
diff changeset
    93
(*dummy implementation*)
2711e0285072 added dummy makestring function
obua
parents: 23139
diff changeset
    94
fun makestring x = "dummy string for SML New Jersey";
2711e0285072 added dummy makestring function
obua
parents: 23139
diff changeset
    95
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    96
24290
5607b8b752bb tuned comments;
wenzelm
parents: 24145
diff changeset
    97
(* toplevel pretty printing (see also Pure/pure_setup.ML) *)
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    98
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    99
fun make_pp path pprint =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   100
  let
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   101
    open PrettyPrint;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   102
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   103
    fun pp pps obj =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   104
      pprint obj
24145
c6402b61d44a preparations for proper type int;
wenzelm
parents: 23965
diff changeset
   105
        (string pps, openHOVBox pps o Rel o dest_int,
c6402b61d44a preparations for proper type int;
wenzelm
parents: 23965
diff changeset
   106
          fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps,
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   107
          fn () => closeBox pps);
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   108
  in (path, pp) end;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   109
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   110
fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   111
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   112
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   113
(* ML command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   114
26884
67c54c53da28 use_text/file: ignore str_of_pos argument;
wenzelm
parents: 26504
diff changeset
   115
fun use_text (tune: string -> string) _ (line: int, name) (print, err) verbose txt =
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   116
  let
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   117
    val ref out_orig = Control.Print.out;
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   118
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   119
    val out_buffer = ref ([]: string list);
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   120
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   121
    fun output () =
7890
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
   122
      let val str = implode (rev (! out_buffer))
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   123
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   124
  in
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   125
    Control.Print.out := out;
24599
7b0ecf9a9055 use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents: 24329
diff changeset
   126
    Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn =>
22144
c33450acd873 use_text: added name argument;
wenzelm
parents: 21770
diff changeset
   127
      (Control.Print.out := out_orig;
c33450acd873 use_text: added name argument;
wenzelm
parents: 21770
diff changeset
   128
        err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   129
    Control.Print.out := out_orig;
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   130
    if verbose then print (output ()) else ()
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   131
  end;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   132
26885
cfd5eb167706 use_file: pass str_of_pos;
wenzelm
parents: 26884
diff changeset
   133
fun use_file tune str_of_pos output verbose name =
24599
7b0ecf9a9055 use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents: 24329
diff changeset
   134
  let
7b0ecf9a9055 use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents: 24329
diff changeset
   135
    val instream = TextIO.openIn name;
26504
6e87c0a60104 before close: Exn.capture/release;
wenzelm
parents: 26474
diff changeset
   136
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
26885
cfd5eb167706 use_file: pass str_of_pos;
wenzelm
parents: 26884
diff changeset
   137
  in use_text tune str_of_pos (1, name) output verbose txt end;
21770
ea6f846d8c4b added use_file;
wenzelm
parents: 21298
diff changeset
   138
26474
94735cff132c added forget_structure;
wenzelm
parents: 26385
diff changeset
   139
fun forget_structure name =
26885
cfd5eb167706 use_file: pass str_of_pos;
wenzelm
parents: 26884
diff changeset
   140
  use_text (fn x => x) (fn _ => "") (1, "ML") (TextIO.print, fn s => raise Fail s) false
26474
94735cff132c added forget_structure;
wenzelm
parents: 26385
diff changeset
   141
    ("structure " ^ name ^ " = struct end");
94735cff132c added forget_structure;
wenzelm
parents: 26385
diff changeset
   142
21770
ea6f846d8c4b added use_file;
wenzelm
parents: 21298
diff changeset
   143
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   144
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   145
(** interrupts **)
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   146
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   147
exception Interrupt;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   148
26084
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   149
local
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   150
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   151
fun change_signal new_handler f x =
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   152
  let
26084
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   153
    val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   154
    val result = Exn.capture (f old_handler) x;
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   155
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
23965
f93e509659c1 ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents: 23921
diff changeset
   156
  in Exn.release result end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   157
26084
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   158
in
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   159
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   160
fun interruptible (f: 'a -> 'b) x =
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   161
  let
26084
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   162
    val result = ref (Exn.Exn Interrupt: 'b Exn.result);
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   163
    val old_handler = Signals.inqHandler Signals.sigINT;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   164
  in
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   165
    SMLofNJ.Cont.callcc (fn cont =>
26084
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   166
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   167
        result := Exn.capture f x));
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   168
    Signals.setHandler (Signals.sigINT, old_handler);
26084
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   169
    Exn.release (! result)
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   170
  end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   171
26084
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   172
fun uninterruptible f =
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   173
  change_signal Signals.IGNORE
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   174
    (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   175
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   176
end;
a7475459c740 replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents: 25732
diff changeset
   177
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   178
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   179
(* basis library fixes *)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   180
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   181
structure TextIO =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   182
struct
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   183
  open TextIO;
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22144
diff changeset
   184
  fun inputLine is = TextIO.inputLine is
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   185
    handle IO.Io _ => raise Interrupt;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   186
end;
17511
wenzelm
parents: 16681
diff changeset
   187
18790
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   188
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   189
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   190
    Posix.Signal.signal and Signals.signal differ **)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   191
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   192
structure IsaSignal =
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   193
struct
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   194
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   195
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   196
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   197
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   198
  signal. When signal is delivered to the process, the execution state
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   199
  of the current thread will be bundled up as a continuation k, then
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   200
  f(signal,n,k) will be called. The number n is the number of times
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   201
  signal has been signalled since the last time f was invoked for it."*)
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   202
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   203
fun toAction SIG_DFL = Signals.DEFAULT
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   204
  | toAction SIG_IGN = Signals.IGNORE
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   205
  | toAction (SIG_HANDLE iu) =
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   206
      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   207
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   208
(*The types are correct, but I'm not sure about the semantics!*)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   209
fun fromAction Signals.DEFAULT = SIG_DFL
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   210
  | fromAction Signals.IGNORE = SIG_IGN
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   211
  | fromAction (Signals.HANDLER f) =
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   212
      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   213
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   214
(*Poly/ML version has type  int * sig_handle -> sig_handle*)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   215
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   216
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   217
val usr1 = UnixSignals.sigUSR1
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   218
val usr2 = UnixSignals.sigUSR2
17763
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   219
val alrm = UnixSignals.sigALRM
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   220
val chld = UnixSignals.sigCHLD
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   221
val cont = UnixSignals.sigCONT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   222
val int = UnixSignals.sigINT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   223
val quit = UnixSignals.sigQUIT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   224
val term = UnixSignals.sigTERM
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   225
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   226
end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   227
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   228
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   229
(** OS related **)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   230
23826
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   231
(* current directory *)
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   232
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   233
val cd = OS.FileSys.chDir;
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   234
val pwd = OS.FileSys.getDir;
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   235
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   236
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   237
(* system command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   238
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents: 26084
diff changeset
   239
val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   240
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   241
17824
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   242
(*Convert a process ID to a decimal string (chiefly for tracing)*)
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   243
fun string_of_pid pid =
26220
d34b68c21f9a common setup for system_out/system;
wenzelm
parents: 26084
diff changeset
   244
  Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
17824
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   245
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   246
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   247
(* getenv *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   248
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   249
fun getenv var =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   250
  (case OS.Process.getEnv var of
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   251
    NONE => ""
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   252
  | SOME txt => txt);