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