src/Pure/Tools/codegen_serializer.ML
changeset 20600 6d75e02ed285
parent 20466 7c20ddbd911b
child 20699 0cc77abb185a
equal deleted inserted replaced
20599:65bd267ae23f 20600:6d75e02ed285
    33   val mk_flat_ml_resolver: string list -> string -> string;
    33   val mk_flat_ml_resolver: string list -> string -> string;
    34   val eval_term: string -> string -> string list list
    34   val eval_term: string -> string -> string list list
    35     -> (string -> CodegenThingol.itype pretty_syntax option)
    35     -> (string -> CodegenThingol.itype pretty_syntax option)
    36           * (string -> CodegenThingol.iterm pretty_syntax option)
    36           * (string -> CodegenThingol.iterm pretty_syntax option)
    37     -> string list
    37     -> string list
    38     -> (string * 'a ref) * CodegenThingol.iterm -> CodegenThingol.module
    38     -> (string * 'a option ref) * CodegenThingol.iterm -> CodegenThingol.module
    39     -> 'a;
    39     -> 'a;
    40   val eval_verbose: bool ref;
    40   val eval_verbose: bool ref;
    41   val ml_fun_datatype: string
    41   val ml_fun_datatype: string
    42     -> ((string -> CodegenThingol.itype pretty_syntax option)
    42     -> ((string -> CodegenThingol.itype pretty_syntax option)
    43         * (string -> CodegenThingol.iterm pretty_syntax option))
    43         * (string -> CodegenThingol.iterm pretty_syntax option))
   150       |-> (fn x => pair (Quote x));
   150       |-> (fn x => pair (Quote x));
   151     val sym_any = Scan.lift (Scan.one Symbol.not_eof);
   151     val sym_any = Scan.lift (Scan.one Symbol.not_eof);
   152     val parse = Scan.repeat (
   152     val parse = Scan.repeat (
   153          (sym "_" -- sym "_" >> K (Arg NOBR))
   153          (sym "_" -- sym "_" >> K (Arg NOBR))
   154       || (sym "_" >> K (Arg BR))
   154       || (sym "_" >> K (Arg BR))
   155       || (sym "?" >> K Ignore)
       
   156       || (sym "/" |-- Scan.repeat (sym " ") >> (Pretty o Pretty.brk o length))
   155       || (sym "/" |-- Scan.repeat (sym " ") >> (Pretty o Pretty.brk o length))
   157       || Scan.depend (fn ctxt => $$ "{" |-- $$ "*" |-- Scan.repeat1
   156       || Scan.depend (fn ctxt => $$ "{" |-- $$ "*" |-- Scan.repeat1
   158            (   $$ "'" |-- Scan.one Symbol.not_eof
   157            (   $$ "'" |-- Scan.one Symbol.not_eof
   159             || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --|
   158             || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --|
   160          $$ "*" --| $$ "}" >> (implode #> lift_reader ctxt #> swap))
   159          $$ "*" --| $$ "}" >> (implode #> lift_reader ctxt #> swap))
   189         || pair (parse_nonatomic, BR)
   188         || pair (parse_nonatomic, BR)
   190       ) -- OuterParse.string >> (fn ((p, fxy), s) => (p s, fxy));
   189       ) -- OuterParse.string >> (fn ((p, fxy), s) => (p s, fxy));
   191     fun mk fixity mfx ctxt =
   190     fun mk fixity mfx ctxt =
   192       let
   191       let
   193         val i = (length o List.filter is_arg) mfx;
   192         val i = (length o List.filter is_arg) mfx;
   194         val _ = if i > num_args ctxt then error "Too many arguments in codegen syntax" else ();
   193         val _ = if i > num_args ctxt then error "Too many arguments in code syntax" else ();
   195       in (((i, i), fillin_mixfix fixity mfx), ctxt) end;
   194       in (((i, i), fillin_mixfix fixity mfx), ctxt) end;
   196   in
   195   in
   197     parse
   196     parse
   198     #-> (fn (mfx_reader, fixity) =>
   197     #-> (fn (mfx_reader, fixity) =>
   199       pair (mfx_reader #-> (fn mfx => mk fixity mfx))
   198       pair (mfx_reader #-> (fn mfx => mk fixity mfx))
   435               )
   434               )
   436           | from_inst fxy (Context (classes, (v, ~1))) =
   435           | from_inst fxy (Context (classes, (v, ~1))) =
   437               from_lookup BR classes
   436               from_lookup BR classes
   438                 ((str o ml_from_dictvar) v)
   437                 ((str o ml_from_dictvar) v)
   439           | from_inst fxy (Context (classes, (v, i))) =
   438           | from_inst fxy (Context (classes, (v, i))) =
   440               from_lookup BR (string_of_int (i+1) :: classes)
   439               from_lookup BR (classes @ [string_of_int (i+1)])
   441                 ((str o ml_from_dictvar) v)
   440                 ((str o ml_from_dictvar) v)
   442       in case lss
   441       in case lss
   443        of [] => str "()"
   442        of [] => str "()"
   444         | [ls] => from_inst fxy ls
   443         | [ls] => from_inst fxy ls
   445         | lss => (Pretty.list "(" ")" o map (from_inst NOBR)) lss
   444         | lss => (Pretty.list "(" ")" o map (from_inst NOBR)) lss
   550       if is_cons f andalso length es > 1 then
   549       if is_cons f andalso length es > 1 then
   551         [(str o resolv) f, Pretty.enum "," "(" ")" (map (ml_from_expr BR) es)]
   550         [(str o resolv) f, Pretty.enum "," "(" ")" (map (ml_from_expr BR) es)]
   552       else
   551       else
   553         (str o resolv) f :: map (ml_from_expr BR) es
   552         (str o resolv) f :: map (ml_from_expr BR) es
   554     and ml_from_app fxy (app_expr as ((c, (lss, ty)), es)) =
   553     and ml_from_app fxy (app_expr as ((c, (lss, ty)), es)) =
   555       case (map (ml_from_insts BR) o filter_out null) lss
   554       case if is_cons c then [] else (map (ml_from_insts BR) o filter_out null) lss
   556        of [] =>
   555        of [] =>
   557             from_app ml_mk_app ml_from_expr const_syntax fxy app_expr
   556             from_app ml_mk_app ml_from_expr const_syntax fxy app_expr
   558         | lss =>
   557         | lss =>
   559             if (is_none o const_syntax) c then
   558             if (is_none o const_syntax) c then
   560               brackify fxy (
   559               brackify fxy (
   641           | mk_cons (co, tys) =
   640           | mk_cons (co, tys) =
   642               Pretty.block [
   641               Pretty.block [
   643                 str (resolv co),
   642                 str (resolv co),
   644                 str " of",
   643                 str " of",
   645                 Pretty.brk 1,
   644                 Pretty.brk 1,
   646                 Pretty.enum " *" "" "" (map (ml_from_type NOBR) tys)
   645                 Pretty.enum " *" "" "" (map (ml_from_type (INFX (2, L))) tys)
   647               ]
   646               ]
   648         fun mk_datatype definer (t, (vs, cs)) =
   647         fun mk_datatype definer (t, (vs, cs)) =
   649           (Pretty.block o Pretty.breaks) (
   648           (Pretty.block o Pretty.breaks) (
   650             str definer
   649             str definer
   651             :: ml_from_tycoexpr NOBR (t, map (ITyVar o fst) vs)
   650             :: ml_from_tycoexpr NOBR (t, map (ITyVar o fst) vs)
   836     val serializer = ml_serializer struct_name "ml" nsp_dtcon nspgrp
   835     val serializer = ml_serializer struct_name "ml" nsp_dtcon nspgrp
   837       (fn "" => (fn p => (use_text Context.ml_output (!eval_verbose) (output p); NONE))
   836       (fn "" => (fn p => (use_text Context.ml_output (!eval_verbose) (output p); NONE))
   838         | _ => SOME) (K NONE, syntax_tyco, syntax_const) (hidden, SOME [NameSpace.pack [nsp_eval, val_name]]);
   837         | _ => SOME) (K NONE, syntax_tyco, syntax_const) (hidden, SOME [NameSpace.pack [nsp_eval, val_name]]);
   839     val _ = serializer modl';
   838     val _ = serializer modl';
   840     val val_name_struct = NameSpace.append struct_name val_name;
   839     val val_name_struct = NameSpace.append struct_name val_name;
   841     val _ = use_text Context.ml_output (!eval_verbose) ("val _ = (" ^ ref_name ^ " := " ^ val_name_struct ^ ")");
   840     val _ = reff := NONE;
   842     val value = ! reff;
   841     val _ = use_text Context.ml_output (!eval_verbose) ("val _ = (" ^ ref_name ^ " := SOME (" ^ val_name_struct ^ "))");
   843   in value end;
   842   in case !reff
       
   843    of NONE => error ("Could not retrieve value of ML reference " ^ quote ref_name
       
   844         ^ " (reference probably has been shadowed)")
       
   845     | SOME value => value
       
   846   end;
   844 
   847 
   845 fun mk_flat_ml_resolver names =
   848 fun mk_flat_ml_resolver names =
   846   let
   849   let
   847     val mangler =
   850     val mangler =
   848       NameMangler.empty
   851       NameMangler.empty
   934               str "->",
   937               str "->",
   935               hs_from_expr NOBR e
   938               hs_from_expr NOBR e
   936             ])
   939             ])
   937           end
   940           end
   938       | hs_from_expr fxy (INum (n, _)) =
   941       | hs_from_expr fxy (INum (n, _)) =
   939           (str o IntInf.toString) n
   942           if n > 0 then
       
   943             (str o IntInf.toString) n
       
   944           else
       
   945             brackify BR [(str o Library.prefix "-" o IntInf.toString o IntInf.~) n]
   940       | hs_from_expr fxy (IChar (c, _)) =
   946       | hs_from_expr fxy (IChar (c, _)) =
   941           (str o enclose "'" "'")
   947           (str o enclose "'" "'")
   942             (let val i = (Char.ord o the o Char.fromString) c
   948             (let val i = (Char.ord o the o Char.fromString) c
   943               in if i < 32
   949               in if i < 32
   944                 then Library.prefix "\\" (string_of_int i)
   950                 then Library.prefix "\\" (string_of_int i)