src/Pure/ML-Systems/polyml.ML
author wenzelm
Thu Sep 04 16:03:47 2008 +0200 (2008-09-04)
changeset 28124 10a1f1f4c6ae
parent 26883 ae6ae88f9240
child 28125 e99427bcf7f3
permissions -rw-r--r--
moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm@2341
     1
(*  Title:      Pure/ML-Systems/polyml.ML
wenzelm@2341
     2
    ID:         $Id$
wenzelm@2341
     3
wenzelm@26379
     4
Compatibility wrapper for Poly/ML (after 5.1).
wenzelm@2341
     5
*)
wenzelm@2341
     6
wenzelm@26215
     7
use "ML-Systems/polyml_common.ML";
wenzelm@26215
     8
use "ML-Systems/multithreading_polyml.ML";
wenzelm@26215
     9
wenzelm@26215
    10
val pointer_eq = PolyML.pointerEq;
wenzelm@23921
    11
wenzelm@25023
    12
wenzelm@26390
    13
(* runtime compilation *)
wenzelm@3588
    14
wenzelm@26883
    15
local
wenzelm@26883
    16
wenzelm@26883
    17
fun drop_newline s =
wenzelm@26883
    18
  if String.isSuffix "\n" s then String.substring (s, 0, size s - 1)
wenzelm@26883
    19
  else s;
wenzelm@26883
    20
wenzelm@26883
    21
in
wenzelm@26883
    22
wenzelm@26883
    23
fun use_text (tune: string -> string) str_of_pos (start_line, name) (print, err) verbose txt =
wenzelm@3588
    24
  let
wenzelm@26883
    25
    val current_line = ref start_line;
wenzelm@26379
    26
    val in_buffer = ref (String.explode (tune txt));
wenzelm@5090
    27
    val out_buffer = ref ([]: string list);
wenzelm@26883
    28
    fun output () = drop_newline (implode (rev (! out_buffer)));
wenzelm@5090
    29
wenzelm@5038
    30
    fun get () =
wenzelm@5090
    31
      (case ! in_buffer of
wenzelm@26379
    32
        [] => NONE
wenzelm@26385
    33
      | c :: cs =>
wenzelm@26385
    34
          (in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c));
wenzelm@5090
    35
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@26883
    36
    fun message (msg, is_err, line) =
wenzelm@26883
    37
      (if is_err then "Error: " else "Warning: ") ^ drop_newline msg ^ str_of_pos line name ^ "\n";
wenzelm@5090
    38
wenzelm@26879
    39
    val parameters =
wenzelm@26879
    40
     [PolyML.Compiler.CPOutStream put,
wenzelm@26879
    41
      PolyML.Compiler.CPLineNo (fn () => ! current_line),
wenzelm@26883
    42
      PolyML.Compiler.CPErrorMessageProc (put o message)];
wenzelm@26379
    43
    val _ =
wenzelm@26379
    44
      (while not (List.null (! in_buffer)) do
wenzelm@26879
    45
        PolyML.compiler (get, parameters) ())
wenzelm@26625
    46
      handle exn =>
wenzelm@26625
    47
       (put ("Exception- " ^ General.exnMessage exn ^ " raised");
wenzelm@26625
    48
        err (output ()); raise exn);
wenzelm@26883
    49
  in if verbose then print (output ()) else () end;
wenzelm@3588
    50
wenzelm@26883
    51
fun use_file tune str_of_pos output verbose name =
wenzelm@24598
    52
  let
wenzelm@24598
    53
    val instream = TextIO.openIn name;
wenzelm@26504
    54
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@26883
    55
  in use_text tune str_of_pos (1, name) output verbose txt end;
wenzelm@26379
    56
wenzelm@26883
    57
end;
wenzelm@26883
    58