src/Tools/Code/code_namespace.ML
changeset 39147 3c284a152bd6
parent 39055 81e0368812ad
child 39203 b2f9a6f4b84b
equal deleted inserted replaced
39145:154fd9c06c63 39147:3c284a152bd6
     8 sig
     8 sig
     9   val dest_name: string -> string * string
     9   val dest_name: string -> string * string
    10   datatype ('a, 'b) node =
    10   datatype ('a, 'b) node =
    11       Dummy
    11       Dummy
    12     | Stmt of 'a
    12     | Stmt of 'a
    13     | Module of ('b * (string * ('a, 'b) node) Graph.T);
    13     | Module of ('b * (string * ('a, 'b) node) Graph.T)
       
    14   type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T
    14   val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
    15   val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
    15     reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    16     reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    16     namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    17     namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    17     cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
    18     cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
    18     modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
    19     modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
    19       -> Code_Thingol.program
    20       -> Code_Thingol.program
    20       -> { deresolver: string list -> string -> string,
    21       -> { deresolver: string list -> string -> string,
    21            hierarchical_program: (string * ('a, 'b) node) Graph.T }
    22            hierarchical_program: ('a, 'b) hierarchical_program }
       
    23   val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
       
    24     print_stmt: string list -> string * 'a -> 'c,
       
    25     lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
       
    26       -> ('a, 'b) hierarchical_program -> 'c list
    22 end;
    27 end;
    23 
    28 
    24 structure Code_Namespace : CODE_NAMESPACE =
    29 structure Code_Namespace : CODE_NAMESPACE =
    25 struct
    30 struct
    26 
    31 
    34 
    39 
    35 datatype ('a, 'b) node =
    40 datatype ('a, 'b) node =
    36     Dummy
    41     Dummy
    37   | Stmt of 'a
    42   | Stmt of 'a
    38   | Module of ('b * (string * ('a, 'b) node) Graph.T);
    43   | Module of ('b * (string * ('a, 'b) node) Graph.T);
       
    44 
       
    45 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T;
    39 
    46 
    40 fun map_module_content f (Module content) = Module (f content);
    47 fun map_module_content f (Module content) = Module (f content);
    41 
    48 
    42 fun map_module [] = I
    49 fun map_module [] = I
    43   | map_module (name_fragment :: name_fragments) =
    50   | map_module (name_fragment :: name_fragments) =
   138       in Long_Name.implode (remainder @ [base']) end
   145       in Long_Name.implode (remainder @ [base']) end
   139         handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   146         handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   140 
   147 
   141   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   148   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   142 
   149 
       
   150 fun print_hierarchical { print_module, print_stmt, lift_markup } =
       
   151   let
       
   152     fun print_node _ (_, Dummy) =
       
   153           NONE
       
   154       | print_node prefix_fragments (name, Stmt stmt) =
       
   155           SOME (lift_markup (Code_Printer.markup_stmt name)
       
   156             (print_stmt prefix_fragments (name, stmt)))
       
   157       | print_node prefix_fragments (name_fragment, Module (data, nodes)) =
       
   158           let
       
   159             val prefix_fragments' = prefix_fragments @ [name_fragment]
       
   160           in
       
   161             Option.map (print_module prefix_fragments'
       
   162               name_fragment data) (print_nodes prefix_fragments' nodes)
       
   163           end
       
   164     and print_nodes prefix_fragments nodes =
       
   165       let
       
   166         val xs = (map_filter (fn name => print_node prefix_fragments
       
   167           (name, snd (Graph.get_node nodes name))) o rev o flat o Graph.strong_conn) nodes
       
   168       in if null xs then NONE else SOME xs end;
       
   169   in these o print_nodes [] end;
       
   170 
   143 end;
   171 end;