442 | _ => false; |
442 | _ => false; |
443 val print_stmt = print_scala_stmt labelled_name syntax_tyco syntax_const |
443 val print_stmt = print_scala_stmt labelled_name syntax_tyco syntax_const |
444 (make_vars reserved) args_num is_singleton_constr; |
444 (make_vars reserved) args_num is_singleton_constr; |
445 |
445 |
446 (* print nodes *) |
446 (* print nodes *) |
|
447 fun print_module base implicit_ps p = Pretty.chunks2 |
|
448 ([str ("object " ^ base ^ " {")] |
|
449 @ (if null implicit_ps then [] else (single o Pretty.block) |
|
450 (str "import /*implicits*/" :: Pretty.brk 1 :: commas implicit_ps)) |
|
451 @ [p, str ("} /* object " ^ base ^ " */")]); |
447 fun print_implicit prefix_fragments implicit = |
452 fun print_implicit prefix_fragments implicit = |
448 let |
453 let |
449 val s = deresolver prefix_fragments implicit; |
454 val s = deresolver prefix_fragments implicit; |
450 in if length (Long_Name.explode s) = 1 then NONE else SOME (str s) end; |
455 in if length (Long_Name.explode s) = 1 then NONE else SOME (str s) end; |
451 fun print_implicits prefix_fragments implicits = |
|
452 case map_filter (print_implicit prefix_fragments) implicits |
|
453 of [] => NONE |
|
454 | ps => (SOME o Pretty.block) |
|
455 (str "import /*implicits*/" :: Pretty.brk 1 :: commas ps); |
|
456 fun print_module prefix_fragments base implicits p = Pretty.chunks2 |
|
457 ([str ("object " ^ base ^ " {")] @ the_list (print_implicits prefix_fragments implicits) |
|
458 @ [p, str ("} /* object " ^ base ^ " */")]); |
|
459 fun print_node _ (_, Dummy) = NONE |
456 fun print_node _ (_, Dummy) = NONE |
460 | print_node prefix_fragments (name, Stmt stmt) = |
457 | print_node prefix_fragments (name, Stmt stmt) = |
461 if null presentation_stmt_names |
458 if null presentation_stmt_names |
462 orelse member (op =) presentation_stmt_names name |
459 orelse member (op =) presentation_stmt_names name |
463 then SOME (print_stmt (deresolver prefix_fragments, deresolver []) (name, stmt)) |
460 then SOME (print_stmt (deresolver prefix_fragments, deresolver []) (name, stmt)) |
464 else NONE |
461 else NONE |
465 | print_node prefix_fragments (name_fragment, Module (implicits, nodes)) = |
462 | print_node prefix_fragments (name_fragment, Module (implicits, nodes)) = |
466 if null presentation_stmt_names |
463 if null presentation_stmt_names |
467 then case print_nodes (prefix_fragments @ [name_fragment]) nodes |
464 then |
468 of NONE => NONE |
465 let |
469 | SOME p => SOME (print_module prefix_fragments |
466 val prefix_fragments' = prefix_fragments @ [name_fragment]; |
470 (Long_Name.base_name name_fragment) implicits p) |
467 in |
|
468 Option.map (print_module name_fragment |
|
469 (map_filter (print_implicit prefix_fragments') implicits)) |
|
470 (print_nodes prefix_fragments' nodes) |
|
471 end |
471 else print_nodes [] nodes |
472 else print_nodes [] nodes |
472 and print_nodes prefix_fragments nodes = let |
473 and print_nodes prefix_fragments nodes = let |
473 val ps = map_filter (fn name => print_node prefix_fragments (name, |
474 val ps = map_filter (fn name => print_node prefix_fragments (name, |
474 snd (Graph.get_node nodes name))) |
475 snd (Graph.get_node nodes name))) |
475 ((rev o flat o Graph.strong_conn) nodes); |
476 ((rev o flat o Graph.strong_conn) nodes); |
476 in if null ps then NONE else SOME (Pretty.chunks2 ps) end; |
477 in if null ps then NONE else SOME (Pretty.chunks2 ps) end; |
477 |
478 |
478 (* serialization *) |
479 (* serialization *) |
479 val p_includes = if null presentation_stmt_names |
480 val p_includes = if null presentation_stmt_names |
480 then map (fn (base, p) => print_module [] base [] p) includes else []; |
481 then map (fn (base, p) => print_module base [] p) includes else []; |
481 val p = Pretty.chunks2 (p_includes @ the_list (print_nodes [] sca_program)); |
482 val p = Pretty.chunks2 (p_includes @ the_list (print_nodes [] sca_program)); |
482 in |
483 in |
483 Code_Target.mk_serialization target |
484 Code_Target.mk_serialization target |
484 (fn NONE => code_writeln | SOME file => File.write file o code_of_pretty) |
485 (fn NONE => code_writeln | SOME file => File.write file o code_of_pretty) |
485 (rpair [] o code_of_pretty) p destination |
486 (rpair [] o code_of_pretty) p destination |