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