author | haftmann |
Thu, 28 Aug 2008 22:09:20 +0200 | |
changeset 28054 | 2b84d34c5d02 |
parent 27868 | a28b3cd0077b |
child 28064 | d4a6460c53d1 |
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 |
|
13 |
val add_target: string * serializer -> theory -> theory |
|
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 |
|
31 |
val serialize_custom: theory -> string * serializer |
|
32 |
-> Code_Thingol.program -> string list -> string * string list |
|
33 |
val compile: serialization -> unit |
|
34 |
val export: serialization -> unit |
|
35 |
val file: Path.T -> serialization -> unit |
|
36 |
val string: string list -> serialization -> string |
|
37 |
||
38 |
val code_of: theory -> string -> string -> string list -> string list -> string |
|
39 |
val shell_command: string (*theory name*) -> string (*export_code expr*) -> unit |
|
40 |
val code_width: int ref |
|
41 |
||
42 |
val allow_abort: string -> theory -> theory |
|
24219 | 43 |
val add_syntax_class: string -> class |
28054 | 44 |
-> (string * (string * string) list) option -> theory -> theory |
45 |
val add_syntax_inst: string -> string * class -> bool -> theory -> theory |
|
46 |
val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory |
|
24219 | 47 |
val add_syntax_tycoP: string -> string -> OuterParse.token list |
28054 | 48 |
-> (theory -> theory) * OuterParse.token list |
49 |
val add_syntax_const: string -> string -> const_syntax option -> theory -> theory |
|
24219 | 50 |
val add_syntax_constP: string -> string -> OuterParse.token list |
28054 | 51 |
-> (theory -> theory) * OuterParse.token list |
52 |
val add_reserved: string -> string -> theory -> theory |
|
24219 | 53 |
|
28054 | 54 |
val add_pretty_list: string -> string -> string -> theory -> theory |
24219 | 55 |
val add_pretty_list_string: string -> string -> string |
28054 | 56 |
-> string -> string list -> theory -> theory |
24219 | 57 |
val add_pretty_char: string -> string -> string list -> theory -> theory |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
58 |
val add_pretty_numeral: string -> bool -> bool -> string -> string -> string |
28054 | 59 |
-> string -> string -> theory -> theory |
24992 | 60 |
val add_pretty_message: string -> string -> string list -> string |
28054 | 61 |
-> string -> string -> theory -> theory |
24219 | 62 |
end; |
63 |
||
28054 | 64 |
structure Code_Target : CODE_TARGET = |
24219 | 65 |
struct |
66 |
||
28054 | 67 |
open Basic_Code_Thingol; |
68 |
open Code_Printer; |
|
24219 | 69 |
|
70 |
(** basics **) |
|
71 |
||
27304 | 72 |
datatype destination = Compile | Export | File of Path.T | String of string list; |
27436 | 73 |
type serialization = destination -> (string * string list) option; |
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
74 |
|
27103 | 75 |
val code_width = ref 80; (*FIXME after Pretty module no longer depends on print mode*) |
76 |
fun code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin (!code_width) f); |
|
77 |
fun code_of_pretty p = code_setmp Pretty.string_of p ^ "\n"; |
|
78 |
fun code_writeln p = Pretty.setmp_margin (!code_width) Pretty.writeln p; |
|
26998 | 79 |
|
27103 | 80 |
(*FIXME why another code_setmp?*) |
81 |
fun compile f = (code_setmp f Compile; ()); |
|
27304 | 82 |
fun export f = (code_setmp f Export; ()); |
27103 | 83 |
fun file p f = (code_setmp f (File p); ()); |
27436 | 84 |
fun string cs f = fst (the (code_setmp f (String cs))); |
27304 | 85 |
|
86 |
fun stmt_names_of_destination (String stmts) = stmts |
|
87 |
| stmt_names_of_destination _ = []; |
|
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
88 |
|
28054 | 89 |
fun mk_serialization target (SOME comp) _ _ code Compile = (comp code; NONE) |
90 |
| mk_serialization target NONE _ _ _ Compile = error (target ^ ": no internal compilation") |
|
91 |
| mk_serialization target _ output _ code Export = (output NONE code ; NONE) |
|
92 |
| mk_serialization target _ output _ code (File file) = (output (SOME file) code; NONE) |
|
93 |
| mk_serialization target _ _ string code (String _) = SOME (string code); |
|
27550 | 94 |
|
24219 | 95 |
|
28054 | 96 |
(** pretty syntax **) |
24219 | 97 |
|
98 |
(* list, char, string, numeral and monad abstract syntax transformations *) |
|
99 |
||
100 |
fun implode_list c_nil c_cons t = |
|
101 |
let |
|
102 |
fun dest_cons (IConst (c, _) `$ t1 `$ t2) = |
|
103 |
if c = c_cons |
|
104 |
then SOME (t1, t2) |
|
105 |
else NONE |
|
106 |
| dest_cons _ = NONE; |
|
28054 | 107 |
val (ts, t') = Code_Thingol.unfoldr dest_cons t; |
24219 | 108 |
in case t' |
109 |
of IConst (c, _) => if c = c_nil then SOME ts else NONE |
|
110 |
| _ => NONE |
|
111 |
end; |
|
112 |
||
113 |
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) = |
|
114 |
let |
|
115 |
fun idx c = find_index (curry (op =) c) c_nibbles; |
|
116 |
fun decode ~1 _ = NONE |
|
117 |
| decode _ ~1 = NONE |
|
118 |
| decode n m = SOME (chr (n * 16 + m)); |
|
119 |
in decode (idx c1) (idx c2) end |
|
120 |
| decode_char _ _ = NONE; |
|
121 |
||
122 |
fun implode_string c_char c_nibbles mk_char mk_string ts = |
|
123 |
let |
|
124 |
fun implode_char (IConst (c, _) `$ t1 `$ t2) = |
|
125 |
if c = c_char then decode_char c_nibbles (t1, t2) else NONE |
|
126 |
| implode_char _ = NONE; |
|
127 |
val ts' = map implode_char ts; |
|
128 |
in if forall is_some ts' |
|
129 |
then (SOME o str o mk_string o implode o map_filter I) ts' |
|
130 |
else NONE |
|
131 |
end; |
|
132 |
||
27304 | 133 |
fun implode_numeral thm negative c_pls c_min c_bit0 c_bit1 = |
24219 | 134 |
let |
25936 | 135 |
fun dest_bit (IConst (c, _)) = if c = c_bit0 then 0 |
136 |
else if c = c_bit1 then 1 |
|
27304 | 137 |
else nerror thm "Illegal numeral expression: illegal bit" |
138 |
| dest_bit _ = nerror thm "Illegal numeral expression: illegal bit"; |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
139 |
fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0 |
25936 | 140 |
else if c = c_min then |
141 |
if negative then SOME ~1 else NONE |
|
27304 | 142 |
else nerror thm "Illegal numeral expression: illegal leading digit" |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
143 |
| dest_numeral (t1 `$ t2) = |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
144 |
let val (n, b) = (dest_numeral t2, dest_bit t1) |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
145 |
in case n of SOME n => SOME (2 * n + b) | NONE => NONE end |
27304 | 146 |
| dest_numeral _ = nerror thm "Illegal numeral expression: illegal term"; |
25936 | 147 |
in dest_numeral #> the_default 0 end; |
24219 | 148 |
|
149 |
||
28054 | 150 |
(* pretty syntax printing *) |
24219 | 151 |
|
152 |
local |
|
153 |
||
26448 | 154 |
fun ocaml_char c = |
155 |
let |
|
156 |
fun chr i = |
|
157 |
let |
|
158 |
val xs = string_of_int i; |
|
159 |
val ys = replicate_string (3 - length (explode xs)) "0"; |
|
160 |
in "\\" ^ ys ^ xs end; |
|
161 |
val i = ord c; |
|
162 |
val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126 |
|
163 |
then chr i else c |
|
164 |
in s end; |
|
165 |
||
166 |
fun haskell_char c = |
|
167 |
let |
|
168 |
val s = ML_Syntax.print_char c; |
|
169 |
in if s = "'" then "\\'" else s end; |
|
170 |
||
24219 | 171 |
val pretty : (string * { |
172 |
pretty_char: string -> string, |
|
173 |
pretty_string: string -> string, |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
174 |
pretty_numeral: bool -> int -> string, |
24219 | 175 |
pretty_list: Pretty.T list -> Pretty.T, |
176 |
infix_cons: int * string |
|
177 |
}) list = [ |
|
178 |
("SML", { pretty_char = prefix "#" o quote o ML_Syntax.print_char, |
|
26448 | 179 |
pretty_string = quote o translate_string ML_Syntax.print_char, |
24219 | 180 |
pretty_numeral = fn unbounded => fn k => |
24750 | 181 |
if unbounded then "(" ^ string_of_int k ^ " : IntInf.int)" |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
182 |
else string_of_int k, |
24219 | 183 |
pretty_list = Pretty.enum "," "[" "]", |
184 |
infix_cons = (7, "::")}), |
|
26448 | 185 |
("OCaml", { pretty_char = enclose "'" "'" o ocaml_char, |
186 |
pretty_string = quote o translate_string ocaml_char, |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
187 |
pretty_numeral = fn unbounded => fn k => if k >= 0 then |
24219 | 188 |
if unbounded then |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
189 |
"(Big_int.big_int_of_int " ^ string_of_int k ^ ")" |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
190 |
else string_of_int k |
24219 | 191 |
else |
192 |
if unbounded then |
|
193 |
"(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-" |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
194 |
o string_of_int o op ~) k ^ ")" |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
195 |
else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k, |
24219 | 196 |
pretty_list = Pretty.enum ";" "[" "]", |
197 |
infix_cons = (6, "::")}), |
|
26448 | 198 |
("Haskell", { pretty_char = enclose "'" "'" o haskell_char, |
199 |
pretty_string = quote o translate_string haskell_char, |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
200 |
pretty_numeral = fn unbounded => fn k => if k >= 0 then string_of_int k |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
201 |
else enclose "(" ")" (signed_string_of_int k), |
24219 | 202 |
pretty_list = Pretty.enum "," "[" "]", |
203 |
infix_cons = (5, ":")}) |
|
204 |
]; |
|
205 |
||
206 |
in |
|
207 |
||
208 |
fun pr_pretty target = case AList.lookup (op =) pretty target |
|
209 |
of SOME x => x |
|
210 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
211 |
||
212 |
fun default_list (target_fxy, target_cons) pr fxy t1 t2 = |
|
213 |
brackify_infix (target_fxy, R) fxy [ |
|
214 |
pr (INFX (target_fxy, X)) t1, |
|
215 |
str target_cons, |
|
216 |
pr (INFX (target_fxy, R)) t2 |
|
217 |
]; |
|
218 |
||
219 |
fun pretty_list c_nil c_cons target = |
|
220 |
let |
|
221 |
val pretty_ops = pr_pretty target; |
|
222 |
val mk_list = #pretty_list pretty_ops; |
|
27304 | 223 |
fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] = |
24219 | 224 |
case Option.map (cons t1) (implode_list c_nil c_cons t2) |
225 |
of SOME ts => mk_list (map (pr vars NOBR) ts) |
|
226 |
| NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2; |
|
227 |
in (2, pretty) end; |
|
228 |
||
229 |
fun pretty_list_string c_nil c_cons c_char c_nibbles target = |
|
230 |
let |
|
231 |
val pretty_ops = pr_pretty target; |
|
232 |
val mk_list = #pretty_list pretty_ops; |
|
233 |
val mk_char = #pretty_char pretty_ops; |
|
234 |
val mk_string = #pretty_string pretty_ops; |
|
27304 | 235 |
fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] = |
24219 | 236 |
case Option.map (cons t1) (implode_list c_nil c_cons t2) |
26448 | 237 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts |
24219 | 238 |
of SOME p => p |
26448 | 239 |
| NONE => mk_list (map (pr vars NOBR) ts)) |
24219 | 240 |
| NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2; |
241 |
in (2, pretty) end; |
|
242 |
||
243 |
fun pretty_char c_char c_nibbles target = |
|
244 |
let |
|
245 |
val mk_char = #pretty_char (pr_pretty target); |
|
27304 | 246 |
fun pretty _ thm _ _ _ [(t1, _), (t2, _)] = |
24219 | 247 |
case decode_char c_nibbles (t1, t2) |
248 |
of SOME c => (str o mk_char) c |
|
27304 | 249 |
| NONE => nerror thm "Illegal character expression"; |
24219 | 250 |
in (2, pretty) end; |
251 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
252 |
fun pretty_numeral unbounded negative c_pls c_min c_bit0 c_bit1 target = |
24219 | 253 |
let |
254 |
val mk_numeral = #pretty_numeral (pr_pretty target); |
|
27304 | 255 |
fun pretty _ thm _ _ _ [(t, _)] = |
256 |
(str o mk_numeral unbounded o implode_numeral thm negative c_pls c_min c_bit0 c_bit1) t; |
|
24219 | 257 |
in (1, pretty) end; |
258 |
||
24992 | 259 |
fun pretty_message c_char c_nibbles c_nil c_cons target = |
24219 | 260 |
let |
261 |
val pretty_ops = pr_pretty target; |
|
262 |
val mk_char = #pretty_char pretty_ops; |
|
263 |
val mk_string = #pretty_string pretty_ops; |
|
27304 | 264 |
fun pretty _ thm _ _ _ [(t, _)] = |
24219 | 265 |
case implode_list c_nil c_cons t |
266 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts |
|
267 |
of SOME p => p |
|
27304 | 268 |
| NONE => nerror thm "Illegal message expression") |
269 |
| NONE => nerror thm "Illegal message expression"; |
|
24219 | 270 |
in (1, pretty) end; |
271 |
||
272 |
end; (*local*) |
|
273 |
||
27000 | 274 |
|
28054 | 275 |
(** theory data **) |
276 |
||
277 |
datatype name_syntax_table = NameSyntaxTable of { |
|
278 |
class: class_syntax Symtab.table, |
|
279 |
inst: unit Symtab.table, |
|
280 |
tyco: tyco_syntax Symtab.table, |
|
281 |
const: const_syntax Symtab.table |
|
282 |
}; |
|
27000 | 283 |
|
28054 | 284 |
fun mk_name_syntax_table ((class, inst), (tyco, const)) = |
285 |
NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const }; |
|
286 |
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) = |
|
287 |
mk_name_syntax_table (f ((class, inst), (tyco, const))); |
|
288 |
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 }, |
|
289 |
NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) = |
|
290 |
mk_name_syntax_table ( |
|
291 |
(Symtab.join (K snd) (class1, class2), |
|
292 |
Symtab.join (K snd) (inst1, inst2)), |
|
293 |
(Symtab.join (K snd) (tyco1, tyco2), |
|
294 |
Symtab.join (K snd) (const1, const2)) |
|
295 |
); |
|
296 |
||
297 |
type serializer = |
|
298 |
string option (*module name*) |
|
299 |
-> Args.T list (*arguments*) |
|
300 |
-> (string -> string) (*labelled_name*) |
|
301 |
-> string list (*reserved symbols*) |
|
302 |
-> (string * Pretty.T) list (*includes*) |
|
303 |
-> (string -> string option) (*module aliasses*) |
|
304 |
-> (string -> class_syntax option) |
|
305 |
-> (string -> tyco_syntax option) |
|
306 |
-> (string -> const_syntax option) |
|
307 |
-> Code_Thingol.program |
|
308 |
-> string list (*selected statements*) |
|
309 |
-> serialization; |
|
27000 | 310 |
|
28054 | 311 |
datatype serializer_entry = Serializer of serializer |
312 |
| Extends of string * (Code_Thingol.program -> Code_Thingol.program); |
|
313 |
||
314 |
datatype target = Target of { |
|
315 |
serial: serial, |
|
316 |
serializer: serializer_entry, |
|
317 |
reserved: string list, |
|
318 |
includes: Pretty.T Symtab.table, |
|
319 |
name_syntax_table: name_syntax_table, |
|
320 |
module_alias: string Symtab.table |
|
321 |
}; |
|
27103 | 322 |
|
28054 | 323 |
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) = |
324 |
Target { serial = serial, serializer = serializer, reserved = reserved, |
|
325 |
includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias }; |
|
326 |
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) = |
|
327 |
mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias)))); |
|
328 |
fun merge_target strict target (Target { serial = serial1, serializer = serializer, |
|
329 |
reserved = reserved1, includes = includes1, |
|
330 |
name_syntax_table = name_syntax_table1, module_alias = module_alias1 }, |
|
331 |
Target { serial = serial2, serializer = _, |
|
332 |
reserved = reserved2, includes = includes2, |
|
333 |
name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) = |
|
334 |
if serial1 = serial2 orelse not strict then |
|
335 |
mk_target ((serial1, serializer), |
|
336 |
((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)), |
|
337 |
(merge_name_syntax_table (name_syntax_table1, name_syntax_table2), |
|
338 |
Symtab.join (K snd) (module_alias1, module_alias2)) |
|
339 |
)) |
|
340 |
else |
|
341 |
error ("Incompatible serializers: " ^ quote target); |
|
27103 | 342 |
|
28054 | 343 |
structure CodeTargetData = TheoryDataFun |
27436 | 344 |
( |
28054 | 345 |
type T = target Symtab.table * string list; |
346 |
val empty = (Symtab.empty, []); |
|
347 |
val copy = I; |
|
348 |
val extend = I; |
|
349 |
fun merge _ ((target1, exc1) : T, (target2, exc2)) = |
|
350 |
(Symtab.join (merge_target true) (target1, target2), Library.merge (op =) (exc1, exc2)); |
|
27436 | 351 |
); |
352 |
||
28054 | 353 |
fun the_serializer (Target { serializer, ... }) = serializer; |
354 |
fun the_reserved (Target { reserved, ... }) = reserved; |
|
355 |
fun the_includes (Target { includes, ... }) = includes; |
|
356 |
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x; |
|
357 |
fun the_module_alias (Target { module_alias , ... }) = module_alias; |
|
27437 | 358 |
|
28054 | 359 |
val abort_allowed = snd o CodeTargetData.get; |
27436 | 360 |
|
28054 | 361 |
fun assert_target thy target = |
362 |
case Symtab.lookup (fst (CodeTargetData.get thy)) target |
|
363 |
of SOME data => target |
|
364 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
365 |
||
366 |
fun put_target (target, seri) thy = |
|
27304 | 367 |
let |
28054 | 368 |
val defined_target = is_some o Symtab.lookup (fst (CodeTargetData.get thy)); |
369 |
val _ = case seri |
|
370 |
of Extends (super, _) => if defined_target super then () |
|
371 |
else error ("Unknown code target language: " ^ quote super) |
|
372 |
| _ => (); |
|
373 |
val _ = if defined_target target |
|
374 |
then warning ("Overwriting existing target " ^ quote target) |
|
375 |
else (); |
|
376 |
in |
|
377 |
thy |
|
378 |
|> (CodeTargetData.map o apfst oo Symtab.map_default) |
|
379 |
(target, mk_target ((serial (), seri), (([], Symtab.empty), |
|
380 |
(mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)), |
|
381 |
Symtab.empty)))) |
|
382 |
((map_target o apfst o apsnd o K) seri) |
|
383 |
end; |
|
27436 | 384 |
|
28054 | 385 |
fun add_target (target, seri) = put_target (target, Serializer seri); |
386 |
fun extend_target (target, (super, modify)) = |
|
387 |
put_target (target, Extends (super, modify)); |
|
27436 | 388 |
|
28054 | 389 |
fun map_target_data target f thy = |
27436 | 390 |
let |
28054 | 391 |
val _ = assert_target thy target; |
392 |
in |
|
393 |
thy |
|
394 |
|> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f |
|
395 |
end; |
|
27304 | 396 |
|
28054 | 397 |
fun map_reserved target = |
398 |
map_target_data target o apsnd o apfst o apfst; |
|
399 |
fun map_includes target = |
|
400 |
map_target_data target o apsnd o apfst o apsnd; |
|
401 |
fun map_name_syntax target = |
|
402 |
map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table; |
|
403 |
fun map_module_alias target = |
|
404 |
map_target_data target o apsnd o apsnd o apsnd; |
|
27000 | 405 |
|
406 |
||
28054 | 407 |
(** serializer configuration **) |
27000 | 408 |
|
409 |
(* data access *) |
|
24219 | 410 |
|
411 |
local |
|
412 |
||
24992 | 413 |
fun cert_class thy class = |
414 |
let |
|
415 |
val _ = AxClass.get_info thy class; |
|
416 |
in class end; |
|
417 |
||
27103 | 418 |
fun read_class thy = cert_class thy o Sign.intern_class thy; |
24992 | 419 |
|
420 |
fun cert_tyco thy tyco = |
|
421 |
let |
|
422 |
val _ = if Sign.declared_tyname thy tyco then () |
|
423 |
else error ("No such type constructor: " ^ quote tyco); |
|
424 |
in tyco end; |
|
425 |
||
27103 | 426 |
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy; |
24219 | 427 |
|
428 |
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy = |
|
429 |
let |
|
24841 | 430 |
val class = prep_class thy raw_class; |
28054 | 431 |
val class' = Code_Name.class thy class; |
24841 | 432 |
fun mk_classparam c = case AxClass.class_of_param thy c |
28054 | 433 |
of SOME class'' => if class = class'' then Code_Name.const thy c |
24219 | 434 |
else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c) |
435 |
| NONE => error ("Not a class operation: " ^ quote c); |
|
24841 | 436 |
fun mk_syntax_params raw_params = AList.lookup (op =) |
437 |
((map o apfst) (mk_classparam o prep_const thy) raw_params); |
|
24219 | 438 |
in case raw_syn |
24841 | 439 |
of SOME (syntax, raw_params) => |
24219 | 440 |
thy |
27024 | 441 |
|> (map_name_syntax target o apfst o apfst) |
24841 | 442 |
(Symtab.update (class', (syntax, mk_syntax_params raw_params))) |
24219 | 443 |
| NONE => |
444 |
thy |
|
27024 | 445 |
|> (map_name_syntax target o apfst o apfst) |
24841 | 446 |
(Symtab.delete_safe class') |
24219 | 447 |
end; |
448 |
||
449 |
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy = |
|
450 |
let |
|
28054 | 451 |
val inst = Code_Name.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco); |
24219 | 452 |
in if add_del then |
453 |
thy |
|
27024 | 454 |
|> (map_name_syntax target o apfst o apsnd) |
24219 | 455 |
(Symtab.update (inst, ())) |
456 |
else |
|
457 |
thy |
|
27024 | 458 |
|> (map_name_syntax target o apfst o apsnd) |
24219 | 459 |
(Symtab.delete_safe inst) |
460 |
end; |
|
461 |
||
462 |
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy = |
|
463 |
let |
|
464 |
val tyco = prep_tyco thy raw_tyco; |
|
28054 | 465 |
val tyco' = if tyco = "fun" then "fun" else Code_Name.tyco thy tyco; |
24219 | 466 |
fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco |
467 |
then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco) |
|
468 |
else syntax |
|
469 |
in case raw_syn |
|
470 |
of SOME syntax => |
|
471 |
thy |
|
27024 | 472 |
|> (map_name_syntax target o apsnd o apfst) |
24219 | 473 |
(Symtab.update (tyco', check_args syntax)) |
474 |
| NONE => |
|
475 |
thy |
|
27024 | 476 |
|> (map_name_syntax target o apsnd o apfst) |
24219 | 477 |
(Symtab.delete_safe tyco') |
478 |
end; |
|
479 |
||
480 |
fun gen_add_syntax_const prep_const target raw_c raw_syn thy = |
|
481 |
let |
|
482 |
val c = prep_const thy raw_c; |
|
28054 | 483 |
val c' = Code_Name.const thy c; |
484 |
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
|
485 |
then error ("Too many arguments in syntax for constant " ^ quote c) |
24219 | 486 |
else syntax; |
487 |
in case raw_syn |
|
488 |
of SOME syntax => |
|
489 |
thy |
|
27024 | 490 |
|> (map_name_syntax target o apsnd o apsnd) |
24219 | 491 |
(Symtab.update (c', check_args syntax)) |
492 |
| NONE => |
|
493 |
thy |
|
27024 | 494 |
|> (map_name_syntax target o apsnd o apsnd) |
24219 | 495 |
(Symtab.delete_safe c') |
496 |
end; |
|
497 |
||
498 |
fun add_reserved target = |
|
499 |
let |
|
500 |
fun add sym syms = if member (op =) syms sym |
|
501 |
then error ("Reserved symbol " ^ quote sym ^ " already declared") |
|
502 |
else insert (op =) sym syms |
|
27103 | 503 |
in map_reserved target o add end; |
24992 | 504 |
|
505 |
fun add_include target = |
|
506 |
let |
|
507 |
fun add (name, SOME content) incls = |
|
508 |
let |
|
509 |
val _ = if Symtab.defined incls name |
|
510 |
then warning ("Overwriting existing include " ^ name) |
|
511 |
else (); |
|
512 |
in Symtab.update (name, str content) incls end |
|
513 |
| add (name, NONE) incls = |
|
514 |
Symtab.delete name incls; |
|
27103 | 515 |
in map_includes target o add end; |
24219 | 516 |
|
27103 | 517 |
fun add_module_alias target = |
28054 | 518 |
map_module_alias target o Symtab.update o apsnd Code_Name.check_modulename; |
24219 | 519 |
|
27103 | 520 |
fun gen_allow_abort prep_cs raw_c thy = |
24841 | 521 |
let |
522 |
val c = prep_cs thy raw_c; |
|
28054 | 523 |
val c' = Code_Name.const thy c; |
24841 | 524 |
in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end; |
525 |
||
24219 | 526 |
fun zip_list (x::xs) f g = |
527 |
f |
|
528 |
#-> (fn y => |
|
529 |
fold_map (fn x => g |-- f >> pair x) xs |
|
530 |
#-> (fn xys => pair ((x, y) :: xys))); |
|
531 |
||
27000 | 532 |
|
27103 | 533 |
(* concrete syntax *) |
27000 | 534 |
|
24219 | 535 |
structure P = OuterParse |
536 |
and K = OuterKeyword |
|
537 |
||
538 |
fun parse_multi_syntax parse_thing parse_syntax = |
|
539 |
P.and_list1 parse_thing |
|
540 |
#-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name -- |
|
541 |
(zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")")); |
|
542 |
||
543 |
in |
|
544 |
||
545 |
val parse_syntax = parse_syntax; |
|
546 |
||
547 |
val add_syntax_class = gen_add_syntax_class cert_class (K I); |
|
548 |
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco; |
|
549 |
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco; |
|
550 |
val add_syntax_const = gen_add_syntax_const (K I); |
|
27103 | 551 |
val allow_abort = gen_allow_abort (K I); |
28054 | 552 |
val add_reserved = add_reserved; |
24219 | 553 |
|
28054 | 554 |
val add_syntax_class_cmd = gen_add_syntax_class read_class Code_Unit.read_const; |
24219 | 555 |
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco; |
556 |
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco; |
|
28054 | 557 |
val add_syntax_const_cmd = gen_add_syntax_const Code_Unit.read_const; |
558 |
val allow_abort_cmd = gen_allow_abort Code_Unit.read_const; |
|
24219 | 559 |
|
28054 | 560 |
fun add_syntax_tycoP target tyco = parse_syntax I |
561 |
>> add_syntax_tyco_cmd target tyco; |
|
562 |
fun add_syntax_constP target c = parse_syntax fst |
|
563 |
>> (add_syntax_const_cmd target c o Code_Printer.simple_const_syntax); |
|
24219 | 564 |
|
565 |
fun add_pretty_list target nill cons thy = |
|
566 |
let |
|
28054 | 567 |
val nil' = Code_Name.const thy nill; |
568 |
val cons' = Code_Name.const thy cons; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
569 |
val pr = pretty_list nil' cons' target; |
24219 | 570 |
in |
571 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
572 |
|> add_syntax_const target cons (SOME pr) |
24219 | 573 |
end; |
574 |
||
575 |
fun add_pretty_list_string target nill cons charr nibbles thy = |
|
576 |
let |
|
28054 | 577 |
val nil' = Code_Name.const thy nill; |
578 |
val cons' = Code_Name.const thy cons; |
|
579 |
val charr' = Code_Name.const thy charr; |
|
580 |
val nibbles' = map (Code_Name.const thy) nibbles; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
581 |
val pr = pretty_list_string nil' cons' charr' nibbles' target; |
24219 | 582 |
in |
583 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
584 |
|> add_syntax_const target cons (SOME pr) |
24219 | 585 |
end; |
586 |
||
587 |
fun add_pretty_char target charr nibbles thy = |
|
588 |
let |
|
28054 | 589 |
val charr' = Code_Name.const thy charr; |
590 |
val nibbles' = map (Code_Name.const thy) nibbles; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
591 |
val pr = pretty_char charr' nibbles' target; |
24219 | 592 |
in |
593 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
594 |
|> add_syntax_const target charr (SOME pr) |
24219 | 595 |
end; |
596 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
597 |
fun add_pretty_numeral target unbounded negative number_of pls min bit0 bit1 thy = |
24219 | 598 |
let |
28054 | 599 |
val pls' = Code_Name.const thy pls; |
600 |
val min' = Code_Name.const thy min; |
|
601 |
val bit0' = Code_Name.const thy bit0; |
|
602 |
val bit1' = Code_Name.const thy bit1; |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
603 |
val pr = pretty_numeral unbounded negative pls' min' bit0' bit1' target; |
24219 | 604 |
in |
605 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
606 |
|> add_syntax_const target number_of (SOME pr) |
24219 | 607 |
end; |
608 |
||
24992 | 609 |
fun add_pretty_message target charr nibbles nill cons str thy = |
24219 | 610 |
let |
28054 | 611 |
val charr' = Code_Name.const thy charr; |
612 |
val nibbles' = map (Code_Name.const thy) nibbles; |
|
613 |
val nil' = Code_Name.const thy nill; |
|
614 |
val cons' = Code_Name.const thy cons; |
|
24992 | 615 |
val pr = pretty_message charr' nibbles' nil' cons' target; |
24219 | 616 |
in |
617 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
618 |
|> add_syntax_const target str (SOME pr) |
24219 | 619 |
end; |
620 |
||
24867 | 621 |
|
28054 | 622 |
(** serializer usage **) |
623 |
||
624 |
(* montage *) |
|
625 |
||
626 |
fun invoke_serializer thy modify abortable serializer reserved includes |
|
627 |
module_alias class inst tyco const module args program1 cs1 = |
|
628 |
let |
|
629 |
val program2 = modify program1; |
|
630 |
val hidden = Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const; |
|
631 |
val cs2 = subtract (op =) hidden cs1; |
|
632 |
val program3 = Graph.subgraph (not o member (op =) hidden) program2; |
|
633 |
val all_cs = Graph.all_succs program2 cs2; |
|
634 |
val program4 = Graph.subgraph (member (op =) all_cs) program3; |
|
635 |
val empty_funs = filter_out (member (op =) abortable) |
|
636 |
(Code_Thingol.empty_funs program3); |
|
637 |
val _ = if null empty_funs then () else error ("No defining equations for " |
|
638 |
^ commas (map (Code_Name.labelled_name thy) empty_funs)); |
|
639 |
in |
|
640 |
serializer module args (Code_Name.labelled_name thy) reserved includes |
|
641 |
(Symtab.lookup module_alias) (Symtab.lookup class) |
|
642 |
(Symtab.lookup tyco) (Symtab.lookup const) |
|
643 |
program4 cs2 |
|
644 |
end; |
|
645 |
||
646 |
fun mount_serializer thy alt_serializer target = |
|
647 |
let |
|
648 |
val (targets, abortable) = CodeTargetData.get thy; |
|
649 |
fun collapse_hierarchy target = |
|
650 |
let |
|
651 |
val data = case Symtab.lookup targets target |
|
652 |
of SOME data => data |
|
653 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
654 |
in case the_serializer data |
|
655 |
of Serializer _ => (I, data) |
|
656 |
| Extends (super, modify) => let |
|
657 |
val (modify', data') = collapse_hierarchy super |
|
658 |
in (modify' #> modify, merge_target false target (data', data)) end |
|
659 |
end; |
|
660 |
val (modify, data) = collapse_hierarchy target; |
|
661 |
val serializer = the_default (case the_serializer data |
|
662 |
of Serializer seri => seri) alt_serializer; |
|
663 |
val reserved = the_reserved data; |
|
664 |
val includes = Symtab.dest (the_includes data); |
|
665 |
val module_alias = the_module_alias data; |
|
666 |
val { class, inst, tyco, const } = the_name_syntax data; |
|
667 |
in |
|
668 |
invoke_serializer thy modify abortable serializer reserved |
|
669 |
includes module_alias class inst tyco const |
|
670 |
end; |
|
671 |
||
672 |
fun serialize thy = mount_serializer thy NONE; |
|
673 |
||
674 |
fun serialize_custom thy (target_name, seri) program cs = |
|
675 |
mount_serializer thy (SOME seri) target_name NONE [] program cs (String []) |
|
676 |
|> the; |
|
677 |
||
678 |
fun parse_args f args = |
|
679 |
case Scan.read OuterLex.stopper f args |
|
680 |
of SOME x => x |
|
681 |
| NONE => error "Bad serializer arguments"; |
|
682 |
||
683 |
||
684 |
(* code presentation *) |
|
685 |
||
686 |
fun code_of thy target module_name cs stmt_names = |
|
687 |
let |
|
688 |
val (cs', program) = Code_Thingol.consts_program thy cs; |
|
689 |
in |
|
690 |
string stmt_names (serialize thy target (SOME module_name) [] program cs') |
|
691 |
end; |
|
692 |
||
693 |
||
694 |
(* code generation *) |
|
695 |
||
696 |
fun read_const_exprs thy cs = |
|
697 |
let |
|
698 |
val (cs1, cs2) = Code_Name.read_const_exprs thy cs; |
|
699 |
val (cs3, program) = Code_Thingol.consts_program thy cs2; |
|
700 |
val cs4 = Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy); |
|
701 |
val cs5 = map_filter |
|
702 |
(fn (c, c') => if member (op =) cs4 c' then SOME c else NONE) (cs2 ~~ cs3); |
|
703 |
in fold (insert (op =)) cs5 cs1 end; |
|
704 |
||
705 |
fun cached_program thy = |
|
706 |
let |
|
707 |
val program = Code_Thingol.cached_program thy; |
|
708 |
in (Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy), program) end |
|
709 |
||
710 |
fun export_code thy cs seris = |
|
711 |
let |
|
712 |
val (cs', program) = if null cs then cached_program thy |
|
713 |
else Code_Thingol.consts_program thy cs; |
|
714 |
fun mk_seri_dest dest = case dest |
|
715 |
of NONE => compile |
|
716 |
| SOME "-" => export |
|
717 |
| SOME f => file (Path.explode f) |
|
718 |
val _ = map (fn (((target, module), dest), args) => |
|
719 |
(mk_seri_dest dest (serialize thy target module args program cs'))) seris; |
|
720 |
in () end; |
|
721 |
||
722 |
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris; |
|
723 |
||
27103 | 724 |
|
27304 | 725 |
(** Isar setup **) |
27103 | 726 |
|
727 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file"); |
|
728 |
||
729 |
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
|
730 |
(Scan.repeat P.term_group |
27103 | 731 |
-- Scan.repeat (P.$$$ inK |-- P.name |
732 |
-- Scan.option (P.$$$ module_nameK |-- P.name) |
|
733 |
-- Scan.option (P.$$$ fileK |-- P.name) |
|
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27757
diff
changeset
|
734 |
-- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") [] |
27103 | 735 |
) >> (fn (raw_cs, seris) => cmd raw_cs seris)); |
736 |
||
28054 | 737 |
val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK]; |
24867 | 738 |
|
739 |
val _ = |
|
24992 | 740 |
OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl ( |
24219 | 741 |
parse_multi_syntax P.xname |
742 |
(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
|
743 |
(P.term_group --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) [])) |
24219 | 744 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
745 |
fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns) |
|
746 |
); |
|
747 |
||
24867 | 748 |
val _ = |
24992 | 749 |
OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl ( |
24219 | 750 |
parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) |
751 |
((P.minus >> K true) || Scan.succeed false) |
|
752 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
753 |
fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns) |
|
754 |
); |
|
755 |
||
24867 | 756 |
val _ = |
24992 | 757 |
OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl ( |
24219 | 758 |
parse_multi_syntax P.xname (parse_syntax I) |
759 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
760 |
fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns) |
|
761 |
); |
|
762 |
||
24867 | 763 |
val _ = |
24992 | 764 |
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
|
765 |
parse_multi_syntax P.term_group (parse_syntax fst) |
24219 | 766 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
28054 | 767 |
fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const |
768 |
(Code_Printer.simple_const_syntax syn)) syns) |
|
24219 | 769 |
); |
770 |
||
24867 | 771 |
val _ = |
24992 | 772 |
OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl ( |
24219 | 773 |
P.name -- Scan.repeat1 P.name |
774 |
>> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds) |
|
24841 | 775 |
); |
24219 | 776 |
|
24867 | 777 |
val _ = |
24992 | 778 |
OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl ( |
779 |
P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s)) |
|
780 |
>> (fn ((target, name), content) => (Toplevel.theory o add_include target) |
|
781 |
(name, content)) |
|
782 |
); |
|
783 |
||
784 |
val _ = |
|
785 |
OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl ( |
|
24219 | 786 |
P.name -- Scan.repeat1 (P.name -- P.name) |
27103 | 787 |
>> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames) |
788 |
); |
|
789 |
||
790 |
val _ = |
|
791 |
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
|
792 |
Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd) |
24841 | 793 |
); |
24219 | 794 |
|
24867 | 795 |
val _ = |
27103 | 796 |
OuterSyntax.command "export_code" "generate executable code for constants" |
797 |
K.diag (P.!!! (code_exprP export_code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
|
798 |
||
799 |
fun shell_command thyname cmd = Toplevel.program (fn _ => |
|
27845 | 800 |
(use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP export_code_cmd)) |
801 |
((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd) |
|
27103 | 802 |
of SOME f => (writeln "Now generating code..."; f (theory thyname)) |
803 |
| NONE => error ("Bad directive " ^ quote cmd))) |
|
804 |
handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure; |
|
805 |
||
24219 | 806 |
end; (*local*) |
807 |
||
808 |
end; (*struct*) |