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