author | wenzelm |
Fri, 18 Mar 2016 17:58:19 +0100 | |
changeset 62668 | 360d3464919c |
parent 62662 | 291cc01f56f5 |
child 62716 | d80b9f4990e4 |
permissions | -rw-r--r-- |
62508
d0b68218ea55
discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents:
62505
diff
changeset
|
1 |
(* Title: Pure/ML/ml_compiler0.ML |
31312 | 2 |
|
62494 | 3 |
Runtime compilation and evaluation (bootstrap version of |
4 |
Pure/ML/ml_compiler.ML). |
|
31312 | 5 |
*) |
6 |
||
62494 | 7 |
signature ML_COMPILER0 = |
8 |
sig |
|
9 |
type context = |
|
10 |
{name_space: ML_Name_Space.T, |
|
11 |
here: int -> string -> string, |
|
12 |
print: string -> unit, |
|
13 |
error: string -> unit} |
|
14 |
val use_text: context -> {debug: bool, file: string, line: int, verbose: bool} -> string -> unit |
|
15 |
val use_file: context -> {debug: bool, verbose: bool} -> string -> unit |
|
16 |
val debug_option: bool option -> bool |
|
17 |
val use_operations: (bool option -> string -> unit) -> |
|
18 |
{use: string -> unit, use_debug: string -> unit, use_no_debug: string -> unit} |
|
19 |
end; |
|
20 |
||
21 |
structure ML_Compiler0: ML_COMPILER0 = |
|
22 |
struct |
|
23 |
||
24 |
type context = |
|
62490
39d01eaf5292
ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents:
62489
diff
changeset
|
25 |
{name_space: ML_Name_Space.T, |
62493 | 26 |
here: int -> string -> string, |
62490
39d01eaf5292
ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents:
62489
diff
changeset
|
27 |
print: string -> unit, |
39d01eaf5292
ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents:
62489
diff
changeset
|
28 |
error: string -> unit}; |
39d01eaf5292
ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents:
62489
diff
changeset
|
29 |
|
31312 | 30 |
fun drop_newline s = |
31 |
if String.isSuffix "\n" s then String.substring (s, 0, size s - 1) |
|
32 |
else s; |
|
33 |
||
62529
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
62508
diff
changeset
|
34 |
fun ml_input start_line name txt = |
62504 | 35 |
let |
62662
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
36 |
fun input line (#"@" :: #"{" :: #"h" :: #"e" :: #"r" :: #"e" :: #"}" :: cs) res = |
62504 | 37 |
let val s = "(Position.line_file_only " ^ Int.toString line ^ " \"" ^ name ^ "\")" |
62662
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
38 |
in input line cs (s :: res) end |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
39 |
| input line (#"@" :: #"{" :: #"m" :: #"a" :: #"k" :: #"e" :: #"_" :: |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
40 |
#"s" :: #"t" :: #"r" :: #"i" :: #"n" :: #"g" :: #"}" :: cs) res = |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
41 |
input line cs (ML_Pretty.make_string_fn "" :: res) |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
42 |
| input line (#"\\" :: #"<" :: cs) res = input line cs ("\\\\<" :: res) |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
43 |
| input line (#"\n" :: cs) res = input (line + 1) cs ("\n" :: res) |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
44 |
| input line (c :: cs) res = input line cs (str c :: res) |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
45 |
| input _ [] res = rev res; |
291cc01f56f5
@{make_string} is available during Pure bootstrap;
wenzelm
parents:
62529
diff
changeset
|
46 |
in String.concat (input start_line (String.explode txt) []) end; |
62504 | 47 |
|
62494 | 48 |
fun use_text ({name_space, here, print, error, ...}: context) {line, file, verbose, debug} text = |
31312 | 49 |
let |
60955 | 50 |
val current_line = Unsynchronized.ref line; |
62529
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
62508
diff
changeset
|
51 |
val in_buffer = Unsynchronized.ref (String.explode (ml_input line file text)); |
32738 | 52 |
val out_buffer = Unsynchronized.ref ([]: string list); |
31312 | 53 |
fun output () = drop_newline (implode (rev (! out_buffer))); |
54 |
||
55 |
fun get () = |
|
56 |
(case ! in_buffer of |
|
57 |
[] => NONE |
|
60955 | 58 |
| c :: cs => |
59 |
(in_buffer := cs; if c = #"\n" then current_line := ! current_line + 1 else (); SOME c)); |
|
31312 | 60 |
fun put s = out_buffer := s :: ! out_buffer; |
60955 | 61 |
fun put_message {message = msg1, hard, location = {startLine = message_line, ...}, context} = |
31312 | 62 |
(put (if hard then "Error: " else "Warning: "); |
63 |
PolyML.prettyPrint (put, 76) msg1; |
|
64 |
(case context of NONE => () | SOME msg2 => PolyML.prettyPrint (put, 76) msg2); |
|
62493 | 65 |
put ("At" ^ here (FixedInt.toInt message_line) file ^ "\n")); |
31312 | 66 |
|
67 |
val parameters = |
|
68 |
[PolyML.Compiler.CPOutStream put, |
|
31471 | 69 |
PolyML.Compiler.CPNameSpace name_space, |
31312 | 70 |
PolyML.Compiler.CPErrorMessageProc put_message, |
60955 | 71 |
PolyML.Compiler.CPLineNo (fn () => ! current_line), |
72 |
PolyML.Compiler.CPFileName file, |
|
62501 | 73 |
PolyML.Compiler.CPPrintInAlphabeticalOrder false, |
74 |
PolyML.Compiler.CPDebug debug]; |
|
31312 | 75 |
val _ = |
76 |
(while not (List.null (! in_buffer)) do |
|
77 |
PolyML.compiler (get, parameters) ()) |
|
78 |
handle exn => |
|
62505 | 79 |
if Exn.is_interrupt exn then Exn.reraise exn |
39242
28d3809ff91f
primitive use_text: let interrupts pass unhindered;
wenzelm
parents:
38470
diff
changeset
|
80 |
else |
28d3809ff91f
primitive use_text: let interrupts pass unhindered;
wenzelm
parents:
38470
diff
changeset
|
81 |
(put ("Exception- " ^ General.exnMessage exn ^ " raised"); |
62505 | 82 |
error (output ()); Exn.reraise exn); |
31312 | 83 |
in if verbose then print (output ()) else () end; |
84 |
||
60956 | 85 |
fun use_file context {verbose, debug} file = |
31312 | 86 |
let |
60955 | 87 |
val instream = TextIO.openIn file; |
60956 | 88 |
val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream); |
89 |
in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end; |
|
31312 | 90 |
|
62490
39d01eaf5292
ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents:
62489
diff
changeset
|
91 |
|
62494 | 92 |
fun debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true" |
93 |
| debug_option (SOME debug) = debug; |
|
62490
39d01eaf5292
ML debugger support in Pure (again, see 3565c9f407ec);
wenzelm
parents:
62489
diff
changeset
|
94 |
|
62493 | 95 |
fun use_operations (use_ : bool option -> string -> unit) = |
96 |
{use = use_ NONE, use_debug = use_ (SOME true), use_no_debug = use_ (SOME false)}; |
|
62489 | 97 |
|
62494 | 98 |
end; |
99 |
||
62493 | 100 |
val {use, use_debug, use_no_debug} = |
62494 | 101 |
let |
102 |
val context: ML_Compiler0.context = |
|
103 |
{name_space = ML_Name_Space.global, |
|
104 |
here = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")", |
|
105 |
print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut), |
|
106 |
error = fn s => error s}; |
|
107 |
in |
|
108 |
ML_Compiler0.use_operations (fn opt_debug => fn file => |
|
109 |
ML_Compiler0.use_file context |
|
110 |
{verbose = true, debug = ML_Compiler0.debug_option opt_debug} file |
|
111 |
handle ERROR msg => (#print context msg; raise error "ML error")) |
|
112 |
end; |