author | blanchet |
Thu, 27 Sep 2012 18:39:17 +0200 | |
changeset 49622 | a93f976c3307 |
parent 47980 | c81801f881b3 |
child 50910 | 54f06ba192ef |
permissions | -rw-r--r-- |
47980
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
wenzelm
parents:
41717
diff
changeset
|
1 |
(* Title: Pure/ML-Systems/compiler_polyml.ML |
31312 | 2 |
|
47980
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
wenzelm
parents:
41717
diff
changeset
|
3 |
Runtime compilation for Poly/ML 5.3.0 or later. |
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
wenzelm
parents:
41717
diff
changeset
|
4 |
|
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
wenzelm
parents:
41717
diff
changeset
|
5 |
See also Pure/ML/ml_compiler_polyml.ML for advanced version. |
31312 | 6 |
*) |
7 |
||
8 |
local |
|
9 |
||
10 |
fun drop_newline s = |
|
11 |
if String.isSuffix "\n" s then String.substring (s, 0, size s - 1) |
|
12 |
else s; |
|
13 |
||
14 |
in |
|
15 |
||
16 |
fun use_text ({tune_source, name_space, str_of_pos, print, error, ...}: use_context) |
|
17 |
(start_line, name) verbose txt = |
|
18 |
let |
|
32738 | 19 |
val line = Unsynchronized.ref start_line; |
20 |
val in_buffer = Unsynchronized.ref (String.explode (tune_source txt)); |
|
21 |
val out_buffer = Unsynchronized.ref ([]: string list); |
|
31312 | 22 |
fun output () = drop_newline (implode (rev (! out_buffer))); |
23 |
||
24 |
fun get () = |
|
25 |
(case ! in_buffer of |
|
26 |
[] => NONE |
|
31471 | 27 |
| c :: cs => (in_buffer := cs; if c = #"\n" then line := ! line + 1 else (); SOME c)); |
31312 | 28 |
fun put s = out_buffer := s :: ! out_buffer; |
29 |
fun put_message {message = msg1, hard, location = {startLine = line, ...}, context} = |
|
30 |
(put (if hard then "Error: " else "Warning: "); |
|
31 |
PolyML.prettyPrint (put, 76) msg1; |
|
32 |
(case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2); |
|
33 |
put ("At" ^ str_of_pos line name ^ "\n")); |
|
34 |
||
35 |
val parameters = |
|
36 |
[PolyML.Compiler.CPOutStream put, |
|
31471 | 37 |
PolyML.Compiler.CPNameSpace name_space, |
31312 | 38 |
PolyML.Compiler.CPErrorMessageProc put_message, |
31471 | 39 |
PolyML.Compiler.CPLineNo (fn () => ! line), |
40 |
PolyML.Compiler.CPFileName name, |
|
31312 | 41 |
PolyML.Compiler.CPPrintInAlphabeticalOrder false]; |
42 |
val _ = |
|
43 |
(while not (List.null (! in_buffer)) do |
|
44 |
PolyML.compiler (get, parameters) ()) |
|
45 |
handle exn => |
|
39242
28d3809ff91f
primitive use_text: let interrupts pass unhindered;
wenzelm
parents:
38470
diff
changeset
|
46 |
if Exn.is_interrupt exn then reraise exn |
28d3809ff91f
primitive use_text: let interrupts pass unhindered;
wenzelm
parents:
38470
diff
changeset
|
47 |
else |
28d3809ff91f
primitive use_text: let interrupts pass unhindered;
wenzelm
parents:
38470
diff
changeset
|
48 |
(put ("Exception- " ^ General.exnMessage exn ^ " raised"); |
28d3809ff91f
primitive use_text: let interrupts pass unhindered;
wenzelm
parents:
38470
diff
changeset
|
49 |
error (output ()); reraise exn); |
31312 | 50 |
in if verbose then print (output ()) else () end; |
51 |
||
52 |
fun use_file context verbose name = |
|
53 |
let |
|
54 |
val instream = TextIO.openIn name; |
|
55 |
val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream); |
|
56 |
in use_text context (1, name) verbose txt end; |
|
57 |
||
58 |
end; |
|
59 |