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