src/Pure/Tools/codegen_serializer.ML
changeset 22197 461130ccfef4
parent 22186 5203eb387a0c
child 22248 74ea64617c89
equal deleted inserted replaced
22196:680b04dbd51c 22197:461130ccfef4
   146   end;
   146   end;
   147 
   147 
   148 fun parse_args f args =
   148 fun parse_args f args =
   149   case Scan.read Args.stopper f args
   149   case Scan.read Args.stopper f args
   150    of SOME x => x
   150    of SOME x => x
   151     | NONE => error "bad serializer arguments";
   151     | NONE => error "Bad serializer arguments";
   152 
   152 
   153 
   153 
   154 (* generic serializer combinators *)
   154 (* generic serializer combinators *)
   155 
   155 
   156 fun gen_pr_app pr_app' pr_term const_syntax vars fxy (app as ((c, (_, ty)), ts)) =
   156 fun gen_pr_app pr_app' pr_term const_syntax vars fxy (app as ((c, (_, ty)), ts)) =
   247 
   247 
   248 val pretty_imperative_monad_bind =
   248 val pretty_imperative_monad_bind =
   249   let
   249   let
   250     fun pretty (pr : CodegenNames.var_ctxt -> fixity -> iterm -> Pretty.T) vars fxy [t1, (v, ty) `|-> t2] =
   250     fun pretty (pr : CodegenNames.var_ctxt -> fixity -> iterm -> Pretty.T) vars fxy [t1, (v, ty) `|-> t2] =
   251           pr vars fxy (ICase ((t1, ty), ([(IVar v, t2)])))
   251           pr vars fxy (ICase ((t1, ty), ([(IVar v, t2)])))
   252       | pretty _ _ _ _ = error "bad arguments for imperative monad bind";
   252       | pretty _ _ _ _ = error "Bad arguments for imperative monad bind";
   253   in (2, pretty) end;
   253   in (2, pretty) end;
   254 
   254 
   255 
   255 
   256 
   256 
   257 (** name auxiliary **)
   257 (** name auxiliary **)
  1005         val defname' =
  1005         val defname' =
  1006           nodes
  1006           nodes
  1007           |> fold (fn m => fn g => case Graph.get_node g m
  1007           |> fold (fn m => fn g => case Graph.get_node g m
  1008               of Module (_, (_, g)) => g) modl'
  1008               of Module (_, (_, g)) => g) modl'
  1009           |> (fn g => case Graph.get_node g name of Def (defname, _) => defname);
  1009           |> (fn g => case Graph.get_node g name of Def (defname, _) => defname);
  1010       in NameSpace.implode (remainder @ [defname']) end handle Graph.UNDEF _ =>
  1010       in
  1011         "(raise Fail \"undefined name " ^ name ^ "\")";
  1011         NameSpace.implode (remainder @ [defname'])
       
  1012       end handle Graph.UNDEF _ =>
       
  1013         error "Unknown name: " ^ quote name;
  1012     fun the_prolog modlname = case module_prolog modlname
  1014     fun the_prolog modlname = case module_prolog modlname
  1013      of NONE => []
  1015      of NONE => []
  1014       | SOME p => [p, str ""];
  1016       | SOME p => [p, str ""];
  1015     fun pr_node prefix (Def (_, NONE)) =
  1017     fun pr_node prefix (Def (_, NONE)) =
  1016           NONE
  1018           NONE
  1026 
  1028 
  1027 val isar_seri_sml =
  1029 val isar_seri_sml =
  1028   let
  1030   let
  1029     fun output_file file = File.write (Path.explode file) o code_output;
  1031     fun output_file file = File.write (Path.explode file) o code_output;
  1030     val output_diag = writeln o code_output;
  1032     val output_diag = writeln o code_output;
  1031     val output_internal = use_text "" Output.ml_output false o code_output;
  1033     val output_internal = use_text "generated code" Output.ml_output false o code_output;
  1032   in
  1034   in
  1033     parse_args ((Args.$$$ "-" >> K output_diag
  1035     parse_args ((Args.$$$ "-" >> K output_diag
  1034       || Args.$$$ "#" >> K output_internal
  1036       || Args.$$$ "#" >> K output_internal
  1035       || Args.name >> output_file)
  1037       || Args.name >> output_file)
  1036     >> (fn output => seri_ml pr_sml pr_sml_modl output))
  1038     >> (fn output => seri_ml pr_sml pr_sml_modl output))
  1594     val { class, inst, tyco, const } = the_syntax_expr data;
  1596     val { class, inst, tyco, const } = the_syntax_expr data;
  1595     fun fun_of sys = (Option.map fst oo Symtab.lookup) sys;
  1597     fun fun_of sys = (Option.map fst oo Symtab.lookup) sys;
  1596     fun eval p = (
  1598     fun eval p = (
  1597       reff := NONE;
  1599       reff := NONE;
  1598       if !eval_verbose then Pretty.writeln p else ();
  1600       if !eval_verbose then Pretty.writeln p else ();
  1599       use_text "" Output.ml_output (!eval_verbose)
  1601       use_text "generated code for evaluation" Output.ml_output (!eval_verbose)
  1600         ((Pretty.output o Pretty.chunks) [p,
  1602         ((Pretty.output o Pretty.chunks) [p,
  1601           str ("val _ = (" ^ ref_name ^ " := SOME " ^ val_name' ^ ")")
  1603           str ("val _ = (" ^ ref_name ^ " := SOME " ^ val_name' ^ ")")
  1602         ]);
  1604         ]);
  1603       case !reff
  1605       case !reff
  1604        of NONE => error ("Could not retrieve value of ML reference " ^ quote ref_name
  1606        of NONE => error ("Could not retrieve value of ML reference " ^ quote ref_name