| author | wenzelm | 
| Sun, 02 Mar 2014 19:00:45 +0100 | |
| changeset 55837 | 154855d9a564 | 
| parent 54389 | a4051679a7bf | 
| child 56265 | 785569927666 | 
| permissions | -rw-r--r-- | 
| 
47980
 
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
 
wenzelm 
parents: 
45666 
diff
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  | 
|
| 50910 | 4  | 
Advanced runtime compilation for Poly/ML.  | 
| 
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  | 
|
| 
31429
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
10  | 
(* source locations *)  | 
| 
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
11  | 
|
| 
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
12  | 
fun position_of (loc: PolyML.location) =  | 
| 
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
13  | 
let  | 
| 
50911
 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 
wenzelm 
parents: 
50910 
diff
changeset
 | 
14  | 
    val {startLine = line, startPosition = offset, endPosition = end_offset, ...} = loc;
 | 
| 
 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 
wenzelm 
parents: 
50910 
diff
changeset
 | 
15  | 
val props = Exn_Properties.of_location loc;  | 
| 
31429
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
16  | 
in  | 
| 
43710
 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 
wenzelm 
parents: 
42327 
diff
changeset
 | 
17  | 
    Position.make {line = line, offset = offset, end_offset = end_offset, props = props}
 | 
| 41484 | 18  | 
end;  | 
| 
31429
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
19  | 
|
| 
51639
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
20  | 
|
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
21  | 
(* exn_info *)  | 
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
22  | 
|
| 31477 | 23  | 
fun exn_position exn =  | 
| 
31429
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
24  | 
(case PolyML.exceptionLocation exn of  | 
| 
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
25  | 
NONE => Position.none  | 
| 
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
26  | 
| SOME loc => position_of loc);  | 
| 
31333
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
27  | 
|
| 
51639
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
28  | 
fun pretty_exn (exn: exn) =  | 
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
29  | 
Pretty.from_ML (pretty_ml (PolyML.prettyRepresentation (exn, get_print_depth ())));  | 
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
30  | 
|
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
31  | 
val exn_info = {exn_position = exn_position, pretty_exn = pretty_exn};
 | 
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
32  | 
|
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
33  | 
|
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
34  | 
(* exn_message *)  | 
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
35  | 
|
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
36  | 
val exn_messages_ids = Runtime.exn_messages_ids exn_info;  | 
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
37  | 
val exn_messages = Runtime.exn_messages exn_info;  | 
| 
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
38  | 
val exn_message = Runtime.exn_message exn_info;  | 
| 54387 | 39  | 
|
40  | 
val exn_error_message = Output.error_message o exn_message;  | 
|
| 
53709
 
84522727f9d3
improved printing of exception trace in Poly/ML 5.5.1;
 
wenzelm 
parents: 
51639 
diff
changeset
 | 
41  | 
fun exn_trace e = print_exception_trace exn_message e;  | 
| 31477 | 42  | 
|
| 
31333
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
43  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
44  | 
(* parse trees *)  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
45  | 
|
| 
31429
 
8a7c0899e0b1
convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
 
wenzelm 
parents: 
31333 
diff
changeset
 | 
46  | 
fun report_parse_tree 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
 | 
47  | 
let  | 
| 44737 | 48  | 
val reported_text =  | 
| 
38720
 
7f8bc335e203
ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
 
wenzelm 
parents: 
38228 
diff
changeset
 | 
49  | 
(case Context.thread_data () of  | 
| 44737 | 50  | 
SOME (Context.Proof ctxt) => Context_Position.reported_text ctxt  | 
51  | 
| _ => Position.reported_text);  | 
|
| 
38720
 
7f8bc335e203
ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
 
wenzelm 
parents: 
38228 
diff
changeset
 | 
52  | 
|
| 44737 | 53  | 
fun reported_entity kind loc decl =  | 
54  | 
reported_text (position_of loc)  | 
|
| 
50201
 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 
wenzelm 
parents: 
49828 
diff
changeset
 | 
55  | 
(Markup.entityN, (Markup.kindN, kind) :: Position.def_properties_of (position_of decl)) "";  | 
| 
41503
 
a7462e442e35
refined report_parse_tree: reverse reports happen to produce proper type information for inlined @{term}, @{typ} etc.;
 
wenzelm 
parents: 
41501 
diff
changeset
 | 
56  | 
|
| 44737 | 57  | 
fun reported loc (PolyML.PTtype types) =  | 
58  | 
cons  | 
|
59  | 
(PolyML.NameSpace.displayTypeExpression (types, depth, space)  | 
|
60  | 
|> pretty_ml |> Pretty.from_ML |> Pretty.string_of  | 
|
| 
50201
 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 
wenzelm 
parents: 
49828 
diff
changeset
 | 
61  | 
|> reported_text (position_of loc) Markup.ML_typing)  | 
| 44737 | 62  | 
| reported loc (PolyML.PTdeclaredAt decl) =  | 
| 
50201
 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 
wenzelm 
parents: 
49828 
diff
changeset
 | 
63  | 
cons (reported_entity Markup.ML_defN loc decl)  | 
| 44737 | 64  | 
| reported loc (PolyML.PTopenedAt decl) =  | 
| 
50201
 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 
wenzelm 
parents: 
49828 
diff
changeset
 | 
65  | 
cons (reported_entity Markup.ML_openN loc decl)  | 
| 44737 | 66  | 
| reported loc (PolyML.PTstructureAt decl) =  | 
| 55837 | 67  | 
cons (reported_entity Markup.ML_structureN loc decl)  | 
| 44737 | 68  | 
| reported _ (PolyML.PTnextSibling tree) = reported_tree (tree ())  | 
69  | 
| reported _ (PolyML.PTfirstChild tree) = reported_tree (tree ())  | 
|
70  | 
| reported _ _ = I  | 
|
71  | 
and reported_tree (loc, props) = fold (reported loc) props;  | 
|
72  | 
in fn tree => Output.report (implode (reported_tree tree [])) end;  | 
|
| 
31333
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
73  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
74  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
75  | 
(* 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
 | 
76  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
77  | 
fun eval verbose pos toks =  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
78  | 
let  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
79  | 
val _ = Secure.secure_mltext ();  | 
| 39227 | 80  | 
val space = ML_Env.name_space;  | 
| 
31333
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
81  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
82  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
83  | 
(* input *)  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
84  | 
|
| 
50911
 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 
wenzelm 
parents: 
50910 
diff
changeset
 | 
85  | 
    val location_props = op ^ (YXML.output_markup (":", #props (Position.dest pos)));
 | 
| 31437 | 86  | 
|
| 
41501
 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 
wenzelm 
parents: 
41484 
diff
changeset
 | 
87  | 
val input_buffer =  | 
| 
 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 
wenzelm 
parents: 
41484 
diff
changeset
 | 
88  | 
Unsynchronized.ref (toks |> map  | 
| 
 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 
wenzelm 
parents: 
41484 
diff
changeset
 | 
89  | 
(`(maps (String.explode o Symbol.esc) o Symbol.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
 | 
90  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
91  | 
fun get () =  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
92  | 
(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: 
41484 
diff
changeset
 | 
93  | 
(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: 
41484 
diff
changeset
 | 
94  | 
| ([], _) :: 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: 
41484 
diff
changeset
 | 
95  | 
| [] => NONE);  | 
| 
 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 
wenzelm 
parents: 
41484 
diff
changeset
 | 
96  | 
|
| 
 
b5ad6b0d6d7c
ML compiler: more careful treatment of input tokens -- trailing space ensures proper separation and end position (cf. 82c1e348bc18, 08240feb69c7);
 
wenzelm 
parents: 
41484 
diff
changeset
 | 
97  | 
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: 
41484 
diff
changeset
 | 
98  | 
(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: 
41484 
diff
changeset
 | 
99  | 
(_ :: _, 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: 
41484 
diff
changeset
 | 
100  | 
| ([], 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: 
41484 
diff
changeset
 | 
101  | 
| [] => 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
 | 
102  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
103  | 
|
| 
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: 
39227 
diff
changeset
 | 
104  | 
(* 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: 
39227 
diff
changeset
 | 
105  | 
|
| 
 
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: 
39227 
diff
changeset
 | 
106  | 
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: 
39227 
diff
changeset
 | 
107  | 
fun write s = Unsynchronized.change writeln_buffer (Buffer.add s);  | 
| 54389 | 108  | 
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: 
39227 
diff
changeset
 | 
109  | 
|
| 
 
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: 
39227 
diff
changeset
 | 
110  | 
val warnings = Unsynchronized.ref ([]: string list);  | 
| 
39231
 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 
wenzelm 
parents: 
39230 
diff
changeset
 | 
111  | 
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: 
39227 
diff
changeset
 | 
112  | 
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: 
39227 
diff
changeset
 | 
113  | 
|
| 
 
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: 
39227 
diff
changeset
 | 
114  | 
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: 
39230 
diff
changeset
 | 
115  | 
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: 
39227 
diff
changeset
 | 
116  | 
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: 
39227 
diff
changeset
 | 
117  | 
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
 | 
118  | 
|
| 
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: 
39227 
diff
changeset
 | 
119  | 
    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: 
39227 
diff
changeset
 | 
120  | 
let  | 
| 41504 | 121  | 
val pos = 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: 
39227 
diff
changeset
 | 
122  | 
val txt =  | 
| 
49828
 
5631ee099293
more basic ML compiler messages -- avoid conflict of 638cefe3ee99 and cb7264721c91 concerning Protocol.message_positions;
 
wenzelm 
parents: 
48992 
diff
changeset
 | 
123  | 
(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: 
48992 
diff
changeset
 | 
124  | 
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: 
39230 
diff
changeset
 | 
125  | 
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
 | 
126  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
127  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
128  | 
(* results *)  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
129  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
130  | 
val depth = get_print_depth ();  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
131  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
132  | 
    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
 | 
133  | 
let  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
134  | 
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
 | 
135  | 
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: 
39227 
diff
changeset
 | 
136  | 
(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
 | 
137  | 
else ();  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
138  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
139  | 
fun apply_fix (a, b) =  | 
| 
40799
 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 
wenzelm 
parents: 
39507 
diff
changeset
 | 
140  | 
(#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
 | 
141  | 
fun apply_type (a, b) =  | 
| 
40799
 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 
wenzelm 
parents: 
39507 
diff
changeset
 | 
142  | 
(#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
 | 
143  | 
fun apply_sig (a, b) =  | 
| 
40799
 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 
wenzelm 
parents: 
39507 
diff
changeset
 | 
144  | 
(#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
 | 
145  | 
fun apply_struct (a, b) =  | 
| 
40799
 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 
wenzelm 
parents: 
39507 
diff
changeset
 | 
146  | 
(#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
 | 
147  | 
fun apply_funct (a, b) =  | 
| 
40799
 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 
wenzelm 
parents: 
39507 
diff
changeset
 | 
148  | 
(#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
 | 
149  | 
fun apply_val (a, b) =  | 
| 
40799
 
d44c87988a24
ML results: enter before printing (cf. Poly/ML SVN 1218);
 
wenzelm 
parents: 
39507 
diff
changeset
 | 
150  | 
(#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
 | 
151  | 
in  | 
| 
 
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_fix fixes;  | 
| 
 
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_type types;  | 
| 
 
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_sig signatures;  | 
| 
 
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_struct structures;  | 
| 
 
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_funct functors;  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
157  | 
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
 | 
158  | 
end;  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
159  | 
|
| 
39231
 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 
wenzelm 
parents: 
39230 
diff
changeset
 | 
160  | 
exception STATIC_ERRORS of unit;  | 
| 
39230
 
184507f6e8d0
ML_Compiler.eval: discontinued extra "Static Errors" of raw Poly/ML;
 
wenzelm 
parents: 
39228 
diff
changeset
 | 
161  | 
|
| 
31333
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
162  | 
fun result_fun (phase1, phase2) () =  | 
| 31477 | 163  | 
((case phase1 of  | 
164  | 
NONE => ()  | 
|
165  | 
| SOME parse_tree => report_parse_tree depth space parse_tree);  | 
|
166  | 
(case phase2 of  | 
|
| 
39231
 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 
wenzelm 
parents: 
39230 
diff
changeset
 | 
167  | 
NONE => raise STATIC_ERRORS ()  | 
| 31477 | 168  | 
| SOME code =>  | 
| 
33603
 
3713a5208671
generalized Runtime.toplevel_error wrt. output function;
 
wenzelm 
parents: 
33538 
diff
changeset
 | 
169  | 
apply_result  | 
| 
 
3713a5208671
generalized Runtime.toplevel_error wrt. output function;
 
wenzelm 
parents: 
33538 
diff
changeset
 | 
170  | 
((code  | 
| 
53709
 
84522727f9d3
improved printing of exception trace in Poly/ML 5.5.1;
 
wenzelm 
parents: 
51639 
diff
changeset
 | 
171  | 
|> Runtime.debugging exn_message  | 
| 
39231
 
25c345302a17
avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
 
wenzelm 
parents: 
39230 
diff
changeset
 | 
172  | 
|> Runtime.toplevel_error (err o 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
 | 
173  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
174  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
175  | 
(* compiler invocation *)  | 
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
176  | 
|
| 
 
fcd010617e6c
added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
 
wenzelm 
parents:  
diff
changeset
 | 
177  | 
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: 
39227 
diff
changeset
 | 
178  | 
[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
 | 
179  | 
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: 
39227 
diff
changeset
 | 
180  | 
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: 
41484 
diff
changeset
 | 
181  | 
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: 
41484 
diff
changeset
 | 
182  | 
PolyML.Compiler.CPLineOffset (the_default 0 o Position.offset_of o get_pos),  | 
| 31437 | 183  | 
PolyML.Compiler.CPFileName location_props,  | 
| 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: 
39231 
diff
changeset
 | 
189  | 
handle exn =>  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
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: 
39231 
diff
changeset
 | 
191  | 
else  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
changeset
 | 
192  | 
let  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
changeset
 | 
193  | 
val exn_msg =  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
changeset
 | 
194  | 
(case exn of  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
changeset
 | 
195  | 
STATIC_ERRORS () => ""  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
changeset
 | 
196  | 
| Runtime.TOPLEVEL_ERROR => ""  | 
| 
51639
 
b7f908c99546
prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
 
wenzelm 
parents: 
50914 
diff
changeset
 | 
197  | 
| _ => "Exception- " ^ Pretty.string_of (pretty_exn exn) ^ " raised");  | 
| 
39232
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
changeset
 | 
198  | 
val _ = output_warnings ();  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
changeset
 | 
199  | 
val _ = output_writeln ();  | 
| 
 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 
wenzelm 
parents: 
39231 
diff
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: 
39227 
diff
changeset
 | 
201  | 
in  | 
| 
 
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: 
39227 
diff
changeset
 | 
202  | 
if verbose then (output_warnings (); flush_error (); output_writeln ())  | 
| 
 
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: 
39227 
diff
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: 
39227 
diff
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  |