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