src/Tools/code/code_target.ML
author haftmann
Thu Aug 28 22:09:20 2008 +0200 (2008-08-28)
changeset 28054 2b84d34c5d02
parent 27868 a28b3cd0077b
child 28064 d4a6460c53d1
permissions -rw-r--r--
restructured and split code serializer module
haftmann@24219
     1
(*  Title:      Tools/code/code_target.ML
haftmann@24219
     2
    ID:         $Id$
haftmann@24219
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     4
haftmann@28054
     5
Serializer from intermediate language ("Thin-gol") to target languages.
haftmann@24219
     6
*)
haftmann@24219
     7
haftmann@24219
     8
signature CODE_TARGET =
haftmann@24219
     9
sig
haftmann@28054
    10
  include CODE_PRINTER
haftmann@28054
    11
haftmann@28054
    12
  type serializer
haftmann@28054
    13
  val add_target: string * serializer -> theory -> theory
haftmann@28054
    14
  val extend_target: string * (string * (Code_Thingol.program -> Code_Thingol.program))
haftmann@28054
    15
    -> theory -> theory
haftmann@28054
    16
  val assert_target: theory -> string -> string
haftmann@24219
    17
haftmann@28054
    18
  type destination
haftmann@28054
    19
  type serialization
haftmann@28054
    20
  val parse_args: (OuterLex.token list -> 'a * OuterLex.token list)
haftmann@28054
    21
    -> OuterLex.token list -> 'a
haftmann@28054
    22
  val stmt_names_of_destination: destination -> string list
haftmann@28054
    23
  val code_of_pretty: Pretty.T -> string
haftmann@28054
    24
  val code_writeln: Pretty.T -> unit
haftmann@28054
    25
  val mk_serialization: string -> ('a -> unit) option
haftmann@28054
    26
    -> (Path.T option -> 'a -> unit)
haftmann@28054
    27
    -> ('a -> string * string list)
haftmann@28054
    28
    -> 'a -> serialization
haftmann@28054
    29
  val serialize: theory -> string -> string option -> Args.T list
haftmann@28054
    30
    -> Code_Thingol.program -> string list -> serialization
haftmann@28054
    31
  val serialize_custom: theory -> string * serializer
haftmann@28054
    32
    -> Code_Thingol.program -> string list -> string * string list
haftmann@28054
    33
  val compile: serialization -> unit
haftmann@28054
    34
  val export: serialization -> unit
haftmann@28054
    35
  val file: Path.T -> serialization -> unit
haftmann@28054
    36
  val string: string list -> serialization -> string
haftmann@28054
    37
haftmann@28054
    38
  val code_of: theory -> string -> string -> string list -> string list -> string
haftmann@28054
    39
  val shell_command: string (*theory name*) -> string (*export_code expr*) -> unit
haftmann@28054
    40
  val code_width: int ref
haftmann@28054
    41
haftmann@28054
    42
  val allow_abort: string -> theory -> theory
haftmann@24219
    43
  val add_syntax_class: string -> class
haftmann@28054
    44
    -> (string * (string * string) list) option -> theory -> theory
haftmann@28054
    45
  val add_syntax_inst: string -> string * class -> bool -> theory -> theory
haftmann@28054
    46
  val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory
haftmann@24219
    47
  val add_syntax_tycoP: string -> string -> OuterParse.token list
haftmann@28054
    48
    -> (theory -> theory) * OuterParse.token list
haftmann@28054
    49
  val add_syntax_const: string -> string -> const_syntax option -> theory -> theory
haftmann@24219
    50
  val add_syntax_constP: string -> string -> OuterParse.token list
haftmann@28054
    51
    -> (theory -> theory) * OuterParse.token list
haftmann@28054
    52
  val add_reserved: string -> string -> theory -> theory
haftmann@24219
    53
haftmann@28054
    54
  val add_pretty_list: string -> string -> string -> theory -> theory
haftmann@24219
    55
  val add_pretty_list_string: string -> string -> string
haftmann@28054
    56
    -> string -> string list -> theory -> theory
haftmann@24219
    57
  val add_pretty_char: string -> string -> string list -> theory -> theory
huffman@26086
    58
  val add_pretty_numeral: string -> bool -> bool -> string -> string -> string
haftmann@28054
    59
    -> string -> string -> theory -> theory
haftmann@24992
    60
  val add_pretty_message: string -> string -> string list -> string
haftmann@28054
    61
    -> string -> string -> theory -> theory
haftmann@24219
    62
end;
haftmann@24219
    63
haftmann@28054
    64
structure Code_Target : CODE_TARGET =
haftmann@24219
    65
struct
haftmann@24219
    66
haftmann@28054
    67
open Basic_Code_Thingol;
haftmann@28054
    68
open Code_Printer;
haftmann@24219
    69
haftmann@24219
    70
(** basics **)
haftmann@24219
    71
haftmann@27304
    72
datatype destination = Compile | Export | File of Path.T | String of string list;
haftmann@27436
    73
type serialization = destination -> (string * string list) option;
haftmann@27014
    74
haftmann@27103
    75
val code_width = ref 80; (*FIXME after Pretty module no longer depends on print mode*)
haftmann@27103
    76
fun code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin (!code_width) f);
haftmann@27103
    77
fun code_of_pretty p = code_setmp Pretty.string_of p ^ "\n";
haftmann@27103
    78
fun code_writeln p = Pretty.setmp_margin (!code_width) Pretty.writeln p;
haftmann@26998
    79
haftmann@27103
    80
(*FIXME why another code_setmp?*)
haftmann@27103
    81
fun compile f = (code_setmp f Compile; ());
haftmann@27304
    82
fun export f = (code_setmp f Export; ());
haftmann@27103
    83
fun file p f = (code_setmp f (File p); ());
haftmann@27436
    84
fun string cs f = fst (the (code_setmp f (String cs)));
haftmann@27304
    85
haftmann@27304
    86
fun stmt_names_of_destination (String stmts) = stmts
haftmann@27304
    87
  | stmt_names_of_destination _ = [];
haftmann@27014
    88
haftmann@28054
    89
fun mk_serialization target (SOME comp) _ _ code Compile = (comp code; NONE)
haftmann@28054
    90
  | mk_serialization target NONE _ _ _ Compile = error (target ^ ": no internal compilation")
haftmann@28054
    91
  | mk_serialization target _ output _ code Export = (output NONE code ; NONE)
haftmann@28054
    92
  | mk_serialization target _ output _ code (File file) = (output (SOME file) code; NONE)
haftmann@28054
    93
  | mk_serialization target _ _ string code (String _) = SOME (string code);
haftmann@27550
    94
haftmann@24219
    95
haftmann@28054
    96
(** pretty syntax **)
haftmann@24219
    97
haftmann@24219
    98
(* list, char, string, numeral and monad abstract syntax transformations *)
haftmann@24219
    99
haftmann@24219
   100
fun implode_list c_nil c_cons t =
haftmann@24219
   101
  let
haftmann@24219
   102
    fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
haftmann@24219
   103
          if c = c_cons
haftmann@24219
   104
          then SOME (t1, t2)
haftmann@24219
   105
          else NONE
haftmann@24219
   106
      | dest_cons _ = NONE;
haftmann@28054
   107
    val (ts, t') = Code_Thingol.unfoldr dest_cons t;
haftmann@24219
   108
  in case t'
haftmann@24219
   109
   of IConst (c, _) => if c = c_nil then SOME ts else NONE
haftmann@24219
   110
    | _ => NONE
haftmann@24219
   111
  end;
haftmann@24219
   112
haftmann@24219
   113
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) =
haftmann@24219
   114
      let
haftmann@24219
   115
        fun idx c = find_index (curry (op =) c) c_nibbles;
haftmann@24219
   116
        fun decode ~1 _ = NONE
haftmann@24219
   117
          | decode _ ~1 = NONE
haftmann@24219
   118
          | decode n m = SOME (chr (n * 16 + m));
haftmann@24219
   119
      in decode (idx c1) (idx c2) end
haftmann@24219
   120
  | decode_char _ _ = NONE;
haftmann@24219
   121
haftmann@24219
   122
fun implode_string c_char c_nibbles mk_char mk_string ts =
haftmann@24219
   123
  let
haftmann@24219
   124
    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
haftmann@24219
   125
          if c = c_char then decode_char c_nibbles (t1, t2) else NONE
haftmann@24219
   126
      | implode_char _ = NONE;
haftmann@24219
   127
    val ts' = map implode_char ts;
haftmann@24219
   128
  in if forall is_some ts'
haftmann@24219
   129
    then (SOME o str o mk_string o implode o map_filter I) ts'
haftmann@24219
   130
    else NONE
haftmann@24219
   131
  end;
haftmann@24219
   132
haftmann@27304
   133
fun implode_numeral thm negative c_pls c_min c_bit0 c_bit1 =
haftmann@24219
   134
  let
haftmann@25936
   135
    fun dest_bit (IConst (c, _)) = if c = c_bit0 then 0
haftmann@25936
   136
          else if c = c_bit1 then 1
haftmann@27304
   137
          else nerror thm "Illegal numeral expression: illegal bit"
haftmann@27304
   138
      | dest_bit _ = nerror thm "Illegal numeral expression: illegal bit";
wenzelm@24630
   139
    fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0
haftmann@25936
   140
          else if c = c_min then
haftmann@25936
   141
            if negative then SOME ~1 else NONE
haftmann@27304
   142
          else nerror thm "Illegal numeral expression: illegal leading digit"
huffman@26086
   143
      | dest_numeral (t1 `$ t2) =
huffman@26086
   144
          let val (n, b) = (dest_numeral t2, dest_bit t1)
huffman@26086
   145
          in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
haftmann@27304
   146
      | dest_numeral _ = nerror thm "Illegal numeral expression: illegal term";
haftmann@25936
   147
  in dest_numeral #> the_default 0 end;
haftmann@24219
   148
haftmann@24219
   149
haftmann@28054
   150
(* pretty syntax printing *)
haftmann@24219
   151
haftmann@24219
   152
local
haftmann@24219
   153
haftmann@26448
   154
fun ocaml_char c =
haftmann@26448
   155
  let
haftmann@26448
   156
    fun chr i =
haftmann@26448
   157
      let
haftmann@26448
   158
        val xs = string_of_int i;
haftmann@26448
   159
        val ys = replicate_string (3 - length (explode xs)) "0";
haftmann@26448
   160
      in "\\" ^ ys ^ xs end;
haftmann@26448
   161
    val i = ord c;
haftmann@26448
   162
    val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126
haftmann@26448
   163
      then chr i else c
haftmann@26448
   164
  in s end;
haftmann@26448
   165
haftmann@26448
   166
fun haskell_char c =
haftmann@26448
   167
  let
haftmann@26448
   168
    val s = ML_Syntax.print_char c;
haftmann@26448
   169
  in if s = "'" then "\\'" else s end;
haftmann@26448
   170
haftmann@24219
   171
val pretty : (string * {
haftmann@24219
   172
    pretty_char: string -> string,
haftmann@24219
   173
    pretty_string: string -> string,
wenzelm@24630
   174
    pretty_numeral: bool -> int -> string,
haftmann@24219
   175
    pretty_list: Pretty.T list -> Pretty.T,
haftmann@24219
   176
    infix_cons: int * string
haftmann@24219
   177
  }) list = [
haftmann@24219
   178
  ("SML", { pretty_char = prefix "#" o quote o ML_Syntax.print_char,
haftmann@26448
   179
      pretty_string = quote o translate_string ML_Syntax.print_char,
haftmann@24219
   180
      pretty_numeral = fn unbounded => fn k =>
haftmann@24750
   181
        if unbounded then "(" ^ string_of_int k ^ " : IntInf.int)"
wenzelm@24630
   182
        else string_of_int k,
haftmann@24219
   183
      pretty_list = Pretty.enum "," "[" "]",
haftmann@24219
   184
      infix_cons = (7, "::")}),
haftmann@26448
   185
  ("OCaml", { pretty_char = enclose "'" "'" o ocaml_char,
haftmann@26448
   186
      pretty_string = quote o translate_string ocaml_char,
wenzelm@24630
   187
      pretty_numeral = fn unbounded => fn k => if k >= 0 then
haftmann@24219
   188
            if unbounded then
wenzelm@24630
   189
              "(Big_int.big_int_of_int " ^ string_of_int k ^ ")"
wenzelm@24630
   190
            else string_of_int k
haftmann@24219
   191
          else
haftmann@24219
   192
            if unbounded then
haftmann@24219
   193
              "(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-"
wenzelm@24630
   194
                o string_of_int o op ~) k ^ ")"
wenzelm@24630
   195
            else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k,
haftmann@24219
   196
      pretty_list = Pretty.enum ";" "[" "]",
haftmann@24219
   197
      infix_cons = (6, "::")}),
haftmann@26448
   198
  ("Haskell", { pretty_char = enclose "'" "'" o haskell_char,
haftmann@26448
   199
      pretty_string = quote o translate_string haskell_char,
wenzelm@24630
   200
      pretty_numeral = fn unbounded => fn k => if k >= 0 then string_of_int k
wenzelm@24630
   201
          else enclose "(" ")" (signed_string_of_int k),
haftmann@24219
   202
      pretty_list = Pretty.enum "," "[" "]",
haftmann@24219
   203
      infix_cons = (5, ":")})
haftmann@24219
   204
];
haftmann@24219
   205
haftmann@24219
   206
in
haftmann@24219
   207
haftmann@24219
   208
fun pr_pretty target = case AList.lookup (op =) pretty target
haftmann@24219
   209
 of SOME x => x
haftmann@24219
   210
  | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@24219
   211
haftmann@24219
   212
fun default_list (target_fxy, target_cons) pr fxy t1 t2 =
haftmann@24219
   213
  brackify_infix (target_fxy, R) fxy [
haftmann@24219
   214
    pr (INFX (target_fxy, X)) t1,
haftmann@24219
   215
    str target_cons,
haftmann@24219
   216
    pr (INFX (target_fxy, R)) t2
haftmann@24219
   217
  ];
haftmann@24219
   218
haftmann@24219
   219
fun pretty_list c_nil c_cons target =
haftmann@24219
   220
  let
haftmann@24219
   221
    val pretty_ops = pr_pretty target;
haftmann@24219
   222
    val mk_list = #pretty_list pretty_ops;
haftmann@27304
   223
    fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] =
haftmann@24219
   224
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
haftmann@24219
   225
       of SOME ts => mk_list (map (pr vars NOBR) ts)
haftmann@24219
   226
        | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
haftmann@24219
   227
  in (2, pretty) end;
haftmann@24219
   228
haftmann@24219
   229
fun pretty_list_string c_nil c_cons c_char c_nibbles target =
haftmann@24219
   230
  let
haftmann@24219
   231
    val pretty_ops = pr_pretty target;
haftmann@24219
   232
    val mk_list = #pretty_list pretty_ops;
haftmann@24219
   233
    val mk_char = #pretty_char pretty_ops;
haftmann@24219
   234
    val mk_string = #pretty_string pretty_ops;
haftmann@27304
   235
    fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] =
haftmann@24219
   236
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
haftmann@26448
   237
       of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
haftmann@24219
   238
           of SOME p => p
haftmann@26448
   239
            | NONE => mk_list (map (pr vars NOBR) ts))
haftmann@24219
   240
        | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
haftmann@24219
   241
  in (2, pretty) end;
haftmann@24219
   242
haftmann@24219
   243
fun pretty_char c_char c_nibbles target =
haftmann@24219
   244
  let
haftmann@24219
   245
    val mk_char = #pretty_char (pr_pretty target);
haftmann@27304
   246
    fun pretty _ thm _ _ _ [(t1, _), (t2, _)] =
haftmann@24219
   247
      case decode_char c_nibbles (t1, t2)
haftmann@24219
   248
       of SOME c => (str o mk_char) c
haftmann@27304
   249
        | NONE => nerror thm "Illegal character expression";
haftmann@24219
   250
  in (2, pretty) end;
haftmann@24219
   251
huffman@26086
   252
fun pretty_numeral unbounded negative c_pls c_min c_bit0 c_bit1 target =
haftmann@24219
   253
  let
haftmann@24219
   254
    val mk_numeral = #pretty_numeral (pr_pretty target);
haftmann@27304
   255
    fun pretty _ thm _ _ _ [(t, _)] =
haftmann@27304
   256
      (str o mk_numeral unbounded o implode_numeral thm negative c_pls c_min c_bit0 c_bit1) t;
haftmann@24219
   257
  in (1, pretty) end;
haftmann@24219
   258
haftmann@24992
   259
fun pretty_message c_char c_nibbles c_nil c_cons target =
haftmann@24219
   260
  let
haftmann@24219
   261
    val pretty_ops = pr_pretty target;
haftmann@24219
   262
    val mk_char = #pretty_char pretty_ops;
haftmann@24219
   263
    val mk_string = #pretty_string pretty_ops;
haftmann@27304
   264
    fun pretty _ thm _ _ _ [(t, _)] =
haftmann@24219
   265
      case implode_list c_nil c_cons t
haftmann@24219
   266
       of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
haftmann@24219
   267
           of SOME p => p
haftmann@27304
   268
            | NONE => nerror thm "Illegal message expression")
haftmann@27304
   269
        | NONE => nerror thm "Illegal message expression";
haftmann@24219
   270
  in (1, pretty) end;
haftmann@24219
   271
haftmann@24219
   272
end; (*local*)
haftmann@24219
   273
haftmann@27000
   274
haftmann@28054
   275
(** theory data **)
haftmann@28054
   276
haftmann@28054
   277
datatype name_syntax_table = NameSyntaxTable of {
haftmann@28054
   278
  class: class_syntax Symtab.table,
haftmann@28054
   279
  inst: unit Symtab.table,
haftmann@28054
   280
  tyco: tyco_syntax Symtab.table,
haftmann@28054
   281
  const: const_syntax Symtab.table
haftmann@28054
   282
};
haftmann@27000
   283
haftmann@28054
   284
fun mk_name_syntax_table ((class, inst), (tyco, const)) =
haftmann@28054
   285
  NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const };
haftmann@28054
   286
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) =
haftmann@28054
   287
  mk_name_syntax_table (f ((class, inst), (tyco, const)));
haftmann@28054
   288
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 },
haftmann@28054
   289
    NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) =
haftmann@28054
   290
  mk_name_syntax_table (
haftmann@28054
   291
    (Symtab.join (K snd) (class1, class2),
haftmann@28054
   292
       Symtab.join (K snd) (inst1, inst2)),
haftmann@28054
   293
    (Symtab.join (K snd) (tyco1, tyco2),
haftmann@28054
   294
       Symtab.join (K snd) (const1, const2))
haftmann@28054
   295
  );
haftmann@28054
   296
haftmann@28054
   297
type serializer =
haftmann@28054
   298
  string option                         (*module name*)
haftmann@28054
   299
  -> Args.T list                        (*arguments*)
haftmann@28054
   300
  -> (string -> string)                 (*labelled_name*)
haftmann@28054
   301
  -> string list                        (*reserved symbols*)
haftmann@28054
   302
  -> (string * Pretty.T) list           (*includes*)
haftmann@28054
   303
  -> (string -> string option)          (*module aliasses*)
haftmann@28054
   304
  -> (string -> class_syntax option)
haftmann@28054
   305
  -> (string -> tyco_syntax option)
haftmann@28054
   306
  -> (string -> const_syntax option)
haftmann@28054
   307
  -> Code_Thingol.program
haftmann@28054
   308
  -> string list                        (*selected statements*)
haftmann@28054
   309
  -> serialization;
haftmann@27000
   310
haftmann@28054
   311
datatype serializer_entry = Serializer of serializer
haftmann@28054
   312
  | Extends of string * (Code_Thingol.program -> Code_Thingol.program);
haftmann@28054
   313
haftmann@28054
   314
datatype target = Target of {
haftmann@28054
   315
  serial: serial,
haftmann@28054
   316
  serializer: serializer_entry,
haftmann@28054
   317
  reserved: string list,
haftmann@28054
   318
  includes: Pretty.T Symtab.table,
haftmann@28054
   319
  name_syntax_table: name_syntax_table,
haftmann@28054
   320
  module_alias: string Symtab.table
haftmann@28054
   321
};
haftmann@27103
   322
haftmann@28054
   323
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) =
haftmann@28054
   324
  Target { serial = serial, serializer = serializer, reserved = reserved, 
haftmann@28054
   325
    includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias };
haftmann@28054
   326
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) =
haftmann@28054
   327
  mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))));
haftmann@28054
   328
fun merge_target strict target (Target { serial = serial1, serializer = serializer,
haftmann@28054
   329
  reserved = reserved1, includes = includes1,
haftmann@28054
   330
  name_syntax_table = name_syntax_table1, module_alias = module_alias1 },
haftmann@28054
   331
    Target { serial = serial2, serializer = _,
haftmann@28054
   332
      reserved = reserved2, includes = includes2,
haftmann@28054
   333
      name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) =
haftmann@28054
   334
  if serial1 = serial2 orelse not strict then
haftmann@28054
   335
    mk_target ((serial1, serializer),
haftmann@28054
   336
      ((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)),
haftmann@28054
   337
        (merge_name_syntax_table (name_syntax_table1, name_syntax_table2),
haftmann@28054
   338
          Symtab.join (K snd) (module_alias1, module_alias2))
haftmann@28054
   339
    ))
haftmann@28054
   340
  else
haftmann@28054
   341
    error ("Incompatible serializers: " ^ quote target);
haftmann@27103
   342
haftmann@28054
   343
structure CodeTargetData = TheoryDataFun
haftmann@27436
   344
(
haftmann@28054
   345
  type T = target Symtab.table * string list;
haftmann@28054
   346
  val empty = (Symtab.empty, []);
haftmann@28054
   347
  val copy = I;
haftmann@28054
   348
  val extend = I;
haftmann@28054
   349
  fun merge _ ((target1, exc1) : T, (target2, exc2)) =
haftmann@28054
   350
    (Symtab.join (merge_target true) (target1, target2), Library.merge (op =) (exc1, exc2));
haftmann@27436
   351
);
haftmann@27436
   352
haftmann@28054
   353
fun the_serializer (Target { serializer, ... }) = serializer;
haftmann@28054
   354
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@28054
   355
fun the_includes (Target { includes, ... }) = includes;
haftmann@28054
   356
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
haftmann@28054
   357
fun the_module_alias (Target { module_alias , ... }) = module_alias;
haftmann@27437
   358
haftmann@28054
   359
val abort_allowed = snd o CodeTargetData.get;
haftmann@27436
   360
haftmann@28054
   361
fun assert_target thy target =
haftmann@28054
   362
  case Symtab.lookup (fst (CodeTargetData.get thy)) target
haftmann@28054
   363
   of SOME data => target
haftmann@28054
   364
    | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28054
   365
haftmann@28054
   366
fun put_target (target, seri) thy =
haftmann@27304
   367
  let
haftmann@28054
   368
    val defined_target = is_some o Symtab.lookup (fst (CodeTargetData.get thy));
haftmann@28054
   369
    val _ = case seri
haftmann@28054
   370
     of Extends (super, _) => if defined_target super then ()
haftmann@28054
   371
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   372
      | _ => ();
haftmann@28054
   373
    val _ = if defined_target target
haftmann@28054
   374
      then warning ("Overwriting existing target " ^ quote target)
haftmann@28054
   375
      else ();
haftmann@28054
   376
  in
haftmann@28054
   377
    thy
haftmann@28054
   378
    |> (CodeTargetData.map o apfst oo Symtab.map_default)
haftmann@28054
   379
          (target, mk_target ((serial (), seri), (([], Symtab.empty),
haftmann@28054
   380
            (mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
haftmann@28054
   381
              Symtab.empty))))
haftmann@28054
   382
          ((map_target o apfst o apsnd o K) seri)
haftmann@28054
   383
  end;
haftmann@27436
   384
haftmann@28054
   385
fun add_target (target, seri) = put_target (target, Serializer seri);
haftmann@28054
   386
fun extend_target (target, (super, modify)) =
haftmann@28054
   387
  put_target (target, Extends (super, modify));
haftmann@27436
   388
haftmann@28054
   389
fun map_target_data target f thy =
haftmann@27436
   390
  let
haftmann@28054
   391
    val _ = assert_target thy target;
haftmann@28054
   392
  in
haftmann@28054
   393
    thy
haftmann@28054
   394
    |> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f
haftmann@28054
   395
  end;
haftmann@27304
   396
haftmann@28054
   397
fun map_reserved target =
haftmann@28054
   398
  map_target_data target o apsnd o apfst o apfst;
haftmann@28054
   399
fun map_includes target =
haftmann@28054
   400
  map_target_data target o apsnd o apfst o apsnd;
haftmann@28054
   401
fun map_name_syntax target =
haftmann@28054
   402
  map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table;
haftmann@28054
   403
fun map_module_alias target =
haftmann@28054
   404
  map_target_data target o apsnd o apsnd o apsnd;
haftmann@27000
   405
haftmann@27000
   406
haftmann@28054
   407
(** serializer configuration **)
haftmann@27000
   408
haftmann@27000
   409
(* data access *)
haftmann@24219
   410
haftmann@24219
   411
local
haftmann@24219
   412
haftmann@24992
   413
fun cert_class thy class =
haftmann@24992
   414
  let
haftmann@24992
   415
    val _ = AxClass.get_info thy class;
haftmann@24992
   416
  in class end;
haftmann@24992
   417
haftmann@27103
   418
fun read_class thy = cert_class thy o Sign.intern_class thy;
haftmann@24992
   419
haftmann@24992
   420
fun cert_tyco thy tyco =
haftmann@24992
   421
  let
haftmann@24992
   422
    val _ = if Sign.declared_tyname thy tyco then ()
haftmann@24992
   423
      else error ("No such type constructor: " ^ quote tyco);
haftmann@24992
   424
  in tyco end;
haftmann@24992
   425
haftmann@27103
   426
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
haftmann@24219
   427
haftmann@24219
   428
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
haftmann@24219
   429
  let
haftmann@24841
   430
    val class = prep_class thy raw_class;
haftmann@28054
   431
    val class' = Code_Name.class thy class;
haftmann@24841
   432
    fun mk_classparam c = case AxClass.class_of_param thy c
haftmann@28054
   433
     of SOME class'' => if class = class'' then Code_Name.const thy c
haftmann@24219
   434
          else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c)
haftmann@24219
   435
      | NONE => error ("Not a class operation: " ^ quote c);
haftmann@24841
   436
    fun mk_syntax_params raw_params = AList.lookup (op =)
haftmann@24841
   437
      ((map o apfst) (mk_classparam o prep_const thy) raw_params);
haftmann@24219
   438
  in case raw_syn
haftmann@24841
   439
   of SOME (syntax, raw_params) =>
haftmann@24219
   440
      thy
haftmann@27024
   441
      |> (map_name_syntax target o apfst o apfst)
haftmann@24841
   442
           (Symtab.update (class', (syntax, mk_syntax_params raw_params)))
haftmann@24219
   443
    | NONE =>
haftmann@24219
   444
      thy
haftmann@27024
   445
      |> (map_name_syntax target o apfst o apfst)
haftmann@24841
   446
           (Symtab.delete_safe class')
haftmann@24219
   447
  end;
haftmann@24219
   448
haftmann@24219
   449
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy =
haftmann@24219
   450
  let
haftmann@28054
   451
    val inst = Code_Name.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco);
haftmann@24219
   452
  in if add_del then
haftmann@24219
   453
    thy
haftmann@27024
   454
    |> (map_name_syntax target o apfst o apsnd)
haftmann@24219
   455
        (Symtab.update (inst, ()))
haftmann@24219
   456
  else
haftmann@24219
   457
    thy
haftmann@27024
   458
    |> (map_name_syntax target o apfst o apsnd)
haftmann@24219
   459
        (Symtab.delete_safe inst)
haftmann@24219
   460
  end;
haftmann@24219
   461
haftmann@24219
   462
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy =
haftmann@24219
   463
  let
haftmann@24219
   464
    val tyco = prep_tyco thy raw_tyco;
haftmann@28054
   465
    val tyco' = if tyco = "fun" then "fun" else Code_Name.tyco thy tyco;
haftmann@24219
   466
    fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco
haftmann@24219
   467
      then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@24219
   468
      else syntax
haftmann@24219
   469
  in case raw_syn
haftmann@24219
   470
   of SOME syntax =>
haftmann@24219
   471
      thy
haftmann@27024
   472
      |> (map_name_syntax target o apsnd o apfst)
haftmann@24219
   473
           (Symtab.update (tyco', check_args syntax))
haftmann@24219
   474
   | NONE =>
haftmann@24219
   475
      thy
haftmann@27024
   476
      |> (map_name_syntax target o apsnd o apfst)
haftmann@24219
   477
           (Symtab.delete_safe tyco')
haftmann@24219
   478
  end;
haftmann@24219
   479
haftmann@24219
   480
fun gen_add_syntax_const prep_const target raw_c raw_syn thy =
haftmann@24219
   481
  let
haftmann@24219
   482
    val c = prep_const thy raw_c;
haftmann@28054
   483
    val c' = Code_Name.const thy c;
haftmann@28054
   484
    fun check_args (syntax as (n, _)) = if n > Code_Unit.no_args thy c
haftmann@24423
   485
      then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@24219
   486
      else syntax;
haftmann@24219
   487
  in case raw_syn
haftmann@24219
   488
   of SOME syntax =>
haftmann@24219
   489
      thy
haftmann@27024
   490
      |> (map_name_syntax target o apsnd o apsnd)
haftmann@24219
   491
           (Symtab.update (c', check_args syntax))
haftmann@24219
   492
   | NONE =>
haftmann@24219
   493
      thy
haftmann@27024
   494
      |> (map_name_syntax target o apsnd o apsnd)
haftmann@24219
   495
           (Symtab.delete_safe c')
haftmann@24219
   496
  end;
haftmann@24219
   497
haftmann@24219
   498
fun add_reserved target =
haftmann@24219
   499
  let
haftmann@24219
   500
    fun add sym syms = if member (op =) syms sym
haftmann@24219
   501
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@24219
   502
      else insert (op =) sym syms
haftmann@27103
   503
  in map_reserved target o add end;
haftmann@24992
   504
haftmann@24992
   505
fun add_include target =
haftmann@24992
   506
  let
haftmann@24992
   507
    fun add (name, SOME content) incls =
haftmann@24992
   508
          let
haftmann@24992
   509
            val _ = if Symtab.defined incls name
haftmann@24992
   510
              then warning ("Overwriting existing include " ^ name)
haftmann@24992
   511
              else ();
haftmann@24992
   512
          in Symtab.update (name, str content) incls end
haftmann@24992
   513
      | add (name, NONE) incls =
haftmann@24992
   514
          Symtab.delete name incls;
haftmann@27103
   515
  in map_includes target o add end;
haftmann@24219
   516
haftmann@27103
   517
fun add_module_alias target =
haftmann@28054
   518
  map_module_alias target o Symtab.update o apsnd Code_Name.check_modulename;
haftmann@24219
   519
haftmann@27103
   520
fun gen_allow_abort prep_cs raw_c thy =
haftmann@24841
   521
  let
haftmann@24841
   522
    val c = prep_cs thy raw_c;
haftmann@28054
   523
    val c' = Code_Name.const thy c;
haftmann@24841
   524
  in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end;
haftmann@24841
   525
haftmann@24219
   526
fun zip_list (x::xs) f g =
haftmann@24219
   527
  f
haftmann@24219
   528
  #-> (fn y =>
haftmann@24219
   529
    fold_map (fn x => g |-- f >> pair x) xs
haftmann@24219
   530
    #-> (fn xys => pair ((x, y) :: xys)));
haftmann@24219
   531
haftmann@27000
   532
haftmann@27103
   533
(* concrete syntax *)
haftmann@27000
   534
haftmann@24219
   535
structure P = OuterParse
haftmann@24219
   536
and K = OuterKeyword
haftmann@24219
   537
haftmann@24219
   538
fun parse_multi_syntax parse_thing parse_syntax =
haftmann@24219
   539
  P.and_list1 parse_thing
haftmann@24219
   540
  #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
haftmann@24219
   541
        (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
haftmann@24219
   542
haftmann@24219
   543
in
haftmann@24219
   544
haftmann@24219
   545
val parse_syntax = parse_syntax;
haftmann@24219
   546
haftmann@24219
   547
val add_syntax_class = gen_add_syntax_class cert_class (K I);
haftmann@24219
   548
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
haftmann@24219
   549
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
haftmann@24219
   550
val add_syntax_const = gen_add_syntax_const (K I);
haftmann@27103
   551
val allow_abort = gen_allow_abort (K I);
haftmann@28054
   552
val add_reserved = add_reserved;
haftmann@24219
   553
haftmann@28054
   554
val add_syntax_class_cmd = gen_add_syntax_class read_class Code_Unit.read_const;
haftmann@24219
   555
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
haftmann@24219
   556
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
haftmann@28054
   557
val add_syntax_const_cmd = gen_add_syntax_const Code_Unit.read_const;
haftmann@28054
   558
val allow_abort_cmd = gen_allow_abort Code_Unit.read_const;
haftmann@24219
   559
haftmann@28054
   560
fun add_syntax_tycoP target tyco = parse_syntax I
haftmann@28054
   561
  >> add_syntax_tyco_cmd target tyco;
haftmann@28054
   562
fun add_syntax_constP target c = parse_syntax fst
haftmann@28054
   563
  >> (add_syntax_const_cmd target c o Code_Printer.simple_const_syntax);
haftmann@24219
   564
haftmann@24219
   565
fun add_pretty_list target nill cons thy =
haftmann@24219
   566
  let
haftmann@28054
   567
    val nil' = Code_Name.const thy nill;
haftmann@28054
   568
    val cons' = Code_Name.const thy cons;
haftmann@24423
   569
    val pr = pretty_list nil' cons' target;
haftmann@24219
   570
  in
haftmann@24219
   571
    thy
haftmann@24423
   572
    |> add_syntax_const target cons (SOME pr)
haftmann@24219
   573
  end;
haftmann@24219
   574
haftmann@24219
   575
fun add_pretty_list_string target nill cons charr nibbles thy =
haftmann@24219
   576
  let
haftmann@28054
   577
    val nil' = Code_Name.const thy nill;
haftmann@28054
   578
    val cons' = Code_Name.const thy cons;
haftmann@28054
   579
    val charr' = Code_Name.const thy charr;
haftmann@28054
   580
    val nibbles' = map (Code_Name.const thy) nibbles;
haftmann@24423
   581
    val pr = pretty_list_string nil' cons' charr' nibbles' target;
haftmann@24219
   582
  in
haftmann@24219
   583
    thy
haftmann@24423
   584
    |> add_syntax_const target cons (SOME pr)
haftmann@24219
   585
  end;
haftmann@24219
   586
haftmann@24219
   587
fun add_pretty_char target charr nibbles thy =
haftmann@24219
   588
  let
haftmann@28054
   589
    val charr' = Code_Name.const thy charr;
haftmann@28054
   590
    val nibbles' = map (Code_Name.const thy) nibbles;
haftmann@24423
   591
    val pr = pretty_char charr' nibbles' target;
haftmann@24219
   592
  in
haftmann@24219
   593
    thy
haftmann@24423
   594
    |> add_syntax_const target charr (SOME pr)
haftmann@24219
   595
  end;
haftmann@24219
   596
huffman@26086
   597
fun add_pretty_numeral target unbounded negative number_of pls min bit0 bit1 thy =
haftmann@24219
   598
  let
haftmann@28054
   599
    val pls' = Code_Name.const thy pls;
haftmann@28054
   600
    val min' = Code_Name.const thy min;
haftmann@28054
   601
    val bit0' = Code_Name.const thy bit0;
haftmann@28054
   602
    val bit1' = Code_Name.const thy bit1;
huffman@26086
   603
    val pr = pretty_numeral unbounded negative pls' min' bit0' bit1' target;
haftmann@24219
   604
  in
haftmann@24219
   605
    thy
haftmann@24423
   606
    |> add_syntax_const target number_of (SOME pr)
haftmann@24219
   607
  end;
haftmann@24219
   608
haftmann@24992
   609
fun add_pretty_message target charr nibbles nill cons str thy =
haftmann@24219
   610
  let
haftmann@28054
   611
    val charr' = Code_Name.const thy charr;
haftmann@28054
   612
    val nibbles' = map (Code_Name.const thy) nibbles;
haftmann@28054
   613
    val nil' = Code_Name.const thy nill;
haftmann@28054
   614
    val cons' = Code_Name.const thy cons;
haftmann@24992
   615
    val pr = pretty_message charr' nibbles' nil' cons' target;
haftmann@24219
   616
  in
haftmann@24219
   617
    thy
haftmann@24423
   618
    |> add_syntax_const target str (SOME pr)
haftmann@24219
   619
  end;
haftmann@24219
   620
wenzelm@24867
   621
haftmann@28054
   622
(** serializer usage **)
haftmann@28054
   623
haftmann@28054
   624
(* montage *)
haftmann@28054
   625
haftmann@28054
   626
fun invoke_serializer thy modify abortable serializer reserved includes 
haftmann@28054
   627
    module_alias class inst tyco const module args program1 cs1 =
haftmann@28054
   628
  let
haftmann@28054
   629
    val program2 = modify program1;
haftmann@28054
   630
    val hidden = Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const;
haftmann@28054
   631
    val cs2 = subtract (op =) hidden cs1;
haftmann@28054
   632
    val program3 = Graph.subgraph (not o member (op =) hidden) program2;
haftmann@28054
   633
    val all_cs = Graph.all_succs program2 cs2;
haftmann@28054
   634
    val program4 = Graph.subgraph (member (op =) all_cs) program3;
haftmann@28054
   635
    val empty_funs = filter_out (member (op =) abortable)
haftmann@28054
   636
      (Code_Thingol.empty_funs program3);
haftmann@28054
   637
    val _ = if null empty_funs then () else error ("No defining equations for "
haftmann@28054
   638
      ^ commas (map (Code_Name.labelled_name thy) empty_funs));
haftmann@28054
   639
  in
haftmann@28054
   640
    serializer module args (Code_Name.labelled_name thy) reserved includes
haftmann@28054
   641
      (Symtab.lookup module_alias) (Symtab.lookup class)
haftmann@28054
   642
      (Symtab.lookup tyco) (Symtab.lookup const)
haftmann@28054
   643
      program4 cs2
haftmann@28054
   644
  end;
haftmann@28054
   645
haftmann@28054
   646
fun mount_serializer thy alt_serializer target =
haftmann@28054
   647
  let
haftmann@28054
   648
    val (targets, abortable) = CodeTargetData.get thy;
haftmann@28054
   649
    fun collapse_hierarchy target =
haftmann@28054
   650
      let
haftmann@28054
   651
        val data = case Symtab.lookup targets target
haftmann@28054
   652
         of SOME data => data
haftmann@28054
   653
          | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28054
   654
      in case the_serializer data
haftmann@28054
   655
       of Serializer _ => (I, data)
haftmann@28054
   656
        | Extends (super, modify) => let
haftmann@28054
   657
            val (modify', data') = collapse_hierarchy super
haftmann@28054
   658
          in (modify' #> modify, merge_target false target (data', data)) end
haftmann@28054
   659
      end;
haftmann@28054
   660
    val (modify, data) = collapse_hierarchy target;
haftmann@28054
   661
    val serializer = the_default (case the_serializer data
haftmann@28054
   662
     of Serializer seri => seri) alt_serializer;
haftmann@28054
   663
    val reserved = the_reserved data;
haftmann@28054
   664
    val includes = Symtab.dest (the_includes data);
haftmann@28054
   665
    val module_alias = the_module_alias data;
haftmann@28054
   666
    val { class, inst, tyco, const } = the_name_syntax data;
haftmann@28054
   667
  in
haftmann@28054
   668
    invoke_serializer thy modify abortable serializer reserved
haftmann@28054
   669
      includes module_alias class inst tyco const
haftmann@28054
   670
  end;
haftmann@28054
   671
haftmann@28054
   672
fun serialize thy = mount_serializer thy NONE;
haftmann@28054
   673
haftmann@28054
   674
fun serialize_custom thy (target_name, seri) program cs =
haftmann@28054
   675
  mount_serializer thy (SOME seri) target_name NONE [] program cs (String [])
haftmann@28054
   676
  |> the;
haftmann@28054
   677
haftmann@28054
   678
fun parse_args f args =
haftmann@28054
   679
  case Scan.read OuterLex.stopper f args
haftmann@28054
   680
   of SOME x => x
haftmann@28054
   681
    | NONE => error "Bad serializer arguments";
haftmann@28054
   682
haftmann@28054
   683
haftmann@28054
   684
(* code presentation *)
haftmann@28054
   685
haftmann@28054
   686
fun code_of thy target module_name cs stmt_names =
haftmann@28054
   687
  let
haftmann@28054
   688
    val (cs', program) = Code_Thingol.consts_program thy cs;
haftmann@28054
   689
  in
haftmann@28054
   690
    string stmt_names (serialize thy target (SOME module_name) [] program cs')
haftmann@28054
   691
  end;
haftmann@28054
   692
haftmann@28054
   693
haftmann@28054
   694
(* code generation *)
haftmann@28054
   695
haftmann@28054
   696
fun read_const_exprs thy cs =
haftmann@28054
   697
  let
haftmann@28054
   698
    val (cs1, cs2) = Code_Name.read_const_exprs thy cs;
haftmann@28054
   699
    val (cs3, program) = Code_Thingol.consts_program thy cs2;
haftmann@28054
   700
    val cs4 = Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy);
haftmann@28054
   701
    val cs5 = map_filter
haftmann@28054
   702
      (fn (c, c') => if member (op =) cs4 c' then SOME c else NONE) (cs2 ~~ cs3);
haftmann@28054
   703
  in fold (insert (op =)) cs5 cs1 end;
haftmann@28054
   704
haftmann@28054
   705
fun cached_program thy = 
haftmann@28054
   706
  let
haftmann@28054
   707
    val program = Code_Thingol.cached_program thy;
haftmann@28054
   708
  in (Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy), program) end
haftmann@28054
   709
haftmann@28054
   710
fun export_code thy cs seris =
haftmann@28054
   711
  let
haftmann@28054
   712
    val (cs', program) = if null cs then cached_program thy
haftmann@28054
   713
      else Code_Thingol.consts_program thy cs;
haftmann@28054
   714
    fun mk_seri_dest dest = case dest
haftmann@28054
   715
     of NONE => compile
haftmann@28054
   716
      | SOME "-" => export
haftmann@28054
   717
      | SOME f => file (Path.explode f)
haftmann@28054
   718
    val _ = map (fn (((target, module), dest), args) =>
haftmann@28054
   719
      (mk_seri_dest dest (serialize thy target module args program cs'))) seris;
haftmann@28054
   720
  in () end;
haftmann@28054
   721
haftmann@28054
   722
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
haftmann@28054
   723
haftmann@27103
   724
haftmann@27304
   725
(** Isar setup **)
haftmann@27103
   726
haftmann@27103
   727
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
haftmann@27103
   728
haftmann@27103
   729
fun code_exprP cmd =
wenzelm@27757
   730
  (Scan.repeat P.term_group
haftmann@27103
   731
  -- Scan.repeat (P.$$$ inK |-- P.name
haftmann@27103
   732
     -- Scan.option (P.$$$ module_nameK |-- P.name)
haftmann@27103
   733
     -- Scan.option (P.$$$ fileK |-- P.name)
wenzelm@27809
   734
     -- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") []
haftmann@27103
   735
  ) >> (fn (raw_cs, seris) => cmd raw_cs seris));
haftmann@27103
   736
haftmann@28054
   737
val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK];
wenzelm@24867
   738
wenzelm@24867
   739
val _ =
haftmann@24992
   740
  OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl (
haftmann@24219
   741
    parse_multi_syntax P.xname
haftmann@24219
   742
      (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
wenzelm@27757
   743
        (P.term_group --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) []))
haftmann@24219
   744
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   745
          fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
haftmann@24219
   746
  );
haftmann@24219
   747
wenzelm@24867
   748
val _ =
haftmann@24992
   749
  OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl (
haftmann@24219
   750
    parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
haftmann@24219
   751
      ((P.minus >> K true) || Scan.succeed false)
haftmann@24219
   752
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   753
          fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
haftmann@24219
   754
  );
haftmann@24219
   755
wenzelm@24867
   756
val _ =
haftmann@24992
   757
  OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl (
haftmann@24219
   758
    parse_multi_syntax P.xname (parse_syntax I)
haftmann@24219
   759
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   760
          fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
haftmann@24219
   761
  );
haftmann@24219
   762
wenzelm@24867
   763
val _ =
haftmann@24992
   764
  OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl (
wenzelm@27757
   765
    parse_multi_syntax P.term_group (parse_syntax fst)
haftmann@24219
   766
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@28054
   767
      fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const
haftmann@28054
   768
        (Code_Printer.simple_const_syntax syn)) syns)
haftmann@24219
   769
  );
haftmann@24219
   770
wenzelm@24867
   771
val _ =
haftmann@24992
   772
  OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl (
haftmann@24219
   773
    P.name -- Scan.repeat1 P.name
haftmann@24219
   774
    >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
haftmann@24841
   775
  );
haftmann@24219
   776
wenzelm@24867
   777
val _ =
haftmann@24992
   778
  OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl (
haftmann@24992
   779
    P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s))
haftmann@24992
   780
    >> (fn ((target, name), content) => (Toplevel.theory o add_include target)
haftmann@24992
   781
      (name, content))
haftmann@24992
   782
  );
haftmann@24992
   783
haftmann@24992
   784
val _ =
haftmann@24992
   785
  OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl (
haftmann@24219
   786
    P.name -- Scan.repeat1 (P.name -- P.name)
haftmann@27103
   787
    >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames)
haftmann@27103
   788
  );
haftmann@27103
   789
haftmann@27103
   790
val _ =
haftmann@27103
   791
  OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl (
wenzelm@27757
   792
    Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd)
haftmann@24841
   793
  );
haftmann@24219
   794
wenzelm@24867
   795
val _ =
haftmann@27103
   796
  OuterSyntax.command "export_code" "generate executable code for constants"
haftmann@27103
   797
    K.diag (P.!!! (code_exprP export_code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
haftmann@27103
   798
haftmann@27103
   799
fun shell_command thyname cmd = Toplevel.program (fn _ =>
wenzelm@27845
   800
  (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP export_code_cmd))
wenzelm@27845
   801
    ((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd)
haftmann@27103
   802
   of SOME f => (writeln "Now generating code..."; f (theory thyname))
haftmann@27103
   803
    | NONE => error ("Bad directive " ^ quote cmd)))
haftmann@27103
   804
  handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
haftmann@27103
   805
haftmann@24219
   806
end; (*local*)
haftmann@24219
   807
haftmann@24219
   808
end; (*struct*)