src/Tools/code/code_target.ML
author haftmann
Mon, 01 Sep 2008 10:18:37 +0200
changeset 28064 d4a6460c53d1
parent 28054 2b84d34c5d02
child 28090 29af3c712d2b
permissions -rw-r--r--
restructured code generation of literals
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@24219
    11
haftmann@28054
    12
  type serializer
haftmann@28064
    13
  val add_target: string * (serializer * literals) -> 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@28054
    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@28064
    31
  val serialize_custom: theory -> string * (serializer * literals)
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@28064
    54
  val add_literal_list: string -> string -> string -> theory -> theory
haftmann@28064
    55
  val add_literal_list_string: string -> string -> string
haftmann@28054
    56
    -> string -> string list -> theory -> theory
haftmann@28064
    57
  val add_literal_char: string -> string -> string list -> theory -> theory
haftmann@28064
    58
  val add_literal_numeral: string -> bool -> bool -> string -> string -> string
haftmann@28054
    59
    -> string -> string -> theory -> theory
haftmann@28064
    60
  val add_literal_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@24219
    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@28064
   150
(* literal syntax printing *)
haftmann@24219
   151
haftmann@24219
   152
fun default_list (target_fxy, target_cons) pr fxy t1 t2 =
haftmann@24219
   153
  brackify_infix (target_fxy, R) fxy [
haftmann@24219
   154
    pr (INFX (target_fxy, X)) t1,
haftmann@24219
   155
    str target_cons,
haftmann@24219
   156
    pr (INFX (target_fxy, R)) t2
haftmann@24219
   157
  ];
haftmann@24219
   158
haftmann@28064
   159
fun pretty_list c_nil c_cons literals =
haftmann@24219
   160
  let
haftmann@28064
   161
    val mk_list = literal_list literals;
haftmann@27304
   162
    fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] =
haftmann@24219
   163
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
haftmann@24219
   164
       of SOME ts => mk_list (map (pr vars NOBR) ts)
haftmann@28064
   165
        | NONE => default_list (infix_cons literals) (pr vars) fxy t1 t2;
haftmann@24219
   166
  in (2, pretty) end;
haftmann@24219
   167
haftmann@28064
   168
fun pretty_list_string c_nil c_cons c_char c_nibbles literals =
haftmann@24219
   169
  let
haftmann@28064
   170
    val mk_list = literal_list literals;
haftmann@28064
   171
    val mk_char = literal_char literals;
haftmann@28064
   172
    val mk_string = literal_string literals;
haftmann@27304
   173
    fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] =
haftmann@24219
   174
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
haftmann@26448
   175
       of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
haftmann@24219
   176
           of SOME p => p
haftmann@26448
   177
            | NONE => mk_list (map (pr vars NOBR) ts))
haftmann@28064
   178
        | NONE => default_list (infix_cons literals) (pr vars) fxy t1 t2;
haftmann@24219
   179
  in (2, pretty) end;
haftmann@24219
   180
haftmann@28064
   181
fun pretty_char c_char c_nibbles literals =
haftmann@24219
   182
  let
haftmann@28064
   183
    val mk_char = literal_char literals;
haftmann@27304
   184
    fun pretty _ thm _ _ _ [(t1, _), (t2, _)] =
haftmann@24219
   185
      case decode_char c_nibbles (t1, t2)
haftmann@24219
   186
       of SOME c => (str o mk_char) c
haftmann@27304
   187
        | NONE => nerror thm "Illegal character expression";
haftmann@24219
   188
  in (2, pretty) end;
haftmann@24219
   189
haftmann@28064
   190
fun pretty_numeral unbounded negative c_pls c_min c_bit0 c_bit1 literals =
haftmann@24219
   191
  let
haftmann@28064
   192
    val mk_numeral = literal_numeral literals;
haftmann@27304
   193
    fun pretty _ thm _ _ _ [(t, _)] =
haftmann@27304
   194
      (str o mk_numeral unbounded o implode_numeral thm negative c_pls c_min c_bit0 c_bit1) t;
haftmann@24219
   195
  in (1, pretty) end;
haftmann@24219
   196
haftmann@28064
   197
fun pretty_message c_char c_nibbles c_nil c_cons literals =
haftmann@24219
   198
  let
haftmann@28064
   199
    val mk_char = literal_char literals;
haftmann@28064
   200
    val mk_string = literal_string literals;
haftmann@27304
   201
    fun pretty _ thm _ _ _ [(t, _)] =
haftmann@24219
   202
      case implode_list c_nil c_cons t
haftmann@24219
   203
       of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
haftmann@24219
   204
           of SOME p => p
haftmann@27304
   205
            | NONE => nerror thm "Illegal message expression")
haftmann@27304
   206
        | NONE => nerror thm "Illegal message expression";
haftmann@24219
   207
  in (1, pretty) end;
haftmann@24219
   208
haftmann@27000
   209
haftmann@28054
   210
(** theory data **)
haftmann@27000
   211
haftmann@28054
   212
datatype name_syntax_table = NameSyntaxTable of {
haftmann@28054
   213
  class: class_syntax Symtab.table,
haftmann@28054
   214
  inst: unit Symtab.table,
haftmann@28054
   215
  tyco: tyco_syntax Symtab.table,
haftmann@28054
   216
  const: const_syntax Symtab.table
haftmann@28054
   217
};
haftmann@27000
   218
haftmann@28054
   219
fun mk_name_syntax_table ((class, inst), (tyco, const)) =
haftmann@28054
   220
  NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const };
haftmann@28054
   221
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) =
haftmann@28054
   222
  mk_name_syntax_table (f ((class, inst), (tyco, const)));
haftmann@28054
   223
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 },
haftmann@28054
   224
    NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) =
haftmann@28054
   225
  mk_name_syntax_table (
haftmann@28054
   226
    (Symtab.join (K snd) (class1, class2),
haftmann@28054
   227
       Symtab.join (K snd) (inst1, inst2)),
haftmann@28054
   228
    (Symtab.join (K snd) (tyco1, tyco2),
haftmann@28054
   229
       Symtab.join (K snd) (const1, const2))
haftmann@28054
   230
  );
haftmann@27103
   231
haftmann@28054
   232
type serializer =
haftmann@28054
   233
  string option                         (*module name*)
haftmann@28054
   234
  -> Args.T list                        (*arguments*)
haftmann@28054
   235
  -> (string -> string)                 (*labelled_name*)
haftmann@28054
   236
  -> string list                        (*reserved symbols*)
haftmann@28054
   237
  -> (string * Pretty.T) list           (*includes*)
haftmann@28054
   238
  -> (string -> string option)          (*module aliasses*)
haftmann@28054
   239
  -> (string -> class_syntax option)
haftmann@28054
   240
  -> (string -> tyco_syntax option)
haftmann@28054
   241
  -> (string -> const_syntax option)
haftmann@28054
   242
  -> Code_Thingol.program
haftmann@28054
   243
  -> string list                        (*selected statements*)
haftmann@28054
   244
  -> serialization;
haftmann@27103
   245
haftmann@28064
   246
datatype serializer_entry = Serializer of serializer * literals
haftmann@28054
   247
  | Extends of string * (Code_Thingol.program -> Code_Thingol.program);
haftmann@27103
   248
haftmann@28054
   249
datatype target = Target of {
haftmann@28054
   250
  serial: serial,
haftmann@28054
   251
  serializer: serializer_entry,
haftmann@28054
   252
  reserved: string list,
haftmann@28054
   253
  includes: Pretty.T Symtab.table,
haftmann@28054
   254
  name_syntax_table: name_syntax_table,
haftmann@28054
   255
  module_alias: string Symtab.table
haftmann@28054
   256
};
haftmann@27103
   257
haftmann@28054
   258
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) =
haftmann@28054
   259
  Target { serial = serial, serializer = serializer, reserved = reserved, 
haftmann@28054
   260
    includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias };
haftmann@28054
   261
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) =
haftmann@28054
   262
  mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))));
haftmann@28054
   263
fun merge_target strict target (Target { serial = serial1, serializer = serializer,
haftmann@28054
   264
  reserved = reserved1, includes = includes1,
haftmann@28054
   265
  name_syntax_table = name_syntax_table1, module_alias = module_alias1 },
haftmann@28054
   266
    Target { serial = serial2, serializer = _,
haftmann@28054
   267
      reserved = reserved2, includes = includes2,
haftmann@28054
   268
      name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) =
haftmann@28054
   269
  if serial1 = serial2 orelse not strict then
haftmann@28054
   270
    mk_target ((serial1, serializer),
haftmann@28054
   271
      ((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)),
haftmann@28054
   272
        (merge_name_syntax_table (name_syntax_table1, name_syntax_table2),
haftmann@28054
   273
          Symtab.join (K snd) (module_alias1, module_alias2))
haftmann@28054
   274
    ))
haftmann@28054
   275
  else
haftmann@28054
   276
    error ("Incompatible serializers: " ^ quote target);
haftmann@27436
   277
haftmann@28054
   278
structure CodeTargetData = TheoryDataFun
haftmann@27436
   279
(
haftmann@28054
   280
  type T = target Symtab.table * string list;
haftmann@28054
   281
  val empty = (Symtab.empty, []);
haftmann@28054
   282
  val copy = I;
haftmann@28054
   283
  val extend = I;
haftmann@28054
   284
  fun merge _ ((target1, exc1) : T, (target2, exc2)) =
haftmann@28054
   285
    (Symtab.join (merge_target true) (target1, target2), Library.merge (op =) (exc1, exc2));
haftmann@27436
   286
);
haftmann@27436
   287
haftmann@28054
   288
fun the_serializer (Target { serializer, ... }) = serializer;
haftmann@28054
   289
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@28054
   290
fun the_includes (Target { includes, ... }) = includes;
haftmann@28054
   291
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
haftmann@28054
   292
fun the_module_alias (Target { module_alias , ... }) = module_alias;
haftmann@27436
   293
haftmann@28054
   294
val abort_allowed = snd o CodeTargetData.get;
haftmann@28054
   295
haftmann@28054
   296
fun assert_target thy target =
haftmann@28054
   297
  case Symtab.lookup (fst (CodeTargetData.get thy)) target
haftmann@28054
   298
   of SOME data => target
haftmann@28054
   299
    | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28054
   300
haftmann@28054
   301
fun put_target (target, seri) thy =
haftmann@27437
   302
  let
haftmann@28054
   303
    val defined_target = is_some o Symtab.lookup (fst (CodeTargetData.get thy));
haftmann@28054
   304
    val _ = case seri
haftmann@28054
   305
     of Extends (super, _) => if defined_target super then ()
haftmann@28054
   306
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   307
      | _ => ();
haftmann@28054
   308
    val _ = if defined_target target
haftmann@28054
   309
      then warning ("Overwriting existing target " ^ quote target)
haftmann@28054
   310
      else ();
haftmann@27103
   311
  in
haftmann@28054
   312
    thy
haftmann@28054
   313
    |> (CodeTargetData.map o apfst oo Symtab.map_default)
haftmann@28054
   314
          (target, mk_target ((serial (), seri), (([], Symtab.empty),
haftmann@28054
   315
            (mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
haftmann@28054
   316
              Symtab.empty))))
haftmann@28054
   317
          ((map_target o apfst o apsnd o K) seri)
haftmann@27103
   318
  end;
haftmann@27000
   319
haftmann@28054
   320
fun add_target (target, seri) = put_target (target, Serializer seri);
haftmann@28054
   321
fun extend_target (target, (super, modify)) =
haftmann@28054
   322
  put_target (target, Extends (super, modify));
haftmann@27000
   323
haftmann@28054
   324
fun map_target_data target f thy =
haftmann@27304
   325
  let
haftmann@28054
   326
    val _ = assert_target thy target;
haftmann@27000
   327
  in
haftmann@28054
   328
    thy
haftmann@28054
   329
    |> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f
haftmann@27000
   330
  end;
haftmann@27000
   331
haftmann@28054
   332
fun map_reserved target =
haftmann@28054
   333
  map_target_data target o apsnd o apfst o apfst;
haftmann@28054
   334
fun map_includes target =
haftmann@28054
   335
  map_target_data target o apsnd o apfst o apsnd;
haftmann@28054
   336
fun map_name_syntax target =
haftmann@28054
   337
  map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table;
haftmann@28054
   338
fun map_module_alias target =
haftmann@28054
   339
  map_target_data target o apsnd o apsnd o apsnd;
haftmann@27000
   340
haftmann@27000
   341
haftmann@28054
   342
(** serializer configuration **)
haftmann@28054
   343
haftmann@27000
   344
(* data access *)
haftmann@24219
   345
haftmann@24219
   346
local
haftmann@24219
   347
haftmann@24992
   348
fun cert_class thy class =
haftmann@24992
   349
  let
haftmann@24992
   350
    val _ = AxClass.get_info thy class;
haftmann@24992
   351
  in class end;
haftmann@24992
   352
haftmann@27103
   353
fun read_class thy = cert_class thy o Sign.intern_class thy;
haftmann@24992
   354
haftmann@24992
   355
fun cert_tyco thy tyco =
haftmann@24992
   356
  let
haftmann@24992
   357
    val _ = if Sign.declared_tyname thy tyco then ()
haftmann@24992
   358
      else error ("No such type constructor: " ^ quote tyco);
haftmann@24992
   359
  in tyco end;
haftmann@24992
   360
haftmann@27103
   361
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
haftmann@24219
   362
haftmann@24219
   363
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
haftmann@24219
   364
  let
haftmann@24841
   365
    val class = prep_class thy raw_class;
haftmann@28054
   366
    val class' = Code_Name.class thy class;
haftmann@24841
   367
    fun mk_classparam c = case AxClass.class_of_param thy c
haftmann@28054
   368
     of SOME class'' => if class = class'' then Code_Name.const thy c
haftmann@24219
   369
          else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c)
haftmann@24219
   370
      | NONE => error ("Not a class operation: " ^ quote c);
haftmann@24841
   371
    fun mk_syntax_params raw_params = AList.lookup (op =)
haftmann@24841
   372
      ((map o apfst) (mk_classparam o prep_const thy) raw_params);
haftmann@24219
   373
  in case raw_syn
haftmann@24841
   374
   of SOME (syntax, raw_params) =>
haftmann@24219
   375
      thy
haftmann@27024
   376
      |> (map_name_syntax target o apfst o apfst)
haftmann@24841
   377
           (Symtab.update (class', (syntax, mk_syntax_params raw_params)))
haftmann@24219
   378
    | NONE =>
haftmann@24219
   379
      thy
haftmann@27024
   380
      |> (map_name_syntax target o apfst o apfst)
haftmann@24841
   381
           (Symtab.delete_safe class')
haftmann@24219
   382
  end;
haftmann@24219
   383
haftmann@24219
   384
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy =
haftmann@24219
   385
  let
haftmann@28054
   386
    val inst = Code_Name.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco);
haftmann@24219
   387
  in if add_del then
haftmann@24219
   388
    thy
haftmann@27024
   389
    |> (map_name_syntax target o apfst o apsnd)
haftmann@24219
   390
        (Symtab.update (inst, ()))
haftmann@24219
   391
  else
haftmann@24219
   392
    thy
haftmann@27024
   393
    |> (map_name_syntax target o apfst o apsnd)
haftmann@24219
   394
        (Symtab.delete_safe inst)
haftmann@24219
   395
  end;
haftmann@24219
   396
haftmann@24219
   397
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy =
haftmann@24219
   398
  let
haftmann@24219
   399
    val tyco = prep_tyco thy raw_tyco;
haftmann@28054
   400
    val tyco' = if tyco = "fun" then "fun" else Code_Name.tyco thy tyco;
haftmann@24219
   401
    fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco
haftmann@24219
   402
      then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@24219
   403
      else syntax
haftmann@24219
   404
  in case raw_syn
haftmann@24219
   405
   of SOME syntax =>
haftmann@24219
   406
      thy
haftmann@27024
   407
      |> (map_name_syntax target o apsnd o apfst)
haftmann@24219
   408
           (Symtab.update (tyco', check_args syntax))
haftmann@24219
   409
   | NONE =>
haftmann@24219
   410
      thy
haftmann@27024
   411
      |> (map_name_syntax target o apsnd o apfst)
haftmann@24219
   412
           (Symtab.delete_safe tyco')
haftmann@24219
   413
  end;
haftmann@24219
   414
haftmann@24219
   415
fun gen_add_syntax_const prep_const target raw_c raw_syn thy =
haftmann@24219
   416
  let
haftmann@24219
   417
    val c = prep_const thy raw_c;
haftmann@28054
   418
    val c' = Code_Name.const thy c;
haftmann@28054
   419
    fun check_args (syntax as (n, _)) = if n > Code_Unit.no_args thy c
haftmann@24423
   420
      then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@24219
   421
      else syntax;
haftmann@24219
   422
  in case raw_syn
haftmann@24219
   423
   of SOME syntax =>
haftmann@24219
   424
      thy
haftmann@27024
   425
      |> (map_name_syntax target o apsnd o apsnd)
haftmann@24219
   426
           (Symtab.update (c', check_args syntax))
haftmann@24219
   427
   | NONE =>
haftmann@24219
   428
      thy
haftmann@27024
   429
      |> (map_name_syntax target o apsnd o apsnd)
haftmann@24219
   430
           (Symtab.delete_safe c')
haftmann@24219
   431
  end;
haftmann@24219
   432
haftmann@24219
   433
fun add_reserved target =
haftmann@24219
   434
  let
haftmann@24219
   435
    fun add sym syms = if member (op =) syms sym
haftmann@24219
   436
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@24219
   437
      else insert (op =) sym syms
haftmann@27103
   438
  in map_reserved target o add end;
haftmann@24992
   439
haftmann@24992
   440
fun add_include target =
haftmann@24992
   441
  let
haftmann@24992
   442
    fun add (name, SOME content) incls =
haftmann@24992
   443
          let
haftmann@24992
   444
            val _ = if Symtab.defined incls name
haftmann@24992
   445
              then warning ("Overwriting existing include " ^ name)
haftmann@24992
   446
              else ();
haftmann@24992
   447
          in Symtab.update (name, str content) incls end
haftmann@24992
   448
      | add (name, NONE) incls =
haftmann@24992
   449
          Symtab.delete name incls;
haftmann@27103
   450
  in map_includes target o add end;
haftmann@24219
   451
haftmann@27103
   452
fun add_module_alias target =
haftmann@28054
   453
  map_module_alias target o Symtab.update o apsnd Code_Name.check_modulename;
haftmann@24219
   454
haftmann@27103
   455
fun gen_allow_abort prep_cs raw_c thy =
haftmann@24841
   456
  let
haftmann@24841
   457
    val c = prep_cs thy raw_c;
haftmann@28054
   458
    val c' = Code_Name.const thy c;
haftmann@24841
   459
  in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end;
haftmann@24841
   460
haftmann@24219
   461
fun zip_list (x::xs) f g =
haftmann@24219
   462
  f
haftmann@24219
   463
  #-> (fn y =>
haftmann@24219
   464
    fold_map (fn x => g |-- f >> pair x) xs
haftmann@24219
   465
    #-> (fn xys => pair ((x, y) :: xys)));
haftmann@24219
   466
haftmann@27000
   467
haftmann@27103
   468
(* concrete syntax *)
haftmann@27000
   469
haftmann@24219
   470
structure P = OuterParse
haftmann@24219
   471
and K = OuterKeyword
haftmann@24219
   472
haftmann@24219
   473
fun parse_multi_syntax parse_thing parse_syntax =
haftmann@24219
   474
  P.and_list1 parse_thing
haftmann@24219
   475
  #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
haftmann@24219
   476
        (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
haftmann@24219
   477
haftmann@24219
   478
in
haftmann@24219
   479
haftmann@24219
   480
val parse_syntax = parse_syntax;
haftmann@24219
   481
haftmann@24219
   482
val add_syntax_class = gen_add_syntax_class cert_class (K I);
haftmann@24219
   483
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
haftmann@24219
   484
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
haftmann@24219
   485
val add_syntax_const = gen_add_syntax_const (K I);
haftmann@27103
   486
val allow_abort = gen_allow_abort (K I);
haftmann@28054
   487
val add_reserved = add_reserved;
haftmann@24219
   488
haftmann@28054
   489
val add_syntax_class_cmd = gen_add_syntax_class read_class Code_Unit.read_const;
haftmann@24219
   490
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
haftmann@24219
   491
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
haftmann@28054
   492
val add_syntax_const_cmd = gen_add_syntax_const Code_Unit.read_const;
haftmann@28054
   493
val allow_abort_cmd = gen_allow_abort Code_Unit.read_const;
haftmann@24219
   494
haftmann@28054
   495
fun add_syntax_tycoP target tyco = parse_syntax I
haftmann@28054
   496
  >> add_syntax_tyco_cmd target tyco;
haftmann@28054
   497
fun add_syntax_constP target c = parse_syntax fst
haftmann@28054
   498
  >> (add_syntax_const_cmd target c o Code_Printer.simple_const_syntax);
haftmann@24219
   499
haftmann@28064
   500
fun the_literals thy =
haftmann@28064
   501
  let
haftmann@28064
   502
    val (targets, _) = CodeTargetData.get thy;
haftmann@28064
   503
    fun literals target = case Symtab.lookup targets target
haftmann@28064
   504
     of SOME data => (case the_serializer data
haftmann@28064
   505
         of Serializer (_, literals) => literals
haftmann@28064
   506
          | Extends (super, _) => literals super)
haftmann@28064
   507
      | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28064
   508
  in literals end;
haftmann@28064
   509
haftmann@28064
   510
fun add_literal_list target nill cons thy =
haftmann@24219
   511
  let
haftmann@28054
   512
    val nil' = Code_Name.const thy nill;
haftmann@28054
   513
    val cons' = Code_Name.const thy cons;
haftmann@28064
   514
    val pr = pretty_list nil' cons' (the_literals thy target);
haftmann@24219
   515
  in
haftmann@24219
   516
    thy
haftmann@24423
   517
    |> add_syntax_const target cons (SOME pr)
haftmann@24219
   518
  end;
haftmann@24219
   519
haftmann@28064
   520
fun add_literal_list_string target nill cons charr nibbles thy =
haftmann@24219
   521
  let
haftmann@28054
   522
    val nil' = Code_Name.const thy nill;
haftmann@28054
   523
    val cons' = Code_Name.const thy cons;
haftmann@28054
   524
    val charr' = Code_Name.const thy charr;
haftmann@28054
   525
    val nibbles' = map (Code_Name.const thy) nibbles;
haftmann@28064
   526
    val pr = pretty_list_string nil' cons' charr' nibbles' (the_literals thy target);
haftmann@24219
   527
  in
haftmann@24219
   528
    thy
haftmann@24423
   529
    |> add_syntax_const target cons (SOME pr)
haftmann@24219
   530
  end;
haftmann@24219
   531
haftmann@28064
   532
fun add_literal_char target charr nibbles thy =
haftmann@24219
   533
  let
haftmann@28054
   534
    val charr' = Code_Name.const thy charr;
haftmann@28054
   535
    val nibbles' = map (Code_Name.const thy) nibbles;
haftmann@28064
   536
    val pr = pretty_char charr' nibbles' (the_literals thy target);
haftmann@24219
   537
  in
haftmann@24219
   538
    thy
haftmann@24423
   539
    |> add_syntax_const target charr (SOME pr)
haftmann@24219
   540
  end;
haftmann@24219
   541
haftmann@28064
   542
fun add_literal_numeral target unbounded negative number_of pls min bit0 bit1 thy =
haftmann@24219
   543
  let
haftmann@28054
   544
    val pls' = Code_Name.const thy pls;
haftmann@28054
   545
    val min' = Code_Name.const thy min;
haftmann@28054
   546
    val bit0' = Code_Name.const thy bit0;
haftmann@28054
   547
    val bit1' = Code_Name.const thy bit1;
haftmann@28064
   548
    val pr = pretty_numeral unbounded negative pls' min' bit0' bit1' (the_literals thy target);
haftmann@24219
   549
  in
haftmann@24219
   550
    thy
haftmann@24423
   551
    |> add_syntax_const target number_of (SOME pr)
haftmann@24219
   552
  end;
haftmann@24219
   553
haftmann@28064
   554
fun add_literal_message target charr nibbles nill cons str thy =
haftmann@24219
   555
  let
haftmann@28054
   556
    val charr' = Code_Name.const thy charr;
haftmann@28054
   557
    val nibbles' = map (Code_Name.const thy) nibbles;
haftmann@28054
   558
    val nil' = Code_Name.const thy nill;
haftmann@28054
   559
    val cons' = Code_Name.const thy cons;
haftmann@28064
   560
    val pr = pretty_message charr' nibbles' nil' cons' (the_literals thy target);
haftmann@24219
   561
  in
haftmann@24219
   562
    thy
haftmann@24423
   563
    |> add_syntax_const target str (SOME pr)
haftmann@24219
   564
  end;
haftmann@24219
   565
wenzelm@24867
   566
haftmann@28054
   567
(** serializer usage **)
haftmann@28054
   568
haftmann@28054
   569
(* montage *)
haftmann@28054
   570
haftmann@28054
   571
fun invoke_serializer thy modify abortable serializer reserved includes 
haftmann@28054
   572
    module_alias class inst tyco const module args program1 cs1 =
haftmann@28054
   573
  let
haftmann@28054
   574
    val program2 = modify program1;
haftmann@28054
   575
    val hidden = Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const;
haftmann@28054
   576
    val cs2 = subtract (op =) hidden cs1;
haftmann@28054
   577
    val program3 = Graph.subgraph (not o member (op =) hidden) program2;
haftmann@28054
   578
    val all_cs = Graph.all_succs program2 cs2;
haftmann@28054
   579
    val program4 = Graph.subgraph (member (op =) all_cs) program3;
haftmann@28054
   580
    val empty_funs = filter_out (member (op =) abortable)
haftmann@28054
   581
      (Code_Thingol.empty_funs program3);
haftmann@28054
   582
    val _ = if null empty_funs then () else error ("No defining equations for "
haftmann@28054
   583
      ^ commas (map (Code_Name.labelled_name thy) empty_funs));
haftmann@28054
   584
  in
haftmann@28054
   585
    serializer module args (Code_Name.labelled_name thy) reserved includes
haftmann@28054
   586
      (Symtab.lookup module_alias) (Symtab.lookup class)
haftmann@28054
   587
      (Symtab.lookup tyco) (Symtab.lookup const)
haftmann@28054
   588
      program4 cs2
haftmann@28054
   589
  end;
haftmann@28054
   590
haftmann@28054
   591
fun mount_serializer thy alt_serializer target =
haftmann@28054
   592
  let
haftmann@28054
   593
    val (targets, abortable) = CodeTargetData.get thy;
haftmann@28054
   594
    fun collapse_hierarchy target =
haftmann@28054
   595
      let
haftmann@28054
   596
        val data = case Symtab.lookup targets target
haftmann@28054
   597
         of SOME data => data
haftmann@28054
   598
          | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@28054
   599
      in case the_serializer data
haftmann@28054
   600
       of Serializer _ => (I, data)
haftmann@28054
   601
        | Extends (super, modify) => let
haftmann@28054
   602
            val (modify', data') = collapse_hierarchy super
haftmann@28054
   603
          in (modify' #> modify, merge_target false target (data', data)) end
haftmann@28054
   604
      end;
haftmann@28054
   605
    val (modify, data) = collapse_hierarchy target;
haftmann@28064
   606
    val (serializer, _) = the_default (case the_serializer data
haftmann@28054
   607
     of Serializer seri => seri) alt_serializer;
haftmann@28054
   608
    val reserved = the_reserved data;
haftmann@28054
   609
    val includes = Symtab.dest (the_includes data);
haftmann@28054
   610
    val module_alias = the_module_alias data;
haftmann@28054
   611
    val { class, inst, tyco, const } = the_name_syntax data;
haftmann@28054
   612
  in
haftmann@28054
   613
    invoke_serializer thy modify abortable serializer reserved
haftmann@28054
   614
      includes module_alias class inst tyco const
haftmann@28054
   615
  end;
haftmann@28054
   616
haftmann@28054
   617
fun serialize thy = mount_serializer thy NONE;
haftmann@28054
   618
haftmann@28054
   619
fun serialize_custom thy (target_name, seri) program cs =
haftmann@28054
   620
  mount_serializer thy (SOME seri) target_name NONE [] program cs (String [])
haftmann@28054
   621
  |> the;
haftmann@28054
   622
haftmann@28054
   623
fun parse_args f args =
haftmann@28054
   624
  case Scan.read OuterLex.stopper f args
haftmann@28054
   625
   of SOME x => x
haftmann@28054
   626
    | NONE => error "Bad serializer arguments";
haftmann@28054
   627
haftmann@28054
   628
haftmann@28054
   629
(* code presentation *)
haftmann@28054
   630
haftmann@28054
   631
fun code_of thy target module_name cs stmt_names =
haftmann@28054
   632
  let
haftmann@28054
   633
    val (cs', program) = Code_Thingol.consts_program thy cs;
haftmann@28054
   634
  in
haftmann@28054
   635
    string stmt_names (serialize thy target (SOME module_name) [] program cs')
haftmann@28054
   636
  end;
haftmann@28054
   637
haftmann@28054
   638
haftmann@28054
   639
(* code generation *)
haftmann@28054
   640
haftmann@28054
   641
fun read_const_exprs thy cs =
haftmann@28054
   642
  let
haftmann@28054
   643
    val (cs1, cs2) = Code_Name.read_const_exprs thy cs;
haftmann@28054
   644
    val (cs3, program) = Code_Thingol.consts_program thy cs2;
haftmann@28054
   645
    val cs4 = Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy);
haftmann@28054
   646
    val cs5 = map_filter
haftmann@28054
   647
      (fn (c, c') => if member (op =) cs4 c' then SOME c else NONE) (cs2 ~~ cs3);
haftmann@28054
   648
  in fold (insert (op =)) cs5 cs1 end;
haftmann@28054
   649
haftmann@28054
   650
fun cached_program thy = 
haftmann@28054
   651
  let
haftmann@28054
   652
    val program = Code_Thingol.cached_program thy;
haftmann@28054
   653
  in (Code_Thingol.transitivly_non_empty_funs program (abort_allowed thy), program) end
haftmann@28054
   654
haftmann@28054
   655
fun export_code thy cs seris =
haftmann@28054
   656
  let
haftmann@28054
   657
    val (cs', program) = if null cs then cached_program thy
haftmann@28054
   658
      else Code_Thingol.consts_program thy cs;
haftmann@28054
   659
    fun mk_seri_dest dest = case dest
haftmann@28054
   660
     of NONE => compile
haftmann@28054
   661
      | SOME "-" => export
haftmann@28054
   662
      | SOME f => file (Path.explode f)
haftmann@28054
   663
    val _ = map (fn (((target, module), dest), args) =>
haftmann@28054
   664
      (mk_seri_dest dest (serialize thy target module args program cs'))) seris;
haftmann@28054
   665
  in () end;
haftmann@28054
   666
haftmann@28054
   667
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
haftmann@28054
   668
haftmann@27000
   669
haftmann@27304
   670
(** Isar setup **)
haftmann@27103
   671
haftmann@27103
   672
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
haftmann@27103
   673
haftmann@27103
   674
fun code_exprP cmd =
wenzelm@27757
   675
  (Scan.repeat P.term_group
haftmann@27103
   676
  -- Scan.repeat (P.$$$ inK |-- P.name
haftmann@27103
   677
     -- Scan.option (P.$$$ module_nameK |-- P.name)
haftmann@27103
   678
     -- Scan.option (P.$$$ fileK |-- P.name)
wenzelm@27809
   679
     -- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") []
haftmann@27103
   680
  ) >> (fn (raw_cs, seris) => cmd raw_cs seris));
haftmann@27103
   681
haftmann@28054
   682
val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK];
wenzelm@24867
   683
wenzelm@24867
   684
val _ =
haftmann@24992
   685
  OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl (
haftmann@24219
   686
    parse_multi_syntax P.xname
haftmann@24219
   687
      (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
wenzelm@27757
   688
        (P.term_group --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) []))
haftmann@24219
   689
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   690
          fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
haftmann@24219
   691
  );
haftmann@24219
   692
wenzelm@24867
   693
val _ =
haftmann@24992
   694
  OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl (
haftmann@24219
   695
    parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
haftmann@24219
   696
      ((P.minus >> K true) || Scan.succeed false)
haftmann@24219
   697
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   698
          fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
haftmann@24219
   699
  );
haftmann@24219
   700
wenzelm@24867
   701
val _ =
haftmann@24992
   702
  OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl (
haftmann@24219
   703
    parse_multi_syntax P.xname (parse_syntax I)
haftmann@24219
   704
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@24219
   705
          fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
haftmann@24219
   706
  );
haftmann@24219
   707
wenzelm@24867
   708
val _ =
haftmann@24992
   709
  OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl (
wenzelm@27757
   710
    parse_multi_syntax P.term_group (parse_syntax fst)
haftmann@24219
   711
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
haftmann@28054
   712
      fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const
haftmann@28054
   713
        (Code_Printer.simple_const_syntax syn)) syns)
haftmann@24219
   714
  );
haftmann@24219
   715
wenzelm@24867
   716
val _ =
haftmann@24992
   717
  OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl (
haftmann@24219
   718
    P.name -- Scan.repeat1 P.name
haftmann@24219
   719
    >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
haftmann@24841
   720
  );
haftmann@24219
   721
wenzelm@24867
   722
val _ =
haftmann@24992
   723
  OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl (
haftmann@24992
   724
    P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s))
haftmann@24992
   725
    >> (fn ((target, name), content) => (Toplevel.theory o add_include target)
haftmann@24992
   726
      (name, content))
haftmann@24992
   727
  );
haftmann@24992
   728
haftmann@24992
   729
val _ =
haftmann@24992
   730
  OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl (
haftmann@24219
   731
    P.name -- Scan.repeat1 (P.name -- P.name)
haftmann@27103
   732
    >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames)
haftmann@24841
   733
  );
haftmann@24219
   734
wenzelm@24867
   735
val _ =
haftmann@27103
   736
  OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl (
wenzelm@27757
   737
    Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd)
haftmann@24841
   738
  );
haftmann@24219
   739
haftmann@27103
   740
val _ =
haftmann@27103
   741
  OuterSyntax.command "export_code" "generate executable code for constants"
haftmann@27103
   742
    K.diag (P.!!! (code_exprP export_code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
haftmann@27103
   743
haftmann@27103
   744
fun shell_command thyname cmd = Toplevel.program (fn _ =>
wenzelm@27845
   745
  (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP export_code_cmd))
wenzelm@27845
   746
    ((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd)
haftmann@27103
   747
   of SOME f => (writeln "Now generating code..."; f (theory thyname))
haftmann@27103
   748
    | NONE => error ("Bad directive " ^ quote cmd)))
haftmann@27103
   749
  handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
haftmann@27103
   750
haftmann@24219
   751
end; (*local*)
haftmann@24219
   752
haftmann@24219
   753
end; (*struct*)