src/Pure/ML-Systems/polyml.ML
author wenzelm
Tue, 16 Jan 2001 00:35:50 +0100
changeset 10914 aded4ba99b88
parent 8830 3e95f3a90875
child 11078 c1b32e7124b4
permissions -rw-r--r--
use_text etc.: proper output of error messages;
     1 (*  Title:      Pure/ML-Systems/polyml.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1991  University of Cambridge
     5 
     6 Compatibility file for Poly/ML (versions 2.x and 3.x).
     7 *)
     8 
     9 open PolyML ExtendedIO;
    10 
    11 (*needs the Basis Library emulation*)
    12 use "basis.ML";
    13 
    14 
    15 structure String =
    16 struct
    17   fun substring (s,i,j) = StringBuiltIns.substring (s, i+1, j)
    18                           handle Substring => raise Subscript
    19   fun isPrefix s1 s2 = (s1 = substring(s2,0, size s1))
    20                        handle Subscript => false
    21 end;
    22 
    23 
    24 (** ML system related **)
    25 
    26 (** Compiler-independent timing functions **)
    27 
    28 (*Note start point for timing*)
    29 fun startTiming() = System.processtime ();
    30 
    31 (*Finish timing and return string*)
    32 fun endTiming before =
    33   "User + GC: " ^
    34   makestring (real (System.processtime () - before) / 10.0) ^
    35   " secs";
    36 
    37 
    38 (* prompts *)
    39 
    40 fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
    41 
    42 
    43 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    44 
    45 fun make_pp _ pprint (str, blk, brk, en) obj =
    46   pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
    47     fn () => brk (99999, 0), en);
    48 
    49 
    50 (* ML command execution -- 'eval' *)
    51 
    52 local
    53 
    54 fun drop_last [] = []
    55   | drop_last [x] = []
    56   | drop_last (x :: xs) = x :: drop_last xs;
    57 
    58 val drop_last_char = implode o drop_last o explode;
    59 
    60 in
    61 
    62 fun use_text (print, err) verbose txt =
    63   let
    64     val in_buffer = ref (explode txt);
    65     val out_buffer = ref ([]: string list);
    66     fun output () = drop_last_char (implode (rev (! out_buffer)));
    67 
    68     fun get () =
    69       (case ! in_buffer of
    70         [] => ""
    71       | c :: cs => (in_buffer := cs; c));
    72     fun put s = out_buffer := s :: ! out_buffer;
    73 
    74     fun exec () =
    75       (case ! in_buffer of
    76         [] => ()
    77       | _ => (PolyML.compiler (get, put) (); exec ()));
    78   in
    79     exec () handle exn => (err (output ()); raise exn);
    80     if verbose then print (output ()) else ()
    81   end;
    82 
    83 
    84 
    85 (** interrupts **)      (*Note: may get into race conditions*)
    86 
    87 fun mask_interrupt f x = f x;
    88 fun unmask_interrupt f x = f x;
    89 fun exhibit_interrupt f x = f x;
    90 
    91 
    92 
    93 (** OS related **)
    94 
    95 (* system command execution *)
    96 
    97 (*execute Unix command which doesn't take any input from stdin and
    98   sends its output to stdout*)
    99 fun execute command =
   100   let
   101     val (is, os) =  ExtendedIO.execute command;
   102     val _ = close_out os;
   103     val result = input (is, 999999);
   104   in close_in is; result end;
   105 
   106 fun system cmd = (print (execute cmd); 0);	(* FIXME rc not available *)
   107 
   108 
   109 (* file handling *)
   110 
   111 (*get time of last modification*)
   112 fun file_info name = Int.toString (System.filedate name) handle _ => "";
   113 
   114 structure OS =
   115 struct
   116   structure FileSys =
   117   struct
   118     val chDir = PolyML.cd;
   119     fun remove name = (execute ("rm " ^ name); ());
   120     fun getDir () = drop_last_char (execute "pwd");
   121   end;
   122 end;
   123 
   124 
   125 (* getenv *)
   126 
   127 fun getenv var = drop_last_char
   128   (execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
   129 
   130 
   131 end;
   132 
   133 
   134 (* non-ASCII input (see also Thy/use.ML) *)
   135 
   136 val needs_filtered_use = true;