| author | traytel |
| Mon, 01 Sep 2014 14:14:47 +0200 | |
| changeset 58443 | a23780c22245 |
| parent 55236 | 8d61b0aa7a0d |
| child 62597 | b3f2b8c906a6 |
| permissions | -rw-r--r-- |
| 37744 | 1 |
(* Title: HOL/Tools/string_code.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
3 |
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
4 |
Code generation for character and string literals. |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
5 |
*) |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
6 |
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
7 |
signature STRING_CODE = |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
8 |
sig |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
9 |
val add_literal_list_string: string -> theory -> theory |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
10 |
val add_literal_char: string -> theory -> theory |
|
31205
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents:
31055
diff
changeset
|
11 |
val add_literal_string: string -> theory -> theory |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
12 |
end; |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
13 |
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
14 |
structure String_Code : STRING_CODE = |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
15 |
struct |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
16 |
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
17 |
open Basic_Code_Thingol; |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
18 |
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
19 |
val cs_nibbles = [@{const_name Nibble0}, @{const_name Nibble1},
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
20 |
@{const_name Nibble2}, @{const_name Nibble3},
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
21 |
@{const_name Nibble4}, @{const_name Nibble5},
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
22 |
@{const_name Nibble6}, @{const_name Nibble7},
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
23 |
@{const_name Nibble8}, @{const_name Nibble9},
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
24 |
@{const_name NibbleA}, @{const_name NibbleB},
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
25 |
@{const_name NibbleC}, @{const_name NibbleD},
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
26 |
@{const_name NibbleE}, @{const_name NibbleF}];
|
|
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
27 |
|
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
28 |
fun decode_char tt = |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
29 |
let |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
30 |
fun idx c = find_index (curry (op =) c) cs_nibbles; |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
31 |
fun decode ~1 _ = NONE |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
32 |
| decode _ ~1 = NONE |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
33 |
| decode n m = SOME (chr (n * 16 + m)); |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
34 |
in case tt |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
35 |
of (IConst { sym = Code_Symbol.Constant c1, ... }, IConst { sym = Code_Symbol.Constant c2, ... }) => decode (idx c1) (idx c2)
|
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
36 |
| _ => NONE |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
37 |
end; |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
38 |
|
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
39 |
fun implode_string literals ts = |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
40 |
let |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
41 |
fun implode_char (IConst { sym = Code_Symbol.Constant @{const_name Char}, ... } `$ t1 `$ t2) =
|
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
42 |
decode_char (t1, t2) |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
43 |
| implode_char _ = NONE; |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
44 |
val ts' = map_filter implode_char ts; |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
45 |
in if length ts = length ts' |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
46 |
then (SOME o Code_Printer.str o Code_Printer.literal_string literals o implode) ts' |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
47 |
else NONE |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
48 |
end; |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
49 |
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
50 |
fun add_literal_list_string target = |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
51 |
let |
|
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
52 |
fun pretty literals pr _ vars fxy [(t1, _), (t2, _)] = |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
53 |
case Option.map (cons t1) (List_Code.implode_list t2) |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
54 |
of SOME ts => (case implode_string literals ts |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
55 |
of SOME p => p |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
56 |
| NONE => |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
57 |
Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts)) |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
58 |
| NONE => |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
59 |
List_Code.default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2; |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51160
diff
changeset
|
60 |
in |
|
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51160
diff
changeset
|
61 |
Code_Target.set_printings (Code_Symbol.Constant (@{const_name Cons},
|
|
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
62 |
[(target, SOME (Code_Printer.complex_const_syntax (2, pretty)))])) |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
63 |
end; |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
64 |
|
| 55236 | 65 |
fun add_literal_char target thy = |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
66 |
let |
|
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
67 |
fun pretty literals _ thm _ _ [(t1, _), (t2, _)] = |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
68 |
case decode_char (t1, t2) |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
69 |
of SOME c => (Code_Printer.str o Code_Printer.literal_char literals) c |
| 55236 | 70 |
| NONE => Code_Printer.eqn_error thy thm "Illegal character expression"; |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51160
diff
changeset
|
71 |
in |
|
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51160
diff
changeset
|
72 |
Code_Target.set_printings (Code_Symbol.Constant (@{const_name Char},
|
| 55236 | 73 |
[(target, SOME (Code_Printer.complex_const_syntax (2, pretty)))])) thy |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
74 |
end; |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
75 |
|
| 55236 | 76 |
fun add_literal_string target thy = |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
77 |
let |
|
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
78 |
fun pretty literals _ thm _ _ [(t, _)] = |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
79 |
case List_Code.implode_list t |
|
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
80 |
of SOME ts => (case implode_string literals ts |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
81 |
of SOME p => p |
| 55236 | 82 |
| NONE => Code_Printer.eqn_error thy thm "Illegal string literal expression") |
83 |
| NONE => Code_Printer.eqn_error thy thm "Illegal string literal expression"; |
|
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51160
diff
changeset
|
84 |
in |
|
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
51160
diff
changeset
|
85 |
Code_Target.set_printings (Code_Symbol.Constant (@{const_name STR},
|
| 55236 | 86 |
[(target, SOME (Code_Printer.complex_const_syntax (1, pretty)))])) thy |
|
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
87 |
end; |
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
88 |
|
|
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
diff
changeset
|
89 |
end; |