adamc@25
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@25
|
2 * All rights reserved.
|
adamc@25
|
3 *
|
adamc@25
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@25
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@25
|
6 *
|
adamc@25
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@25
|
8 * this list of conditions and the following disclaimer.
|
adamc@25
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@25
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@25
|
11 * and/or other materials provided with the distribution.
|
adamc@25
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@25
|
13 * derived from this software without specific prior written permission.
|
adamc@25
|
14 *
|
adamc@25
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@25
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@25
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@25
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@25
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@25
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@25
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@25
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@25
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@25
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@25
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@25
|
26 *)
|
adamc@25
|
27
|
adamc@25
|
28 structure Monoize :> MONOIZE = struct
|
adamc@25
|
29
|
adamc@25
|
30 structure E = ErrorMsg
|
adamc@25
|
31 structure Env = CoreEnv
|
adamc@25
|
32
|
adamc@25
|
33 structure L = Core
|
adamc@25
|
34 structure L' = Mono
|
adamc@25
|
35
|
adamc@25
|
36 val dummyTyp = (L'.TNamed 0, E.dummySpan)
|
adamc@25
|
37
|
adamc@25
|
38 fun monoName env (all as (c, loc)) =
|
adamc@25
|
39 let
|
adamc@25
|
40 fun poly () =
|
adamc@25
|
41 (E.errorAt loc "Unsupported name constructor";
|
adamc@25
|
42 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@25
|
43 "")
|
adamc@25
|
44 in
|
adamc@25
|
45 case c of
|
adamc@25
|
46 L.CName s => s
|
adamc@25
|
47 | _ => poly ()
|
adamc@25
|
48 end
|
adamc@25
|
49
|
adamc@25
|
50 fun monoType env (all as (c, loc)) =
|
adamc@25
|
51 let
|
adamc@25
|
52 fun poly () =
|
adamc@25
|
53 (E.errorAt loc "Unsupported type constructor";
|
adamc@25
|
54 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@25
|
55 dummyTyp)
|
adamc@25
|
56 in
|
adamc@25
|
57 case c of
|
adamc@25
|
58 L.TFun (c1, c2) => (L'.TFun (monoType env c1, monoType env c2), loc)
|
adamc@25
|
59 | L.TCFun _ => poly ()
|
adamc@25
|
60 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
|
adamc@25
|
61 (L'.TRecord (map (fn (x, t) => (monoName env x, monoType env t)) xcs), loc)
|
adamc@25
|
62 | L.TRecord _ => poly ()
|
adamc@25
|
63
|
adamc@94
|
64 | L.CApp ((L.CFfi ("Basis", "xml"), _), _) => (L'.TFfi ("Basis", "string"), loc)
|
adamc@94
|
65
|
adamc@25
|
66 | L.CRel _ => poly ()
|
adamc@25
|
67 | L.CNamed n => (L'.TNamed n, loc)
|
adamc@51
|
68 | L.CFfi mx => (L'.TFfi mx, loc)
|
adamc@25
|
69 | L.CApp _ => poly ()
|
adamc@25
|
70 | L.CAbs _ => poly ()
|
adamc@25
|
71
|
adamc@25
|
72 | L.CName _ => poly ()
|
adamc@25
|
73
|
adamc@25
|
74 | L.CRecord _ => poly ()
|
adamc@25
|
75 | L.CConcat _ => poly ()
|
adamc@69
|
76 | L.CFold _ => poly ()
|
adamc@87
|
77 | L.CUnit => poly ()
|
adamc@25
|
78 end
|
adamc@25
|
79
|
adamc@25
|
80 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
|
adamc@25
|
81
|
adamc@111
|
82 fun attrifyExp env (e, tAll as (t, loc)) =
|
adamc@111
|
83 case #1 e of
|
adamc@118
|
84 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
|
adamc@118
|
85 let
|
adamc@118
|
86 val (_, _, _, s) = Env.lookupENamed env fnam
|
adamc@118
|
87 in
|
adamc@118
|
88 (L'.EPrim (Prim.String s), loc)
|
adamc@118
|
89 end
|
adamc@118
|
90 | L'.EClosure (fnam, args) =>
|
adamc@111
|
91 let
|
adamc@111
|
92 val (_, ft, _, s) = Env.lookupENamed env fnam
|
adamc@111
|
93 val ft = monoType env ft
|
adamc@111
|
94
|
adamc@111
|
95 fun attrify (args, ft, e) =
|
adamc@111
|
96 case (args, ft) of
|
adamc@111
|
97 ([], _) => e
|
adamc@111
|
98 | (arg :: args, (L'.TFun (t, ft), _)) =>
|
adamc@111
|
99 (L'.EStrcat (e,
|
adamc@111
|
100 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
|
adamc@111
|
101 attrifyExp env (arg, t)), loc)), loc)
|
adamc@111
|
102 | _ => (E.errorAt loc "Type mismatch encoding attribute";
|
adamc@111
|
103 e)
|
adamc@111
|
104 in
|
adamc@111
|
105 attrify (args, ft, (L'.EPrim (Prim.String s), loc))
|
adamc@111
|
106 end
|
adamc@111
|
107 | _ =>
|
adamc@111
|
108 case t of
|
adamc@111
|
109 L'.TFfi ("Basis", "string") => (L'.EFfiApp ("Basis", "attrifyString", [e]), loc)
|
adamc@111
|
110 | L'.TFfi ("Basis", "int") => (L'.EFfiApp ("Basis", "attrifyInt", [e]), loc)
|
adamc@111
|
111 | L'.TFfi ("Basis", "float") => (L'.EFfiApp ("Basis", "attrifyFloat", [e]), loc)
|
adamc@111
|
112 | L'.TRecord [] => (L'.EPrim (Prim.String ""), loc)
|
adamc@111
|
113
|
adamc@111
|
114 | _ => (E.errorAt loc "Don't know how to encode attribute type";
|
adamc@111
|
115 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
|
adamc@111
|
116 dummyExp)
|
adamc@105
|
117
|
adamc@25
|
118 fun monoExp env (all as (e, loc)) =
|
adamc@25
|
119 let
|
adamc@25
|
120 fun poly () =
|
adamc@25
|
121 (E.errorAt loc "Unsupported expression";
|
adamc@25
|
122 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
|
adamc@25
|
123 dummyExp)
|
adamc@25
|
124 in
|
adamc@25
|
125 case e of
|
adamc@25
|
126 L.EPrim p => (L'.EPrim p, loc)
|
adamc@25
|
127 | L.ERel n => (L'.ERel n, loc)
|
adamc@25
|
128 | L.ENamed n => (L'.ENamed n, loc)
|
adamc@51
|
129 | L.EFfi mx => (L'.EFfi mx, loc)
|
adamc@51
|
130 | L.EFfiApp (m, x, es) => (L'.EFfiApp (m, x, map (monoExp env) es), loc)
|
adamc@94
|
131
|
adamc@94
|
132 | L.EApp ((L.ECApp ((L.EFfi ("Basis", "cdata"), _),
|
adamc@94
|
133 _), _), se) => monoExp env se
|
adamc@95
|
134 | L.EApp (
|
adamc@95
|
135 (L.EApp (
|
adamc@95
|
136 (L.ECApp (
|
adamc@95
|
137 (L.ECApp (
|
adamc@95
|
138 (L.ECApp (
|
adamc@95
|
139 (L.EFfi ("Basis", "join"),
|
adamc@95
|
140 _), _), _),
|
adamc@95
|
141 _), _),
|
adamc@95
|
142 _), _),
|
adamc@95
|
143 xml1), _),
|
adamc@95
|
144 xml2) => (L'.EStrcat (monoExp env xml1, monoExp env xml2), loc)
|
adamc@95
|
145
|
adamc@95
|
146 | L.EApp (
|
adamc@95
|
147 (L.EApp (
|
adamc@104
|
148 (L.EApp (
|
adamc@95
|
149 (L.ECApp (
|
adamc@104
|
150 (L.ECApp (
|
adamc@104
|
151 (L.ECApp (
|
adamc@104
|
152 (L.ECApp (
|
adamc@104
|
153 (L.EFfi ("Basis", "tag"),
|
adamc@104
|
154 _), _), _), _), _), _), _), _), _),
|
adamc@104
|
155 attrs), _),
|
adamc@95
|
156 tag), _),
|
adamc@95
|
157 xml) =>
|
adamc@95
|
158 let
|
adamc@95
|
159 fun getTag (e, _) =
|
adamc@95
|
160 case e of
|
adamc@95
|
161 L.EFfi ("Basis", tag) => tag
|
adamc@95
|
162 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@95
|
163 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@95
|
164 "")
|
adamc@95
|
165
|
adamc@95
|
166 val tag = getTag tag
|
adamc@95
|
167
|
adamc@104
|
168 val attrs = monoExp env attrs
|
adamc@104
|
169
|
adamc@104
|
170 val tagStart =
|
adamc@104
|
171 case #1 attrs of
|
adamc@104
|
172 L'.ERecord xes =>
|
adamc@104
|
173 let
|
adamc@104
|
174 fun lowercaseFirst "" = ""
|
adamc@104
|
175 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@104
|
176
|
adamc@104
|
177 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
|
adamc@104
|
178 in
|
adamc@105
|
179 foldl (fn ((x, e, t), s) =>
|
adamc@104
|
180 let
|
adamc@104
|
181 val xp = " " ^ lowercaseFirst x ^ "=\""
|
adamc@104
|
182 in
|
adamc@104
|
183 (L'.EStrcat (s,
|
adamc@104
|
184 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
|
adamc@111
|
185 (L'.EStrcat (attrifyExp env (e, t),
|
adamc@104
|
186 (L'.EPrim (Prim.String "\""), loc)),
|
adamc@104
|
187 loc)),
|
adamc@104
|
188 loc)), loc)
|
adamc@104
|
189 end)
|
adamc@104
|
190 s xes
|
adamc@104
|
191 end
|
adamc@104
|
192 | _ => raise Fail "Attributes!"
|
adamc@104
|
193
|
adamc@95
|
194 fun normal () =
|
adamc@104
|
195 (L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
|
adamc@95
|
196 (L'.EStrcat (monoExp env xml,
|
adamc@95
|
197 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])), loc)), loc)),
|
adamc@95
|
198 loc)
|
adamc@104
|
199
|
adamc@104
|
200
|
adamc@95
|
201 in
|
adamc@95
|
202 case xml of
|
adamc@95
|
203 (L.EApp ((L.ECApp ((L.EFfi ("Basis", "cdata"), _),
|
adamc@95
|
204 _), _), (L.EPrim (Prim.String s), _)), _) =>
|
adamc@95
|
205 if CharVector.all Char.isSpace s then
|
adamc@104
|
206 (L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc)
|
adamc@95
|
207 else
|
adamc@95
|
208 normal ()
|
adamc@95
|
209 | _ => normal ()
|
adamc@95
|
210 end
|
adamc@94
|
211
|
adamc@25
|
212 | L.EApp (e1, e2) => (L'.EApp (monoExp env e1, monoExp env e2), loc)
|
adamc@26
|
213 | L.EAbs (x, dom, ran, e) =>
|
adamc@26
|
214 (L'.EAbs (x, monoType env dom, monoType env ran, monoExp (Env.pushERel env x dom) e), loc)
|
adamc@25
|
215 | L.ECApp _ => poly ()
|
adamc@25
|
216 | L.ECAbs _ => poly ()
|
adamc@25
|
217
|
adamc@29
|
218 | L.ERecord xes => (L'.ERecord (map (fn (x, e, t) => (monoName env x, monoExp env e, monoType env t)) xes), loc)
|
adamc@25
|
219 | L.EField (e, x, _) => (L'.EField (monoExp env e, monoName env x), loc)
|
adamc@73
|
220 | L.EFold _ => poly ()
|
adamc@102
|
221 | L.EWrite e => (L'.EWrite (monoExp env e), loc)
|
adamc@110
|
222
|
adamc@111
|
223 | L.EClosure (n, es) => (L'.EClosure (n, map (monoExp env) es), loc)
|
adamc@25
|
224 end
|
adamc@25
|
225
|
adamc@25
|
226 fun monoDecl env (all as (d, loc)) =
|
adamc@25
|
227 let
|
adamc@25
|
228 fun poly () =
|
adamc@25
|
229 (E.errorAt loc "Unsupported declaration";
|
adamc@25
|
230 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
|
adamc@25
|
231 NONE)
|
adamc@25
|
232 in
|
adamc@25
|
233 case d of
|
adamc@25
|
234 L.DCon _ => NONE
|
adamc@109
|
235 | L.DVal (x, n, t, e, s) => SOME (Env.pushENamed env x n t (SOME e) s,
|
adamc@109
|
236 (L'.DVal (x, n, monoType env t, monoExp env e, s), loc))
|
adamc@115
|
237 | L.DExport n =>
|
adamc@115
|
238 let
|
adamc@115
|
239 val (_, _, _, s) = Env.lookupENamed env n
|
adamc@115
|
240 in
|
adamc@115
|
241 SOME (env, (L'.DExport (s, n), loc))
|
adamc@115
|
242 end
|
adamc@25
|
243 end
|
adamc@25
|
244
|
adamc@25
|
245 fun monoize env ds =
|
adamc@25
|
246 let
|
adamc@25
|
247 val (_, ds) = List.foldl (fn (d, (env, ds)) =>
|
adamc@25
|
248 case monoDecl env d of
|
adamc@25
|
249 NONE => (env, ds)
|
adamc@25
|
250 | SOME (env, d) => (env, d :: ds)) (env, []) ds
|
adamc@25
|
251 in
|
adamc@25
|
252 rev ds
|
adamc@25
|
253 end
|
adamc@25
|
254
|
adamc@25
|
255 end
|