src/Tools/Code/code_target.ML
changeset 37748 0af0d45257be
parent 37745 6315b6426200
child 37814 120c6e2d7474
equal deleted inserted replaced
37747:3a699743bcba 37748:0af0d45257be
    19 
    19 
    20   type destination
    20   type destination
    21   type serialization
    21   type serialization
    22   val parse_args: 'a parser -> Token.T list -> 'a
    22   val parse_args: 'a parser -> Token.T list -> 'a
    23   val stmt_names_of_destination: destination -> string list
    23   val stmt_names_of_destination: destination -> string list
    24   val mk_serialization: string -> ('a -> unit) option
    24   val mk_serialization: string -> (Path.T option -> 'a -> unit)
    25     -> (Path.T option -> 'a -> unit)
       
    26     -> ('a -> string * string option list)
    25     -> ('a -> string * string option list)
    27     -> 'a -> serialization
    26     -> 'a -> serialization
    28   val serialize: theory -> string -> int option -> string option -> Token.T list
    27   val serialize: theory -> string -> int option -> string option -> Token.T list
    29     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> serialization
    28     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> serialization
    30   val serialize_custom: theory -> string * (serializer * literals)
    29   val serialize_custom: theory -> string * (serializer * literals)
    31     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> string * string option list
    30     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> string * string option list
    32   val the_literals: theory -> string -> literals
    31   val the_literals: theory -> string -> literals
    33   val compile: serialization -> unit
       
    34   val export: serialization -> unit
    32   val export: serialization -> unit
    35   val file: Path.T -> serialization -> unit
    33   val file: Path.T -> serialization -> unit
    36   val string: string list -> serialization -> string
    34   val string: string list -> serialization -> string
    37   val code_of: theory -> string -> int option -> string
    35   val code_of: theory -> string -> int option -> string
    38     -> string list -> (Code_Thingol.naming -> string list) -> string
    36     -> string list -> (Code_Thingol.naming -> string list) -> string
    62 type proto_const_syntax = Code_Printer.proto_const_syntax;
    60 type proto_const_syntax = Code_Printer.proto_const_syntax;
    63 
    61 
    64 
    62 
    65 (** basics **)
    63 (** basics **)
    66 
    64 
    67 datatype destination = Compile | Export | File of Path.T | String of string list;
    65 datatype destination = Export | File of Path.T | String of string list;
    68 type serialization = destination -> (string * string option list) option;
    66 type serialization = destination -> (string * string option list) option;
    69 
    67 
    70 fun compile f = (f Compile; ());
       
    71 fun export f = (f Export; ());
    68 fun export f = (f Export; ());
    72 fun file p f = (f (File p); ());
    69 fun file p f = (f (File p); ());
    73 fun string stmts f = fst (the (f (String stmts)));
    70 fun string stmts f = fst (the (f (String stmts)));
    74 
    71 
    75 fun stmt_names_of_destination (String stmts) = stmts
    72 fun stmt_names_of_destination (String stmts) = stmts
    76   | stmt_names_of_destination _ = [];
    73   | stmt_names_of_destination _ = [];
    77 
    74 
    78 fun mk_serialization target (SOME comp) _ _ code Compile = (comp code; NONE)
    75 fun mk_serialization target output _ code Export = (output NONE code ; NONE)
    79   | mk_serialization target NONE _ _ _ Compile = error (target ^ ": no internal compilation")
    76   | mk_serialization target output _ code (File file) = (output (SOME file) code; NONE)
    80   | mk_serialization target _ output _ code Export = (output NONE code ; NONE)
    77   | mk_serialization target _ string code (String _) = SOME (string code);
    81   | mk_serialization target _ output _ code (File file) = (output (SOME file) code; NONE)
       
    82   | mk_serialization target _ _ string code (String _) = SOME (string code);
       
    83 
    78 
    84 
    79 
    85 (** theory data **)
    80 (** theory data **)
    86 
    81 
    87 datatype name_syntax_table = NameSyntaxTable of {
    82 datatype name_syntax_table = NameSyntaxTable of {
   380 
   375 
   381 fun export_code thy cs seris =
   376 fun export_code thy cs seris =
   382   let
   377   let
   383     val (cs', (naming, program)) = Code_Thingol.consts_program thy false cs;
   378     val (cs', (naming, program)) = Code_Thingol.consts_program thy false cs;
   384     fun mk_seri_dest dest = case dest
   379     fun mk_seri_dest dest = case dest
   385      of NONE => compile
   380      of "-" => export
   386       | SOME "-" => export
   381       | f => file (Path.explode f)
   387       | SOME f => file (Path.explode f)
       
   388     val _ = map (fn (((target, module), dest), args) =>
   382     val _ = map (fn (((target, module), dest), args) =>
   389       (mk_seri_dest dest (serialize thy target NONE module args naming program cs'))) seris;
   383       (mk_seri_dest dest (serialize thy target NONE module args naming program cs'))) seris;
   390   in () end;
   384   in () end;
   391 
   385 
   392 fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
   386 fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
   528 
   522 
   529 val code_exprP =
   523 val code_exprP =
   530   (Scan.repeat1 Parse.term_group
   524   (Scan.repeat1 Parse.term_group
   531   -- Scan.repeat (Parse.$$$ inK |-- Parse.name
   525   -- Scan.repeat (Parse.$$$ inK |-- Parse.name
   532      -- Scan.option (Parse.$$$ module_nameK |-- Parse.name)
   526      -- Scan.option (Parse.$$$ module_nameK |-- Parse.name)
   533      -- Scan.option (Parse.$$$ fileK |-- Parse.name)
   527      --| Parse.$$$ fileK -- Parse.name
   534      -- Scan.optional (Parse.$$$ "(" |-- Args.parse --| Parse.$$$ ")") []
   528      -- Scan.optional (Parse.$$$ "(" |-- Args.parse --| Parse.$$$ ")") []
   535   ) >> (fn (raw_cs, seris) => export_code_cmd raw_cs seris));
   529   ) >> (fn (raw_cs, seris) => export_code_cmd raw_cs seris));
   536 
   530 
   537 val _ = List.app Keyword.keyword [inK, module_nameK, fileK];
   531 val _ = List.app Keyword.keyword [inK, module_nameK, fileK];
   538 
   532