| author | haftmann | 
| Mon, 23 Mar 2015 19:05:14 +0100 | |
| changeset 59816 | 034b13f4efae | 
| parent 59110 | 8a78c7cb5b14 | 
| child 60610 | f52b4b0c10c4 | 
| permissions | -rw-r--r-- | 
| 47980 
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
 wenzelm parents: 
45666diff
changeset | 1 | (* Title: Pure/ML/ml_compiler_polyml.ML | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 3 | |
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56281diff
changeset | 4 | Runtime compilation and evaluation -- Poly/ML version. | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 5 | *) | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 6 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 7 | structure ML_Compiler: ML_COMPILER = | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 8 | struct | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 9 | |
| 56281 | 10 | open ML_Compiler; | 
| 11 | ||
| 12 | ||
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 13 | (* parse trees *) | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 14 | |
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 15 | fun report_parse_tree redirect depth space parse_tree = | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 16 | let | 
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 17 | val is_visible = | 
| 38720 
7f8bc335e203
ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
 wenzelm parents: 
38228diff
changeset | 18 | (case Context.thread_data () of | 
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 19 | SOME context => Context_Position.is_visible_generic context | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 20 | | NONE => true); | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 21 | fun is_reported pos = is_visible andalso Position.is_reported pos; | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 22 | |
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 23 | fun reported_types loc types = | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 24 | let val pos = Exn_Properties.position_of loc in | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 25 | is_reported pos ? | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 26 | let | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 27 | val xml = | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 28 | PolyML.NameSpace.displayTypeExpression (types, depth, space) | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 29 | |> pretty_ml |> Pretty.from_ML |> Pretty.string_of | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 30 | |> Output.output |> YXML.parse_body; | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 31 | in cons (pos, fn () => Markup.ML_typing, fn () => YXML.string_of_body xml) end | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 32 | end; | 
| 38720 
7f8bc335e203
ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
 wenzelm parents: 
38228diff
changeset | 33 | |
| 44737 | 34 | fun reported_entity kind loc decl = | 
| 58991 
92b6f4e68c5a
more careful ML source positions, for improved PIDE markup;
 wenzelm parents: 
56618diff
changeset | 35 | let | 
| 
92b6f4e68c5a
more careful ML source positions, for improved PIDE markup;
 wenzelm parents: 
56618diff
changeset | 36 | val pos = Exn_Properties.position_of loc; | 
| 
92b6f4e68c5a
more careful ML source positions, for improved PIDE markup;
 wenzelm parents: 
56618diff
changeset | 37 | val def_pos = Exn_Properties.position_of decl; | 
| 
92b6f4e68c5a
more careful ML source positions, for improved PIDE markup;
 wenzelm parents: 
56618diff
changeset | 38 | in | 
| 
92b6f4e68c5a
more careful ML source positions, for improved PIDE markup;
 wenzelm parents: 
56618diff
changeset | 39 | (is_reported pos andalso pos <> def_pos) ? | 
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 40 | let | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 41 | fun markup () = | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 42 | (Markup.entityN, (Markup.kindN, kind) :: Position.def_properties_of def_pos); | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 43 | in cons (pos, markup, fn () => "") end | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 44 | end; | 
| 41503 
a7462e442e35
refined report_parse_tree: reverse reports happen to produce proper type information for inlined @{term}, @{typ} etc.;
 wenzelm parents: 
41501diff
changeset | 45 | |
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 46 | fun reported loc (PolyML.PTtype types) = reported_types loc types | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 47 | | reported loc (PolyML.PTdeclaredAt decl) = reported_entity Markup.ML_defN loc decl | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 48 | | reported loc (PolyML.PTopenedAt decl) = reported_entity Markup.ML_openN loc decl | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 49 | | reported loc (PolyML.PTstructureAt decl) = reported_entity Markup.ML_structureN loc decl | 
| 44737 | 50 | | reported _ (PolyML.PTnextSibling tree) = reported_tree (tree ()) | 
| 51 | | reported _ (PolyML.PTfirstChild tree) = reported_tree (tree ()) | |
| 52 | | reported _ _ = I | |
| 53 | and reported_tree (loc, props) = fold (reported loc) props; | |
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 54 | |
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 55 | val persistent_reports = reported_tree parse_tree []; | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 56 | |
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 57 | fun output () = | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 58 | persistent_reports | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 59 | |> map (fn (pos, markup, text) => Position.reported_text pos (markup ()) (text ())) | 
| 56333 
38f1422ef473
support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
 wenzelm parents: 
56305diff
changeset | 60 | |> Output.report; | 
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 61 | in | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 62 | if not (null persistent_reports) andalso redirect andalso Multithreading.enabled () | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 63 | then | 
| 56305 | 64 | Execution.print | 
| 65 |         {name = "ML_Compiler.report", pos = Position.thread_data (), pri = 1} output
 | |
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 66 | else output () | 
| 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 67 | end; | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 68 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 69 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 70 | (* eval ML source tokens *) | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 71 | |
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 72 | fun eval (flags: flags) pos toks = | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 73 | let | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 74 | val _ = Secure.secure_mltext (); | 
| 56618 
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
 wenzelm parents: 
56333diff
changeset | 75 |     val space = ML_Env.name_space {SML = #SML flags, exchange = #exchange flags}
 | 
| 56265 
785569927666
discontinued Toplevel.debug in favour of system option "exception_trace";
 wenzelm parents: 
55837diff
changeset | 76 | val opt_context = Context.thread_data (); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 77 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 78 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 79 | (* input *) | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 80 | |
| 50911 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 wenzelm parents: 
50910diff
changeset | 81 |     val location_props = op ^ (YXML.output_markup (":", #props (Position.dest pos)));
 | 
| 31437 | 82 | |
| 59110 
8a78c7cb5b14
some special cases for official SML, to treat Isabelle symbols like raw characters;
 wenzelm parents: 
58991diff
changeset | 83 | val input_explode = | 
| 
8a78c7cb5b14
some special cases for official SML, to treat Isabelle symbols like raw characters;
 wenzelm parents: 
58991diff
changeset | 84 | if #SML flags then String.explode | 
| 
8a78c7cb5b14
some special cases for official SML, to treat Isabelle symbols like raw characters;
 wenzelm parents: 
58991diff
changeset | 85 | else maps (String.explode o Symbol.esc) o Symbol.explode; | 
| 
8a78c7cb5b14
some special cases for official SML, to treat Isabelle symbols like raw characters;
 wenzelm parents: 
58991diff
changeset | 86 | |
| 41501 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 87 | val input_buffer = | 
| 59110 
8a78c7cb5b14
some special cases for official SML, to treat Isabelle symbols like raw characters;
 wenzelm parents: 
58991diff
changeset | 88 | Unsynchronized.ref (toks |> map (`(input_explode o ML_Lex.check_content_of))); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 89 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 90 | fun get () = | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 91 | (case ! input_buffer of | 
| 41501 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 92 | (c :: cs, tok) :: rest => (input_buffer := (cs, tok) :: rest; SOME c) | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 93 | | ([], _) :: rest => (input_buffer := rest; SOME #" ") | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 94 | | [] => NONE); | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 95 | |
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 96 | fun get_pos () = | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 97 | (case ! input_buffer of | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 98 | (_ :: _, tok) :: _ => ML_Lex.pos_of tok | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 99 | | ([], tok) :: _ => ML_Lex.end_pos_of tok | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 100 | | [] => Position.none); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 101 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 102 | |
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 103 | (* output channels *) | 
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 104 | |
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 105 | val writeln_buffer = Unsynchronized.ref Buffer.empty; | 
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 106 | fun write s = Unsynchronized.change writeln_buffer (Buffer.add s); | 
| 54389 | 107 | fun output_writeln () = writeln (trim_line (Buffer.content (! writeln_buffer))); | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 108 | |
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 109 | val warnings = Unsynchronized.ref ([]: string list); | 
| 39231 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 wenzelm parents: 
39230diff
changeset | 110 | fun warn msg = Unsynchronized.change warnings (cons msg); | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 111 | fun output_warnings () = List.app warning (rev (! warnings)); | 
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 112 | |
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 113 | val error_buffer = Unsynchronized.ref Buffer.empty; | 
| 39231 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 wenzelm parents: 
39230diff
changeset | 114 | fun err msg = Unsynchronized.change error_buffer (Buffer.add msg #> Buffer.add "\n"); | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 115 | fun flush_error () = writeln (Buffer.content (! error_buffer)); | 
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 116 | fun raise_error msg = error (Buffer.content (Buffer.add msg (! error_buffer))); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 117 | |
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 118 |     fun message {message = msg, hard, location = loc, context = _} =
 | 
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 119 | let | 
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56281diff
changeset | 120 | val pos = Exn_Properties.position_of loc; | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 121 | val txt = | 
| 49828 
5631ee099293
more basic ML compiler messages -- avoid conflict of 638cefe3ee99 and cb7264721c91 concerning Protocol.message_positions;
 wenzelm parents: 
48992diff
changeset | 122 | (if hard then "ML error" else "ML warning") ^ Position.here pos ^ ":\n" ^ | 
| 
5631ee099293
more basic ML compiler messages -- avoid conflict of 638cefe3ee99 and cb7264721c91 concerning Protocol.message_positions;
 wenzelm parents: 
48992diff
changeset | 123 | Pretty.string_of (Pretty.from_ML (pretty_ml msg)); | 
| 39231 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 wenzelm parents: 
39230diff
changeset | 124 | in if hard then err txt else warn txt end; | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 125 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 126 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 127 | (* results *) | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 128 | |
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56281diff
changeset | 129 | val depth = ML_Options.get_print_depth (); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 130 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 131 |     fun apply_result {fixes, types, signatures, structures, functors, values} =
 | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 132 | let | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 133 | fun display disp x = | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 134 | if depth > 0 then | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 135 | (disp x |> pretty_ml |> Pretty.from_ML |> Pretty.string_of |> write; write "\n") | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 136 | else (); | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 137 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 138 | fun apply_fix (a, b) = | 
| 40799 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 wenzelm parents: 
39507diff
changeset | 139 | (#enterFix space (a, b); display PolyML.NameSpace.displayFix (a, b)); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 140 | fun apply_type (a, b) = | 
| 40799 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 wenzelm parents: 
39507diff
changeset | 141 | (#enterType space (a, b); display PolyML.NameSpace.displayType (b, depth, space)); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 142 | fun apply_sig (a, b) = | 
| 40799 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 wenzelm parents: 
39507diff
changeset | 143 | (#enterSig space (a, b); display PolyML.NameSpace.displaySig (b, depth, space)); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 144 | fun apply_struct (a, b) = | 
| 40799 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 wenzelm parents: 
39507diff
changeset | 145 | (#enterStruct space (a, b); display PolyML.NameSpace.displayStruct (b, depth, space)); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 146 | fun apply_funct (a, b) = | 
| 40799 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 wenzelm parents: 
39507diff
changeset | 147 | (#enterFunct space (a, b); display PolyML.NameSpace.displayFunct (b, depth, space)); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 148 | fun apply_val (a, b) = | 
| 40799 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 wenzelm parents: 
39507diff
changeset | 149 | (#enterVal space (a, b); display PolyML.NameSpace.displayVal (b, depth, space)); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 150 | in | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 151 | List.app apply_fix fixes; | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 152 | List.app apply_type types; | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 153 | List.app apply_sig signatures; | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 154 | List.app apply_struct structures; | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 155 | List.app apply_funct functors; | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 156 | List.app apply_val values | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 157 | end; | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 158 | |
| 39231 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 wenzelm parents: 
39230diff
changeset | 159 | exception STATIC_ERRORS of unit; | 
| 39230 
184507f6e8d0
ML_Compiler.eval: discontinued extra "Static Errors" of raw Poly/ML;
 wenzelm parents: 
39228diff
changeset | 160 | |
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 161 | fun result_fun (phase1, phase2) () = | 
| 31477 | 162 | ((case phase1 of | 
| 163 | NONE => () | |
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 164 | | SOME parse_tree => report_parse_tree (#redirect flags) depth space parse_tree); | 
| 31477 | 165 | (case phase2 of | 
| 39231 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 wenzelm parents: 
39230diff
changeset | 166 | NONE => raise STATIC_ERRORS () | 
| 31477 | 167 | | SOME code => | 
| 33603 
3713a5208671
generalized Runtime.toplevel_error wrt. output function;
 wenzelm parents: 
33538diff
changeset | 168 | apply_result | 
| 
3713a5208671
generalized Runtime.toplevel_error wrt. output function;
 wenzelm parents: 
33538diff
changeset | 169 | ((code | 
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56281diff
changeset | 170 | |> Runtime.debugging opt_context | 
| 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56281diff
changeset | 171 | |> Runtime.toplevel_error (err o Runtime.exn_message)) ()))); | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 172 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 173 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 174 | (* compiler invocation *) | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 175 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 176 | val parameters = | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 177 | [PolyML.Compiler.CPOutStream write, | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 178 | PolyML.Compiler.CPNameSpace space, | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 179 | PolyML.Compiler.CPErrorMessageProc message, | 
| 41501 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 180 | PolyML.Compiler.CPLineNo (the_default 0 o Position.line_of o get_pos), | 
| 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 wenzelm parents: 
41484diff
changeset | 181 | PolyML.Compiler.CPLineOffset (the_default 0 o Position.offset_of o get_pos), | 
| 31437 | 182 | PolyML.Compiler.CPFileName location_props, | 
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56281diff
changeset | 183 | PolyML.Compiler.CPPrintDepth ML_Options.get_print_depth, | 
| 31475 | 184 | PolyML.Compiler.CPCompilerResultFun result_fun, | 
| 185 | PolyML.Compiler.CPPrintInAlphabeticalOrder false]; | |
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 186 | val _ = | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 187 | (while not (List.null (! input_buffer)) do | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 188 | PolyML.compiler (get, parameters) ()) | 
| 39232 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 189 | handle exn => | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 190 | if Exn.is_interrupt exn then reraise exn | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 191 | else | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 192 | let | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 193 | val exn_msg = | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 194 | (case exn of | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 195 | STATIC_ERRORS () => "" | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 196 | | Runtime.TOPLEVEL_ERROR => "" | 
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56281diff
changeset | 197 | | _ => "Exception- " ^ Pretty.string_of (Exn_Output.pretty exn) ^ " raised"); | 
| 39232 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 198 | val _ = output_warnings (); | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 199 | val _ = output_writeln (); | 
| 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
39231diff
changeset | 200 | in raise_error exn_msg end; | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 201 | in | 
| 56304 
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
 wenzelm parents: 
56303diff
changeset | 202 | if #verbose flags then (output_warnings (); flush_error (); output_writeln ()) | 
| 39228 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 203 | else () | 
| 
cb7264721c91
ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
 wenzelm parents: 
39227diff
changeset | 204 | end; | 
| 31333 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 205 | |
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 206 | end; | 
| 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 wenzelm parents: diff
changeset | 207 |