src/HOL/Tools/string_code.ML
changeset 31055 2cf6efca6c71
child 31205 98370b26c2ce
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/string_code.ML	Wed May 06 19:09:31 2009 +0200
     1.3 @@ -0,0 +1,88 @@
     1.4 +(* Author: Florian Haftmann, TU Muenchen
     1.5 +
     1.6 +Code generation for character and string literals.
     1.7 +*)
     1.8 +
     1.9 +signature STRING_CODE =
    1.10 +sig
    1.11 +  val add_literal_list_string: string -> theory -> theory
    1.12 +  val add_literal_char: string -> theory -> theory
    1.13 +  val add_literal_message: string -> theory -> theory
    1.14 +end;
    1.15 +
    1.16 +structure String_Code : STRING_CODE =
    1.17 +struct
    1.18 +
    1.19 +open Basic_Code_Thingol;
    1.20 +
    1.21 +fun decode_char nibbles' tt =
    1.22 +  let
    1.23 +    fun idx c = find_index (curry (op =) c) nibbles';
    1.24 +    fun decode ~1 _ = NONE
    1.25 +      | decode _ ~1 = NONE
    1.26 +      | decode n m = SOME (chr (n * 16 + m));
    1.27 +  in case tt
    1.28 +   of (IConst (c1, _), IConst (c2, _)) => decode (idx c1) (idx c2)
    1.29 +    | _ => NONE
    1.30 +  end;
    1.31 +   
    1.32 +fun implode_string char' nibbles' mk_char mk_string ts =
    1.33 +  let
    1.34 +    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
    1.35 +          if c = char' then decode_char nibbles' (t1, t2) else NONE
    1.36 +      | implode_char _ = NONE;
    1.37 +    val ts' = map_filter implode_char ts;
    1.38 +  in if length ts = length ts'
    1.39 +    then (SOME o Code_Printer.str o mk_string o implode) ts'
    1.40 +    else NONE
    1.41 +  end;
    1.42 +
    1.43 +val cs_nibbles = [@{const_name Nibble0}, @{const_name Nibble1},
    1.44 +  @{const_name Nibble2}, @{const_name Nibble3},
    1.45 +  @{const_name Nibble4}, @{const_name Nibble5},
    1.46 +  @{const_name Nibble6}, @{const_name Nibble7},
    1.47 +  @{const_name Nibble8}, @{const_name Nibble9},
    1.48 +  @{const_name NibbleA}, @{const_name NibbleB},
    1.49 +  @{const_name NibbleC}, @{const_name NibbleD},
    1.50 +  @{const_name NibbleE}, @{const_name NibbleF}];
    1.51 +val cs_summa = [@{const_name Nil}, @{const_name Cons}, @{const_name Char}] @ cs_nibbles;
    1.52 +
    1.53 +fun add_literal_list_string target =
    1.54 +  let
    1.55 +    fun pretty literals (nil' :: cons' :: char' :: nibbles') pr thm vars fxy [(t1, _), (t2, _)] =
    1.56 +      case Option.map (cons t1) (List_Code.implode_list nil' cons' t2)
    1.57 +       of SOME ts => (case implode_string char' nibbles'
    1.58 +          (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
    1.59 +             of SOME p => p
    1.60 +              | NONE =>
    1.61 +                  Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts))
    1.62 +        | NONE =>
    1.63 +            List_Code.default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
    1.64 +  in Code_Target.add_syntax_const target
    1.65 +    @{const_name Cons} (SOME (2, (cs_summa, pretty)))
    1.66 +  end;
    1.67 +
    1.68 +fun add_literal_char target =
    1.69 +  let
    1.70 +    fun pretty literals nibbles' _ thm _ _ [(t1, _), (t2, _)] =
    1.71 +      case decode_char nibbles' (t1, t2)
    1.72 +       of SOME c => (Code_Printer.str o Code_Printer.literal_char literals) c
    1.73 +        | NONE => Code_Printer.nerror thm "Illegal character expression";
    1.74 +  in Code_Target.add_syntax_const target
    1.75 +    @{const_name Char} (SOME (2, (cs_nibbles, pretty)))
    1.76 +  end;
    1.77 +
    1.78 +fun add_literal_message target =
    1.79 +  let
    1.80 +    fun pretty literals (nil' :: cons' :: char' :: nibbles') _ thm _ _ [(t, _)] =
    1.81 +      case List_Code.implode_list nil' cons' t
    1.82 +       of SOME ts => (case implode_string char' nibbles'
    1.83 +          (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
    1.84 +             of SOME p => p
    1.85 +              | NONE => Code_Printer.nerror thm "Illegal message expression")
    1.86 +        | NONE => Code_Printer.nerror thm "Illegal message expression";
    1.87 +  in Code_Target.add_syntax_const target 
    1.88 +    @{const_name STR} (SOME (1, (cs_summa, pretty)))
    1.89 +  end;
    1.90 +
    1.91 +end;