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