src/Tools/Code/code_target.ML
changeset 38910 6af1d8673cbf
parent 38909 919c924067f3
child 38912 c79c1e4e1111
equal deleted inserted replaced
38909:919c924067f3 38910:6af1d8673cbf
     1 (*  Title:      Tools/Code/code_target.ML
     1 (*  Title:      Tools/Code/code_target.ML
     2     Author:     Florian Haftmann, TU Muenchen
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     3 
     4 Generic infrastructure for serializers from intermediate language ("Thin-gol"
     4 Generic infrastructure for target language data.
     5 to target languages.
       
     6 *)
     5 *)
     7 
     6 
     8 signature CODE_TARGET =
     7 signature CODE_TARGET =
     9 sig
     8 sig
    10   val cert_tyco: theory -> string -> string
     9   val cert_tyco: theory -> string -> string
    22 
    21 
    23   type destination
    22   type destination
    24   type serialization
    23   type serialization
    25   val parse_args: 'a parser -> Token.T list -> 'a
    24   val parse_args: 'a parser -> Token.T list -> 'a
    26   val stmt_names_of_destination: destination -> string list
    25   val stmt_names_of_destination: destination -> string list
    27   val mk_serialization: string -> (Path.T option -> 'a -> unit)
    26   val mk_serialization: (int -> Path.T option -> 'a -> unit)
    28     -> ('a -> string * string option list)
    27     -> (int -> 'a -> string * string option list)
    29     -> 'a -> serialization
    28     -> 'a -> int -> serialization
    30   val serialize: theory -> string -> int option -> string option -> Token.T list
    29   val serialize: theory -> string -> int option -> string option -> Token.T list
    31     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> serialization
    30     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> serialization
    32   val serialize_custom: theory -> string * serializer -> string option
    31   val serialize_custom: theory -> string * serializer -> string option
    33     -> Code_Thingol.naming -> Code_Thingol.program -> string list
    32     -> Code_Thingol.naming -> Code_Thingol.program -> string list
    34     -> string * string option list
    33     -> string * string option list
    62 type const_syntax = Code_Printer.const_syntax;
    61 type const_syntax = Code_Printer.const_syntax;
    63 
    62 
    64 
    63 
    65 (** basics **)
    64 (** basics **)
    66 
    65 
    67 datatype destination = Export | File of Path.T | String of string list;
    66 datatype destination = File of Path.T option | String of string list;
    68 type serialization = destination -> (string * string option list) option;
    67 type serialization = destination -> (string * string option list) option;
    69 
    68 
    70 fun export f = (f Export; ());
    69 fun export f = (f (File NONE); ());
    71 fun file p f = (f (File p); ());
    70 fun file p f = (f (File (SOME p)); ());
    72 fun string stmts f = fst (the (f (String stmts)));
    71 fun string stmts f = fst (the (f (String stmts)));
    73 
    72 
    74 fun stmt_names_of_destination (String stmts) = stmts
    73 fun stmt_names_of_destination (String stmts) = stmts
    75   | stmt_names_of_destination _ = [];
    74   | stmt_names_of_destination _ = [];
    76 
    75 
    77 fun mk_serialization target output _ code Export = (output NONE code ; NONE)
    76 fun mk_serialization output _ code width (File p) = (output width p code; NONE)
    78   | mk_serialization target output _ code (File file) = (output (SOME file) code; NONE)
    77   | mk_serialization _ string code width (String _) = SOME (string width code);
    79   | mk_serialization target _ string code (String _) = SOME (string code);
       
    80 
    78 
    81 
    79 
    82 (** theory data **)
    80 (** theory data **)
    83 
    81 
    84 datatype name_syntax_table = NameSyntaxTable of {
    82 datatype name_syntax_table = NameSyntaxTable of {
   113   -> (string -> Code_Printer.tyco_syntax option)
   111   -> (string -> Code_Printer.tyco_syntax option)
   114   -> (string -> Code_Printer.activated_const_syntax option)
   112   -> (string -> Code_Printer.activated_const_syntax option)
   115   -> ((Pretty.T -> string) * (Pretty.T -> unit))
   113   -> ((Pretty.T -> string) * (Pretty.T -> unit))
   116   -> Code_Thingol.program
   114   -> Code_Thingol.program
   117   -> (string list * string list)        (*selected statements*)
   115   -> (string list * string list)        (*selected statements*)
       
   116   -> int
   118   -> serialization;
   117   -> serialization;
   119 
   118 
   120 datatype description = Fundamental of { serializer: serializer,
   119 datatype description = Fundamental of { serializer: serializer,
   121       literals: Code_Printer.literals,
   120       literals: literals,
   122       check: { env_var: string, make_destination: Path.T -> Path.T,
   121       check: { env_var: string, make_destination: Path.T -> Path.T,
   123         make_command: string -> string -> string } }
   122         make_command: string -> string -> string } }
   124   | Extension of string *
   123   | Extension of string *
   125       (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
   124       (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
   126 
   125 
   257             in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
   256             in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
   258         | NONE => (NONE, (tab, naming))) (Symtab.keys src_tab)
   257         | NONE => (NONE, (tab, naming))) (Symtab.keys src_tab)
   259   |>> map_filter I;
   258   |>> map_filter I;
   260 
   259 
   261 fun invoke_serializer thy abortable serializer literals reserved abs_includes 
   260 fun invoke_serializer thy abortable serializer literals reserved abs_includes 
   262     module_alias class instance tyco const module_name width args naming program2 (names1, presentation_names) =
   261     module_alias class instance tyco const module_name args naming program2 (names1, presentation_names) width =
   263   let
   262   let
   264     val (names_class, class') =
   263     val (names_class, class') =
   265       activate_syntax (Code_Thingol.lookup_class naming) class;
   264       activate_syntax (Code_Thingol.lookup_class naming) class;
   266     val names_inst = map_filter (Code_Thingol.lookup_instance naming)
   265     val names_inst = map_filter (Code_Thingol.lookup_instance naming)
   267       (Symreltab.keys instance);
   266       (Symreltab.keys instance);
   282   in
   281   in
   283     serializer module_name args (Code_Thingol.labelled_name thy program2) reserved includes
   282     serializer module_name args (Code_Thingol.labelled_name thy program2) reserved includes
   284       (if is_some module_name then K module_name else Symtab.lookup module_alias)
   283       (if is_some module_name then K module_name else Symtab.lookup module_alias)
   285       (Symtab.lookup class') (Symtab.lookup tyco') (Symtab.lookup const')
   284       (Symtab.lookup class') (Symtab.lookup tyco') (Symtab.lookup const')
   286       (Code_Printer.string_of_pretty width, Code_Printer.writeln_pretty width)
   285       (Code_Printer.string_of_pretty width, Code_Printer.writeln_pretty width)
   287       program4 (names1, presentation_names)
   286       program4 (names1, presentation_names) width
   288   end;
   287   end;
   289 
   288 
   290 fun mount_serializer thy alt_serializer target some_width raw_module_name args naming program names destination =
   289 fun mount_serializer thy alt_serializer target some_width raw_module_name args naming program names destination =
   291   let
   290   let
   292     val ((targets, abortable), default_width) = Targets.get thy;
   291     val ((targets, abortable), default_width) = Targets.get thy;
   320     val { class, instance, tyco, const } = the_name_syntax data;
   319     val { class, instance, tyco, const } = the_name_syntax data;
   321     val literals = the_literals thy target;
   320     val literals = the_literals thy target;
   322     val width = the_default default_width some_width;
   321     val width = the_default default_width some_width;
   323   in
   322   in
   324     invoke_serializer thy abortable serializer literals reserved
   323     invoke_serializer thy abortable serializer literals reserved
   325       includes module_alias class instance tyco const module_name width args
   324       includes module_alias class instance tyco const module_name args
   326         naming (modify program) (names, presentation_names) destination
   325         naming (modify program) (names, presentation_names) width destination
   327   end;
   326   end;
   328 
   327 
   329 in
   328 in
   330 
   329 
   331 fun serialize thy = mount_serializer thy NONE;
   330 fun serialize thy = mount_serializer thy NONE;