1 : |
blume |
975 |
(* format.sml
|
2 : |
|
|
*
|
3 : |
|
|
* (C) 2001 Lucent Technologies, Bell Labs
|
4 : |
|
|
*
|
5 : |
|
|
* Format the list-of-edges dependency graph so it becomes a valid
|
6 : |
|
|
* ML program.
|
7 : |
|
|
*
|
8 : |
|
|
* author: Matthias Blume (blume@research.bell-labs.com)
|
9 : |
|
|
*)
|
10 : |
|
|
structure FormatPortable : sig
|
11 : |
|
|
val output : PortableGraph.graph * TextIO.outstream -> unit
|
12 : |
|
|
end = struct
|
13 : |
|
|
structure P = PortableGraph
|
14 : |
|
|
|
15 : |
|
|
fun output (P.GRAPH { imports, defs, export }, outs) = let
|
16 : |
blume |
977 |
val context = "c"
|
17 : |
|
|
fun out l = app (fn x => TextIO.output (outs, x)) l
|
18 : |
blume |
975 |
|
19 : |
blume |
977 |
fun varlist [] = "[]"
|
20 : |
|
|
| varlist [x] = concat ["[", x, "]"]
|
21 : |
|
|
| varlist (h :: t) =
|
22 : |
|
|
concat ("[" :: h :: foldr (fn (x, a) => ", " :: x :: a) ["]"] t)
|
23 : |
blume |
975 |
|
24 : |
blume |
977 |
fun cfc (front, args) =
|
25 : |
|
|
(out [front];
|
26 : |
|
|
app (fn x => out [" ", x]) (context :: args))
|
27 : |
blume |
975 |
|
28 : |
blume |
977 |
fun tos s = concat ["\"", String.toString s, "\""]
|
29 : |
blume |
975 |
|
30 : |
blume |
1011 |
fun tons P.SGN = "sgn"
|
31 : |
|
|
| tons P.STR = "str"
|
32 : |
|
|
| tons P.FCT = "fct"
|
33 : |
|
|
|
34 : |
|
|
fun rhs (P.SYM (ns, n)) = cfc (tons ns, [tos n])
|
35 : |
blume |
977 |
| rhs (P.SYMS syms) = cfc ("syms", [varlist syms])
|
36 : |
|
|
| rhs (P.IMPORT { lib, syms }) = cfc ("import", [lib, syms])
|
37 : |
|
|
| rhs (P.COMPILE { src = (src, native), env, syms }) =
|
38 : |
|
|
cfc (if native then "ncompile" else "compile",
|
39 : |
|
|
[tos src, env, syms])
|
40 : |
|
|
| rhs (P.FILTER { env, syms }) = cfc ("filter", [env, syms])
|
41 : |
|
|
| rhs (P.MERGE l) = cfc ("merge", [varlist l])
|
42 : |
blume |
975 |
|
43 : |
blume |
977 |
fun dodef (P.DEF d) =
|
44 : |
|
|
(out [" val (", context, ", ", #lhs d, ") = "];
|
45 : |
|
|
rhs (#rhs d);
|
46 : |
|
|
out ["\n"])
|
47 : |
blume |
975 |
in
|
48 : |
blume |
977 |
out ["val thelibrary = fn ", context, " => (\n"];
|
49 : |
blume |
1011 |
out ["fn ", varlist imports, " => let open PGOps\n"];
|
50 : |
blume |
977 |
app dodef defs;
|
51 : |
|
|
out [" in\n export ", context, " ", export,
|
52 : |
|
|
"\n end\n\
|
53 : |
|
|
\ | _ => raise Fail \"wrong number of input libraries\")\n"]
|
54 : |
blume |
975 |
end
|
55 : |
|
|
end
|