1 (* Title: Pure/ML/ml_compiler0.ML |
1 (* Title: Pure/ML/ml_compiler0.ML |
|
2 Author: Makarius |
2 |
3 |
3 Runtime compilation and evaluation (bootstrap version of |
4 Runtime compilation and evaluation for bootstrap. |
4 Pure/ML/ml_compiler.ML). |
5 Initial ML use operations. |
5 *) |
6 *) |
6 |
7 |
7 signature ML_COMPILER0 = |
8 signature ML_COMPILER0 = |
8 sig |
9 sig |
9 type context = |
10 type context = |
10 {name_space: ML_Name_Space.T, |
11 {name_space: ML_Name_Space.T, |
11 print_depth: int option, |
12 print_depth: int option, |
12 here: int -> string -> string, |
13 here: int -> string -> string, |
13 print: string -> unit, |
14 print: string -> unit, |
14 error: string -> unit} |
15 error: string -> unit} |
|
16 val make_context: ML_Name_Space.T -> context |
15 val use_text: context -> {debug: bool, file: string, line: int, verbose: bool} -> string -> unit |
17 val use_text: context -> {debug: bool, file: string, line: int, verbose: bool} -> string -> unit |
16 val use_file: context -> {debug: bool, verbose: bool} -> string -> unit |
18 val use_file: context -> {debug: bool, verbose: bool} -> string -> unit |
17 val debug_option: bool option -> bool |
19 val debug_option: bool option -> bool |
18 val use_operations: (bool option -> string -> unit) -> |
20 val use_operations: (bool option -> string -> unit) -> |
19 {use: string -> unit, use_debug: string -> unit, use_no_debug: string -> unit} |
21 {use: string -> unit, use_debug: string -> unit, use_no_debug: string -> unit} |
20 end; |
22 end; |
21 |
23 |
22 structure ML_Compiler0: ML_COMPILER0 = |
24 structure ML_Compiler0: ML_COMPILER0 = |
23 struct |
25 struct |
24 |
26 |
|
27 (* global options *) |
|
28 |
|
29 val _ = PolyML.Compiler.reportUnreferencedIds := true; |
|
30 val _ = PolyML.Compiler.reportExhaustiveHandlers := true; |
|
31 val _ = PolyML.Compiler.printInAlphabeticalOrder := false; |
|
32 val _ = PolyML.Compiler.maxInlineSize := 80; |
|
33 |
|
34 |
|
35 (* context *) |
|
36 |
25 type context = |
37 type context = |
26 {name_space: ML_Name_Space.T, |
38 {name_space: ML_Name_Space.T, |
27 print_depth: int option, |
39 print_depth: int option, |
28 here: int -> string -> string, |
40 here: int -> string -> string, |
29 print: string -> unit, |
41 print: string -> unit, |
30 error: string -> unit}; |
42 error: string -> unit}; |
|
43 |
|
44 fun make_context name_space : context = |
|
45 {name_space = name_space, |
|
46 print_depth = NONE, |
|
47 here = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")", |
|
48 print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut), |
|
49 error = fn s => error s}; |
|
50 |
|
51 |
|
52 (* use operations *) |
|
53 |
|
54 local |
31 |
55 |
32 fun drop_newline s = |
56 fun drop_newline s = |
33 if String.isSuffix "\n" s then String.substring (s, 0, size s - 1) |
57 if String.isSuffix "\n" s then String.substring (s, 0, size s - 1) |
34 else s; |
58 else s; |
35 |
59 |
44 | input line (#"\\" :: #"<" :: cs) res = input line cs ("\\\\<" :: res) |
68 | input line (#"\\" :: #"<" :: cs) res = input line cs ("\\\\<" :: res) |
45 | input line (#"\n" :: cs) res = input (line + 1) cs ("\n" :: res) |
69 | input line (#"\n" :: cs) res = input (line + 1) cs ("\n" :: res) |
46 | input line (c :: cs) res = input line cs (str c :: res) |
70 | input line (c :: cs) res = input line cs (str c :: res) |
47 | input _ [] res = rev res; |
71 | input _ [] res = rev res; |
48 in String.concat (input start_line (String.explode txt) []) end; |
72 in String.concat (input start_line (String.explode txt) []) end; |
|
73 |
|
74 in |
49 |
75 |
50 fun use_text ({name_space, print_depth, here, print, error, ...}: context) |
76 fun use_text ({name_space, print_depth, here, print, error, ...}: context) |
51 {line, file, verbose, debug} text = |
77 {line, file, verbose, debug} text = |
52 let |
78 let |
53 val current_line = Unsynchronized.ref line; |
79 val current_line = Unsynchronized.ref line; |
84 else |
110 else |
85 (put ("Exception- " ^ General.exnMessage exn ^ " raised"); |
111 (put ("Exception- " ^ General.exnMessage exn ^ " raised"); |
86 error (output ()); Exn.reraise exn); |
112 error (output ()); Exn.reraise exn); |
87 in if verbose then print (output ()) else () end; |
113 in if verbose then print (output ()) else () end; |
88 |
114 |
|
115 end; |
|
116 |
89 fun use_file context {verbose, debug} file = |
117 fun use_file context {verbose, debug} file = |
90 let |
118 let |
91 val instream = TextIO.openIn file; |
119 val instream = TextIO.openIn file; |
92 val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream); |
120 val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream); |
93 in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end; |
121 in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end; |
94 |
122 |
|
123 fun use_operations (use_ : bool option -> string -> unit) = |
|
124 {use = use_ NONE, use_debug = use_ (SOME true), use_no_debug = use_ (SOME false)}; |
|
125 |
95 |
126 |
96 fun debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true" |
127 fun debug_option NONE = OS.Process.getEnv "ISABELLE_ML_DEBUGGER" = SOME "true" |
97 | debug_option (SOME debug) = debug; |
128 | debug_option (SOME debug) = debug; |
98 |
129 |
99 fun use_operations (use_ : bool option -> string -> unit) = |
|
100 {use = use_ NONE, use_debug = use_ (SOME true), use_no_debug = use_ (SOME false)}; |
|
101 |
|
102 end; |
130 end; |
103 |
131 |
|
132 |
|
133 (* initial use operations *) |
|
134 |
104 val {use, use_debug, use_no_debug} = |
135 val {use, use_debug, use_no_debug} = |
105 let |
136 let val context = ML_Compiler0.make_context ML_Name_Space.global in |
106 val context: ML_Compiler0.context = |
|
107 {name_space = ML_Name_Space.global, |
|
108 print_depth = NONE, |
|
109 here = fn line => fn file => " (line " ^ Int.toString line ^ " of \"" ^ file ^ "\")", |
|
110 print = fn s => (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut), |
|
111 error = fn s => error s}; |
|
112 in |
|
113 ML_Compiler0.use_operations (fn opt_debug => fn file => |
137 ML_Compiler0.use_operations (fn opt_debug => fn file => |
114 ML_Compiler0.use_file context |
138 ML_Compiler0.use_file context |
115 {verbose = true, debug = ML_Compiler0.debug_option opt_debug} file |
139 {verbose = true, debug = ML_Compiler0.debug_option opt_debug} file |
116 handle ERROR msg => (#print context msg; raise error "ML error")) |
140 handle ERROR msg => (#print context msg; raise error "ML error")) |
117 end; |
141 end; |