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