1 (* Title: Pure/ML/ml_compiler.ML
4 Runtime compilation and evaluation.
7 signature ML_COMPILER =
10 {environment: string, redirect: bool, verbose: bool,
11 debug: bool option, writeln: string -> unit, warning: string -> unit}
12 val debug_flags: bool option -> flags
14 val verbose: bool -> flags -> flags
15 val eval: flags -> Position.T -> ML_Lex.token list -> unit
18 structure ML_Compiler: ML_COMPILER =
24 {environment: string, redirect: bool, verbose: bool,
25 debug: bool option, writeln: string -> unit, warning: string -> unit};
27 fun debug_flags opt_debug : flags =
28 {environment = "", redirect = false, verbose = false,
29 debug = opt_debug, writeln = writeln, warning = warning};
31 val flags = debug_flags NONE;
33 fun verbose b (flags: flags) =
34 {environment = #environment flags, redirect = #redirect flags, verbose = b,
35 debug = #debug flags, writeln = #writeln flags, warning = #warning flags};
40 fun breakpoint_position loc =
41 let val pos = Position.no_range_position (Exn_Properties.position_of_polyml_location loc) in
42 (case Position.offset_of pos of
46 Position.properties_of pos
47 |> Properties.put (Markup.offsetN, Value.print_int (j - 1))
48 |> Position.of_properties)
51 fun report_parse_tree redirect depth name_space parse_tree =
54 (case Context.get_generic_context () of
55 SOME context => Context_Position.is_visible_generic context
57 fun is_reported pos = is_visible andalso Position.is_reported pos;
62 fun reported_types loc types =
63 let val pos = Exn_Properties.position_of_polyml_location loc in
67 PolyML.NameSpace.Values.printType (types, depth, SOME name_space)
68 |> Pretty.from_polyml |> Pretty.string_of
69 |> Output.output |> YXML.parse_body;
70 in cons (pos, fn () => Markup.ML_typing, fn () => YXML.string_of_body xml) end
73 fun reported_entity kind loc decl =
75 val pos = Exn_Properties.position_of_polyml_location loc;
76 val def_pos = Exn_Properties.position_of_polyml_location decl;
78 (is_reported pos andalso pos <> def_pos) ?
81 (Markup.entityN, (Markup.kindN, kind) :: Position.def_properties_of def_pos);
82 in cons (pos, markup, fn () => "") end
85 fun reported_entity_id def id loc =
87 val pos = Exn_Properties.position_of_polyml_location loc;
89 (is_reported pos andalso id <> 0) ?
92 (Markup.entityN, [(if def then Markup.defN else Markup.refN, Value.print_int id)]);
93 in cons (pos, markup, fn () => "") end
96 fun reported_completions loc names =
97 let val pos = Exn_Properties.position_of_polyml_location loc in
98 if is_reported pos andalso not (null names) then
100 val completion = Completion.names pos (map (fn a => (a, ("ML", a))) names);
101 val xml = Completion.encode completion;
102 in cons (pos, fn () => Markup.completion, fn () => YXML.string_of_body xml) end
106 fun reported _ (PolyML.PTnextSibling tree) = reported_tree (tree ())
107 | reported _ (PolyML.PTfirstChild tree) = reported_tree (tree ())
108 | reported loc (PolyML.PTdefId id) = reported_entity_id true (FixedInt.toLarge id) loc
109 | reported loc (PolyML.PTrefId id) = reported_entity_id false (FixedInt.toLarge id) loc
110 | reported loc (PolyML.PTtype types) = reported_types loc types
111 | reported loc (PolyML.PTdeclaredAt decl) = reported_entity Markup.ML_defN loc decl
112 | reported loc (PolyML.PTcompletions names) = reported_completions loc names
114 and reported_tree (loc, props) = fold (reported loc) props;
116 val persistent_reports = reported_tree parse_tree [];
120 |> map (fn (pos, markup, text) => Position.reported_text pos (markup ()) (text ()))
122 ((** )@{print} {a = "### ML_Compiler.output"};( *..NOT yet available 2 *)
123 (**)writeln "### ML_Compiler.output";(**)
126 if not (null persistent_reports) andalso redirect andalso Future.enabled ()
129 {name = "ML_Compiler.report", pos = Position.thread_data (), pri = Task_Queue.urgent_pri}
136 fun breakpoints _ (PolyML.PTnextSibling tree) = breakpoints_tree (tree ())
137 | breakpoints _ (PolyML.PTfirstChild tree) = breakpoints_tree (tree ())
138 | breakpoints loc (PolyML.PTbreakPoint b) =
139 let val pos = breakpoint_position loc in
140 if is_reported pos then
141 let val id = serial ();
142 in cons ((pos, Markup.ML_breakpoint id), (id, (b, pos))) end
145 | breakpoints _ _ = I
146 and breakpoints_tree (loc, props) = fold (breakpoints loc) props;
148 val all_breakpoints = rev (breakpoints_tree parse_tree []);
149 val _ = Position.reports (map #1 all_breakpoints);
150 in map (fn (_, (id, (b, pos))) => (id, (b, Position.dest pos))) all_breakpoints end;
153 (* eval ML source tokens *)
155 fun eval (flags: flags) pos toks =
157 val opt_context = Context.get_generic_context ();
159 val env as {debug, name_space, add_breakpoints} =
160 (case (ML_Recursive.get (), #environment flags <> "") of
161 (SOME env, false) => env
164 (case #debug flags of
166 | NONE => ML_Options.debugger_enabled opt_context),
167 name_space = ML_Env.make_name_space (#environment flags),
168 add_breakpoints = ML_Env.add_breakpoints});
173 val location_props = op ^ (YXML.output_markup (":", #props (Position.dest pos)));
175 val {explode_token, ...} = ML_Env.operations opt_context (#environment flags);
176 fun token_content tok = if ML_Lex.is_comment tok then NONE else SOME (`explode_token tok);
179 Unsynchronized.ref (map_filter token_content toks);
182 (case ! input_buffer of
183 (c :: cs, tok) :: rest => (input_buffer := (cs, tok) :: rest; SOME c)
184 | ([], _) :: rest => (input_buffer := rest; SOME #" ")
188 (case ! input_buffer of
189 (_ :: _, tok) :: _ => ML_Lex.pos_of tok
190 | ([], tok) :: _ => ML_Lex.end_pos_of tok
191 | [] => Position.none);
196 val writeln_buffer = Unsynchronized.ref Buffer.empty;
197 fun write s = Unsynchronized.change writeln_buffer (Buffer.add s);
198 fun output_writeln () = #writeln flags (trim_line (Buffer.content (! writeln_buffer)));
200 val warnings = Unsynchronized.ref ([]: string list);
201 fun warn msg = Unsynchronized.change warnings (cons msg);
202 fun output_warnings () = List.app (#warning flags) (rev (! warnings));
204 val error_buffer = Unsynchronized.ref Buffer.empty;
205 fun err msg = Unsynchronized.change error_buffer (Buffer.add msg #> Buffer.add "\n");
206 fun flush_error () = #writeln flags (trim_line (Buffer.content (! error_buffer)));
207 fun raise_error msg = error (trim_line (Buffer.content (Buffer.add msg (! error_buffer))));
209 fun message {message = msg, hard, location = loc, context = _} =
211 val pos = Exn_Properties.position_of_polyml_location loc;
213 (if hard then "ML error" else "ML warning") ^ Position.here pos ^ ":\n" ^
214 Pretty.string_of (Pretty.from_polyml msg);
215 in if hard then err txt else warn txt end;
220 val depth = FixedInt.fromInt (ML_Print_Depth.get_print_depth ());
222 fun apply_result {fixes, types, signatures, structures, functors, values} =
226 (write (disp x |> Pretty.from_polyml |> Pretty.string_of); write "\n")
229 fun apply_fix (a, b) =
230 (#enterFix name_space (a, b);
231 display PolyML.NameSpace.Infixes.print b);
232 fun apply_type (a, b) =
233 (#enterType name_space (a, b);
234 display PolyML.NameSpace.TypeConstrs.print (b, depth, SOME name_space));
235 fun apply_sig (a, b) =
236 (#enterSig name_space (a, b);
237 display PolyML.NameSpace.Signatures.print (b, depth, SOME name_space));
238 fun apply_struct (a, b) =
239 (#enterStruct name_space (a, b);
240 display PolyML.NameSpace.Structures.print (b, depth, SOME name_space));
241 fun apply_funct (a, b) =
242 (#enterFunct name_space (a, b);
243 display PolyML.NameSpace.Functors.print (b, depth, SOME name_space));
244 fun apply_val (a, b) =
245 (#enterVal name_space (a, b);
246 display PolyML.NameSpace.Values.printWithType (b, depth, SOME name_space));
248 List.app apply_fix fixes;
249 List.app apply_type types;
250 List.app apply_sig signatures;
251 List.app apply_struct structures;
252 List.app apply_funct functors;
253 List.app apply_val values
256 exception STATIC_ERRORS of unit;
258 fun result_fun (phase1, phase2) () =
262 add_breakpoints (report_parse_tree (#redirect flags) depth name_space parse_tree));
264 NONE => raise STATIC_ERRORS ()
268 |> Runtime.debugging opt_context
269 |> Runtime.toplevel_error (err o Runtime.exn_message)) ())));
272 (* compiler invocation *)
275 [PolyML.Compiler.CPOutStream write,
276 PolyML.Compiler.CPNameSpace name_space,
277 PolyML.Compiler.CPErrorMessageProc message,
278 PolyML.Compiler.CPLineNo (the_default 0 o Position.line_of o get_pos),
279 PolyML.Compiler.CPLineOffset (the_default 0 o Position.offset_of o get_pos),
280 PolyML.Compiler.CPFileName location_props,
281 PolyML.Compiler.CPPrintDepth ML_Print_Depth.get_print_depth,
282 PolyML.Compiler.CPCompilerResultFun result_fun,
283 PolyML.Compiler.CPPrintInAlphabeticalOrder false,
284 PolyML.Compiler.CPDebug debug,
285 PolyML.Compiler.CPBindingSeq serial];
288 (while not (List.null (! input_buffer)) do
289 ML_Recursive.recursive env (fn () => PolyML.compiler (get, parameters) ()))
291 if Exn.is_interrupt exn then Exn.reraise exn
296 STATIC_ERRORS () => ""
297 | Runtime.TOPLEVEL_ERROR => ""
298 | _ => "Exception- " ^ Pretty.string_of (Runtime.pretty_exn exn) ^ " raised");
299 val _ = output_warnings ();
300 val _ = output_writeln ();
301 in raise_error exn_msg end;
303 if #verbose flags then (output_warnings (); flush_error (); output_writeln ())