20 * OuterParse.token list; |
20 * OuterParse.token list; |
21 val parse_syntax: ('b -> int) -> (string -> 'b -> 'a * 'b) -> OuterParse.token list -> |
21 val parse_syntax: ('b -> int) -> (string -> 'b -> 'a * 'b) -> OuterParse.token list -> |
22 ('b -> 'a pretty_syntax * 'b) * OuterParse.token list; |
22 ('b -> 'a pretty_syntax * 'b) * OuterParse.token list; |
23 val pretty_list: string -> string -> int * string -> CodegenThingol.iterm pretty_syntax; |
23 val pretty_list: string -> string -> int * string -> CodegenThingol.iterm pretty_syntax; |
24 val serializers: { |
24 val serializers: { |
25 ml: string * (string * string -> serializer), |
25 ml: string * (string -> serializer), |
26 haskell: string * (string * string list -> serializer) |
26 haskell: string * (string * string list -> serializer) |
27 }; |
27 }; |
28 val mk_flat_ml_resolver: string list -> string -> string; |
28 val mk_flat_ml_resolver: string list -> string -> string; |
29 val ml_fun_datatype: string * string |
29 val ml_fun_datatype: string |
30 -> ((string -> CodegenThingol.itype pretty_syntax option) |
30 -> ((string -> CodegenThingol.itype pretty_syntax option) |
31 * (string -> CodegenThingol.iterm pretty_syntax option)) |
31 * (string -> CodegenThingol.iterm pretty_syntax option)) |
32 -> (string -> string) |
32 -> (string -> string) |
33 -> ((string * CodegenThingol.funn) list -> Pretty.T) |
33 -> ((string * CodegenThingol.funn) list -> Pretty.T) |
34 * ((string * CodegenThingol.datatyp) list -> Pretty.T); |
34 * ((string * CodegenThingol.datatyp) list -> Pretty.T); |
341 fun is_valid (reserved_ml : string list) = not o member (op =) reserved_ml; |
341 fun is_valid (reserved_ml : string list) = not o member (op =) reserved_ml; |
342 fun maybe_unique _ _ = NONE; |
342 fun maybe_unique _ _ = NONE; |
343 fun re_mangle _ dst = error ("no such definition name: " ^ quote dst); |
343 fun re_mangle _ dst = error ("no such definition name: " ^ quote dst); |
344 ); |
344 ); |
345 |
345 |
346 fun ml_expr_seri (is_cons, needs_type) (tyco_syntax, const_syntax) resolv = |
346 fun ml_expr_seri is_cons (tyco_syntax, const_syntax) resolv = |
347 let |
347 let |
348 val ml_from_label = |
348 val ml_from_label = |
349 str o translate_string (fn "_" => "__" | "." => "_" | c => c) |
349 str o translate_string (fn "_" => "__" | "." => "_" | c => c) |
350 o NameSpace.base o resolv; |
350 o NameSpace.base o resolv; |
351 fun ml_from_tyvar (v, []) = |
351 fun ml_from_tyvar (v, []) = |
432 ml_from_type (INFX (1, R)) t2 |
432 ml_from_type (INFX (1, R)) t2 |
433 ] |
433 ] |
434 end |
434 end |
435 | ml_from_type fxy (ITyVar v) = |
435 | ml_from_type fxy (ITyVar v) = |
436 str ("'" ^ v); |
436 str ("'" ^ v); |
437 fun typify ty p = |
|
438 let |
|
439 fun needs_type_t (tyco `%% tys) = |
|
440 needs_type tyco |
|
441 orelse exists needs_type_t tys |
|
442 | needs_type_t (ITyVar _) = |
|
443 false |
|
444 | needs_type_t (ty1 `-> ty2) = |
|
445 needs_type_t ty1 orelse needs_type_t ty2; |
|
446 in if needs_type_t ty |
|
447 then |
|
448 Pretty.enclose "(" ")" [ |
|
449 p, |
|
450 str ":", |
|
451 ml_from_type NOBR ty |
|
452 ] |
|
453 else p |
|
454 end; |
|
455 fun ml_from_expr fxy (e as IConst x) = |
437 fun ml_from_expr fxy (e as IConst x) = |
456 ml_from_app fxy (x, []) |
438 ml_from_app fxy (x, []) |
457 | ml_from_expr fxy (IVar v) = |
439 | ml_from_expr fxy (IVar v) = |
458 str v |
440 str v |
459 | ml_from_expr fxy (e as e1 `$ e2) = |
441 | ml_from_expr fxy (e as e1 `$ e2) = |
467 | ml_from_expr fxy (e as _ `|-> _) = |
449 | ml_from_expr fxy (e as _ `|-> _) = |
468 let |
450 let |
469 val (es, be) = CodegenThingol.unfold_abs e; |
451 val (es, be) = CodegenThingol.unfold_abs e; |
470 fun mk_abs (e, ty) = (Pretty.block o Pretty.breaks) [ |
452 fun mk_abs (e, ty) = (Pretty.block o Pretty.breaks) [ |
471 str "fn", |
453 str "fn", |
472 typify ty (ml_from_expr NOBR e), |
454 ml_from_expr NOBR e, |
473 str "=>" |
455 str "=>" |
474 ]; |
456 ]; |
475 in brackify BR (map mk_abs es @ [ml_from_expr NOBR be]) end |
457 in brackify BR (map mk_abs es @ [ml_from_expr NOBR be]) end |
476 | ml_from_expr fxy (INum (n, _)) = |
458 | ml_from_expr fxy (INum (n, _)) = |
477 brackify BR [ |
459 brackify BR [ |
490 let |
472 let |
491 val (ves, be) = CodegenThingol.unfold_let e; |
473 val (ves, be) = CodegenThingol.unfold_let e; |
492 fun mk_val ((ve, vty), se) = Pretty.block [ |
474 fun mk_val ((ve, vty), se) = Pretty.block [ |
493 (Pretty.block o Pretty.breaks) [ |
475 (Pretty.block o Pretty.breaks) [ |
494 str "val", |
476 str "val", |
495 typify vty (ml_from_expr NOBR ve), |
477 ml_from_expr NOBR ve, |
496 str "=", |
478 str "=", |
497 ml_from_expr NOBR se |
479 ml_from_expr NOBR se |
498 ], |
480 ], |
499 str ";" |
481 str ";" |
500 ]; |
482 ]; |
534 brackify fxy ( |
516 brackify fxy ( |
535 (str o resolv) c |
517 (str o resolv) c |
536 :: (lss |
518 :: (lss |
537 @ map (ml_from_expr BR) es) |
519 @ map (ml_from_expr BR) es) |
538 ); |
520 ); |
539 in (ml_from_label, ml_from_tyvar, ml_from_sortlookup, ml_from_tycoexpr, ml_from_type, typify, ml_from_expr) end; |
521 in (ml_from_label, ml_from_tyvar, ml_from_sortlookup, ml_from_tycoexpr, ml_from_type, ml_from_expr) end; |
540 |
522 |
541 fun ml_fun_datatyp (is_cons, needs_type) (tyco_syntax, const_syntax) resolv = |
523 fun ml_fun_datatyp is_cons (tyco_syntax, const_syntax) resolv = |
542 let |
524 let |
543 val (ml_from_label, ml_from_tyvar, ml_from_sortlookup, ml_from_tycoexpr, ml_from_type, typify, ml_from_expr) = |
525 val (ml_from_label, ml_from_tyvar, ml_from_sortlookup, ml_from_tycoexpr, ml_from_type, ml_from_expr) = |
544 ml_expr_seri (is_cons, needs_type) (tyco_syntax, const_syntax) resolv; |
526 ml_expr_seri is_cons (tyco_syntax, const_syntax) resolv; |
545 fun chunk_defs ps = |
527 fun chunk_defs ps = |
546 let |
528 let |
547 val (p_init, p_last) = split_last ps |
529 val (p_init, p_last) = split_last ps |
548 in |
530 in |
549 Pretty.chunks (p_init @ [Pretty.block ([p_last, str ";"])]) |
531 Pretty.chunks (p_init @ [Pretty.block ([p_last, str ";"])]) |
581 let |
563 let |
582 val shift = if null eq_tl then I else |
564 val shift = if null eq_tl then I else |
583 map (Pretty.block o single o Pretty.block o single); |
565 map (Pretty.block o single o Pretty.block o single); |
584 fun mk_arg e ty = |
566 fun mk_arg e ty = |
585 ml_from_expr BR e |
567 ml_from_expr BR e |
586 |> typify ty |
|
587 fun mk_eq definer (pats, expr) = |
568 fun mk_eq definer (pats, expr) = |
588 (Pretty.block o Pretty.breaks) ( |
569 (Pretty.block o Pretty.breaks) ( |
589 [str definer, (str o resolv) name] |
570 [str definer, (str o resolv) name] |
590 @ (if null pats andalso null sortctxt |
571 @ (if null pats andalso null sortctxt |
591 then [str ":", ml_from_type NOBR ty] |
572 then [str ":", ml_from_type NOBR ty] |
632 :: map (mk_datatype "and") defs_tl |
613 :: map (mk_datatype "and") defs_tl |
633 ) |
614 ) |
634 end; |
615 end; |
635 in (ml_from_funs, ml_from_datatypes) end; |
616 in (ml_from_funs, ml_from_datatypes) end; |
636 |
617 |
637 fun ml_from_defs (is_cons, needs_type) |
618 fun ml_from_defs is_cons |
638 (_, tyco_syntax, const_syntax) resolver prefix defs = |
619 (_, tyco_syntax, const_syntax) resolver prefix defs = |
639 let |
620 let |
640 val resolv = resolver prefix; |
621 val resolv = resolver prefix; |
641 val (ml_from_label, ml_from_tyvar, ml_from_sortlookup, ml_from_tycoexpr, ml_from_type, typify, ml_from_expr) = |
622 val (ml_from_label, ml_from_tyvar, ml_from_sortlookup, ml_from_tycoexpr, ml_from_type, ml_from_expr) = |
642 ml_expr_seri (is_cons, needs_type) (tyco_syntax, const_syntax) resolv; |
623 ml_expr_seri is_cons (tyco_syntax, const_syntax) resolv; |
643 val (ml_from_funs, ml_from_datatypes) = |
624 val (ml_from_funs, ml_from_datatypes) = |
644 ml_fun_datatyp (is_cons, needs_type) (tyco_syntax, const_syntax) resolv; |
625 ml_fun_datatyp is_cons (tyco_syntax, const_syntax) resolv; |
645 val filter_datatype = |
626 val filter_datatype = |
646 map_filter |
627 map_filter |
647 (fn (name, CodegenThingol.Datatype info) => SOME (name, info) |
628 (fn (name, CodegenThingol.Datatype info) => SOME (name, info) |
648 | (name, CodegenThingol.Datatypecons _) => NONE |
629 | (name, CodegenThingol.Datatypecons _) => NONE |
649 | (name, def) => error ("datatype block containing illegal def: " |
630 | (name, def) => error ("datatype block containing illegal def: " |
768 | (_, CodegenThingol.Classmember _)::_ => (SOME o ml_from_class o filter_class) defs |
749 | (_, CodegenThingol.Classmember _)::_ => (SOME o ml_from_class o filter_class) defs |
769 | [def] => ml_from_def def |
750 | [def] => ml_from_def def |
770 | defs => error ("illegal mutual dependencies: " ^ (commas o map fst) defs) |
751 | defs => error ("illegal mutual dependencies: " ^ (commas o map fst) defs) |
771 end; |
752 end; |
772 |
753 |
773 fun ml_annotators (nsp_dtcon, nsp_class) = |
754 fun ml_annotators nsp_dtcon = |
774 let |
755 let |
775 fun needs_type tyco = |
|
776 CodegenThingol.has_nsp tyco nsp_class; |
|
777 fun is_cons c = CodegenThingol.has_nsp c nsp_dtcon; |
756 fun is_cons c = CodegenThingol.has_nsp c nsp_dtcon; |
778 in (is_cons, needs_type) end; |
757 in is_cons end; |
779 |
758 |
780 in |
759 in |
781 |
760 |
782 fun ml_from_thingol target (nsp_dtcon, nsp_class) nspgrp = |
761 fun ml_from_thingol target nsp_dtcon nspgrp = |
783 let |
762 let |
784 fun ml_from_module resolv _ ((_, name), ps) = |
763 fun ml_from_module resolv _ ((_, name), ps) = |
785 Pretty.chunks ([ |
764 Pretty.chunks ([ |
786 str ("structure " ^ name ^ " = "), |
765 str ("structure " ^ name ^ " = "), |
787 str "struct", |
766 str "struct", |
788 str "" |
767 str "" |
789 ] @ separate (str "") ps @ [ |
768 ] @ separate (str "") ps @ [ |
790 str "", |
769 str "", |
791 str ("end; (* struct " ^ name ^ " *)") |
770 str ("end; (* struct " ^ name ^ " *)") |
792 ]); |
771 ]); |
793 val (is_cons, needs_type) = ml_annotators (nsp_dtcon, nsp_class); |
772 val is_cons = ml_annotators nsp_dtcon; |
794 val serializer = abstract_serializer (target, nspgrp) |
773 val serializer = abstract_serializer (target, nspgrp) |
795 "ROOT" (ml_from_defs (is_cons, needs_type), ml_from_module, |
774 "ROOT" (ml_from_defs is_cons, ml_from_module, |
796 abstract_validator reserved_ml, snd); |
775 abstract_validator reserved_ml, snd); |
797 fun eta_expander module const_syntax s = |
776 fun eta_expander module const_syntax s = |
798 case const_syntax s |
777 case const_syntax s |
799 of SOME ((i, _), _) => i |
778 of SOME ((i, _), _) => i |
800 | _ => if CodegenThingol.has_nsp s nsp_dtcon |
779 | _ => if CodegenThingol.has_nsp s nsp_dtcon |