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