4 Test of advanced ML compiler invocation in Poly/ML 5.3. |
4 Test of advanced ML compiler invocation in Poly/ML 5.3. |
5 *) |
5 *) |
6 |
6 |
7 signature ML_TEST = |
7 signature ML_TEST = |
8 sig |
8 sig |
9 val get_result: Proof.context -> PolyML.parseTree list |
|
10 val eval: bool -> Position.T -> ML_Lex.token list -> unit |
9 val eval: bool -> Position.T -> ML_Lex.token list -> unit |
11 end |
10 end |
12 |
11 |
13 structure ML_Test: ML_TEST = |
12 structure ML_Test: ML_TEST = |
14 struct |
13 struct |
15 |
14 |
|
15 (* extra ML environment *) |
|
16 |
|
17 structure Extra_Env = GenericDataFun |
|
18 ( |
|
19 type T = Position.T Inttab.table; (*position of registered tokens*) |
|
20 val empty = Inttab.empty; |
|
21 val extend = I; |
|
22 fun merge _ = Inttab.merge (K true); |
|
23 ); |
|
24 |
|
25 fun inherit_env context = |
|
26 ML_Context.inherit_env context #> |
|
27 Extra_Env.put (Extra_Env.get context); |
|
28 |
|
29 fun register_tokens toks context = |
|
30 let |
|
31 val regs = map (fn tok => (serial (), tok)) toks; |
|
32 val context' = context |
|
33 |> Extra_Env.map (fold (fn (i, tok) => Inttab.update (i, ML_Lex.pos_of tok)) regs); |
|
34 in (regs, context') end; |
|
35 |
|
36 fun pos_of_location context |
|
37 ({file, startLine = line, startPosition = i, endPosition = j, ...}: location) = |
|
38 (case pairself (Inttab.lookup (Extra_Env.get context)) (i, j) of |
|
39 (SOME pos1, SOME pos2) => Position.encode_range (pos1, pos2) |
|
40 | (SOME pos, NONE) => pos |
|
41 | _ => Position.line_file line file); |
|
42 |
|
43 |
|
44 (* parse trees *) |
|
45 |
|
46 fun report_parse_tree context = |
|
47 let |
|
48 val pos_of = pos_of_location context; |
|
49 fun report loc (PTtype types) = (* FIXME body text *) |
|
50 Position.report Markup.ML_typing (pos_of loc) |
|
51 | report loc (PTdeclaredAt decl) = |
|
52 Markup.markup (Markup.properties (Position.properties_of (pos_of decl)) Markup.ML_def) "" |
|
53 |> Position.report_text Markup.ML_ref (pos_of loc) |
|
54 | report _ (PTnextSibling tree) = report_tree (tree ()) |
|
55 | report _ (PTfirstChild tree) = report_tree (tree ()) |
|
56 | report _ _ = () |
|
57 and report_tree (loc, props) = List.app (report loc) props; |
|
58 in report_tree end; |
|
59 |
|
60 |
16 (* eval ML source tokens *) |
61 (* eval ML source tokens *) |
17 |
62 |
18 structure Result = GenericDataFun |
|
19 ( |
|
20 type T = PolyML.parseTree list; |
|
21 val empty = []; |
|
22 fun extend _ = []; |
|
23 fun merge _ _ = []; |
|
24 ); |
|
25 |
|
26 val get_result = Result.get o Context.Proof; |
|
27 |
|
28 |
|
29 fun use_text ({name_space = space, print, error, ...}: use_context) verbose pos toks = |
63 fun use_text ({name_space = space, print, error, ...}: use_context) verbose pos toks = |
30 let |
64 let |
31 (* input *) |
65 (* input *) |
32 |
66 |
33 val input = toks |> map (fn tok => |
67 val input = Context.>>> (register_tokens toks); |
34 (serial (), (String.explode (ML_Lex.text_of tok), ML_Lex.pos_of tok))); |
68 val input_buffer = ref (map (apsnd (String.explode o ML_Lex.text_of)) input); |
35 val index_pos = Inttab.lookup (Inttab.make (map (apsnd snd) input)); |
69 |
36 |
|
37 fun pos_of ({file, startLine = line, startPosition = i, endPosition = j, ...}: location) = |
|
38 (case (index_pos i, index_pos j) of |
|
39 (SOME p, SOME q) => Position.encode_range (p, q) |
|
40 | (SOME p, NONE) => p |
|
41 | _ => Position.line_file line file); |
|
42 |
|
43 val in_buffer = ref (map (apsnd fst) input); |
|
44 val current_line = ref (the_default 1 (Position.line_of pos)); |
70 val current_line = ref (the_default 1 (Position.line_of pos)); |
|
71 |
|
72 fun get_index () = (case ! input_buffer of [] => 0 | (i, _) :: _ => i); |
45 fun get () = |
73 fun get () = |
46 (case ! in_buffer of |
74 (case ! input_buffer of |
47 [] => NONE |
75 [] => NONE |
48 | (_, []) :: rest => (in_buffer := rest; get ()) |
76 | (_, []) :: rest => (input_buffer := rest; get ()) |
49 | (i, c :: cs) :: rest => |
77 | (i, c :: cs) :: rest => |
50 (in_buffer := (i, cs) :: rest; |
78 (input_buffer := (i, cs) :: rest; |
51 if c = #"\n" then current_line := ! current_line + 1 else (); |
79 if c = #"\n" then current_line := ! current_line + 1 else (); |
52 SOME c)); |
80 SOME c)); |
53 fun get_index () = (case ! in_buffer of [] => 0 | (i, _) :: _ => i); |
|
54 |
81 |
55 |
82 |
56 (* output *) |
83 (* output *) |
57 |
84 |
58 val out_buffer = ref ([]: string list); |
85 val output_buffer = ref Buffer.empty; |
59 fun output () = implode (rev (! out_buffer)); |
86 fun output () = Buffer.content (! output_buffer); |
60 fun put s = out_buffer := s :: ! out_buffer; |
87 fun put s = change output_buffer (Buffer.add s); |
61 |
88 |
62 fun put_message {message, hard, location, context = _} = |
89 fun put_message {message, hard, location, context = _} = |
63 (put (if hard then "Error: " else "Warning: "); |
90 (put (if hard then "Error: " else "Warning: "); |
64 put (Pretty.string_of (Pretty.from_ML (pretty_ml message))); |
91 put (Pretty.string_of (Pretty.from_ML (pretty_ml message))); |
65 put (Position.str_of (pos_of location) ^ "\n")); |
92 put (Position.str_of (pos_of_location (the (Context.thread_data ())) location) ^ "\n")); |
66 |
93 |
67 |
94 |
68 (* results *) |
95 (* results *) |
69 |
96 |
70 fun apply_result {fixes, types, signatures, structures, functors, values} = |
97 fun apply_result {fixes, types, signatures, structures, functors, values} = |
110 List.app apply_struct structures; |
137 List.app apply_struct structures; |
111 List.app apply_funct functors; |
138 List.app apply_funct functors; |
112 List.app apply_val values |
139 List.app apply_val values |
113 end; |
140 end; |
114 |
141 |
115 fun result_fun (parse_tree, code) () = |
142 fun result_fun (phase1, phase2) () = |
116 (Context.>> (Result.map (append (the_list parse_tree))); |
143 (case phase1 of NONE => () |
117 (case code of NONE => error "Static Errors" | SOME result => apply_result (result ()))); |
144 | SOME parse_tree => report_parse_tree (the (Context.thread_data ())) parse_tree; |
|
145 case phase2 of NONE => error "Static Errors" |
|
146 | SOME code => apply_result (Toplevel.program code)); |
118 |
147 |
119 |
148 |
120 (* compiler invocation *) |
149 (* compiler invocation *) |
121 |
150 |
122 val parameters = |
151 val parameters = |
123 [PolyML.Compiler.CPOutStream put, |
152 [PolyML.Compiler.CPOutStream put, |
124 PolyML.Compiler.CPNameSpace space, |
153 PolyML.Compiler.CPNameSpace space, |
125 PolyML.Compiler.CPErrorMessageProc put_message, |
154 PolyML.Compiler.CPErrorMessageProc put_message, |
126 PolyML.Compiler.CPLineNo (fn () => ! current_line), |
155 PolyML.Compiler.CPLineNo (fn () => ! current_line), |
|
156 PolyML.Compiler.CPFileName (the_default "ML" (Position.file_of pos)), |
127 PolyML.Compiler.CPLineOffset get_index, |
157 PolyML.Compiler.CPLineOffset get_index, |
128 PolyML.Compiler.CPFileName (the_default "ML" (Position.file_of pos)), |
|
129 PolyML.Compiler.CPPrintInAlphabeticalOrder false, |
|
130 PolyML.Compiler.CPCompilerResultFun result_fun]; |
158 PolyML.Compiler.CPCompilerResultFun result_fun]; |
131 val _ = |
159 val _ = |
132 (while not (List.null (! in_buffer)) do |
160 (while not (List.null (! input_buffer)) do |
133 PolyML.compiler (get, parameters) ()) |
161 PolyML.compiler (get, parameters) ()) |
134 handle exn => |
162 handle exn => |
135 (put ("Exception- " ^ General.exnMessage exn ^ " raised"); |
163 (put ("Exception- " ^ General.exnMessage exn ^ " raised"); |
136 error (output ()); raise exn); |
164 error (output ()); raise exn); |
137 in if verbose then print (output ()) else () end; |
165 in if verbose then print (output ()) else () end; |
147 val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos); |
175 val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos); |
148 val ((env, body), env_ctxt) = ML_Context.eval_antiquotes (ants, pos) (Context.thread_data ()); |
176 val ((env, body), env_ctxt) = ML_Context.eval_antiquotes (ants, pos) (Context.thread_data ()); |
149 |
177 |
150 val _ = Context.setmp_thread_data env_ctxt |
178 val _ = Context.setmp_thread_data env_ctxt |
151 (fn () => (eval false Position.none env; Context.thread_data ())) () |
179 (fn () => (eval false Position.none env; Context.thread_data ())) () |
152 |> (fn NONE => () | SOME context' => Context.>> (ML_Context.inherit_env context')); |
180 |> (fn NONE => () | SOME context' => Context.>> (inherit_env context')); |
153 val _ = eval true pos body; |
181 val _ = eval true pos body; |
154 val _ = eval false Position.none (ML_Lex.tokenize "structure Isabelle = struct end"); |
182 val _ = eval false Position.none (ML_Lex.tokenize "structure Isabelle = struct end"); |
155 in () end; |
183 in () end; |
156 |
184 |
157 |
185 |
158 local structure P = OuterParse and K = OuterKeyword in |
186 local structure P = OuterParse and K = OuterKeyword in |
159 |
187 |
160 fun inherit_env (context as Context.Proof lthy) = |
188 fun propagate_env (context as Context.Proof lthy) = |
161 Context.Proof (LocalTheory.map_contexts (ML_Context.inherit_env context) lthy) |
189 Context.Proof (LocalTheory.map_contexts (inherit_env context) lthy) |
162 | inherit_env context = context; |
190 | propagate_env context = context; |
163 |
191 |
164 val _ = |
192 val _ = |
165 OuterSyntax.command "ML_test" "advanced ML compiler test" (K.tag_ml K.thy_decl) |
193 OuterSyntax.command "ML_test" "advanced ML compiler test" (K.tag_ml K.thy_decl) |
166 (P.ML_source >> (fn src => |
194 (P.ML_source >> (fn src => |
167 Toplevel.generic_theory (ML_Context.exec (fn () => ML_test src) #> inherit_env))); |
195 Toplevel.generic_theory (ML_Context.exec (fn () => ML_test src) #> propagate_env))); |
168 |
196 |
169 end; |
197 end; |
170 |
198 |
171 end; |
199 end; |
172 |
200 |