src/Pure/ML-Systems/smlnj.ML
author skalberg
Mon, 05 Apr 2004 13:30:37 +0200
changeset 14520 af9d7fcf873e
parent 14519 4ca3608fdf4f
child 14656 765badface6a
permissions -rw-r--r--
Whoops. Those default cases can be tricky.
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
    Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
12874
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     5
5708
fb09ab6a447f dropped support for SML/NJ 109.x;
wenzelm
parents: 5090
diff changeset
     6
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
     7
*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     8
12874
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
     9
(case #version_id (Compiler.version) of
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
    10
  [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
    11
| _ => ());
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
    12
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    13
(** ML system related **)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    14
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    15
(* restore old-style character / string functions *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    16
10725
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    17
val ord     = SML90.ord;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    18
val chr     = SML90.chr;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    19
val explode = SML90.explode;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    20
val implode = SML90.implode;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    21
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    22
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    23
(* New Jersey ML parameters *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    24
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    25
val _ =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    26
 (Compiler.Control.Print.printLength := 1000;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    27
  Compiler.Control.Print.printDepth := 350;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    28
  Compiler.Control.Print.stringDepth := 250;
12581
dceea9dbdedd Redundant patterns no longer cause errors.
berghofe
parents: 12108
diff changeset
    29
  Compiler.Control.Print.signatures := 2;
dceea9dbdedd Redundant patterns no longer cause errors.
berghofe
parents: 12108
diff changeset
    30
  Compiler.Control.MC.matchRedundantError := false);
4403
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
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    33
(* Poly/ML emulation *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    34
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    35
fun quit () = exit 0;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    36
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    37
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    38
fun print_depth n =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    39
 (Compiler.Control.Print.printDepth := n div 2;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    40
  Compiler.Control.Print.printLength := n);
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    41
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    42
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
    43
(* compiler-independent timing functions *)
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    44
14519
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    45
(case #version_id (Compiler.version) of
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    46
  [110, x] => if x >= 45
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    47
	      then use "ML-Systems/cpu-timer-basis.ML"
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    48
	      else use "ML-Systems/cpu-timer-gc.ML"
14520
af9d7fcf873e Whoops. Those default cases can be tricky.
skalberg
parents: 14519
diff changeset
    49
| _ => use "ML-Systems/cpu-timer-gc.ML");
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    50
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    51
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    52
(* prompts *)
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    53
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    54
fun ml_prompts p1 p2 =
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    55
  (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    56
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    57
14519
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    58
(case #version_id (Compiler.version) of
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    59
  [110, x] => if x >= 45
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    60
	      then use "ML-Systems/smlnj-basis-compat.ML"
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    61
	      else ()
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    62
| _ => ());
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    63
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    64
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    65
14519
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    66
(case #version_id (Compiler.version) of
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    67
  [110, x] => if x >= 45
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    68
	      then use "ML-Systems/smlnj-pp-new.ML"
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    69
	      else use "ML-Systems/smlnj-pp-old.ML"
14520
af9d7fcf873e Whoops. Those default cases can be tricky.
skalberg
parents: 14519
diff changeset
    70
| _ => use "ML-Systems/smlnj-pp-old.ML");
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    71
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    72
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    73
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    74
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    75
(* ML command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    76
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
    77
fun use_text (print, err) verbose txt =
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    78
  let
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    79
    val ref out_orig = Compiler.Control.Print.out;
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    80
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    81
    val out_buffer = ref ([]: string list);
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    82
    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
    83
    fun output () =
7890
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    84
      let val str = implode (rev (! out_buffer))
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
    85
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    86
  in
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    87
    Compiler.Control.Print.out := out;
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    88
    Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
    89
      (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    90
    Compiler.Control.Print.out := out_orig;
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
    91
    if verbose then print (output ()) else ()
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    92
  end;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    93
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    94
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    95
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
    96
(** interrupts **)
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
    97
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
    98
exception Interrupt;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
    99
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   100
local
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   101
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   102
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   103
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   104
fun release NONE = ()
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   105
  | release (SOME exn) = raise exn;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   106
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   107
in
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   108
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   109
fun ignore_interrupt f x =
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   110
  let
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   111
    val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   112
    val result = capture f x;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   113
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   114
  in release result end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   115
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   116
fun raise_interrupt f x =
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   117
  let
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   118
    val interrupted = ref false;
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   119
    val result = ref NONE;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   120
    val old_handler = Signals.inqHandler Signals.sigINT;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   121
  in
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   122
    SMLofNJ.Cont.callcc (fn cont =>
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   123
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   124
      result := capture f x));
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   125
    Signals.setHandler (Signals.sigINT, old_handler);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   126
    if ! interrupted then raise Interrupt else release (! result)
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   127
  end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   128
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   129
end;
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   130
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   131
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   132
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   133
(** OS related **)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   134
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   135
(* system command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   136
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   137
(*execute Unix command which doesn't take any input from stdin and
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   138
  sends its output to stdout; could be done more easily by Unix.execute,
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   139
  but that function doesn't use the PATH*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   140
fun execute command =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   141
  let
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   142
    val tmp_name = OS.FileSys.tmpName ();
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   143
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   144
    val result = TextIO.inputAll is;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   145
  in
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   146
    TextIO.closeIn is;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   147
    OS.FileSys.remove tmp_name;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   148
    result
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   149
  end;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   150
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   151
(*plain version; with return code*)
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   152
val system = OS.Process.system: string -> int;
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   153
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   154
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   155
(* file handling *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   156
6227
3198f547f8af fixed file_info;
wenzelm
parents: 5816
diff changeset
   157
(*get time of last modification*)
3198f547f8af fixed file_info;
wenzelm
parents: 5816
diff changeset
   158
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   159
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   160
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   161
(* getenv *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   162
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   163
fun getenv var =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   164
  (case OS.Process.getEnv var of
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   165
    NONE => ""
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   166
  | SOME txt => txt);