author | haftmann |
Mon, 07 Dec 2009 11:48:40 +0100 | |
changeset 34021 | e820ed4bfd94 |
parent 33969 | 1e7ca47c6c3d |
child 34071 | 93bfbb557e2e |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_target.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
28054 | 4 |
Serializer from intermediate language ("Thin-gol") to target languages. |
24219 | 5 |
*) |
6 |
||
7 |
signature CODE_TARGET = |
|
8 |
sig |
|
28054 | 9 |
include CODE_PRINTER |
10 |
||
11 |
type serializer |
|
28064 | 12 |
val add_target: string * (serializer * literals) -> theory -> theory |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
13 |
val extend_target: string * |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
14 |
(string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program)) |
28054 | 15 |
-> theory -> theory |
16 |
val assert_target: theory -> string -> string |
|
24219 | 17 |
|
28054 | 18 |
type destination |
19 |
type serialization |
|
20 |
val parse_args: (OuterLex.token list -> 'a * OuterLex.token list) |
|
21 |
-> OuterLex.token list -> 'a |
|
22 |
val stmt_names_of_destination: destination -> string list |
|
23 |
val code_of_pretty: Pretty.T -> string |
|
24 |
val code_writeln: Pretty.T -> unit |
|
25 |
val mk_serialization: string -> ('a -> unit) option |
|
26 |
-> (Path.T option -> 'a -> unit) |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
27 |
-> ('a -> string * string option list) |
28054 | 28 |
-> 'a -> serialization |
30513 | 29 |
val serialize: theory -> string -> string option -> OuterLex.token list |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
30 |
-> Code_Thingol.naming -> Code_Thingol.program -> string list -> serialization |
28064 | 31 |
val serialize_custom: theory -> string * (serializer * literals) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
32 |
-> Code_Thingol.naming -> Code_Thingol.program -> string list -> string * string option list |
28090
29af3c712d2b
distributed literal code generation out of central infrastructure
haftmann
parents:
28064
diff
changeset
|
33 |
val the_literals: theory -> string -> literals |
28054 | 34 |
val compile: serialization -> unit |
35 |
val export: serialization -> unit |
|
36 |
val file: Path.T -> serialization -> unit |
|
37 |
val string: string list -> serialization -> string |
|
28690 | 38 |
val code_of: theory -> string -> string |
39 |
-> string list -> (Code_Thingol.naming -> string list) -> string |
|
30494
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
40 |
val shell_command: string (*theory name*) -> string (*export_code expr*) -> unit |
32740 | 41 |
val code_width: int Unsynchronized.ref |
28054 | 42 |
|
43 |
val allow_abort: string -> theory -> theory |
|
28690 | 44 |
val add_syntax_class: string -> class -> string option -> theory -> theory |
28054 | 45 |
val add_syntax_inst: string -> string * class -> bool -> theory -> theory |
46 |
val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory |
|
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
31036
diff
changeset
|
47 |
val add_syntax_const: string -> string -> proto_const_syntax option -> theory -> theory |
28054 | 48 |
val add_reserved: string -> string -> theory -> theory |
33969 | 49 |
val add_include: string -> string * (string * string list) option -> theory -> theory |
24219 | 50 |
end; |
51 |
||
28054 | 52 |
structure Code_Target : CODE_TARGET = |
24219 | 53 |
struct |
54 |
||
28054 | 55 |
open Basic_Code_Thingol; |
56 |
open Code_Printer; |
|
24219 | 57 |
|
58 |
(** basics **) |
|
59 |
||
27304 | 60 |
datatype destination = Compile | Export | File of Path.T | String of string list; |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
61 |
type serialization = destination -> (string * string option list) option; |
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
62 |
|
32740 | 63 |
val code_width = Unsynchronized.ref 80; (*FIXME after Pretty module no longer depends on print mode*) |
32966
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
wenzelm
parents:
32894
diff
changeset
|
64 |
fun code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin_CRITICAL (!code_width) f); |
27103 | 65 |
fun code_of_pretty p = code_setmp Pretty.string_of p ^ "\n"; |
32966
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
wenzelm
parents:
32894
diff
changeset
|
66 |
fun code_writeln p = Pretty.setmp_margin_CRITICAL (!code_width) Pretty.writeln p; |
26998 | 67 |
|
27103 | 68 |
(*FIXME why another code_setmp?*) |
69 |
fun compile f = (code_setmp f Compile; ()); |
|
27304 | 70 |
fun export f = (code_setmp f Export; ()); |
27103 | 71 |
fun file p f = (code_setmp f (File p); ()); |
30963 | 72 |
fun string stmts f = fst (the (code_setmp f (String stmts))); |
27304 | 73 |
|
74 |
fun stmt_names_of_destination (String stmts) = stmts |
|
75 |
| stmt_names_of_destination _ = []; |
|
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
76 |
|
28054 | 77 |
fun mk_serialization target (SOME comp) _ _ code Compile = (comp code; NONE) |
78 |
| mk_serialization target NONE _ _ _ Compile = error (target ^ ": no internal compilation") |
|
79 |
| mk_serialization target _ output _ code Export = (output NONE code ; NONE) |
|
80 |
| mk_serialization target _ output _ code (File file) = (output (SOME file) code; NONE) |
|
81 |
| mk_serialization target _ _ string code (String _) = SOME (string code); |
|
27550 | 82 |
|
24219 | 83 |
|
28054 | 84 |
(** theory data **) |
85 |
||
86 |
datatype name_syntax_table = NameSyntaxTable of { |
|
28690 | 87 |
class: string Symtab.table, |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30513
diff
changeset
|
88 |
instance: unit Symreltab.table, |
28054 | 89 |
tyco: tyco_syntax Symtab.table, |
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
31036
diff
changeset
|
90 |
const: proto_const_syntax Symtab.table |
28054 | 91 |
}; |
27000 | 92 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
93 |
fun mk_name_syntax_table ((class, instance), (tyco, const)) = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
94 |
NameSyntaxTable { class = class, instance = instance, tyco = tyco, const = const }; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
95 |
fun map_name_syntax_table f (NameSyntaxTable { class, instance, tyco, const }) = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
96 |
mk_name_syntax_table (f ((class, instance), (tyco, const))); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
97 |
fun merge_name_syntax_table (NameSyntaxTable { class = class1, instance = instance1, tyco = tyco1, const = const1 }, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
98 |
NameSyntaxTable { class = class2, instance = instance2, tyco = tyco2, const = const2 }) = |
28054 | 99 |
mk_name_syntax_table ( |
100 |
(Symtab.join (K snd) (class1, class2), |
|
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30513
diff
changeset
|
101 |
Symreltab.join (K snd) (instance1, instance2)), |
28054 | 102 |
(Symtab.join (K snd) (tyco1, tyco2), |
103 |
Symtab.join (K snd) (const1, const2)) |
|
104 |
); |
|
105 |
||
106 |
type serializer = |
|
107 |
string option (*module name*) |
|
30513 | 108 |
-> OuterLex.token list (*arguments*) |
28054 | 109 |
-> (string -> string) (*labelled_name*) |
110 |
-> string list (*reserved symbols*) |
|
111 |
-> (string * Pretty.T) list (*includes*) |
|
112 |
-> (string -> string option) (*module aliasses*) |
|
28690 | 113 |
-> (string -> string option) (*class syntax*) |
28054 | 114 |
-> (string -> tyco_syntax option) |
115 |
-> (string -> const_syntax option) |
|
116 |
-> Code_Thingol.program |
|
117 |
-> string list (*selected statements*) |
|
118 |
-> serialization; |
|
27000 | 119 |
|
28064 | 120 |
datatype serializer_entry = Serializer of serializer * literals |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
121 |
| Extends of string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program); |
28054 | 122 |
|
123 |
datatype target = Target of { |
|
124 |
serial: serial, |
|
125 |
serializer: serializer_entry, |
|
126 |
reserved: string list, |
|
28926 | 127 |
includes: (Pretty.T * string list) Symtab.table, |
28054 | 128 |
name_syntax_table: name_syntax_table, |
129 |
module_alias: string Symtab.table |
|
130 |
}; |
|
27103 | 131 |
|
31599 | 132 |
fun make_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) = |
28054 | 133 |
Target { serial = serial, serializer = serializer, reserved = reserved, |
134 |
includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias }; |
|
135 |
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) = |
|
31599 | 136 |
make_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias)))); |
28054 | 137 |
fun merge_target strict target (Target { serial = serial1, serializer = serializer, |
138 |
reserved = reserved1, includes = includes1, |
|
139 |
name_syntax_table = name_syntax_table1, module_alias = module_alias1 }, |
|
140 |
Target { serial = serial2, serializer = _, |
|
141 |
reserved = reserved2, includes = includes2, |
|
142 |
name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) = |
|
143 |
if serial1 = serial2 orelse not strict then |
|
31599 | 144 |
make_target ((serial1, serializer), |
28054 | 145 |
((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)), |
146 |
(merge_name_syntax_table (name_syntax_table1, name_syntax_table2), |
|
147 |
Symtab.join (K snd) (module_alias1, module_alias2)) |
|
148 |
)) |
|
149 |
else |
|
150 |
error ("Incompatible serializers: " ^ quote target); |
|
27103 | 151 |
|
33522 | 152 |
structure CodeTargetData = Theory_Data |
27436 | 153 |
( |
28054 | 154 |
type T = target Symtab.table * string list; |
155 |
val empty = (Symtab.empty, []); |
|
156 |
val extend = I; |
|
33522 | 157 |
fun merge ((target1, exc1), (target2, exc2)) : T = |
28054 | 158 |
(Symtab.join (merge_target true) (target1, target2), Library.merge (op =) (exc1, exc2)); |
27436 | 159 |
); |
160 |
||
28054 | 161 |
fun the_serializer (Target { serializer, ... }) = serializer; |
162 |
fun the_reserved (Target { reserved, ... }) = reserved; |
|
163 |
fun the_includes (Target { includes, ... }) = includes; |
|
164 |
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x; |
|
165 |
fun the_module_alias (Target { module_alias , ... }) = module_alias; |
|
27437 | 166 |
|
28054 | 167 |
val abort_allowed = snd o CodeTargetData.get; |
27436 | 168 |
|
33969 | 169 |
fun assert_target thy target = if Symtab.defined (fst (CodeTargetData.get thy)) target |
170 |
then target |
|
171 |
else error ("Unknown code target language: " ^ quote target); |
|
28054 | 172 |
|
173 |
fun put_target (target, seri) thy = |
|
27304 | 174 |
let |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
175 |
val lookup_target = Symtab.lookup (fst (CodeTargetData.get thy)); |
28054 | 176 |
val _ = case seri |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
177 |
of Extends (super, _) => if is_some (lookup_target super) then () |
28054 | 178 |
else error ("Unknown code target language: " ^ quote super) |
179 |
| _ => (); |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
180 |
val overwriting = case (Option.map the_serializer o lookup_target) target |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
181 |
of NONE => false |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
182 |
| SOME (Extends _) => true |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
183 |
| SOME (Serializer _) => (case seri |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
184 |
of Extends _ => error ("Will not overwrite existing target " ^ quote target) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
185 |
| _ => true); |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
186 |
val _ = if overwriting |
28054 | 187 |
then warning ("Overwriting existing target " ^ quote target) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
188 |
else (); |
28054 | 189 |
in |
190 |
thy |
|
191 |
|> (CodeTargetData.map o apfst oo Symtab.map_default) |
|
31599 | 192 |
(target, make_target ((serial (), seri), (([], Symtab.empty), |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30513
diff
changeset
|
193 |
(mk_name_syntax_table ((Symtab.empty, Symreltab.empty), (Symtab.empty, Symtab.empty)), |
28054 | 194 |
Symtab.empty)))) |
195 |
((map_target o apfst o apsnd o K) seri) |
|
196 |
end; |
|
27436 | 197 |
|
28054 | 198 |
fun add_target (target, seri) = put_target (target, Serializer seri); |
199 |
fun extend_target (target, (super, modify)) = |
|
200 |
put_target (target, Extends (super, modify)); |
|
27436 | 201 |
|
28054 | 202 |
fun map_target_data target f thy = |
27436 | 203 |
let |
28054 | 204 |
val _ = assert_target thy target; |
205 |
in |
|
206 |
thy |
|
207 |
|> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f |
|
208 |
end; |
|
27304 | 209 |
|
28054 | 210 |
fun map_reserved target = |
211 |
map_target_data target o apsnd o apfst o apfst; |
|
212 |
fun map_includes target = |
|
213 |
map_target_data target o apsnd o apfst o apsnd; |
|
214 |
fun map_name_syntax target = |
|
215 |
map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table; |
|
216 |
fun map_module_alias target = |
|
217 |
map_target_data target o apsnd o apsnd o apsnd; |
|
27000 | 218 |
|
219 |
||
34021 | 220 |
(** serializer usage **) |
221 |
||
222 |
(* montage *) |
|
223 |
||
224 |
fun the_literals thy = |
|
225 |
let |
|
226 |
val (targets, _) = CodeTargetData.get thy; |
|
227 |
fun literals target = case Symtab.lookup targets target |
|
228 |
of SOME data => (case the_serializer data |
|
229 |
of Serializer (_, literals) => literals |
|
230 |
| Extends (super, _) => literals super) |
|
231 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
232 |
in literals end; |
|
233 |
||
234 |
local |
|
235 |
||
236 |
fun activate_syntax lookup_name src_tab = Symtab.empty |
|
237 |
|> fold_map (fn thing_identifier => fn tab => case lookup_name thing_identifier |
|
238 |
of SOME name => (SOME name, |
|
239 |
Symtab.update_new (name, the (Symtab.lookup src_tab thing_identifier)) tab) |
|
240 |
| NONE => (NONE, tab)) (Symtab.keys src_tab) |
|
241 |
|>> map_filter I; |
|
242 |
||
243 |
fun activate_const_syntax thy literals src_tab naming = (Symtab.empty, naming) |
|
244 |
|> fold_map (fn thing_identifier => fn (tab, naming) => |
|
245 |
case Code_Thingol.lookup_const naming thing_identifier |
|
246 |
of SOME name => let |
|
247 |
val (syn, naming') = Code_Printer.activate_const_syntax thy |
|
248 |
literals (the (Symtab.lookup src_tab thing_identifier)) naming |
|
249 |
in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end |
|
250 |
| NONE => (NONE, (tab, naming))) (Symtab.keys src_tab) |
|
251 |
|>> map_filter I; |
|
252 |
||
253 |
fun invoke_serializer thy abortable serializer literals reserved abs_includes |
|
254 |
module_alias class instance tyco const module args naming program2 names1 = |
|
255 |
let |
|
256 |
val (names_class, class') = |
|
257 |
activate_syntax (Code_Thingol.lookup_class naming) class; |
|
258 |
val names_inst = map_filter (Code_Thingol.lookup_instance naming) |
|
259 |
(Symreltab.keys instance); |
|
260 |
val (names_tyco, tyco') = |
|
261 |
activate_syntax (Code_Thingol.lookup_tyco naming) tyco; |
|
262 |
val (names_const, (const', _)) = |
|
263 |
activate_const_syntax thy literals const naming; |
|
264 |
val names_hidden = names_class @ names_inst @ names_tyco @ names_const; |
|
265 |
val names2 = subtract (op =) names_hidden names1; |
|
266 |
val program3 = Graph.subgraph (not o member (op =) names_hidden) program2; |
|
267 |
val names_all = Graph.all_succs program3 names2; |
|
268 |
val includes = abs_includes names_all; |
|
269 |
val program4 = Graph.subgraph (member (op =) names_all) program3; |
|
270 |
val empty_funs = filter_out (member (op =) abortable) |
|
271 |
(Code_Thingol.empty_funs program3); |
|
272 |
val _ = if null empty_funs then () else error ("No code equations for " |
|
273 |
^ commas (map (Sign.extern_const thy) empty_funs)); |
|
274 |
in |
|
275 |
serializer module args (Code_Thingol.labelled_name thy program2) reserved includes |
|
276 |
(Symtab.lookup module_alias) (Symtab.lookup class') |
|
277 |
(Symtab.lookup tyco') (Symtab.lookup const') |
|
278 |
program4 names2 |
|
279 |
end; |
|
280 |
||
281 |
fun mount_serializer thy alt_serializer target module args naming program names = |
|
282 |
let |
|
283 |
val (targets, abortable) = CodeTargetData.get thy; |
|
284 |
fun collapse_hierarchy target = |
|
285 |
let |
|
286 |
val data = case Symtab.lookup targets target |
|
287 |
of SOME data => data |
|
288 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
289 |
in case the_serializer data |
|
290 |
of Serializer _ => (I, data) |
|
291 |
| Extends (super, modify) => let |
|
292 |
val (modify', data') = collapse_hierarchy super |
|
293 |
in (modify' #> modify naming, merge_target false target (data', data)) end |
|
294 |
end; |
|
295 |
val (modify, data) = collapse_hierarchy target; |
|
296 |
val (serializer, _) = the_default (case the_serializer data |
|
297 |
of Serializer seri => seri) alt_serializer; |
|
298 |
val reserved = the_reserved data; |
|
299 |
fun select_include names_all (name, (content, cs)) = |
|
300 |
if null cs then SOME (name, content) |
|
301 |
else if exists (fn c => case Code_Thingol.lookup_const naming c |
|
302 |
of SOME name => member (op =) names_all name |
|
303 |
| NONE => false) cs |
|
304 |
then SOME (name, content) else NONE; |
|
305 |
fun includes names_all = map_filter (select_include names_all) |
|
306 |
((Symtab.dest o the_includes) data); |
|
307 |
val module_alias = the_module_alias data; |
|
308 |
val { class, instance, tyco, const } = the_name_syntax data; |
|
309 |
val literals = the_literals thy target; |
|
310 |
in |
|
311 |
invoke_serializer thy abortable serializer literals reserved |
|
312 |
includes module_alias class instance tyco const module args naming (modify program) names |
|
313 |
end; |
|
314 |
||
315 |
in |
|
316 |
||
317 |
fun serialize thy = mount_serializer thy NONE; |
|
318 |
||
319 |
fun serialize_custom thy (target_name, seri) naming program names = |
|
320 |
mount_serializer thy (SOME seri) target_name NONE [] naming program names (String []) |
|
321 |
|> the; |
|
322 |
||
323 |
end; (* local *) |
|
324 |
||
325 |
||
326 |
(* code presentation *) |
|
327 |
||
328 |
fun code_of thy target module_name cs names_stmt = |
|
329 |
let |
|
330 |
val (names_cs, (naming, program)) = Code_Thingol.consts_program thy cs; |
|
331 |
in |
|
332 |
string (names_stmt naming) (serialize thy target (SOME module_name) [] |
|
333 |
naming program names_cs) |
|
334 |
end; |
|
335 |
||
336 |
||
337 |
(* code generation *) |
|
338 |
||
339 |
fun transitivly_non_empty_funs thy naming program = |
|
340 |
let |
|
341 |
val cs = subtract (op =) (abort_allowed thy) (Code_Thingol.empty_funs program); |
|
342 |
val names = map_filter (Code_Thingol.lookup_const naming) cs; |
|
343 |
in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end; |
|
344 |
||
345 |
fun read_const_exprs thy cs = |
|
346 |
let |
|
347 |
val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs; |
|
348 |
val (names3, (naming, program)) = Code_Thingol.consts_program thy cs2; |
|
349 |
val names4 = transitivly_non_empty_funs thy naming program; |
|
350 |
val cs5 = map_filter |
|
351 |
(fn (c, name) => if member (op =) names4 name then SOME c else NONE) (cs2 ~~ names3); |
|
352 |
in fold (insert (op =)) cs5 cs1 end; |
|
353 |
||
354 |
fun cached_program thy = |
|
355 |
let |
|
356 |
val (naming, program) = Code_Thingol.cached_program thy; |
|
357 |
in (transitivly_non_empty_funs thy naming program, (naming, program)) end |
|
358 |
||
359 |
fun export_code thy cs seris = |
|
360 |
let |
|
361 |
val (cs', (naming, program)) = if null cs then cached_program thy |
|
362 |
else Code_Thingol.consts_program thy cs; |
|
363 |
fun mk_seri_dest dest = case dest |
|
364 |
of NONE => compile |
|
365 |
| SOME "-" => export |
|
366 |
| SOME f => file (Path.explode f) |
|
367 |
val _ = map (fn (((target, module), dest), args) => |
|
368 |
(mk_seri_dest dest (serialize thy target module args naming program cs'))) seris; |
|
369 |
in () end; |
|
370 |
||
371 |
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris; |
|
372 |
||
373 |
||
28054 | 374 |
(** serializer configuration **) |
27000 | 375 |
|
376 |
(* data access *) |
|
24219 | 377 |
|
24992 | 378 |
fun cert_class thy class = |
379 |
let |
|
380 |
val _ = AxClass.get_info thy class; |
|
381 |
in class end; |
|
382 |
||
27103 | 383 |
fun read_class thy = cert_class thy o Sign.intern_class thy; |
24992 | 384 |
|
385 |
fun cert_tyco thy tyco = |
|
386 |
let |
|
387 |
val _ = if Sign.declared_tyname thy tyco then () |
|
388 |
else error ("No such type constructor: " ^ quote tyco); |
|
389 |
in tyco end; |
|
390 |
||
27103 | 391 |
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy; |
24219 | 392 |
|
33969 | 393 |
fun gen_add_syntax_class prep_class target raw_class raw_syn thy = |
24219 | 394 |
let |
24841 | 395 |
val class = prep_class thy raw_class; |
24219 | 396 |
in case raw_syn |
28690 | 397 |
of SOME syntax => |
24219 | 398 |
thy |
27024 | 399 |
|> (map_name_syntax target o apfst o apfst) |
28690 | 400 |
(Symtab.update (class, syntax)) |
24219 | 401 |
| NONE => |
402 |
thy |
|
27024 | 403 |
|> (map_name_syntax target o apfst o apfst) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
404 |
(Symtab.delete_safe class) |
24219 | 405 |
end; |
406 |
||
407 |
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy = |
|
408 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
409 |
val inst = (prep_class thy raw_class, prep_tyco thy raw_tyco); |
24219 | 410 |
in if add_del then |
411 |
thy |
|
27024 | 412 |
|> (map_name_syntax target o apfst o apsnd) |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30513
diff
changeset
|
413 |
(Symreltab.update (inst, ())) |
24219 | 414 |
else |
415 |
thy |
|
27024 | 416 |
|> (map_name_syntax target o apfst o apsnd) |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30513
diff
changeset
|
417 |
(Symreltab.delete_safe inst) |
24219 | 418 |
end; |
419 |
||
420 |
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy = |
|
421 |
let |
|
422 |
val tyco = prep_tyco thy raw_tyco; |
|
423 |
fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco |
|
424 |
then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco) |
|
425 |
else syntax |
|
426 |
in case raw_syn |
|
427 |
of SOME syntax => |
|
428 |
thy |
|
27024 | 429 |
|> (map_name_syntax target o apsnd o apfst) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
430 |
(Symtab.update (tyco, check_args syntax)) |
28690 | 431 |
| NONE => |
24219 | 432 |
thy |
27024 | 433 |
|> (map_name_syntax target o apsnd o apfst) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
434 |
(Symtab.delete_safe tyco) |
24219 | 435 |
end; |
436 |
||
437 |
fun gen_add_syntax_const prep_const target raw_c raw_syn thy = |
|
438 |
let |
|
439 |
val c = prep_const thy raw_c; |
|
31957 | 440 |
fun check_args (syntax as (n, _)) = if n > Code.args_number thy c |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
441 |
then error ("Too many arguments in syntax for constant " ^ quote c) |
24219 | 442 |
else syntax; |
443 |
in case raw_syn |
|
444 |
of SOME syntax => |
|
445 |
thy |
|
27024 | 446 |
|> (map_name_syntax target o apsnd o apsnd) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
447 |
(Symtab.update (c, check_args syntax)) |
28690 | 448 |
| NONE => |
24219 | 449 |
thy |
27024 | 450 |
|> (map_name_syntax target o apsnd o apsnd) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
451 |
(Symtab.delete_safe c) |
24219 | 452 |
end; |
453 |
||
454 |
fun add_reserved target = |
|
455 |
let |
|
456 |
fun add sym syms = if member (op =) syms sym |
|
457 |
then error ("Reserved symbol " ^ quote sym ^ " already declared") |
|
458 |
else insert (op =) sym syms |
|
27103 | 459 |
in map_reserved target o add end; |
24992 | 460 |
|
28926 | 461 |
fun gen_add_include read_const target args thy = |
24992 | 462 |
let |
28926 | 463 |
fun add (name, SOME (content, raw_cs)) incls = |
24992 | 464 |
let |
465 |
val _ = if Symtab.defined incls name |
|
466 |
then warning ("Overwriting existing include " ^ name) |
|
467 |
else (); |
|
28926 | 468 |
val cs = map (read_const thy) raw_cs; |
469 |
in Symtab.update (name, (str content, cs)) incls end |
|
470 |
| add (name, NONE) incls = Symtab.delete name incls; |
|
471 |
in map_includes target (add args) thy end; |
|
472 |
||
33969 | 473 |
val add_include = gen_add_include (K I); |
31156 | 474 |
val add_include_cmd = gen_add_include Code.read_const; |
24219 | 475 |
|
31013 | 476 |
fun add_module_alias target (thyname, modlname) = |
477 |
let |
|
478 |
val xs = Long_Name.explode modlname; |
|
31036 | 479 |
val xs' = map (Name.desymbolize true) xs; |
31013 | 480 |
in if xs' = xs |
481 |
then map_module_alias target (Symtab.update (thyname, modlname)) |
|
482 |
else error ("Invalid module name: " ^ quote modlname ^ "\n" |
|
483 |
^ "perhaps try " ^ quote (Long_Name.implode xs')) |
|
484 |
end; |
|
24219 | 485 |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
486 |
fun gen_allow_abort prep_const raw_c thy = |
24841 | 487 |
let |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
488 |
val c = prep_const thy raw_c; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28090
diff
changeset
|
489 |
in thy |> (CodeTargetData.map o apsnd) (insert (op =) c) end; |
24841 | 490 |
|
24219 | 491 |
fun zip_list (x::xs) f g = |
492 |
f |
|
493 |
#-> (fn y => |
|
494 |
fold_map (fn x => g |-- f >> pair x) xs |
|
495 |
#-> (fn xys => pair ((x, y) :: xys))); |
|
496 |
||
27000 | 497 |
|
27103 | 498 |
(* concrete syntax *) |
27000 | 499 |
|
34021 | 500 |
local |
501 |
||
24219 | 502 |
structure P = OuterParse |
503 |
and K = OuterKeyword |
|
504 |
||
505 |
fun parse_multi_syntax parse_thing parse_syntax = |
|
506 |
P.and_list1 parse_thing |
|
507 |
#-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name -- |
|
508 |
(zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")")); |
|
509 |
||
510 |
in |
|
511 |
||
33969 | 512 |
val add_syntax_class = gen_add_syntax_class cert_class; |
24219 | 513 |
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco; |
514 |
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco; |
|
515 |
val add_syntax_const = gen_add_syntax_const (K I); |
|
27103 | 516 |
val allow_abort = gen_allow_abort (K I); |
28054 | 517 |
val add_reserved = add_reserved; |
33969 | 518 |
val add_include = add_include; |
24219 | 519 |
|
33969 | 520 |
val add_syntax_class_cmd = gen_add_syntax_class read_class; |
24219 | 521 |
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco; |
522 |
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco; |
|
31156 | 523 |
val add_syntax_const_cmd = gen_add_syntax_const Code.read_const; |
524 |
val allow_abort_cmd = gen_allow_abort Code.read_const; |
|
24219 | 525 |
|
28054 | 526 |
fun parse_args f args = |
527 |
case Scan.read OuterLex.stopper f args |
|
528 |
of SOME x => x |
|
529 |
| NONE => error "Bad serializer arguments"; |
|
530 |
||
531 |
||
27304 | 532 |
(** Isar setup **) |
27103 | 533 |
|
534 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file"); |
|
535 |
||
30494
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
536 |
val code_exprP = |
27757
650af1991b8b
fall back on P.term_group, to avoid problems with inner_syntax markup (due to CodeName.read_const_exprs);
wenzelm
parents:
27710
diff
changeset
|
537 |
(Scan.repeat P.term_group |
27103 | 538 |
-- Scan.repeat (P.$$$ inK |-- P.name |
539 |
-- Scan.option (P.$$$ module_nameK |-- P.name) |
|
540 |
-- Scan.option (P.$$$ fileK |-- P.name) |
|
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27757
diff
changeset
|
541 |
-- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") [] |
30494
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
542 |
) >> (fn (raw_cs, seris) => export_code_cmd raw_cs seris)); |
27103 | 543 |
|
28054 | 544 |
val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK]; |
24867 | 545 |
|
546 |
val _ = |
|
24992 | 547 |
OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl ( |
28690 | 548 |
parse_multi_syntax P.xname (Scan.option P.string) |
24219 | 549 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
550 |
fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns) |
|
551 |
); |
|
552 |
||
24867 | 553 |
val _ = |
24992 | 554 |
OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl ( |
24219 | 555 |
parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) |
556 |
((P.minus >> K true) || Scan.succeed false) |
|
557 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
558 |
fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns) |
|
559 |
); |
|
560 |
||
24867 | 561 |
val _ = |
24992 | 562 |
OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl ( |
24219 | 563 |
parse_multi_syntax P.xname (parse_syntax I) |
564 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
565 |
fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns) |
|
566 |
); |
|
567 |
||
24867 | 568 |
val _ = |
24992 | 569 |
OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl ( |
27757
650af1991b8b
fall back on P.term_group, to avoid problems with inner_syntax markup (due to CodeName.read_const_exprs);
wenzelm
parents:
27710
diff
changeset
|
570 |
parse_multi_syntax P.term_group (parse_syntax fst) |
24219 | 571 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
28054 | 572 |
fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const |
573 |
(Code_Printer.simple_const_syntax syn)) syns) |
|
24219 | 574 |
); |
575 |
||
24867 | 576 |
val _ = |
24992 | 577 |
OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl ( |
24219 | 578 |
P.name -- Scan.repeat1 P.name |
579 |
>> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds) |
|
24841 | 580 |
); |
24219 | 581 |
|
24867 | 582 |
val _ = |
24992 | 583 |
OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl ( |
28926 | 584 |
P.name -- P.name -- (P.text :|-- (fn "-" => Scan.succeed NONE |
585 |
| s => Scan.optional (P.$$$ "attach" |-- Scan.repeat1 P.term) [] >> pair s >> SOME)) |
|
586 |
>> (fn ((target, name), content_consts) => |
|
587 |
(Toplevel.theory o add_include_cmd target) (name, content_consts)) |
|
24992 | 588 |
); |
589 |
||
590 |
val _ = |
|
591 |
OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl ( |
|
24219 | 592 |
P.name -- Scan.repeat1 (P.name -- P.name) |
27103 | 593 |
>> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames) |
594 |
); |
|
595 |
||
596 |
val _ = |
|
597 |
OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl ( |
|
27757
650af1991b8b
fall back on P.term_group, to avoid problems with inner_syntax markup (due to CodeName.read_const_exprs);
wenzelm
parents:
27710
diff
changeset
|
598 |
Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd) |
24841 | 599 |
); |
24219 | 600 |
|
24867 | 601 |
val _ = |
27103 | 602 |
OuterSyntax.command "export_code" "generate executable code for constants" |
30494
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
603 |
K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
604 |
|
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
605 |
fun shell_command thyname cmd = Toplevel.program (fn _ => |
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
606 |
(use_thy thyname; case Scan.read OuterLex.stopper (P.!!! code_exprP) |
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
607 |
((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd) |
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
608 |
of SOME f => (writeln "Now generating code..."; f (theory thyname)) |
c150e6fa4e0d
consider exit status of code generation direcitve
haftmann
parents:
30242
diff
changeset
|
609 |
| NONE => error ("Bad directive " ^ quote cmd))) |
33969 | 610 |
handle Runtime.TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure; |
27103 | 611 |
|
24219 | 612 |
end; (*local*) |
613 |
||
614 |
end; (*struct*) |