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