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