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