src/Tools/Code/code_target.ML
author haftmann
Sun Jan 26 16:23:46 2014 +0100 (2014-01-26)
changeset 55153 eedd549de3ef
parent 55150 0940309ed8f1
child 55188 7ca0204ece66
permissions -rw-r--r--
more suitable names, without any notion of "activating"
haftmann@37745
     1
(*  Title:      Tools/Code/code_target.ML
haftmann@24219
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     3
haftmann@38910
     4
Generic infrastructure for target language data.
haftmann@24219
     5
*)
haftmann@24219
     6
haftmann@24219
     7
signature CODE_TARGET =
haftmann@24219
     8
sig
haftmann@36471
     9
  val cert_tyco: theory -> string -> string
haftmann@36471
    10
  val read_tyco: theory -> string -> string
haftmann@39057
    11
  val read_const_exprs: theory -> string list -> string list
haftmann@36471
    12
haftmann@38933
    13
  val export_code_for: theory -> Path.T option -> string -> int option -> string -> Token.T list
haftmann@55150
    14
    -> Code_Thingol.program -> Code_Symbol.T list -> unit
haftmann@38933
    15
  val produce_code_for: theory -> string -> int option -> string -> Token.T list
haftmann@55150
    16
    -> Code_Thingol.program -> Code_Symbol.T list -> (string * string) list * string option list
haftmann@38933
    17
  val present_code_for: theory -> string -> int option -> string -> Token.T list
haftmann@55150
    18
    -> Code_Thingol.program -> Code_Symbol.T list * Code_Symbol.T list -> string
haftmann@38918
    19
  val check_code_for: theory -> string -> bool -> Token.T list
haftmann@55150
    20
    -> Code_Thingol.program -> Code_Symbol.T list -> unit
haftmann@38918
    21
haftmann@38918
    22
  val export_code: theory -> string list
haftmann@38933
    23
    -> (((string * string) * Path.T option) * Token.T list) list -> unit
haftmann@38929
    24
  val produce_code: theory -> string list
haftmann@48568
    25
    -> string -> int option -> string -> Token.T list -> (string * string) list * string option list
haftmann@55150
    26
  val present_code: theory -> string list -> Code_Symbol.T list
haftmann@38933
    27
    -> string -> int option -> string -> Token.T list -> string
haftmann@38918
    28
  val check_code: theory -> string list
haftmann@38918
    29
    -> ((string * bool) * Token.T list) list -> unit
haftmann@38918
    30
haftmann@48568
    31
  val generatedN: string
haftmann@55147
    32
  val evaluator: theory -> string -> Code_Thingol.program
haftmann@55150
    33
    -> Code_Symbol.T list -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
haftmann@48568
    34
    -> (string * string) list * string
haftmann@41344
    35
haftmann@28054
    36
  type serializer
haftmann@34152
    37
  type literals = Code_Printer.literals
haftmann@37822
    38
  val add_target: string * { serializer: serializer, literals: literals,
wenzelm@41940
    39
    check: { env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string } }
wenzelm@41940
    40
    -> theory -> theory
haftmann@28663
    41
  val extend_target: string *
haftmann@55147
    42
      (string * (Code_Thingol.program -> Code_Thingol.program))
haftmann@28054
    43
    -> theory -> theory
haftmann@28054
    44
  val assert_target: theory -> string -> string
haftmann@38918
    45
  val the_literals: theory -> string -> literals
haftmann@28054
    46
  type serialization
wenzelm@36959
    47
  val parse_args: 'a parser -> Token.T list -> 'a
haftmann@38916
    48
  val serialization: (int -> Path.T option -> 'a -> unit)
haftmann@55150
    49
    -> (Code_Symbol.T list -> int -> 'a -> (string * string) list * (Code_Symbol.T -> string option))
haftmann@38925
    50
    -> 'a -> serialization
haftmann@38918
    51
  val set_default_code_width: int -> theory -> theory
haftmann@38917
    52
haftmann@52137
    53
  type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
haftmann@52138
    54
  type identifier_data
haftmann@52138
    55
  val set_identifiers: (string, string, string, string, string, string) symbol_attr_decl
haftmann@52138
    56
    -> theory -> theory
haftmann@55153
    57
  type raw_const_syntax = Code_Printer.raw_const_syntax
haftmann@34152
    58
  type tyco_syntax = Code_Printer.tyco_syntax
haftmann@55153
    59
  val set_printings: (raw_const_syntax, tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
haftmann@52137
    60
    -> theory -> theory
haftmann@55153
    61
  val add_const_syntax: string -> string -> raw_const_syntax option -> theory -> theory
haftmann@52137
    62
  val add_tyco_syntax: string -> string -> tyco_syntax option -> theory -> theory
haftmann@38923
    63
  val add_class_syntax: string -> class -> string option -> theory -> theory
haftmann@38923
    64
  val add_instance_syntax: string -> class * string -> unit option -> theory -> theory
haftmann@28054
    65
  val add_reserved: string -> string -> theory -> theory
haftmann@33969
    66
  val add_include: string -> string * (string * string list) option -> theory -> theory
haftmann@39646
    67
haftmann@39750
    68
  val codegen_tool: string (*theory name*) -> string (*export_code expr*) -> unit
wenzelm@43564
    69
wenzelm@43564
    70
  val setup: theory -> theory
haftmann@24219
    71
end;
haftmann@24219
    72
haftmann@28054
    73
structure Code_Target : CODE_TARGET =
haftmann@24219
    74
struct
haftmann@24219
    75
haftmann@55150
    76
open Basic_Code_Symbol;
haftmann@28054
    77
open Basic_Code_Thingol;
haftmann@34152
    78
haftmann@34152
    79
type literals = Code_Printer.literals;
haftmann@52137
    80
type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl =
haftmann@52137
    81
  (string * (string * 'a option) list, string * (string * 'b option) list,
haftmann@52137
    82
    class * (string * 'c option) list, (class * class) * (string * 'd option) list,
haftmann@52137
    83
    (class * string) * (string * 'e option) list,
haftmann@52137
    84
    string * (string * 'f option) list) Code_Symbol.attr;
haftmann@52138
    85
type identifier_data = (string, string, string, string, string, string) Code_Symbol.data;
haftmann@52137
    86
haftmann@34152
    87
type tyco_syntax = Code_Printer.tyco_syntax;
haftmann@55153
    88
type raw_const_syntax = Code_Printer.raw_const_syntax;
haftmann@34152
    89
haftmann@24219
    90
haftmann@52377
    91
(** checking and parsing of symbols **)
haftmann@52377
    92
haftmann@52377
    93
fun cert_const thy const =
haftmann@52377
    94
  let
haftmann@52377
    95
    val _ = if Sign.declared_const thy const then ()
haftmann@52377
    96
      else error ("No such constant: " ^ quote const);
haftmann@52377
    97
  in const end;
haftmann@52377
    98
haftmann@52377
    99
fun cert_tyco thy tyco =
haftmann@52377
   100
  let
haftmann@52377
   101
    val _ = if Sign.declared_tyname thy tyco then ()
haftmann@52377
   102
      else error ("No such type constructor: " ^ quote tyco);
haftmann@52377
   103
  in tyco end;
haftmann@52377
   104
haftmann@52377
   105
fun read_tyco thy = #1 o dest_Type
haftmann@52377
   106
  o Proof_Context.read_type_name_proper (Proof_Context.init_global thy) true;
haftmann@52377
   107
haftmann@52377
   108
fun cert_class thy class =
haftmann@52377
   109
  let
haftmann@52377
   110
    val _ = Axclass.get_info thy class;
haftmann@52377
   111
  in class end;
haftmann@52377
   112
haftmann@52377
   113
fun read_class thy = Proof_Context.read_class (Proof_Context.init_global thy);
haftmann@52377
   114
haftmann@52377
   115
val parse_classrel_ident = Parse.class --| @{keyword "<"} -- Parse.class;
haftmann@52377
   116
haftmann@52377
   117
fun cert_inst thy (class, tyco) =
haftmann@52377
   118
  (cert_class thy class, cert_tyco thy tyco);
haftmann@52377
   119
haftmann@52377
   120
fun read_inst thy (raw_tyco, raw_class) =
haftmann@55147
   121
  (read_tyco thy raw_tyco, read_class thy raw_class);
haftmann@52377
   122
haftmann@52377
   123
val parse_inst_ident = Parse.xname --| @{keyword "::"} -- Parse.class;
haftmann@52377
   124
haftmann@52377
   125
fun cert_syms thy =
haftmann@52377
   126
  Code_Symbol.map_attr (apfst (cert_const thy)) (apfst (cert_tyco thy))
haftmann@52377
   127
    (apfst (cert_class thy)) ((apfst o pairself) (cert_class thy)) (apfst (cert_inst thy)) I;
haftmann@52377
   128
haftmann@52377
   129
fun read_syms thy =
haftmann@52377
   130
  Code_Symbol.map_attr (apfst (Code.read_const thy)) (apfst (read_tyco thy))
haftmann@52377
   131
    (apfst (read_class thy)) ((apfst o pairself) (read_class thy)) (apfst (read_inst thy)) I;
haftmann@52377
   132
haftmann@52377
   133
fun check_name is_module s =
haftmann@52377
   134
  let
haftmann@52377
   135
    val _ = if s = "" then error "Bad empty code name" else ();
haftmann@52377
   136
    val xs = Long_Name.explode s;
haftmann@52377
   137
    val xs' = if is_module
haftmann@52377
   138
        then map (Name.desymbolize true) xs
haftmann@52377
   139
      else if length xs < 2
haftmann@52377
   140
        then error ("Bad code name without module component: " ^ quote s)
haftmann@52377
   141
      else
haftmann@52377
   142
        let
haftmann@52377
   143
          val (ys, y) = split_last xs;
haftmann@52377
   144
          val ys' = map (Name.desymbolize true) ys;
haftmann@52377
   145
          val y' = Name.desymbolize false y;
haftmann@52377
   146
        in ys' @ [y'] end;
haftmann@52377
   147
  in if xs' = xs
haftmann@52377
   148
    then s
haftmann@52377
   149
    else error ("Invalid code name: " ^ quote s ^ "\n"
haftmann@52377
   150
      ^ "better try " ^ quote (Long_Name.implode xs'))
haftmann@52377
   151
  end;
haftmann@52377
   152
haftmann@52377
   153
haftmann@52137
   154
(** serializations and serializer **)
haftmann@52137
   155
haftmann@52137
   156
(* serialization: abstract nonsense to cover different destinies for generated code *)
haftmann@24219
   157
haftmann@55150
   158
datatype destination = Export of Path.T option | Produce | Present of Code_Symbol.T list;
haftmann@55150
   159
type serialization = int -> destination -> ((string * string) list * (Code_Symbol.T -> string option)) option;
haftmann@27014
   160
haftmann@39659
   161
fun serialization output _ content width (Export some_path) =
haftmann@39661
   162
      (output width some_path content; NONE)
haftmann@39661
   163
  | serialization _ string content width Produce =
haftmann@39661
   164
      string [] width content |> SOME
haftmann@55147
   165
  | serialization _ string content width (Present syms) =
haftmann@55147
   166
     string syms width content
haftmann@48568
   167
     |> (apfst o map o apsnd) (Pretty.output (SOME width) o Pretty.str)
haftmann@39661
   168
     |> SOME;
haftmann@39659
   169
haftmann@39661
   170
fun export some_path f = (f (Export some_path); ());
haftmann@39661
   171
fun produce f = the (f Produce);
haftmann@55147
   172
fun present syms f = space_implode "\n\n" (map snd (fst (the (f (Present syms)))));
haftmann@38917
   173
haftmann@24219
   174
haftmann@52137
   175
(* serializers: functions producing serializations *)
haftmann@28054
   176
haftmann@39142
   177
type serializer = Token.T list
haftmann@52138
   178
  -> Proof.context
haftmann@39142
   179
  -> {
haftmann@52138
   180
    module_name: string,
haftmann@38926
   181
    reserved_syms: string list,
haftmann@52138
   182
    identifiers: identifier_data,
haftmann@38926
   183
    includes: (string * Pretty.T) list,
haftmann@38926
   184
    class_syntax: string -> string option,
haftmann@38926
   185
    tyco_syntax: string -> Code_Printer.tyco_syntax option,
haftmann@55153
   186
    const_syntax: string -> Code_Printer.const_syntax option }
haftmann@41342
   187
  -> Code_Thingol.program
haftmann@28054
   188
  -> serialization;
haftmann@27000
   189
haftmann@52137
   190
datatype description =
haftmann@52137
   191
    Fundamental of { serializer: serializer,
haftmann@38910
   192
      literals: literals,
haftmann@37822
   193
      check: { env_var: string, make_destination: Path.T -> Path.T,
wenzelm@41940
   194
        make_command: string -> string } }
haftmann@38909
   195
  | Extension of string *
haftmann@55147
   196
      (Code_Thingol.program -> Code_Thingol.program);
haftmann@28054
   197
haftmann@52137
   198
haftmann@52137
   199
(** theory data **)
haftmann@52137
   200
haftmann@28054
   201
datatype target = Target of {
haftmann@28054
   202
  serial: serial,
haftmann@37821
   203
  description: description,
haftmann@28054
   204
  reserved: string list,
haftmann@52138
   205
  identifiers: identifier_data,
haftmann@55153
   206
  printings: (Code_Printer.const_syntax, Code_Printer.tyco_syntax, string, unit, unit,
haftmann@52137
   207
    (Pretty.T * string list)) Code_Symbol.data
haftmann@28054
   208
};
haftmann@27103
   209
haftmann@52138
   210
fun make_target ((serial, description), (reserved, (identifiers, printings))) =
wenzelm@43564
   211
  Target { serial = serial, description = description, reserved = reserved,
haftmann@52138
   212
    identifiers = identifiers, printings = printings };
haftmann@52138
   213
fun map_target f (Target { serial, description, reserved, identifiers, printings }) =
haftmann@52138
   214
  make_target (f ((serial, description), (reserved, (identifiers, printings))));
haftmann@37821
   215
fun merge_target strict target (Target { serial = serial1, description = description,
haftmann@52138
   216
  reserved = reserved1, identifiers = identifiers1, printings = printings1 },
haftmann@37821
   217
    Target { serial = serial2, description = _,
haftmann@52138
   218
      reserved = reserved2, identifiers = identifiers2, printings = printings2 }) =
haftmann@28054
   219
  if serial1 = serial2 orelse not strict then
haftmann@52138
   220
    make_target ((serial1, description), (merge (op =) (reserved1, reserved2),
haftmann@52138
   221
      (Code_Symbol.merge_data (identifiers1, identifiers2),
haftmann@52138
   222
        Code_Symbol.merge_data (printings1, printings2))))
haftmann@28054
   223
  else
haftmann@37821
   224
    error ("Incompatible targets: " ^ quote target);
haftmann@27103
   225
haftmann@37821
   226
fun the_description (Target { description, ... }) = description;
haftmann@28054
   227
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@52138
   228
fun the_identifiers (Target { identifiers , ... }) = identifiers;
haftmann@52137
   229
fun the_printings (Target { printings, ... }) = printings;
haftmann@27437
   230
haftmann@34248
   231
structure Targets = Theory_Data
haftmann@34071
   232
(
haftmann@54890
   233
  type T = target Symtab.table * int;
haftmann@54890
   234
  val empty = (Symtab.empty, 80);
haftmann@34071
   235
  val extend = I;
haftmann@54890
   236
  fun merge ((target1, width1), (target2, width2)) : T =
haftmann@54890
   237
    (Symtab.join (merge_target true) (target1, target2), Int.max (width1, width2));
haftmann@34071
   238
);
haftmann@27436
   239
haftmann@54890
   240
fun assert_target thy target = if Symtab.defined (fst (Targets.get thy)) target
haftmann@33969
   241
  then target
haftmann@33969
   242
  else error ("Unknown code target language: " ^ quote target);
haftmann@28054
   243
haftmann@28054
   244
fun put_target (target, seri) thy =
haftmann@27304
   245
  let
haftmann@54890
   246
    val lookup_target = Symtab.lookup (fst (Targets.get thy));
haftmann@28054
   247
    val _ = case seri
haftmann@37821
   248
     of Extension (super, _) => if is_some (lookup_target super) then ()
haftmann@28054
   249
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   250
      | _ => ();
haftmann@37821
   251
    val overwriting = case (Option.map the_description o lookup_target) target
haftmann@28663
   252
     of NONE => false
haftmann@37821
   253
      | SOME (Extension _) => true
haftmann@37821
   254
      | SOME (Fundamental _) => (case seri
haftmann@37821
   255
         of Extension _ => error ("Will not overwrite existing target " ^ quote target)
haftmann@28663
   256
          | _ => true);
haftmann@28663
   257
    val _ = if overwriting
haftmann@28054
   258
      then warning ("Overwriting existing target " ^ quote target)
wenzelm@43564
   259
      else ();
haftmann@28054
   260
  in
haftmann@28054
   261
    thy
haftmann@54890
   262
    |> (Targets.map o apfst o Symtab.update)
haftmann@52138
   263
        (target, make_target ((serial (), seri),
haftmann@52138
   264
          ([], (Code_Symbol.empty_data, Code_Symbol.empty_data))))
haftmann@28054
   265
  end;
haftmann@27436
   266
haftmann@37821
   267
fun add_target (target, seri) = put_target (target, Fundamental seri);
haftmann@28054
   268
fun extend_target (target, (super, modify)) =
haftmann@37821
   269
  put_target (target, Extension (super, modify));
haftmann@27436
   270
haftmann@28054
   271
fun map_target_data target f thy =
haftmann@27436
   272
  let
haftmann@28054
   273
    val _ = assert_target thy target;
haftmann@28054
   274
  in
haftmann@28054
   275
    thy
haftmann@54890
   276
    |> (Targets.map o apfst o Symtab.map_entry target o map_target o apsnd) f
haftmann@28054
   277
  end;
haftmann@27304
   278
haftmann@28054
   279
fun map_reserved target =
haftmann@52138
   280
  map_target_data target o apfst;
haftmann@52138
   281
fun map_identifiers target =
haftmann@52138
   282
  map_target_data target o apsnd o apfst;
haftmann@52137
   283
fun map_printings target =
haftmann@52137
   284
  map_target_data target o apsnd o apsnd;
haftmann@27000
   285
haftmann@34248
   286
fun set_default_code_width k = (Targets.map o apsnd) (K k);
haftmann@34071
   287
haftmann@27000
   288
haftmann@34021
   289
(** serializer usage **)
haftmann@34021
   290
haftmann@34021
   291
(* montage *)
haftmann@34021
   292
haftmann@37822
   293
fun the_fundamental thy =
haftmann@34021
   294
  let
haftmann@54890
   295
    val (targets, _) = Targets.get thy;
haftmann@37822
   296
    fun fundamental target = case Symtab.lookup targets target
haftmann@37821
   297
     of SOME data => (case the_description data
haftmann@37822
   298
         of Fundamental data => data
haftmann@37822
   299
          | Extension (super, _) => fundamental super)
haftmann@34021
   300
      | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@37822
   301
  in fundamental end;
haftmann@37822
   302
haftmann@37822
   303
fun the_literals thy = #literals o the_fundamental thy;
haftmann@34021
   304
haftmann@39817
   305
fun collapse_hierarchy thy =
haftmann@38927
   306
  let
haftmann@54890
   307
    val (targets, _) = Targets.get thy;
wenzelm@43564
   308
    fun collapse target =
haftmann@38927
   309
      let
haftmann@38927
   310
        val data = case Symtab.lookup targets target
haftmann@38927
   311
         of SOME data => data
haftmann@38927
   312
          | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@38927
   313
      in case the_description data
haftmann@55147
   314
       of Fundamental _ => (I, data)
haftmann@38927
   315
        | Extension (super, modify) => let
haftmann@39817
   316
            val (modify', data') = collapse super
haftmann@55147
   317
          in (modify' #> modify, merge_target false target (data', data)) end
haftmann@38927
   318
      end;
haftmann@39817
   319
  in collapse end;
haftmann@39817
   320
haftmann@39817
   321
local
haftmann@39817
   322
haftmann@39817
   323
fun activate_target thy target =
haftmann@39817
   324
  let
haftmann@54890
   325
    val (_, default_width) = Targets.get thy;
haftmann@39817
   326
    val (modify, data) = collapse_hierarchy thy target;
haftmann@54890
   327
  in (default_width, data, modify) end;
haftmann@38927
   328
haftmann@55147
   329
fun project_program thy syms_hidden syms1 program2 =
haftmann@38927
   330
  let
wenzelm@42361
   331
    val ctxt = Proof_Context.init_global thy;
haftmann@55147
   332
    val syms2 = subtract (op =) syms_hidden syms1;
haftmann@55147
   333
    val program3 = Code_Symbol.Graph.restrict (not o member (op =) syms_hidden) program2;
haftmann@55147
   334
    val syms4 = Code_Symbol.Graph.all_succs program3 syms2;
haftmann@54890
   335
    val unimplemented = Code_Thingol.unimplemented program3;
wenzelm@42359
   336
    val _ =
haftmann@54889
   337
      if null unimplemented then ()
wenzelm@42359
   338
      else error ("No code equations for " ^
haftmann@54889
   339
        commas (map (Proof_Context.extern_const ctxt) unimplemented));
haftmann@55147
   340
    val program4 = Code_Symbol.Graph.restrict (member (op =) syms4) program3;
haftmann@55147
   341
  in (syms4, program4) end;
haftmann@38927
   342
haftmann@55149
   343
fun prepare_serializer thy (serializer : serializer) reserved identifiers
haftmann@55147
   344
    printings module_name args proto_program syms =
haftmann@38927
   345
  let
haftmann@55149
   346
    val syms_hidden = Code_Symbol.symbols_of printings;
haftmann@55147
   347
    val (syms_all, program) = project_program thy syms_hidden syms proto_program;
haftmann@39484
   348
    fun select_include (name, (content, cs)) =
haftmann@55150
   349
      if null cs orelse exists (fn c => member (op =) syms_all (Constant c)) cs
haftmann@39484
   350
      then SOME (name, content) else NONE;
haftmann@52137
   351
    val includes = map_filter select_include (Code_Symbol.dest_module_data printings);
haftmann@34021
   352
  in
haftmann@52138
   353
    (serializer args (Proof_Context.init_global thy) {
haftmann@52138
   354
      module_name = module_name,
haftmann@38926
   355
      reserved_syms = reserved,
haftmann@52138
   356
      identifiers = identifiers,
haftmann@38926
   357
      includes = includes,
haftmann@55149
   358
      const_syntax = Code_Symbol.lookup_constant_data printings,
haftmann@55149
   359
      tyco_syntax = Code_Symbol.lookup_type_constructor_data printings,
haftmann@55149
   360
      class_syntax = Code_Symbol.lookup_type_class_data printings },
haftmann@41342
   361
      program)
haftmann@34021
   362
  end;
haftmann@34021
   363
haftmann@55147
   364
fun mount_serializer thy target some_width module_name args program syms =
haftmann@34021
   365
  let
haftmann@54890
   366
    val (default_width, data, modify) = activate_target thy target;
haftmann@38921
   367
    val serializer = case the_description data
haftmann@38927
   368
     of Fundamental seri => #serializer seri;
haftmann@52138
   369
    val (prepared_serializer, prepared_program) =
haftmann@55149
   370
      prepare_serializer thy serializer
haftmann@52138
   371
        (the_reserved data) (the_identifiers data) (the_printings data)
haftmann@55147
   372
        module_name args (modify program) syms
haftmann@34071
   373
    val width = the_default default_width some_width;
haftmann@41344
   374
  in (fn program => prepared_serializer program width, prepared_program) end;
haftmann@41344
   375
haftmann@55147
   376
fun invoke_serializer thy target some_width module_name args program syms =
haftmann@41344
   377
  let
haftmann@53413
   378
    val check = if module_name = "" then I else check_name true;
haftmann@41344
   379
    val (mounted_serializer, prepared_program) = mount_serializer thy
haftmann@55147
   380
      target some_width (check module_name) args program syms;
haftmann@41344
   381
  in mounted_serializer prepared_program end;
haftmann@34021
   382
haftmann@52138
   383
fun assert_module_name "" = error "Empty module name not allowed here"
haftmann@38933
   384
  | assert_module_name module_name = module_name;
haftmann@38933
   385
haftmann@48426
   386
fun using_master_directory thy =
haftmann@48426
   387
  Option.map (Path.append (File.pwd ()) o Path.append (Thy_Load.master_directory thy));
haftmann@48371
   388
haftmann@34021
   389
in
haftmann@34021
   390
haftmann@48568
   391
val generatedN = "Generated_Code";
haftmann@48568
   392
haftmann@39484
   393
fun export_code_for thy some_path target some_width module_name args =
haftmann@48371
   394
  export (using_master_directory thy some_path)
haftmann@55147
   395
  oo invoke_serializer thy target some_width module_name args;
haftmann@38918
   396
haftmann@39484
   397
fun produce_code_for thy target some_width module_name args =
haftmann@39102
   398
  let
haftmann@41344
   399
    val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
haftmann@55147
   400
  in fn program => fn syms =>
haftmann@55147
   401
    produce (serializer program syms) |> apsnd (fn deresolve => map deresolve syms)
haftmann@39484
   402
  end;
haftmann@38929
   403
haftmann@39484
   404
fun present_code_for thy target some_width module_name args =
haftmann@39484
   405
  let
haftmann@41344
   406
    val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
haftmann@55147
   407
  in fn program => fn (syms, selects) =>
haftmann@55147
   408
    present selects (serializer program syms)
haftmann@39484
   409
  end;
haftmann@38918
   410
haftmann@55147
   411
fun check_code_for thy target strict args program syms =
haftmann@37824
   412
  let
haftmann@37824
   413
    val { env_var, make_destination, make_command } =
haftmann@37824
   414
      (#check o the_fundamental thy) target;
wenzelm@41939
   415
    fun ext_check p =
wenzelm@43564
   416
      let
haftmann@37824
   417
        val destination = make_destination p;
haftmann@41344
   418
        val _ = export (SOME destination) (invoke_serializer thy target (SOME 80)
haftmann@55147
   419
          generatedN args program syms);
haftmann@48568
   420
        val cmd = make_command generatedN;
wenzelm@43850
   421
      in
wenzelm@43850
   422
        if Isabelle_System.bash ("cd " ^ File.shell_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
haftmann@37824
   423
        then error ("Code check failed for " ^ target ^ ": " ^ cmd)
haftmann@37824
   424
        else ()
haftmann@37824
   425
      end;
wenzelm@43850
   426
  in
wenzelm@43850
   427
    if getenv env_var = ""
haftmann@37825
   428
    then if strict
haftmann@37825
   429
      then error (env_var ^ " not set; cannot check code for " ^ target)
haftmann@37825
   430
      else warning (env_var ^ " not set; skipped checking code for " ^ target)
wenzelm@41939
   431
    else Isabelle_System.with_tmp_dir "Code_Test" ext_check
haftmann@37824
   432
  end;
haftmann@37824
   433
haftmann@55147
   434
fun evaluation mounted_serializer prepared_program syms ((vs, ty), t) =
haftmann@41344
   435
  let
haftmann@41344
   436
    val _ = if Code_Thingol.contains_dict_var t then
haftmann@41344
   437
      error "Term to be evaluated contains free dictionaries" else ();
wenzelm@43324
   438
    val v' = singleton (Name.variant_list (map fst vs)) "a";
haftmann@41344
   439
    val vs' = (v', []) :: vs;
haftmann@55147
   440
    val ty' = ITyVar v' `-> ty;
haftmann@41344
   441
    val program = prepared_program
haftmann@55147
   442
      |> Code_Symbol.Graph.new_node (Code_Symbol.value,
haftmann@55147
   443
          Code_Thingol.Fun (((vs', ty'), [(([IVar NONE], t), (NONE, true))]), NONE))
haftmann@55147
   444
      |> fold (curry (perhaps o try o
haftmann@55147
   445
          Code_Symbol.Graph.add_edge) Code_Symbol.value) syms;
haftmann@41344
   446
    val (program_code, deresolve) = produce (mounted_serializer program);
haftmann@55147
   447
    val value_name = the (deresolve Code_Symbol.value);
haftmann@55147
   448
  in (program_code, value_name) end;
haftmann@41344
   449
haftmann@55147
   450
fun evaluator thy target program syms =
haftmann@41344
   451
  let
haftmann@55147
   452
    val (mounted_serializer, prepared_program) =
haftmann@55147
   453
      mount_serializer thy target NONE generatedN [] program syms;
haftmann@55147
   454
  in evaluation mounted_serializer prepared_program syms end;
haftmann@41344
   455
haftmann@34021
   456
end; (* local *)
haftmann@34021
   457
haftmann@34021
   458
haftmann@34021
   459
(* code generation *)
haftmann@34021
   460
haftmann@55147
   461
fun read_const_exprs thy const_exprs =
haftmann@34021
   462
  let
haftmann@55147
   463
    val (cs1, cs2) = Code_Thingol.read_const_exprs thy const_exprs;
haftmann@55147
   464
    val program = Code_Thingol.consts_program thy true cs2;
haftmann@55147
   465
    val cs3 = Code_Thingol.implemented_deps program;
haftmann@36271
   466
  in union (op =) cs3 cs1 end;
haftmann@34021
   467
haftmann@38918
   468
fun prep_destination "" = NONE
haftmann@38918
   469
  | prep_destination s = SOME (Path.explode s);
haftmann@38918
   470
haftmann@34021
   471
fun export_code thy cs seris =
haftmann@34021
   472
  let
haftmann@55147
   473
    val program = Code_Thingol.consts_program thy false cs;
haftmann@38918
   474
    val _ = map (fn (((target, module_name), some_path), args) =>
haftmann@55150
   475
      export_code_for thy some_path target NONE module_name args program (map Constant cs)) seris;
haftmann@34021
   476
  in () end;
haftmann@34021
   477
haftmann@38918
   478
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs)
haftmann@38918
   479
  ((map o apfst o apsnd) prep_destination seris);
haftmann@38918
   480
haftmann@38929
   481
fun produce_code thy cs target some_width some_module_name args =
haftmann@38918
   482
  let
haftmann@55147
   483
    val program = Code_Thingol.consts_program thy false cs;
haftmann@55150
   484
  in produce_code_for thy target some_width some_module_name args program (map Constant cs) end;
haftmann@38929
   485
haftmann@55147
   486
fun present_code thy cs syms target some_width some_module_name args =
haftmann@38929
   487
  let
haftmann@55147
   488
    val program = Code_Thingol.consts_program thy false cs;
haftmann@55150
   489
  in present_code_for thy target some_width some_module_name args program (map Constant cs, syms) end;
haftmann@34021
   490
haftmann@37824
   491
fun check_code thy cs seris =
haftmann@37824
   492
  let
haftmann@55147
   493
    val program = Code_Thingol.consts_program thy false cs;
haftmann@38918
   494
    val _ = map (fn ((target, strict), args) =>
haftmann@55150
   495
      check_code_for thy target strict args program (map Constant cs)) seris;
haftmann@37824
   496
  in () end;
haftmann@37824
   497
haftmann@37824
   498
fun check_code_cmd raw_cs seris thy = check_code thy (read_const_exprs thy raw_cs) seris;
haftmann@37824
   499
haftmann@39480
   500
local
haftmann@39480
   501
haftmann@39480
   502
val parse_const_terms = Scan.repeat1 Args.term
haftmann@39480
   503
  >> (fn ts => fn thy => map (Code.check_const thy) ts);
haftmann@39480
   504
haftmann@55147
   505
fun parse_names category parse internalize mark_symbol =
haftmann@39480
   506
  Scan.lift (Args.parens (Args.$$$ category)) |-- Scan.repeat1 parse
haftmann@55147
   507
  >> (fn xs => fn thy => map (mark_symbol o internalize thy) xs);
wenzelm@43564
   508
haftmann@39480
   509
val parse_consts = parse_names "consts" Args.term
haftmann@55150
   510
  Code.check_const Constant;
haftmann@39480
   511
haftmann@39480
   512
val parse_types = parse_names "types" (Scan.lift Args.name)
haftmann@55150
   513
  Sign.intern_type Type_Constructor;
haftmann@39480
   514
haftmann@39480
   515
val parse_classes = parse_names "classes" (Scan.lift Args.name)
haftmann@55150
   516
  Sign.intern_class Type_Class;
haftmann@39480
   517
haftmann@39480
   518
val parse_instances = parse_names "instances" (Scan.lift (Args.name --| Args.$$$ "::" -- Args.name))
haftmann@55147
   519
  (fn thy => fn (raw_tyco, raw_class) => (Sign.intern_class thy raw_tyco, Sign.intern_type thy raw_class))
haftmann@55150
   520
    Class_Instance;
haftmann@39480
   521
haftmann@39480
   522
in
haftmann@39480
   523
wenzelm@43564
   524
val antiq_setup =
wenzelm@43564
   525
  Thy_Output.antiquotation @{binding code_stmts}
wenzelm@43564
   526
    (parse_const_terms --
wenzelm@43564
   527
      Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances)
wenzelm@43564
   528
      -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int)))
wenzelm@43564
   529
    (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), (target, some_width)) =>
wenzelm@43564
   530
      let val thy = Proof_Context.theory_of ctxt in
wenzelm@43564
   531
        present_code thy (mk_cs thy)
haftmann@55147
   532
          (maps (fn f => f thy) mk_stmtss)
wenzelm@43564
   533
          target some_width "Example" []
wenzelm@43564
   534
      end);
haftmann@39480
   535
haftmann@39480
   536
end;
haftmann@39480
   537
haftmann@34021
   538
haftmann@28054
   539
(** serializer configuration **)
haftmann@27000
   540
haftmann@52138
   541
(* reserved symbol names *)
haftmann@52138
   542
haftmann@52138
   543
fun add_reserved target sym thy =
haftmann@52138
   544
  let
haftmann@52138
   545
    val (_, data) = collapse_hierarchy thy target;
haftmann@52138
   546
    val _ = if member (op =) (the_reserved data) sym
haftmann@52138
   547
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@52138
   548
      else ();
haftmann@52138
   549
  in
haftmann@52138
   550
    thy
haftmann@52138
   551
    |> map_reserved target (insert (op =) sym)
haftmann@52138
   552
  end;
haftmann@52138
   553
haftmann@52138
   554
haftmann@52377
   555
(* checking of syntax *)
haftmann@52138
   556
haftmann@55149
   557
fun check_const_syntax thy target c syn =
haftmann@52137
   558
  if Code_Printer.requires_args syn > Code.args_number thy c
haftmann@52137
   559
  then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@55153
   560
  else Code_Printer.prep_const_syntax thy (the_literals thy target) c syn;
haftmann@52137
   561
haftmann@55149
   562
fun check_tyco_syntax thy target tyco syn =
haftmann@52137
   563
  if fst syn <> Sign.arity_number thy tyco
haftmann@52137
   564
  then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@52137
   565
  else syn;
haftmann@34071
   566
haftmann@52138
   567
haftmann@52138
   568
(* custom symbol names *)
haftmann@52138
   569
wenzelm@52218
   570
fun arrange_name_decls x =
haftmann@52138
   571
  let
haftmann@52138
   572
    fun arrange is_module (sym, target_names) = map (fn (target, some_name) =>
haftmann@52138
   573
      (target, (sym, Option.map (check_name is_module) some_name))) target_names;
haftmann@52138
   574
  in
haftmann@52138
   575
    Code_Symbol.maps_attr' (arrange false) (arrange false) (arrange false)
wenzelm@52218
   576
      (arrange false) (arrange false) (arrange true) x
haftmann@52138
   577
  end;
haftmann@52138
   578
haftmann@52138
   579
fun cert_name_decls thy = cert_syms thy #> arrange_name_decls;
haftmann@52138
   580
haftmann@52138
   581
fun read_name_decls thy = read_syms thy #> arrange_name_decls;
haftmann@52138
   582
haftmann@52138
   583
fun set_identifier (target, sym_name) = map_identifiers target (Code_Symbol.set_data sym_name);
haftmann@52138
   584
haftmann@52138
   585
fun gen_set_identifiers prep_name_decl raw_name_decls thy =
haftmann@52138
   586
  fold set_identifier (prep_name_decl thy raw_name_decls) thy;
haftmann@52138
   587
haftmann@52138
   588
val set_identifiers = gen_set_identifiers cert_name_decls;
haftmann@52138
   589
val set_identifiers_cmd = gen_set_identifiers read_name_decls;
haftmann@52138
   590
wenzelm@54312
   591
fun add_module_alias_cmd target aliasses thy =
haftmann@52435
   592
  let
haftmann@52435
   593
    val _ = legacy_feature "prefer \"code_identifier\" over \"code_modulename\"";
haftmann@52435
   594
  in
haftmann@52435
   595
    fold (fn (sym, name) => set_identifier
wenzelm@54312
   596
      (target, Code_Symbol.Module (sym, if name = "" then NONE else SOME (check_name true name))))
wenzelm@54312
   597
      aliasses thy
haftmann@52435
   598
  end;
haftmann@52138
   599
haftmann@52138
   600
haftmann@52138
   601
(* custom printings *)
haftmann@52138
   602
haftmann@52137
   603
fun arrange_printings prep_const thy =
haftmann@52137
   604
  let
haftmann@52137
   605
    fun arrange check (sym, target_syns) =
haftmann@55149
   606
      map (fn (target, some_syn) => (target, (sym, Option.map (check thy target sym) some_syn))) target_syns;
haftmann@52137
   607
  in
haftmann@52137
   608
    Code_Symbol.maps_attr'
haftmann@52137
   609
      (arrange check_const_syntax) (arrange check_tyco_syntax)
haftmann@55149
   610
        (arrange ((K o K o K) I)) (arrange ((K o K o K) I)) (arrange ((K o K o K) I))
haftmann@55149
   611
        (arrange (fn thy => fn _ => fn _ => fn (raw_content, raw_cs) =>
haftmann@52137
   612
          (Code_Printer.str raw_content, map (prep_const thy) raw_cs)))
haftmann@52137
   613
  end;
haftmann@52137
   614
haftmann@52137
   615
fun cert_printings thy = cert_syms thy #> arrange_printings cert_const thy;
haftmann@24219
   616
haftmann@52137
   617
fun read_printings thy = read_syms thy #> arrange_printings Code.read_const thy;
haftmann@52137
   618
haftmann@52137
   619
fun set_printing (target, sym_syn) = map_printings target (Code_Symbol.set_data sym_syn);
haftmann@52137
   620
haftmann@52137
   621
fun gen_set_printings prep_print_decl raw_print_decls thy =
haftmann@52137
   622
  fold set_printing (prep_print_decl thy raw_print_decls) thy;
haftmann@52137
   623
haftmann@52137
   624
val set_printings = gen_set_printings cert_printings;
haftmann@52137
   625
val set_printings_cmd = gen_set_printings read_printings;
haftmann@52137
   626
haftmann@52137
   627
fun gen_add_syntax Symbol prep_x prep_syn target raw_x some_raw_syn thy =
haftmann@24992
   628
  let
haftmann@52435
   629
    val _ = legacy_feature "prefer \"code_printing\" for custom serialisations"
haftmann@52137
   630
    val x = prep_x thy raw_x;
haftmann@55149
   631
  in set_printing (target, Symbol (x, Option.map (prep_syn thy target x) some_raw_syn)) thy end;
haftmann@28926
   632
haftmann@52137
   633
fun gen_add_const_syntax prep_const =
haftmann@55150
   634
  gen_add_syntax Constant prep_const check_const_syntax;
haftmann@52137
   635
haftmann@52137
   636
fun gen_add_tyco_syntax prep_tyco =
haftmann@55150
   637
  gen_add_syntax Type_Constructor prep_tyco check_tyco_syntax;
haftmann@52137
   638
haftmann@52137
   639
fun gen_add_class_syntax prep_class =
haftmann@55150
   640
  gen_add_syntax Type_Class prep_class ((K o K o K) I);
haftmann@24219
   641
haftmann@52137
   642
fun gen_add_instance_syntax prep_inst =
haftmann@55150
   643
  gen_add_syntax Class_Instance prep_inst ((K o K o K) I);
haftmann@52137
   644
haftmann@52137
   645
fun gen_add_include prep_const target (name, some_content) thy =
haftmann@55150
   646
  gen_add_syntax Module (K I)
haftmann@55149
   647
    (fn thy => fn _ => fn _ => fn (raw_content, raw_cs) => (Code_Printer.str raw_content, map (prep_const thy) raw_cs))
haftmann@52137
   648
    target name some_content thy;
haftmann@52137
   649
haftmann@52137
   650
haftmann@27103
   651
(* concrete syntax *)
haftmann@27000
   652
haftmann@34021
   653
local
haftmann@34021
   654
haftmann@47576
   655
fun zip_list (x :: xs) f g =
haftmann@34152
   656
  f
haftmann@37881
   657
  :|-- (fn y =>
haftmann@34152
   658
    fold_map (fn x => g |-- f >> pair x) xs
haftmann@37881
   659
    :|-- (fn xys => pair ((x, y) :: xys)));
haftmann@34152
   660
haftmann@37881
   661
fun process_multi_syntax parse_thing parse_syntax change =
haftmann@37881
   662
  (Parse.and_list1 parse_thing
wenzelm@46949
   663
  :|-- (fn things => Scan.repeat1 (@{keyword "("} |-- Parse.name --
haftmann@52068
   664
        (zip_list things (Scan.option parse_syntax) @{keyword "and"}) --| @{keyword ")"})))
haftmann@37881
   665
  >> (Toplevel.theory oo fold)
haftmann@37881
   666
    (fn (target, syns) => fold (fn (raw_x, syn) => change target raw_x syn) syns);
haftmann@24219
   667
haftmann@24219
   668
in
haftmann@24219
   669
haftmann@52137
   670
val add_reserved = add_reserved;
haftmann@52137
   671
val add_const_syntax = gen_add_const_syntax (K I);
haftmann@52137
   672
val add_tyco_syntax = gen_add_tyco_syntax cert_tyco;
haftmann@38923
   673
val add_class_syntax = gen_add_class_syntax cert_class;
haftmann@38923
   674
val add_instance_syntax = gen_add_instance_syntax cert_inst;
haftmann@52137
   675
val add_include = gen_add_include (K I);
haftmann@24219
   676
haftmann@52137
   677
val add_const_syntax_cmd = gen_add_const_syntax Code.read_const;
haftmann@52137
   678
val add_tyco_syntax_cmd = gen_add_tyco_syntax read_tyco;
haftmann@38923
   679
val add_class_syntax_cmd = gen_add_class_syntax read_class;
haftmann@38923
   680
val add_instance_syntax_cmd = gen_add_instance_syntax read_inst;
haftmann@52137
   681
val add_include_cmd = gen_add_include Code.read_const;
haftmann@24219
   682
haftmann@28054
   683
fun parse_args f args =
wenzelm@36959
   684
  case Scan.read Token.stopper f args
haftmann@28054
   685
   of SOME x => x
haftmann@28054
   686
    | NONE => error "Bad serializer arguments";
haftmann@28054
   687
haftmann@28054
   688
haftmann@27304
   689
(** Isar setup **)
haftmann@27103
   690
haftmann@52137
   691
fun parse_single_symbol_pragma parse_keyword parse_isa parse_target =
haftmann@52434
   692
  parse_keyword |-- Parse.!!! (parse_isa --| (@{keyword "\<rightharpoonup>"} || @{keyword "=>"})
haftmann@52434
   693
    -- Parse.and_list1 (@{keyword "("} |-- (Parse.name --| @{keyword ")"} -- Scan.option parse_target)));
haftmann@52137
   694
haftmann@52137
   695
fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
wenzelm@52801
   696
  parse_single_symbol_pragma @{keyword "constant"} Parse.term parse_const
haftmann@55150
   697
    >> Constant
haftmann@52377
   698
  || parse_single_symbol_pragma @{keyword "type_constructor"} Parse.type_const parse_tyco
haftmann@55150
   699
    >> Type_Constructor
haftmann@52377
   700
  || parse_single_symbol_pragma @{keyword "type_class"} Parse.class parse_class
haftmann@55150
   701
    >> Type_Class
haftmann@52137
   702
  || parse_single_symbol_pragma @{keyword "class_relation"} parse_classrel_ident parse_classrel
haftmann@55150
   703
    >> Class_Relation
haftmann@52137
   704
  || parse_single_symbol_pragma @{keyword "class_instance"} parse_inst_ident parse_inst
haftmann@55150
   705
    >> Class_Instance
haftmann@52137
   706
  || parse_single_symbol_pragma @{keyword "code_module"} Parse.name parse_module
haftmann@52137
   707
    >> Code_Symbol.Module;
haftmann@52137
   708
haftmann@52137
   709
fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
haftmann@52137
   710
  Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma")
haftmann@52137
   711
    (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module));
haftmann@52137
   712
haftmann@52138
   713
val code_expr_argsP = Scan.optional (@{keyword "("} |-- Args.parse --| @{keyword ")"}) [];
haftmann@52138
   714
haftmann@52434
   715
fun code_expr_inP raw_cs =
haftmann@52434
   716
  Scan.repeat (@{keyword "in"} |-- Parse.!!! (Parse.name
haftmann@52434
   717
    -- Scan.optional (@{keyword "module_name"} |-- Parse.name) ""
haftmann@52434
   718
    -- Scan.optional (@{keyword "file"} |-- Parse.name) ""
haftmann@52434
   719
    -- code_expr_argsP))
haftmann@52434
   720
      >> (fn seri_args => export_code_cmd raw_cs seri_args);
haftmann@52434
   721
haftmann@52434
   722
fun code_expr_checkingP raw_cs =
haftmann@52434
   723
  (@{keyword "checking"} |-- Parse.!!!
haftmann@52434
   724
    (Scan.repeat (Parse.name -- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true)
haftmann@52434
   725
    -- code_expr_argsP)))
haftmann@52434
   726
      >> (fn seri_args => check_code_cmd raw_cs seri_args);
haftmann@52434
   727
wenzelm@52801
   728
val code_exprP = Scan.repeat1 Parse.term
haftmann@52434
   729
  :|-- (fn raw_cs => (code_expr_checkingP raw_cs || code_expr_inP raw_cs));
haftmann@52138
   730
haftmann@52138
   731
val _ =
haftmann@52138
   732
  Outer_Syntax.command @{command_spec "code_reserved"}
haftmann@52138
   733
    "declare words as reserved for target language"
haftmann@52138
   734
    (Parse.name -- Scan.repeat1 Parse.name
haftmann@52138
   735
      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
haftmann@52138
   736
haftmann@52138
   737
val _ =
haftmann@52138
   738
  Outer_Syntax.command @{command_spec "code_identifier"} "declare mandatory names for code symbols"
haftmann@52138
   739
    (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name
haftmann@52138
   740
      >> (Toplevel.theory o fold set_identifiers_cmd));
haftmann@52138
   741
haftmann@52138
   742
val _ =
haftmann@52138
   743
  Outer_Syntax.command @{command_spec "code_modulename"} "alias module to other name"
haftmann@52138
   744
    (Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
haftmann@52138
   745
      >> (fn (target, modlnames) => (Toplevel.theory o add_module_alias_cmd target) modlnames));
haftmann@52138
   746
haftmann@52137
   747
val _ =
haftmann@52137
   748
  Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
haftmann@52137
   749
    (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
haftmann@52137
   750
      Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
haftmann@52137
   751
      (Parse.text -- Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [])
haftmann@52137
   752
      >> (Toplevel.theory o fold set_printings_cmd));
haftmann@52137
   753
haftmann@52137
   754
val _ =
haftmann@52137
   755
  Outer_Syntax.command @{command_spec "code_const"} "define code syntax for constant"
wenzelm@52801
   756
    (process_multi_syntax Parse.term Code_Printer.parse_const_syntax
haftmann@52137
   757
      add_const_syntax_cmd);
haftmann@52137
   758
haftmann@52137
   759
val _ =
haftmann@52137
   760
  Outer_Syntax.command @{command_spec "code_type"} "define code syntax for type constructor"
haftmann@52377
   761
    (process_multi_syntax Parse.type_const Code_Printer.parse_tyco_syntax
haftmann@52137
   762
      add_tyco_syntax_cmd);
haftmann@52137
   763
wenzelm@24867
   764
val _ =
wenzelm@46961
   765
  Outer_Syntax.command @{command_spec "code_class"} "define code syntax for class"
haftmann@52377
   766
    (process_multi_syntax Parse.class Parse.string
wenzelm@46961
   767
      add_class_syntax_cmd);
haftmann@24219
   768
wenzelm@24867
   769
val _ =
wenzelm@46961
   770
  Outer_Syntax.command @{command_spec "code_instance"} "define code syntax for instance"
haftmann@52377
   771
    (process_multi_syntax parse_inst_ident (Parse.minus >> K ())
wenzelm@46961
   772
      add_instance_syntax_cmd);
haftmann@24219
   773
wenzelm@24867
   774
val _ =
wenzelm@46961
   775
  Outer_Syntax.command @{command_spec "code_include"}
wenzelm@46961
   776
    "declare piece of code to be included in generated code"
wenzelm@46961
   777
    (Parse.name -- Parse.name -- (Parse.text :|--
wenzelm@46961
   778
      (fn "-" => Scan.succeed NONE
wenzelm@46961
   779
        | s => Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [] >> pair s >> SOME))
wenzelm@46961
   780
      >> (fn ((target, name), content_consts) =>
wenzelm@46961
   781
          (Toplevel.theory o add_include_cmd target) (name, content_consts)));
haftmann@24992
   782
haftmann@24992
   783
val _ =
wenzelm@46961
   784
  Outer_Syntax.command @{command_spec "export_code"} "generate executable code for constants"
wenzelm@46961
   785
    (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
haftmann@30494
   786
haftmann@24219
   787
end; (*local*)
haftmann@24219
   788
haftmann@39646
   789
haftmann@39646
   790
(** external entrance point -- for codegen tool **)
haftmann@39646
   791
haftmann@39750
   792
fun codegen_tool thyname cmd_expr =
haftmann@39646
   793
  let
haftmann@39646
   794
    val thy = Thy_Info.get_theory thyname;
haftmann@39646
   795
    val parse = Scan.read Token.stopper (Parse.!!! code_exprP) o
haftmann@39646
   796
      (filter Token.is_proper o Outer_Syntax.scan Position.none);
haftmann@39646
   797
  in case parse cmd_expr
haftmann@39646
   798
   of SOME f => (writeln "Now generating code..."; f thy)
haftmann@39646
   799
    | NONE => error ("Bad directive " ^ quote cmd_expr)
haftmann@39646
   800
  end;
haftmann@39646
   801
wenzelm@43564
   802
wenzelm@43564
   803
(** theory setup **)
wenzelm@43564
   804
wenzelm@43564
   805
val setup = antiq_setup;
wenzelm@43564
   806
haftmann@24219
   807
end; (*struct*)