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