src/Pure/ML-Systems/smlnj.ML
author wenzelm
Wed Nov 26 14:35:55 2014 +0100 (2014-11-26 ago)
changeset 59055 5a7157b8e870
parent 56627 cb912b7de3cf
child 59127 723b11f8ffbf
permissions -rw-r--r--
more informative failure of protocol commands, with exception trace;
eliminated obsolete Runtime.TERMINATE (left-over from former 'exit' command);
wenzelm@4403
     1
(*  Title:      Pure/ML-Systems/smlnj.ML
wenzelm@4403
     2
wenzelm@50910
     3
Compatibility file for Standard ML of New Jersey.
wenzelm@4403
     4
*)
wenzelm@4403
     5
wenzelm@54342
     6
val io_buffer_size = 4096;
wenzelm@44249
     7
use "ML-Systems/proper_int.ML";
wenzelm@44249
     8
wenzelm@28443
     9
exception Interrupt;
wenzelm@31427
    10
fun reraise exn = raise exn;
wenzelm@28443
    11
wenzelm@56627
    12
fun exit rc = Posix.Process.exit (Word8.fromInt rc);
wenzelm@56627
    13
fun quit () = exit 0;
wenzelm@56627
    14
wenzelm@24599
    15
use "ML-Systems/overloading_smlnj.ML";
wenzelm@34136
    16
use "General/exn.ML";
wenzelm@35014
    17
use "ML-Systems/single_assignment.ML";
wenzelm@25732
    18
use "ML-Systems/universal.ML";
wenzelm@28151
    19
use "ML-Systems/thread_dummy.ML";
wenzelm@24688
    20
use "ML-Systems/multithreading.ML";
wenzelm@28268
    21
use "ML-Systems/ml_name_space.ML";
wenzelm@30619
    22
use "ML-Systems/ml_pretty.ML";
wenzelm@38635
    23
structure PolyML = struct end;
wenzelm@38635
    24
use "ML-Systems/pp_dummy.ML";
wenzelm@30672
    25
use "ML-Systems/use_context.ML";
wenzelm@56435
    26
use "ML-Systems/ml_positions.ML";
wenzelm@23921
    27
wenzelm@16542
    28
wenzelm@42012
    29
val seconds = Time.fromReal;
wenzelm@42012
    30
wenzelm@16542
    31
(*low-level pointer equality*)
wenzelm@21298
    32
CM.autoload "$smlnj/init/init.cmi";
wenzelm@21298
    33
val pointer_eq = InlineT.ptreql;
paulson@16528
    34
wenzelm@16502
    35
wenzelm@4403
    36
(* restore old-style character / string functions *)
wenzelm@4403
    37
wenzelm@40627
    38
val ord = mk_int o SML90.ord;
wenzelm@40627
    39
val chr = SML90.chr o dest_int;
wenzelm@40627
    40
val raw_explode = SML90.explode;
paulson@10725
    41
val implode = SML90.implode;
wenzelm@4403
    42
wenzelm@4403
    43
wenzelm@4403
    44
(* New Jersey ML parameters *)
wenzelm@4403
    45
wenzelm@4403
    46
val _ =
wenzelm@21298
    47
 (Control.Print.printLength := 1000;
wenzelm@21298
    48
  Control.Print.printDepth := 350;
wenzelm@21298
    49
  Control.Print.stringDepth := 250;
wenzelm@21298
    50
  Control.Print.signatures := 2;
wenzelm@21298
    51
  Control.MC.matchRedundantError := false);
wenzelm@4403
    52
wenzelm@4403
    53
wenzelm@4403
    54
(* Poly/ML emulation *)
wenzelm@4403
    55
wenzelm@4403
    56
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
wenzelm@24329
    57
local
wenzelm@24599
    58
  val depth = ref (10: int);
wenzelm@24329
    59
in
wenzelm@56281
    60
  fun get_default_print_depth () = ! depth;
wenzelm@56285
    61
  fun default_print_depth n =
wenzelm@24329
    62
   (depth := n;
wenzelm@24329
    63
    Control.Print.printDepth := dest_int n div 2;
wenzelm@24329
    64
    Control.Print.printLength := dest_int n);
wenzelm@56285
    65
  val _ = default_print_depth 10;
wenzelm@24329
    66
end;
wenzelm@4403
    67
wenzelm@36162
    68
val ml_make_string = "(fn _ => \"?\")";
wenzelm@36162
    69
wenzelm@26474
    70
wenzelm@16660
    71
(*prompts*)
wenzelm@4977
    72
fun ml_prompts p1 p2 =
wenzelm@21298
    73
  (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
wenzelm@4977
    74
wenzelm@17511
    75
(*dummy implementation*)
wenzelm@16681
    76
fun profile (n: int) f x = f x;
wenzelm@16681
    77
wenzelm@17511
    78
(*dummy implementation*)
wenzelm@59055
    79
fun print_exception_trace (_: exn -> string) (_: string -> unit) f = f ();
wenzelm@4977
    80
wenzelm@21298
    81
wenzelm@4403
    82
(* ML command execution *)
wenzelm@4403
    83
wenzelm@30672
    84
fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
wenzelm@5090
    85
  let
wenzelm@21298
    86
    val ref out_orig = Control.Print.out;
wenzelm@5090
    87
wenzelm@5090
    88
    val out_buffer = ref ([]: string list);
wenzelm@5090
    89
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@10914
    90
    fun output () =
wenzelm@7890
    91
      let val str = implode (rev (! out_buffer))
wenzelm@10914
    92
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
wenzelm@5090
    93
  in
wenzelm@21298
    94
    Control.Print.out := out;
wenzelm@56435
    95
    Backend.Interact.useStream (TextIO.openString (tune_source (ml_positions line name txt)))
wenzelm@56435
    96
      handle exn =>
wenzelm@56435
    97
        (Control.Print.out := out_orig;
wenzelm@56435
    98
          error ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
wenzelm@21298
    99
    Control.Print.out := out_orig;
wenzelm@10914
   100
    if verbose then print (output ()) else ()
wenzelm@5090
   101
  end;
wenzelm@4403
   102
wenzelm@30672
   103
fun use_file context verbose name =
wenzelm@24599
   104
  let
wenzelm@24599
   105
    val instream = TextIO.openIn name;
wenzelm@26504
   106
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@30672
   107
  in use_text context (1, name) verbose txt end;
wenzelm@21770
   108
wenzelm@21770
   109
wenzelm@30626
   110
(* toplevel pretty printing *)
wenzelm@30626
   111
wenzelm@30626
   112
fun ml_pprint pps =
wenzelm@30626
   113
  let
wenzelm@30626
   114
    fun str "" = ()
wenzelm@30626
   115
      | str s = PrettyPrint.string pps s;
wenzelm@30626
   116
    fun pprint (ML_Pretty.Block ((bg, en), prts, ind)) =
wenzelm@30626
   117
          (str bg; PrettyPrint.openHOVBox pps (PrettyPrint.Rel (dest_int ind));
wenzelm@30626
   118
            List.app pprint prts; PrettyPrint.closeBox pps; str en)
wenzelm@30626
   119
      | pprint (ML_Pretty.String (s, _)) = str s
wenzelm@30626
   120
      | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
wenzelm@30626
   121
      | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps;
wenzelm@30626
   122
  in pprint end;
wenzelm@30626
   123
wenzelm@30672
   124
fun toplevel_pp context path pp =
wenzelm@30672
   125
  use_text context (1, "pp") false
wenzelm@30626
   126
    ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
wenzelm@30626
   127
      "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
wenzelm@30626
   128
wenzelm@30626
   129
wenzelm@4403
   130
wenzelm@5816
   131
(** interrupts **)
wenzelm@5816
   132
wenzelm@26084
   133
local
wenzelm@26084
   134
wenzelm@26084
   135
fun change_signal new_handler f x =
wenzelm@5816
   136
  let
wenzelm@26084
   137
    val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
wenzelm@26084
   138
    val result = Exn.capture (f old_handler) x;
wenzelm@12990
   139
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@23965
   140
  in Exn.release result end;
wenzelm@5816
   141
wenzelm@26084
   142
in
wenzelm@26084
   143
wenzelm@26084
   144
fun interruptible (f: 'a -> 'b) x =
wenzelm@12990
   145
  let
wenzelm@39232
   146
    val result = ref (Exn.interrupt_exn: 'b Exn.result);
wenzelm@12990
   147
    val old_handler = Signals.inqHandler Signals.sigINT;
wenzelm@5816
   148
  in
wenzelm@12990
   149
    SMLofNJ.Cont.callcc (fn cont =>
wenzelm@26084
   150
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
wenzelm@26084
   151
        result := Exn.capture f x));
wenzelm@12990
   152
    Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@26084
   153
    Exn.release (! result)
wenzelm@12990
   154
  end;
wenzelm@5816
   155
wenzelm@26084
   156
fun uninterruptible f =
wenzelm@26084
   157
  change_signal Signals.IGNORE
wenzelm@26084
   158
    (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
wenzelm@26084
   159
wenzelm@26084
   160
end;
wenzelm@26084
   161
wenzelm@21298
   162
wenzelm@39616
   163
use "ML-Systems/unsynchronized.ML";
wenzelm@39616
   164
wenzelm@48416
   165
wenzelm@48416
   166
(* ML system operations *)
wenzelm@48416
   167
wenzelm@43948
   168
use "ML-Systems/ml_system.ML";
wenzelm@43948
   169
wenzelm@48416
   170
structure ML_System =
wenzelm@48416
   171
struct
wenzelm@48416
   172
wenzelm@48416
   173
open ML_System;
wenzelm@48416
   174
wenzelm@48416
   175
fun save_state name =
wenzelm@48416
   176
  if SMLofNJ.exportML name then ()
wenzelm@48416
   177
  else OS.FileSys.rename {old = name ^ "." ^ platform, new = name};
wenzelm@48416
   178
wenzelm@48416
   179
end;
wenzelm@48416
   180