src/HOL/Tools/string_code.ML
author haftmann
Tue May 19 16:54:55 2009 +0200 (2009-05-19)
changeset 31205 98370b26c2ce
parent 31055 2cf6efca6c71
child 33989 cb136b5f6050
permissions -rw-r--r--
String.literal replaces message_string, code_numeral replaces (code_)index
     1 (* Author: Florian Haftmann, TU Muenchen
     2 
     3 Code generation for character and string literals.
     4 *)
     5 
     6 signature STRING_CODE =
     7 sig
     8   val add_literal_list_string: string -> theory -> theory
     9   val add_literal_char: string -> theory -> theory
    10   val add_literal_string: string -> theory -> theory
    11 end;
    12 
    13 structure String_Code : STRING_CODE =
    14 struct
    15 
    16 open Basic_Code_Thingol;
    17 
    18 fun decode_char nibbles' tt =
    19   let
    20     fun idx c = find_index (curry (op =) c) nibbles';
    21     fun decode ~1 _ = NONE
    22       | decode _ ~1 = NONE
    23       | decode n m = SOME (chr (n * 16 + m));
    24   in case tt
    25    of (IConst (c1, _), IConst (c2, _)) => decode (idx c1) (idx c2)
    26     | _ => NONE
    27   end;
    28    
    29 fun implode_string char' nibbles' mk_char mk_string ts =
    30   let
    31     fun implode_char (IConst (c, _) `$ t1 `$ t2) =
    32           if c = char' then decode_char nibbles' (t1, t2) else NONE
    33       | implode_char _ = NONE;
    34     val ts' = map_filter implode_char ts;
    35   in if length ts = length ts'
    36     then (SOME o Code_Printer.str o mk_string o implode) ts'
    37     else NONE
    38   end;
    39 
    40 val cs_nibbles = [@{const_name Nibble0}, @{const_name Nibble1},
    41   @{const_name Nibble2}, @{const_name Nibble3},
    42   @{const_name Nibble4}, @{const_name Nibble5},
    43   @{const_name Nibble6}, @{const_name Nibble7},
    44   @{const_name Nibble8}, @{const_name Nibble9},
    45   @{const_name NibbleA}, @{const_name NibbleB},
    46   @{const_name NibbleC}, @{const_name NibbleD},
    47   @{const_name NibbleE}, @{const_name NibbleF}];
    48 val cs_summa = [@{const_name Nil}, @{const_name Cons}, @{const_name Char}] @ cs_nibbles;
    49 
    50 fun add_literal_list_string target =
    51   let
    52     fun pretty literals (nil' :: cons' :: char' :: nibbles') pr thm vars fxy [(t1, _), (t2, _)] =
    53       case Option.map (cons t1) (List_Code.implode_list nil' cons' t2)
    54        of SOME ts => (case implode_string char' nibbles'
    55           (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
    56              of SOME p => p
    57               | NONE =>
    58                   Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts))
    59         | NONE =>
    60             List_Code.default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
    61   in Code_Target.add_syntax_const target
    62     @{const_name Cons} (SOME (2, (cs_summa, pretty)))
    63   end;
    64 
    65 fun add_literal_char target =
    66   let
    67     fun pretty literals nibbles' _ thm _ _ [(t1, _), (t2, _)] =
    68       case decode_char nibbles' (t1, t2)
    69        of SOME c => (Code_Printer.str o Code_Printer.literal_char literals) c
    70         | NONE => Code_Printer.nerror thm "Illegal character expression";
    71   in Code_Target.add_syntax_const target
    72     @{const_name Char} (SOME (2, (cs_nibbles, pretty)))
    73   end;
    74 
    75 fun add_literal_string target =
    76   let
    77     fun pretty literals (nil' :: cons' :: char' :: nibbles') _ thm _ _ [(t, _)] =
    78       case List_Code.implode_list nil' cons' t
    79        of SOME ts => (case implode_string char' nibbles'
    80           (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
    81              of SOME p => p
    82               | NONE => Code_Printer.nerror thm "Illegal message expression")
    83         | NONE => Code_Printer.nerror thm "Illegal message expression";
    84   in Code_Target.add_syntax_const target 
    85     @{const_name STR} (SOME (1, (cs_summa, pretty)))
    86   end;
    87 
    88 end;