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@120
|
82 fun fooifyExp name env =
|
adamc@120
|
83 let
|
adamc@120
|
84 fun fooify (e, tAll as (t, loc)) =
|
adamc@120
|
85 case #1 e of
|
adamc@120
|
86 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
|
adamc@120
|
87 let
|
adamc@120
|
88 val (_, _, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
89 in
|
adamc@120
|
90 (L'.EPrim (Prim.String s), loc)
|
adamc@120
|
91 end
|
adamc@120
|
92 | L'.EClosure (fnam, args) =>
|
adamc@120
|
93 let
|
adamc@120
|
94 val (_, ft, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
95 val ft = monoType env ft
|
adamc@111
|
96
|
adamc@120
|
97 fun attrify (args, ft, e) =
|
adamc@120
|
98 case (args, ft) of
|
adamc@120
|
99 ([], _) => e
|
adamc@120
|
100 | (arg :: args, (L'.TFun (t, ft), _)) =>
|
adamc@121
|
101 attrify (args, ft,
|
adamc@121
|
102 (L'.EStrcat (e,
|
adamc@121
|
103 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
|
adamc@121
|
104 fooify (arg, t)), loc)), loc))
|
adamc@120
|
105 | _ => (E.errorAt loc "Type mismatch encoding attribute";
|
adamc@120
|
106 e)
|
adamc@120
|
107 in
|
adamc@120
|
108 attrify (args, ft, (L'.EPrim (Prim.String s), loc))
|
adamc@120
|
109 end
|
adamc@120
|
110 | _ =>
|
adamc@120
|
111 case t of
|
adamc@120
|
112 L'.TFfi ("Basis", "string") => (L'.EFfiApp ("Basis", name ^ "ifyString", [e]), loc)
|
adamc@120
|
113 | L'.TFfi ("Basis", "int") => (L'.EFfiApp ("Basis", name ^ "ifyInt", [e]), loc)
|
adamc@120
|
114 | L'.TFfi ("Basis", "float") => (L'.EFfiApp ("Basis", name ^ "ifyFloat", [e]), loc)
|
adamc@120
|
115 | L'.TRecord [] => (L'.EPrim (Prim.String ""), loc)
|
adamc@111
|
116
|
adamc@120
|
117 | _ => (E.errorAt loc "Don't know how to encode attribute type";
|
adamc@120
|
118 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
|
adamc@120
|
119 dummyExp)
|
adamc@120
|
120 in
|
adamc@120
|
121 fooify
|
adamc@120
|
122 end
|
adamc@120
|
123
|
adamc@120
|
124 val attrifyExp = fooifyExp "attr"
|
adamc@120
|
125 val urlifyExp = fooifyExp "url"
|
adamc@105
|
126
|
adamc@25
|
127 fun monoExp env (all as (e, loc)) =
|
adamc@25
|
128 let
|
adamc@25
|
129 fun poly () =
|
adamc@25
|
130 (E.errorAt loc "Unsupported expression";
|
adamc@25
|
131 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
|
adamc@25
|
132 dummyExp)
|
adamc@25
|
133 in
|
adamc@25
|
134 case e of
|
adamc@25
|
135 L.EPrim p => (L'.EPrim p, loc)
|
adamc@25
|
136 | L.ERel n => (L'.ERel n, loc)
|
adamc@25
|
137 | L.ENamed n => (L'.ENamed n, loc)
|
adamc@51
|
138 | L.EFfi mx => (L'.EFfi mx, loc)
|
adamc@51
|
139 | L.EFfiApp (m, x, es) => (L'.EFfiApp (m, x, map (monoExp env) es), loc)
|
adamc@94
|
140
|
adamc@139
|
141 | L.EApp (
|
adamc@139
|
142 (L.ECApp (
|
adamc@139
|
143 (L.ECApp (
|
adamc@139
|
144 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
|
adamc@139
|
145 _), _),
|
adamc@139
|
146 _), _),
|
adamc@139
|
147 se) => (L'.EFfiApp ("Basis", "htmlifyString", [monoExp env se]), loc)
|
adamc@95
|
148 | L.EApp (
|
adamc@95
|
149 (L.EApp (
|
adamc@95
|
150 (L.ECApp (
|
adamc@95
|
151 (L.ECApp (
|
adamc@95
|
152 (L.ECApp (
|
adamc@139
|
153 (L.ECApp (
|
adamc@139
|
154 (L.ECApp (
|
adamc@139
|
155 (L.ECApp (
|
adamc@139
|
156 (L.ECApp (
|
adamc@139
|
157 (L.EFfi ("Basis", "join"),
|
adamc@139
|
158 _), _), _),
|
adamc@139
|
159 _), _),
|
adamc@139
|
160 _), _),
|
adamc@139
|
161 _), _),
|
adamc@139
|
162 _), _),
|
adamc@95
|
163 _), _),
|
adamc@95
|
164 _), _),
|
adamc@95
|
165 xml1), _),
|
adamc@95
|
166 xml2) => (L'.EStrcat (monoExp env xml1, monoExp env xml2), loc)
|
adamc@95
|
167
|
adamc@95
|
168 | L.EApp (
|
adamc@95
|
169 (L.EApp (
|
adamc@104
|
170 (L.EApp (
|
adamc@95
|
171 (L.ECApp (
|
adamc@104
|
172 (L.ECApp (
|
adamc@104
|
173 (L.ECApp (
|
adamc@104
|
174 (L.ECApp (
|
adamc@139
|
175 (L.ECApp (
|
adamc@139
|
176 (L.ECApp (
|
adamc@139
|
177 (L.ECApp (
|
adamc@139
|
178 (L.ECApp (
|
adamc@139
|
179 (L.EFfi ("Basis", "tag"),
|
adamc@139
|
180 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@104
|
181 attrs), _),
|
adamc@95
|
182 tag), _),
|
adamc@95
|
183 xml) =>
|
adamc@95
|
184 let
|
adamc@95
|
185 fun getTag (e, _) =
|
adamc@95
|
186 case e of
|
adamc@95
|
187 L.EFfi ("Basis", tag) => tag
|
adamc@95
|
188 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@95
|
189 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@95
|
190 "")
|
adamc@95
|
191
|
adamc@95
|
192 val tag = getTag tag
|
adamc@95
|
193
|
adamc@104
|
194 val attrs = monoExp env attrs
|
adamc@104
|
195
|
adamc@104
|
196 val tagStart =
|
adamc@104
|
197 case #1 attrs of
|
adamc@104
|
198 L'.ERecord xes =>
|
adamc@104
|
199 let
|
adamc@104
|
200 fun lowercaseFirst "" = ""
|
adamc@104
|
201 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@104
|
202
|
adamc@104
|
203 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
|
adamc@104
|
204 in
|
adamc@105
|
205 foldl (fn ((x, e, t), s) =>
|
adamc@104
|
206 let
|
adamc@104
|
207 val xp = " " ^ lowercaseFirst x ^ "=\""
|
adamc@120
|
208
|
adamc@120
|
209 val fooify =
|
adamc@120
|
210 case x of
|
adamc@120
|
211 "Link" => urlifyExp
|
adamc@120
|
212 | _ => attrifyExp
|
adamc@104
|
213 in
|
adamc@104
|
214 (L'.EStrcat (s,
|
adamc@104
|
215 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
|
adamc@120
|
216 (L'.EStrcat (fooify env (e, t),
|
adamc@104
|
217 (L'.EPrim (Prim.String "\""), loc)),
|
adamc@104
|
218 loc)),
|
adamc@104
|
219 loc)), loc)
|
adamc@104
|
220 end)
|
adamc@104
|
221 s xes
|
adamc@104
|
222 end
|
adamc@104
|
223 | _ => raise Fail "Attributes!"
|
adamc@104
|
224
|
adamc@95
|
225 fun normal () =
|
adamc@104
|
226 (L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
|
adamc@95
|
227 (L'.EStrcat (monoExp env xml,
|
adamc@95
|
228 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])), loc)), loc)),
|
adamc@95
|
229 loc)
|
adamc@104
|
230
|
adamc@104
|
231
|
adamc@95
|
232 in
|
adamc@95
|
233 case xml of
|
adamc@95
|
234 (L.EApp ((L.ECApp ((L.EFfi ("Basis", "cdata"), _),
|
adamc@95
|
235 _), _), (L.EPrim (Prim.String s), _)), _) =>
|
adamc@95
|
236 if CharVector.all Char.isSpace s then
|
adamc@104
|
237 (L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc)
|
adamc@95
|
238 else
|
adamc@95
|
239 normal ()
|
adamc@95
|
240 | _ => normal ()
|
adamc@95
|
241 end
|
adamc@94
|
242
|
adamc@25
|
243 | L.EApp (e1, e2) => (L'.EApp (monoExp env e1, monoExp env e2), loc)
|
adamc@26
|
244 | L.EAbs (x, dom, ran, e) =>
|
adamc@26
|
245 (L'.EAbs (x, monoType env dom, monoType env ran, monoExp (Env.pushERel env x dom) e), loc)
|
adamc@25
|
246 | L.ECApp _ => poly ()
|
adamc@25
|
247 | L.ECAbs _ => poly ()
|
adamc@25
|
248
|
adamc@29
|
249 | L.ERecord xes => (L'.ERecord (map (fn (x, e, t) => (monoName env x, monoExp env e, monoType env t)) xes), loc)
|
adamc@25
|
250 | L.EField (e, x, _) => (L'.EField (monoExp env e, monoName env x), loc)
|
adamc@73
|
251 | L.EFold _ => poly ()
|
adamc@102
|
252 | L.EWrite e => (L'.EWrite (monoExp env e), loc)
|
adamc@110
|
253
|
adamc@111
|
254 | L.EClosure (n, es) => (L'.EClosure (n, map (monoExp env) es), loc)
|
adamc@25
|
255 end
|
adamc@25
|
256
|
adamc@25
|
257 fun monoDecl env (all as (d, loc)) =
|
adamc@25
|
258 let
|
adamc@25
|
259 fun poly () =
|
adamc@25
|
260 (E.errorAt loc "Unsupported declaration";
|
adamc@25
|
261 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
|
adamc@25
|
262 NONE)
|
adamc@25
|
263 in
|
adamc@25
|
264 case d of
|
adamc@25
|
265 L.DCon _ => NONE
|
adamc@109
|
266 | L.DVal (x, n, t, e, s) => SOME (Env.pushENamed env x n t (SOME e) s,
|
adamc@109
|
267 (L'.DVal (x, n, monoType env t, monoExp env e, s), loc))
|
adamc@128
|
268 | L.DValRec vis =>
|
adamc@128
|
269 let
|
adamc@128
|
270 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
|
adamc@128
|
271 in
|
adamc@128
|
272 SOME (env,
|
adamc@128
|
273 (L'.DValRec (map (fn (x, n, t, e, s) => (x, n, monoType env t, monoExp env e, s)) vis), loc))
|
adamc@128
|
274 end
|
adamc@115
|
275 | L.DExport n =>
|
adamc@115
|
276 let
|
adamc@120
|
277 val (_, t, _, s) = Env.lookupENamed env n
|
adamc@120
|
278
|
adamc@120
|
279 fun unwind (t, _) =
|
adamc@120
|
280 case t of
|
adamc@120
|
281 L.TFun (dom, ran) => dom :: unwind ran
|
adamc@120
|
282 | _ => []
|
adamc@120
|
283
|
adamc@120
|
284 val ts = map (monoType env) (unwind t)
|
adamc@115
|
285 in
|
adamc@120
|
286 SOME (env, (L'.DExport (s, n, ts), loc))
|
adamc@115
|
287 end
|
adamc@25
|
288 end
|
adamc@25
|
289
|
adamc@25
|
290 fun monoize env ds =
|
adamc@25
|
291 let
|
adamc@25
|
292 val (_, ds) = List.foldl (fn (d, (env, ds)) =>
|
adamc@25
|
293 case monoDecl env d of
|
adamc@25
|
294 NONE => (env, ds)
|
adamc@25
|
295 | SOME (env, d) => (env, d :: ds)) (env, []) ds
|
adamc@25
|
296 in
|
adamc@25
|
297 rev ds
|
adamc@25
|
298 end
|
adamc@25
|
299
|
adamc@25
|
300 end
|