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@168
|
36 val dummyTyp = (L'.TDatatype (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@143
|
64 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
|
adamc@143
|
65 (L'.TFfi ("Basis", "string"), loc)
|
adamc@183
|
66 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
|
adamc@183
|
67 (L'.TFfi ("Basis", "string"), loc)
|
adamc@94
|
68
|
adamc@25
|
69 | L.CRel _ => poly ()
|
adamc@168
|
70 | L.CNamed n =>
|
adamc@168
|
71 let
|
adamc@168
|
72 val (_, xncs) = Env.lookupDatatype env n
|
adamc@168
|
73
|
adamc@168
|
74 val xncs = map (fn (x, n, to) => (x, n, Option.map (monoType env) to)) xncs
|
adamc@168
|
75 in
|
adamc@168
|
76 (L'.TDatatype (n, xncs), loc)
|
adamc@168
|
77 end
|
adamc@51
|
78 | L.CFfi mx => (L'.TFfi mx, loc)
|
adamc@25
|
79 | L.CApp _ => poly ()
|
adamc@25
|
80 | L.CAbs _ => poly ()
|
adamc@25
|
81
|
adamc@25
|
82 | L.CName _ => poly ()
|
adamc@25
|
83
|
adamc@25
|
84 | L.CRecord _ => poly ()
|
adamc@25
|
85 | L.CConcat _ => poly ()
|
adamc@69
|
86 | L.CFold _ => poly ()
|
adamc@87
|
87 | L.CUnit => poly ()
|
adamc@25
|
88 end
|
adamc@25
|
89
|
adamc@25
|
90 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
|
adamc@25
|
91
|
adamc@179
|
92 structure IM = IntBinaryMap
|
adamc@179
|
93
|
adamc@179
|
94 datatype foo_kind =
|
adamc@179
|
95 Attr
|
adamc@179
|
96 | Url
|
adamc@179
|
97
|
adamc@179
|
98 fun fk2s fk =
|
adamc@179
|
99 case fk of
|
adamc@179
|
100 Attr => "attr"
|
adamc@179
|
101 | Url => "url"
|
adamc@179
|
102
|
adamc@179
|
103 structure Fm :> sig
|
adamc@179
|
104 type t
|
adamc@179
|
105
|
adamc@179
|
106 val empty : int -> t
|
adamc@179
|
107
|
adamc@179
|
108 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
|
adamc@179
|
109 val enter : t -> t
|
adamc@179
|
110 val decls : t -> L'.decl list
|
adamc@179
|
111 end = struct
|
adamc@179
|
112
|
adamc@179
|
113 structure M = BinaryMapFn(struct
|
adamc@179
|
114 type ord_key = foo_kind
|
adamc@179
|
115 fun compare x =
|
adamc@179
|
116 case x of
|
adamc@179
|
117 (Attr, Attr) => EQUAL
|
adamc@179
|
118 | (Attr, _) => LESS
|
adamc@179
|
119 | (_, Attr) => GREATER
|
adamc@179
|
120
|
adamc@179
|
121 | (Url, Url) => EQUAL
|
adamc@179
|
122 end)
|
adamc@179
|
123
|
adamc@179
|
124 type t = {
|
adamc@179
|
125 count : int,
|
adamc@179
|
126 map : int IM.map M.map,
|
adamc@179
|
127 decls : L'.decl list
|
adamc@179
|
128 }
|
adamc@179
|
129
|
adamc@179
|
130 fun empty count = {
|
adamc@179
|
131 count = count,
|
adamc@179
|
132 map = M.empty,
|
adamc@179
|
133 decls = []
|
adamc@179
|
134 }
|
adamc@179
|
135
|
adamc@179
|
136 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
|
adamc@179
|
137 fun decls ({decls, ...} : t) = decls
|
adamc@179
|
138
|
adamc@179
|
139 fun lookup (t as {count, map, decls}) k n thunk =
|
adamc@120
|
140 let
|
adamc@179
|
141 val im = Option.getOpt (M.find (map, k), IM.empty)
|
adamc@179
|
142 in
|
adamc@179
|
143 case IM.find (im, n) of
|
adamc@179
|
144 NONE =>
|
adamc@179
|
145 let
|
adamc@179
|
146 val n' = count
|
adamc@179
|
147 val (d, {count, map, decls}) = thunk count {count = count + 1,
|
adamc@179
|
148 map = M.insert (map, k, IM.insert (im, n, n')),
|
adamc@179
|
149 decls = decls}
|
adamc@179
|
150 in
|
adamc@179
|
151 ({count = count,
|
adamc@179
|
152 map = map,
|
adamc@179
|
153 decls = d :: decls}, n')
|
adamc@179
|
154 end
|
adamc@179
|
155 | SOME n' => (t, n')
|
adamc@179
|
156 end
|
adamc@179
|
157
|
adamc@179
|
158 end
|
adamc@185
|
159
|
adamc@185
|
160
|
adamc@185
|
161 fun capitalize s =
|
adamc@185
|
162 if s = "" then
|
adamc@185
|
163 s
|
adamc@185
|
164 else
|
adamc@185
|
165 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@179
|
166
|
adamc@179
|
167 fun fooifyExp fk env =
|
adamc@179
|
168 let
|
adamc@179
|
169 fun fooify fm (e, tAll as (t, loc)) =
|
adamc@120
|
170 case #1 e of
|
adamc@120
|
171 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
|
adamc@120
|
172 let
|
adamc@120
|
173 val (_, _, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
174 in
|
adamc@183
|
175 ((L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
|
adamc@120
|
176 end
|
adamc@120
|
177 | L'.EClosure (fnam, args) =>
|
adamc@120
|
178 let
|
adamc@120
|
179 val (_, ft, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
180 val ft = monoType env ft
|
adamc@111
|
181
|
adamc@179
|
182 fun attrify (args, ft, e, fm) =
|
adamc@120
|
183 case (args, ft) of
|
adamc@179
|
184 ([], _) => (e, fm)
|
adamc@120
|
185 | (arg :: args, (L'.TFun (t, ft), _)) =>
|
adamc@179
|
186 let
|
adamc@179
|
187 val (arg', fm) = fooify fm (arg, t)
|
adamc@179
|
188 in
|
adamc@179
|
189 attrify (args, ft,
|
adamc@179
|
190 (L'.EStrcat (e,
|
adamc@179
|
191 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
|
adamc@179
|
192 arg'), loc)), loc),
|
adamc@179
|
193 fm)
|
adamc@179
|
194 end
|
adamc@120
|
195 | _ => (E.errorAt loc "Type mismatch encoding attribute";
|
adamc@179
|
196 (e, fm))
|
adamc@120
|
197 in
|
adamc@183
|
198 attrify (args, ft, (L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
|
adamc@120
|
199 end
|
adamc@120
|
200 | _ =>
|
adamc@120
|
201 case t of
|
adamc@185
|
202 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
|
adamc@179
|
203 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@111
|
204
|
adamc@179
|
205 | L'.TDatatype (i, _) =>
|
adamc@179
|
206 let
|
adamc@179
|
207 fun makeDecl n fm =
|
adamc@179
|
208 let
|
adamc@179
|
209 val (x, xncs) = Env.lookupDatatype env i
|
adamc@179
|
210
|
adamc@179
|
211 val (branches, fm) =
|
adamc@179
|
212 ListUtil.foldlMap
|
adamc@179
|
213 (fn ((x, n, to), fm) =>
|
adamc@179
|
214 case to of
|
adamc@179
|
215 NONE =>
|
adamc@179
|
216 (((L'.PCon (L'.PConVar n, NONE), loc),
|
adamc@179
|
217 (L'.EPrim (Prim.String x), loc)),
|
adamc@179
|
218 fm)
|
adamc@179
|
219 | SOME t =>
|
adamc@179
|
220 let
|
adamc@182
|
221 val t = monoType env t
|
adamc@182
|
222 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
|
adamc@179
|
223 in
|
adamc@182
|
224 (((L'.PCon (L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
|
adamc@179
|
225 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
|
adamc@179
|
226 arg), loc)),
|
adamc@179
|
227 fm)
|
adamc@179
|
228 end)
|
adamc@179
|
229 fm xncs
|
adamc@179
|
230
|
adamc@179
|
231 val dom = tAll
|
adamc@179
|
232 val ran = (L'.TFfi ("Basis", "string"), loc)
|
adamc@179
|
233 in
|
adamc@179
|
234 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
|
adamc@179
|
235 n,
|
adamc@179
|
236 (L'.TFun (dom, ran), loc),
|
adamc@179
|
237 (L'.EAbs ("x",
|
adamc@179
|
238 dom,
|
adamc@179
|
239 ran,
|
adamc@179
|
240 (L'.ECase ((L'.ERel 0, loc),
|
adamc@179
|
241 branches,
|
adamc@182
|
242 {disc = dom,
|
adamc@182
|
243 result = ran}), loc)), loc),
|
adamc@179
|
244 "")], loc),
|
adamc@179
|
245 fm)
|
adamc@179
|
246 end
|
adamc@179
|
247
|
adamc@179
|
248 val (fm, n) = Fm.lookup fm fk i makeDecl
|
adamc@179
|
249 in
|
adamc@179
|
250 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
|
adamc@179
|
251 end
|
adamc@164
|
252
|
adamc@120
|
253 | _ => (E.errorAt loc "Don't know how to encode attribute type";
|
adamc@120
|
254 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
|
adamc@179
|
255 (dummyExp, fm))
|
adamc@120
|
256 in
|
adamc@120
|
257 fooify
|
adamc@120
|
258 end
|
adamc@120
|
259
|
adamc@179
|
260 val attrifyExp = fooifyExp Attr
|
adamc@179
|
261 val urlifyExp = fooifyExp Url
|
adamc@105
|
262
|
adamc@143
|
263 datatype 'a failable_search =
|
adamc@143
|
264 Found of 'a
|
adamc@143
|
265 | NotFound
|
adamc@143
|
266 | Error
|
adamc@143
|
267
|
adamc@153
|
268 structure St :> sig
|
adamc@153
|
269 type t
|
adamc@153
|
270
|
adamc@153
|
271 val empty : t
|
adamc@153
|
272
|
adamc@153
|
273 val radioGroup : t -> string option
|
adamc@153
|
274 val setRadioGroup : t * string -> t
|
adamc@153
|
275 end = struct
|
adamc@153
|
276
|
adamc@153
|
277 type t = {
|
adamc@153
|
278 radioGroup : string option
|
adamc@153
|
279 }
|
adamc@153
|
280
|
adamc@153
|
281 val empty = {radioGroup = NONE}
|
adamc@153
|
282
|
adamc@153
|
283 fun radioGroup (t : t) = #radioGroup t
|
adamc@153
|
284
|
adamc@153
|
285 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
|
adamc@153
|
286
|
adamc@153
|
287 end
|
adamc@153
|
288
|
adamc@178
|
289 fun monoPatCon pc =
|
adamc@178
|
290 case pc of
|
adamc@178
|
291 L.PConVar n => L'.PConVar n
|
adamc@178
|
292 | L.PConFfi mx => L'.PConFfi mx
|
adamc@178
|
293
|
adamc@182
|
294 fun monoPat env (p, loc) =
|
adamc@178
|
295 case p of
|
adamc@178
|
296 L.PWild => (L'.PWild, loc)
|
adamc@182
|
297 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
|
adamc@178
|
298 | L.PPrim p => (L'.PPrim p, loc)
|
adamc@182
|
299 | L.PCon (pc, po) => (L'.PCon (monoPatCon pc, Option.map (monoPat env) po), loc)
|
adamc@182
|
300 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
|
adamc@178
|
301
|
adamc@179
|
302 fun monoExp (env, st, fm) (all as (e, loc)) =
|
adamc@25
|
303 let
|
adamc@25
|
304 fun poly () =
|
adamc@25
|
305 (E.errorAt loc "Unsupported expression";
|
adamc@25
|
306 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
|
adamc@179
|
307 (dummyExp, fm))
|
adamc@25
|
308 in
|
adamc@25
|
309 case e of
|
adamc@179
|
310 L.EPrim p => ((L'.EPrim p, loc), fm)
|
adamc@179
|
311 | L.ERel n => ((L'.ERel n, loc), fm)
|
adamc@179
|
312 | L.ENamed n => ((L'.ENamed n, loc), fm)
|
adamc@185
|
313 | L.ECon (pc, eo) =>
|
adamc@179
|
314 let
|
adamc@179
|
315 val (eo, fm) =
|
adamc@179
|
316 case eo of
|
adamc@179
|
317 NONE => (NONE, fm)
|
adamc@179
|
318 | SOME e =>
|
adamc@179
|
319 let
|
adamc@179
|
320 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
321 in
|
adamc@179
|
322 (SOME e, fm)
|
adamc@179
|
323 end
|
adamc@179
|
324 in
|
adamc@185
|
325 ((L'.ECon (monoPatCon pc, eo), loc), fm)
|
adamc@179
|
326 end
|
adamc@179
|
327 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
|
adamc@179
|
328 | L.EFfiApp (m, x, es) =>
|
adamc@179
|
329 let
|
adamc@179
|
330 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
|
adamc@179
|
331 in
|
adamc@179
|
332 ((L'.EFfiApp (m, x, es), loc), fm)
|
adamc@179
|
333 end
|
adamc@94
|
334
|
adamc@139
|
335 | L.EApp (
|
adamc@139
|
336 (L.ECApp (
|
adamc@141
|
337 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
|
adamc@139
|
338 _), _),
|
adamc@179
|
339 se) =>
|
adamc@179
|
340 let
|
adamc@179
|
341 val (se, fm) = monoExp (env, st, fm) se
|
adamc@179
|
342 in
|
adamc@179
|
343 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
|
adamc@179
|
344 end
|
adamc@179
|
345
|
adamc@95
|
346 | L.EApp (
|
adamc@95
|
347 (L.EApp (
|
adamc@95
|
348 (L.ECApp (
|
adamc@95
|
349 (L.ECApp (
|
adamc@95
|
350 (L.ECApp (
|
adamc@139
|
351 (L.ECApp (
|
adamc@140
|
352 (L.EFfi ("Basis", "join"),
|
adamc@139
|
353 _), _), _),
|
adamc@139
|
354 _), _),
|
adamc@95
|
355 _), _),
|
adamc@95
|
356 _), _),
|
adamc@95
|
357 xml1), _),
|
adamc@179
|
358 xml2) =>
|
adamc@179
|
359 let
|
adamc@179
|
360 val (xml1, fm) = monoExp (env, st, fm) xml1
|
adamc@179
|
361 val (xml2, fm) = monoExp (env, st, fm) xml2
|
adamc@179
|
362 in
|
adamc@179
|
363 ((L'.EStrcat (xml1, xml2), loc), fm)
|
adamc@179
|
364 end
|
adamc@95
|
365
|
adamc@95
|
366 | L.EApp (
|
adamc@95
|
367 (L.EApp (
|
adamc@104
|
368 (L.EApp (
|
adamc@95
|
369 (L.ECApp (
|
adamc@104
|
370 (L.ECApp (
|
adamc@104
|
371 (L.ECApp (
|
adamc@104
|
372 (L.ECApp (
|
adamc@139
|
373 (L.ECApp (
|
adamc@139
|
374 (L.ECApp (
|
adamc@139
|
375 (L.ECApp (
|
adamc@139
|
376 (L.ECApp (
|
adamc@139
|
377 (L.EFfi ("Basis", "tag"),
|
adamc@139
|
378 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@104
|
379 attrs), _),
|
adamc@95
|
380 tag), _),
|
adamc@95
|
381 xml) =>
|
adamc@95
|
382 let
|
adamc@140
|
383 fun getTag' (e, _) =
|
adamc@140
|
384 case e of
|
adamc@143
|
385 L.EFfi ("Basis", tag) => (tag, [])
|
adamc@143
|
386 | L.ECApp (e, t) => let
|
adamc@143
|
387 val (tag, ts) = getTag' e
|
adamc@143
|
388 in
|
adamc@143
|
389 (tag, ts @ [t])
|
adamc@143
|
390 end
|
adamc@140
|
391 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@140
|
392 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
393 ("", []))
|
adamc@140
|
394
|
adamc@95
|
395 fun getTag (e, _) =
|
adamc@95
|
396 case e of
|
adamc@143
|
397 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
|
adamc@140
|
398 | L.EApp (e, (L.ERecord [], _)) => getTag' e
|
adamc@95
|
399 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@95
|
400 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
401 ("", []))
|
adamc@95
|
402
|
adamc@143
|
403 val (tag, targs) = getTag tag
|
adamc@95
|
404
|
adamc@179
|
405 val (attrs, fm) = monoExp (env, st, fm) attrs
|
adamc@104
|
406
|
adamc@143
|
407 fun tagStart tag =
|
adamc@104
|
408 case #1 attrs of
|
adamc@104
|
409 L'.ERecord xes =>
|
adamc@104
|
410 let
|
adamc@104
|
411 fun lowercaseFirst "" = ""
|
adamc@143
|
412 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
|
adamc@143
|
413 ^ String.extract (s, 1, NONE)
|
adamc@104
|
414
|
adamc@104
|
415 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
|
adamc@104
|
416 in
|
adamc@179
|
417 foldl (fn ((x, e, t), (s, fm)) =>
|
adamc@104
|
418 let
|
adamc@104
|
419 val xp = " " ^ lowercaseFirst x ^ "=\""
|
adamc@120
|
420
|
adamc@120
|
421 val fooify =
|
adamc@120
|
422 case x of
|
adamc@185
|
423 "Href" => urlifyExp
|
adamc@185
|
424 | "Link" => urlifyExp
|
adamc@143
|
425 | "Action" => urlifyExp
|
adamc@120
|
426 | _ => attrifyExp
|
adamc@179
|
427
|
adamc@179
|
428 val (e, fm) = fooify env fm (e, t)
|
adamc@104
|
429 in
|
adamc@179
|
430 ((L'.EStrcat (s,
|
adamc@179
|
431 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
|
adamc@179
|
432 (L'.EStrcat (e,
|
adamc@179
|
433 (L'.EPrim (Prim.String "\""),
|
adamc@179
|
434 loc)),
|
adamc@179
|
435 loc)),
|
adamc@179
|
436 loc)), loc),
|
adamc@179
|
437 fm)
|
adamc@104
|
438 end)
|
adamc@179
|
439 (s, fm) xes
|
adamc@104
|
440 end
|
adamc@143
|
441 | _ => raise Fail "Non-record attributes!"
|
adamc@104
|
442
|
adamc@143
|
443 fun input typ =
|
adamc@143
|
444 case targs of
|
adamc@155
|
445 [_, (L.CName name, _)] =>
|
adamc@179
|
446 let
|
adamc@179
|
447 val (ts, fm) = tagStart "input"
|
adamc@179
|
448 in
|
adamc@179
|
449 ((L'.EStrcat (ts,
|
adamc@179
|
450 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
451 loc)), loc), fm)
|
adamc@179
|
452 end
|
adamc@143
|
453 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
454 raise Fail "No name passed to input tag")
|
adamc@104
|
455
|
adamc@152
|
456 fun normal (tag, extra) =
|
adamc@143
|
457 let
|
adamc@179
|
458 val (tagStart, fm) = tagStart tag
|
adamc@152
|
459 val tagStart = case extra of
|
adamc@152
|
460 NONE => tagStart
|
adamc@152
|
461 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
|
adamc@152
|
462
|
adamc@143
|
463 fun normal () =
|
adamc@179
|
464 let
|
adamc@179
|
465 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
466 in
|
adamc@179
|
467 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
|
adamc@179
|
468 (L'.EStrcat (xml,
|
adamc@179
|
469 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
|
adamc@179
|
470 loc)), loc)),
|
adamc@179
|
471 loc),
|
adamc@179
|
472 fm)
|
adamc@179
|
473 end
|
adamc@143
|
474 in
|
adamc@143
|
475 case xml of
|
adamc@143
|
476 (L.EApp ((L.ECApp (
|
adamc@143
|
477 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
|
adamc@143
|
478 _), _),
|
adamc@143
|
479 _), _),
|
adamc@143
|
480 (L.EPrim (Prim.String s), _)), _) =>
|
adamc@143
|
481 if CharVector.all Char.isSpace s then
|
adamc@179
|
482 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
|
adamc@143
|
483 else
|
adamc@143
|
484 normal ()
|
adamc@143
|
485 | _ => normal ()
|
adamc@143
|
486 end
|
adamc@152
|
487 in
|
adamc@152
|
488 case tag of
|
adamc@179
|
489 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
|
adamc@152
|
490
|
adamc@152
|
491 | "textbox" =>
|
adamc@152
|
492 (case targs of
|
adamc@152
|
493 [_, (L.CName name, _)] =>
|
adamc@179
|
494 let
|
adamc@179
|
495 val (ts, fm) = tagStart "input"
|
adamc@179
|
496 in
|
adamc@179
|
497 ((L'.EStrcat (ts,
|
adamc@179
|
498 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
499 loc)), loc), fm)
|
adamc@179
|
500 end
|
adamc@152
|
501 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
502 raise Fail "No name passed to textarea tag"))
|
adamc@155
|
503 | "password" => input "password"
|
adamc@152
|
504 | "ltextarea" =>
|
adamc@152
|
505 (case targs of
|
adamc@152
|
506 [_, (L.CName name, _)] =>
|
adamc@179
|
507 let
|
adamc@179
|
508 val (ts, fm) = tagStart "textarea"
|
adamc@179
|
509 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
510 in
|
adamc@179
|
511 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
512 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
513 (L'.EStrcat (xml,
|
adamc@179
|
514 (L'.EPrim (Prim.String "</textarea>"),
|
adamc@179
|
515 loc)), loc)),
|
adamc@179
|
516 loc), fm)
|
adamc@179
|
517 end
|
adamc@152
|
518 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
519 raise Fail "No name passed to ltextarea tag"))
|
adamc@153
|
520
|
adamc@153
|
521 | "radio" =>
|
adamc@153
|
522 (case targs of
|
adamc@153
|
523 [_, (L.CName name, _)] =>
|
adamc@179
|
524 monoExp (env, St.setRadioGroup (st, name), fm) xml
|
adamc@153
|
525 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
526 raise Fail "No name passed to radio tag"))
|
adamc@153
|
527 | "radioOption" =>
|
adamc@153
|
528 (case St.radioGroup st of
|
adamc@153
|
529 NONE => raise Fail "No name for radioGroup"
|
adamc@153
|
530 | SOME name =>
|
adamc@153
|
531 normal ("input",
|
adamc@153
|
532 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
|
adamc@152
|
533
|
adamc@154
|
534 | "lselect" =>
|
adamc@154
|
535 (case targs of
|
adamc@154
|
536 [_, (L.CName name, _)] =>
|
adamc@179
|
537 let
|
adamc@179
|
538 val (ts, fm) = tagStart "select"
|
adamc@179
|
539 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
540 in
|
adamc@179
|
541 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
542 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
543 (L'.EStrcat (xml,
|
adamc@179
|
544 (L'.EPrim (Prim.String "</select>"),
|
adamc@179
|
545 loc)), loc)),
|
adamc@179
|
546 loc),
|
adamc@179
|
547 fm)
|
adamc@179
|
548 end
|
adamc@154
|
549 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@154
|
550 raise Fail "No name passed to lselect tag"))
|
adamc@154
|
551
|
adamc@154
|
552 | "loption" => normal ("option", NONE)
|
adamc@154
|
553
|
adamc@152
|
554 | _ => normal (tag, NONE)
|
adamc@95
|
555 end
|
adamc@94
|
556
|
adamc@141
|
557 | L.EApp ((L.ECApp (
|
adamc@141
|
558 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
|
adamc@141
|
559 _), _),
|
adamc@141
|
560 xml) =>
|
adamc@143
|
561 let
|
adamc@143
|
562 fun findSubmit (e, _) =
|
adamc@143
|
563 case e of
|
adamc@143
|
564 L.EApp (
|
adamc@143
|
565 (L.EApp (
|
adamc@143
|
566 (L.ECApp (
|
adamc@143
|
567 (L.ECApp (
|
adamc@143
|
568 (L.ECApp (
|
adamc@143
|
569 (L.ECApp (
|
adamc@143
|
570 (L.EFfi ("Basis", "join"),
|
adamc@143
|
571 _), _), _),
|
adamc@143
|
572 _), _),
|
adamc@143
|
573 _), _),
|
adamc@143
|
574 _), _),
|
adamc@143
|
575 xml1), _),
|
adamc@143
|
576 xml2) => (case findSubmit xml1 of
|
adamc@143
|
577 Error => Error
|
adamc@143
|
578 | NotFound => findSubmit xml2
|
adamc@143
|
579 | Found e =>
|
adamc@143
|
580 case findSubmit xml2 of
|
adamc@143
|
581 NotFound => Found e
|
adamc@143
|
582 | _ => Error)
|
adamc@143
|
583 | L.EApp (
|
adamc@143
|
584 (L.EApp (
|
adamc@143
|
585 (L.EApp (
|
adamc@143
|
586 (L.ECApp (
|
adamc@143
|
587 (L.ECApp (
|
adamc@143
|
588 (L.ECApp (
|
adamc@143
|
589 (L.ECApp (
|
adamc@143
|
590 (L.ECApp (
|
adamc@143
|
591 (L.ECApp (
|
adamc@143
|
592 (L.ECApp (
|
adamc@143
|
593 (L.ECApp (
|
adamc@143
|
594 (L.EFfi ("Basis", "tag"),
|
adamc@143
|
595 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@143
|
596 attrs), _),
|
adamc@143
|
597 _), _),
|
adamc@143
|
598 xml) =>
|
adamc@143
|
599 (case #1 attrs of
|
adamc@143
|
600 L.ERecord xes =>
|
adamc@143
|
601 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
|
adamc@143
|
602 | _ => NONE) xes of
|
adamc@143
|
603 NONE => findSubmit xml
|
adamc@143
|
604 | SOME et =>
|
adamc@143
|
605 case findSubmit xml of
|
adamc@143
|
606 NotFound => Found et
|
adamc@143
|
607 | _ => Error)
|
adamc@143
|
608 | _ => findSubmit xml)
|
adamc@143
|
609 | _ => NotFound
|
adamc@143
|
610
|
adamc@143
|
611 val (action, actionT) = case findSubmit xml of
|
adamc@143
|
612 NotFound => raise Fail "No submit found"
|
adamc@143
|
613 | Error => raise Fail "Not ready for multi-submit lforms yet"
|
adamc@143
|
614 | Found et => et
|
adamc@143
|
615
|
adamc@143
|
616 val actionT = monoType env actionT
|
adamc@179
|
617 val (action, fm) = monoExp (env, st, fm) action
|
adamc@179
|
618 val (action, fm) = urlifyExp env fm (action, actionT)
|
adamc@179
|
619 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@143
|
620 in
|
adamc@179
|
621 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
|
adamc@179
|
622 (L'.EStrcat (action,
|
adamc@179
|
623 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
|
adamc@179
|
624 (L'.EStrcat (xml,
|
adamc@179
|
625 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
|
adamc@179
|
626 fm)
|
adamc@143
|
627 end
|
adamc@141
|
628
|
adamc@148
|
629 | L.EApp ((L.ECApp (
|
adamc@148
|
630 (L.ECApp (
|
adamc@148
|
631 (L.ECApp (
|
adamc@148
|
632 (L.ECApp (
|
adamc@148
|
633 (L.EFfi ("Basis", "useMore"), _), _), _),
|
adamc@148
|
634 _), _),
|
adamc@148
|
635 _), _),
|
adamc@148
|
636 _), _),
|
adamc@179
|
637 xml) => monoExp (env, st, fm) xml
|
adamc@148
|
638
|
adamc@179
|
639 | L.EApp (e1, e2) =>
|
adamc@179
|
640 let
|
adamc@179
|
641 val (e1, fm) = monoExp (env, st, fm) e1
|
adamc@179
|
642 val (e2, fm) = monoExp (env, st, fm) e2
|
adamc@179
|
643 in
|
adamc@179
|
644 ((L'.EApp (e1, e2), loc), fm)
|
adamc@179
|
645 end
|
adamc@26
|
646 | L.EAbs (x, dom, ran, e) =>
|
adamc@179
|
647 let
|
adamc@179
|
648 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
|
adamc@179
|
649 in
|
adamc@179
|
650 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
|
adamc@179
|
651 end
|
adamc@25
|
652 | L.ECApp _ => poly ()
|
adamc@25
|
653 | L.ECAbs _ => poly ()
|
adamc@25
|
654
|
adamc@179
|
655 | L.ERecord xes =>
|
adamc@179
|
656 let
|
adamc@179
|
657 val (xes, fm) = ListUtil.foldlMap
|
adamc@179
|
658 (fn ((x, e, t), fm) =>
|
adamc@179
|
659 let
|
adamc@179
|
660 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
661 in
|
adamc@179
|
662 ((monoName env x,
|
adamc@179
|
663 e,
|
adamc@179
|
664 monoType env t), fm)
|
adamc@179
|
665 end) fm xes
|
adamc@179
|
666 in
|
adamc@179
|
667 ((L'.ERecord xes, loc), fm)
|
adamc@179
|
668 end
|
adamc@179
|
669 | L.EField (e, x, _) =>
|
adamc@179
|
670 let
|
adamc@179
|
671 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
672 in
|
adamc@179
|
673 ((L'.EField (e, monoName env x), loc), fm)
|
adamc@179
|
674 end
|
adamc@149
|
675 | L.ECut _ => poly ()
|
adamc@73
|
676 | L.EFold _ => poly ()
|
adamc@177
|
677
|
adamc@182
|
678 | L.ECase (e, pes, {disc, result}) =>
|
adamc@179
|
679 let
|
adamc@179
|
680 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
681 val (pes, fm) = ListUtil.foldlMap
|
adamc@179
|
682 (fn ((p, e), fm) =>
|
adamc@179
|
683 let
|
adamc@179
|
684 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
685 in
|
adamc@182
|
686 ((monoPat env p, e), fm)
|
adamc@179
|
687 end) fm pes
|
adamc@179
|
688 in
|
adamc@182
|
689 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
|
adamc@179
|
690 end
|
adamc@177
|
691
|
adamc@179
|
692 | L.EWrite e =>
|
adamc@179
|
693 let
|
adamc@179
|
694 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
695 in
|
adamc@179
|
696 ((L'.EWrite e, loc), fm)
|
adamc@179
|
697 end
|
adamc@110
|
698
|
adamc@179
|
699 | L.EClosure (n, es) =>
|
adamc@179
|
700 let
|
adamc@179
|
701 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
|
adamc@179
|
702 monoExp (env, st, fm) e)
|
adamc@179
|
703 fm es
|
adamc@179
|
704 in
|
adamc@179
|
705 ((L'.EClosure (n, es), loc), fm)
|
adamc@179
|
706 end
|
adamc@25
|
707 end
|
adamc@25
|
708
|
adamc@179
|
709 fun monoDecl (env, fm) (all as (d, loc)) =
|
adamc@25
|
710 let
|
adamc@25
|
711 fun poly () =
|
adamc@25
|
712 (E.errorAt loc "Unsupported declaration";
|
adamc@25
|
713 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
|
adamc@25
|
714 NONE)
|
adamc@25
|
715 in
|
adamc@25
|
716 case d of
|
adamc@25
|
717 L.DCon _ => NONE
|
adamc@164
|
718 | L.DDatatype (x, n, xncs) =>
|
adamc@164
|
719 let
|
adamc@164
|
720 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env) to)) xncs), loc)
|
adamc@164
|
721 in
|
adamc@179
|
722 SOME (Env.declBinds env all, fm, d)
|
adamc@164
|
723 end
|
adamc@179
|
724 | L.DVal (x, n, t, e, s) =>
|
adamc@179
|
725 let
|
adamc@179
|
726 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
727 in
|
adamc@179
|
728 SOME (Env.pushENamed env x n t NONE s,
|
adamc@179
|
729 fm,
|
adamc@179
|
730 (L'.DVal (x, n, monoType env t, e, s), loc))
|
adamc@179
|
731 end
|
adamc@128
|
732 | L.DValRec vis =>
|
adamc@128
|
733 let
|
adamc@128
|
734 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
|
adamc@179
|
735
|
adamc@179
|
736 val (vis, fm) = ListUtil.foldlMap
|
adamc@179
|
737 (fn ((x, n, t, e, s), fm) =>
|
adamc@179
|
738 let
|
adamc@179
|
739 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
740 in
|
adamc@179
|
741 ((x, n, monoType env t, e, s), fm)
|
adamc@179
|
742 end)
|
adamc@179
|
743 fm vis
|
adamc@128
|
744 in
|
adamc@128
|
745 SOME (env,
|
adamc@179
|
746 fm,
|
adamc@179
|
747 (L'.DValRec vis, loc))
|
adamc@128
|
748 end
|
adamc@144
|
749 | L.DExport (ek, n) =>
|
adamc@115
|
750 let
|
adamc@120
|
751 val (_, t, _, s) = Env.lookupENamed env n
|
adamc@120
|
752
|
adamc@120
|
753 fun unwind (t, _) =
|
adamc@120
|
754 case t of
|
adamc@120
|
755 L.TFun (dom, ran) => dom :: unwind ran
|
adamc@120
|
756 | _ => []
|
adamc@120
|
757
|
adamc@120
|
758 val ts = map (monoType env) (unwind t)
|
adamc@115
|
759 in
|
adamc@179
|
760 SOME (env, fm, (L'.DExport (ek, s, n, ts), loc))
|
adamc@115
|
761 end
|
adamc@25
|
762 end
|
adamc@25
|
763
|
adamc@25
|
764 fun monoize env ds =
|
adamc@25
|
765 let
|
adamc@179
|
766 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
|
adamc@179
|
767 case monoDecl (env, fm) d of
|
adamc@179
|
768 NONE => (env, fm, ds)
|
adamc@179
|
769 | SOME (env, fm, d) =>
|
adamc@179
|
770 (env,
|
adamc@179
|
771 Fm.enter fm,
|
adamc@179
|
772 d :: Fm.decls fm @ ds))
|
adamc@179
|
773 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
|
adamc@25
|
774 in
|
adamc@25
|
775 rev ds
|
adamc@25
|
776 end
|
adamc@25
|
777
|
adamc@25
|
778 end
|