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@385
|
38 val urlPrefix = ref "/"
|
adamc@385
|
39
|
adamc@196
|
40 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
|
adamc@25
|
41
|
adamc@252
|
42 structure U = MonoUtil
|
adamc@252
|
43
|
adamc@252
|
44 val liftExpInExp =
|
adamc@252
|
45 U.Exp.mapB {typ = fn t => t,
|
adamc@252
|
46 exp = fn bound => fn e =>
|
adamc@252
|
47 case e of
|
adamc@252
|
48 L'.ERel xn =>
|
adamc@252
|
49 if xn < bound then
|
adamc@252
|
50 e
|
adamc@252
|
51 else
|
adamc@252
|
52 L'.ERel (xn + 1)
|
adamc@252
|
53 | _ => e,
|
adamc@252
|
54 bind = fn (bound, U.Exp.RelE _) => bound + 1
|
adamc@252
|
55 | (bound, _) => bound}
|
adamc@252
|
56
|
adamc@25
|
57 fun monoName env (all as (c, loc)) =
|
adamc@25
|
58 let
|
adamc@25
|
59 fun poly () =
|
adamc@25
|
60 (E.errorAt loc "Unsupported name constructor";
|
adamc@25
|
61 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@25
|
62 "")
|
adamc@25
|
63 in
|
adamc@25
|
64 case c of
|
adamc@25
|
65 L.CName s => s
|
adamc@25
|
66 | _ => poly ()
|
adamc@25
|
67 end
|
adamc@25
|
68
|
adamc@292
|
69 fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
|
adamc@292
|
70 (L'.TOption t, loc)), loc)
|
adamc@292
|
71 fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
|
adamc@292
|
72 t), loc)
|
adamc@292
|
73 fun readType (t, loc) =
|
adamc@292
|
74 (L'.TRecord [("Read", readType' (t, loc)),
|
adamc@292
|
75 ("ReadError", readErrType (t, loc))],
|
adamc@292
|
76 loc)
|
adamc@292
|
77
|
adamc@196
|
78 fun monoType env =
|
adamc@25
|
79 let
|
adamc@196
|
80 fun mt env dtmap (all as (c, loc)) =
|
adamc@196
|
81 let
|
adamc@196
|
82 fun poly () =
|
adamc@196
|
83 (E.errorAt loc "Unsupported type constructor";
|
adamc@196
|
84 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@196
|
85 dummyTyp)
|
adamc@196
|
86 in
|
adamc@196
|
87 case c of
|
adamc@196
|
88 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
|
adamc@196
|
89 | L.TCFun _ => poly ()
|
adamc@196
|
90 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
|
adamc@196
|
91 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
|
adamc@196
|
92 | L.TRecord _ => poly ()
|
adamc@196
|
93
|
adamc@288
|
94 | L.CApp ((L.CFfi ("Basis", "option"), _), t) =>
|
adamc@288
|
95 (L'.TOption (mt env dtmap t), loc)
|
adamc@288
|
96
|
adamc@387
|
97 | L.CApp ((L.CFfi ("Basis", "eq"), _), t) =>
|
adamc@387
|
98 let
|
adamc@387
|
99 val t = mt env dtmap t
|
adamc@387
|
100 in
|
adamc@387
|
101 (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)
|
adamc@387
|
102 end
|
adamc@286
|
103 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
|
adamc@286
|
104 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
|
adamc@290
|
105 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
|
adamc@292
|
106 readType (mt env dtmap t, loc)
|
adamc@286
|
107
|
adamc@196
|
108 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
|
adamc@196
|
109 (L'.TFfi ("Basis", "string"), loc)
|
adamc@196
|
110 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
|
adamc@196
|
111 (L'.TFfi ("Basis", "string"), loc)
|
adamc@196
|
112
|
adamc@251
|
113 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
|
adamc@252
|
114 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
|
adamc@252
|
115 | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) =>
|
adamc@252
|
116 (L'.TFfi ("Basis", "string"), loc)
|
adamc@338
|
117 | L.CFfi ("Basis", "sql_sequence") =>
|
adamc@338
|
118 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
119 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
|
adamc@252
|
120 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
121 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
|
adamc@252
|
122 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
123 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
|
adamc@252
|
124 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
125
|
adamc@252
|
126 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
|
adamc@252
|
127 (L'.TRecord [], loc)
|
adamc@252
|
128 | L.CFfi ("Basis", "sql_relop") =>
|
adamc@252
|
129 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
130 | L.CFfi ("Basis", "sql_direction") =>
|
adamc@252
|
131 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
132 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
|
adamc@252
|
133 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
134 | L.CFfi ("Basis", "sql_limit") =>
|
adamc@252
|
135 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
136 | L.CFfi ("Basis", "sql_offset") =>
|
adamc@252
|
137 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
138
|
adamc@252
|
139 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
|
adamc@252
|
140 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
|
adamc@252
|
141 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
|
adamc@252
|
142 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
143 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
|
adamc@252
|
144 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
145 | L.CFfi ("Basis", "sql_comparison") =>
|
adamc@252
|
146 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
147 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
|
adamc@252
|
148 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
149 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
|
adamc@252
|
150 (L'.TRecord [], loc)
|
adamc@252
|
151 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
|
adamc@252
|
152 (L'.TRecord [], loc)
|
adamc@251
|
153
|
adamc@196
|
154 | L.CRel _ => poly ()
|
adamc@196
|
155 | L.CNamed n =>
|
adamc@196
|
156 (case IM.find (dtmap, n) of
|
adamc@196
|
157 SOME r => (L'.TDatatype (n, r), loc)
|
adamc@196
|
158 | NONE =>
|
adamc@196
|
159 let
|
adamc@196
|
160 val r = ref (L'.Default, [])
|
adamc@196
|
161 val (_, xs, xncs) = Env.lookupDatatype env n
|
adamc@196
|
162
|
adamc@196
|
163 val dtmap' = IM.insert (dtmap, n, r)
|
adamc@196
|
164
|
adamc@196
|
165 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
|
adamc@196
|
166 in
|
adamc@196
|
167 case xs of
|
adamc@198
|
168 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
|
adamc@196
|
169 (L'.TDatatype (n, r), loc))
|
adamc@196
|
170 | _ => poly ()
|
adamc@196
|
171 end)
|
adamc@196
|
172 | L.CFfi mx => (L'.TFfi mx, loc)
|
adamc@196
|
173 | L.CApp _ => poly ()
|
adamc@196
|
174 | L.CAbs _ => poly ()
|
adamc@196
|
175
|
adamc@196
|
176 | L.CName _ => poly ()
|
adamc@196
|
177
|
adamc@196
|
178 | L.CRecord _ => poly ()
|
adamc@196
|
179 | L.CConcat _ => poly ()
|
adamc@196
|
180 | L.CFold _ => poly ()
|
adamc@196
|
181 | L.CUnit => poly ()
|
adamc@214
|
182
|
adamc@214
|
183 | L.CTuple _ => poly ()
|
adamc@214
|
184 | L.CProj _ => poly ()
|
adamc@196
|
185 end
|
adamc@25
|
186 in
|
adamc@196
|
187 mt env IM.empty
|
adamc@25
|
188 end
|
adamc@25
|
189
|
adamc@25
|
190 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
|
adamc@25
|
191
|
adamc@179
|
192 structure IM = IntBinaryMap
|
adamc@179
|
193
|
adamc@179
|
194 datatype foo_kind =
|
adamc@179
|
195 Attr
|
adamc@179
|
196 | Url
|
adamc@179
|
197
|
adamc@179
|
198 fun fk2s fk =
|
adamc@179
|
199 case fk of
|
adamc@179
|
200 Attr => "attr"
|
adamc@179
|
201 | Url => "url"
|
adamc@179
|
202
|
adamc@179
|
203 structure Fm :> sig
|
adamc@179
|
204 type t
|
adamc@179
|
205
|
adamc@179
|
206 val empty : int -> t
|
adamc@179
|
207
|
adamc@179
|
208 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
|
adamc@179
|
209 val enter : t -> t
|
adamc@179
|
210 val decls : t -> L'.decl list
|
adamc@179
|
211 end = struct
|
adamc@179
|
212
|
adamc@179
|
213 structure M = BinaryMapFn(struct
|
adamc@179
|
214 type ord_key = foo_kind
|
adamc@179
|
215 fun compare x =
|
adamc@179
|
216 case x of
|
adamc@179
|
217 (Attr, Attr) => EQUAL
|
adamc@179
|
218 | (Attr, _) => LESS
|
adamc@179
|
219 | (_, Attr) => GREATER
|
adamc@179
|
220
|
adamc@179
|
221 | (Url, Url) => EQUAL
|
adamc@179
|
222 end)
|
adamc@179
|
223
|
adamc@179
|
224 type t = {
|
adamc@179
|
225 count : int,
|
adamc@179
|
226 map : int IM.map M.map,
|
adamc@179
|
227 decls : L'.decl list
|
adamc@179
|
228 }
|
adamc@179
|
229
|
adamc@179
|
230 fun empty count = {
|
adamc@179
|
231 count = count,
|
adamc@179
|
232 map = M.empty,
|
adamc@179
|
233 decls = []
|
adamc@179
|
234 }
|
adamc@179
|
235
|
adamc@179
|
236 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
|
adamc@179
|
237 fun decls ({decls, ...} : t) = decls
|
adamc@179
|
238
|
adamc@179
|
239 fun lookup (t as {count, map, decls}) k n thunk =
|
adamc@120
|
240 let
|
adamc@179
|
241 val im = Option.getOpt (M.find (map, k), IM.empty)
|
adamc@179
|
242 in
|
adamc@179
|
243 case IM.find (im, n) of
|
adamc@179
|
244 NONE =>
|
adamc@179
|
245 let
|
adamc@179
|
246 val n' = count
|
adamc@179
|
247 val (d, {count, map, decls}) = thunk count {count = count + 1,
|
adamc@179
|
248 map = M.insert (map, k, IM.insert (im, n, n')),
|
adamc@179
|
249 decls = decls}
|
adamc@179
|
250 in
|
adamc@179
|
251 ({count = count,
|
adamc@179
|
252 map = map,
|
adamc@179
|
253 decls = d :: decls}, n')
|
adamc@179
|
254 end
|
adamc@179
|
255 | SOME n' => (t, n')
|
adamc@179
|
256 end
|
adamc@179
|
257
|
adamc@179
|
258 end
|
adamc@185
|
259
|
adamc@185
|
260
|
adamc@185
|
261 fun capitalize s =
|
adamc@185
|
262 if s = "" then
|
adamc@185
|
263 s
|
adamc@185
|
264 else
|
adamc@185
|
265 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@179
|
266
|
adamc@179
|
267 fun fooifyExp fk env =
|
adamc@179
|
268 let
|
adamc@179
|
269 fun fooify fm (e, tAll as (t, loc)) =
|
adamc@120
|
270 case #1 e of
|
adamc@120
|
271 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
|
adamc@120
|
272 let
|
adamc@120
|
273 val (_, _, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
274 in
|
adamc@385
|
275 ((L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
|
adamc@120
|
276 end
|
adamc@120
|
277 | L'.EClosure (fnam, args) =>
|
adamc@120
|
278 let
|
adamc@120
|
279 val (_, ft, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
280 val ft = monoType env ft
|
adamc@111
|
281
|
adamc@179
|
282 fun attrify (args, ft, e, fm) =
|
adamc@120
|
283 case (args, ft) of
|
adamc@179
|
284 ([], _) => (e, fm)
|
adamc@120
|
285 | (arg :: args, (L'.TFun (t, ft), _)) =>
|
adamc@179
|
286 let
|
adamc@179
|
287 val (arg', fm) = fooify fm (arg, t)
|
adamc@179
|
288 in
|
adamc@179
|
289 attrify (args, ft,
|
adamc@179
|
290 (L'.EStrcat (e,
|
adamc@179
|
291 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
|
adamc@179
|
292 arg'), loc)), loc),
|
adamc@179
|
293 fm)
|
adamc@179
|
294 end
|
adamc@120
|
295 | _ => (E.errorAt loc "Type mismatch encoding attribute";
|
adamc@179
|
296 (e, fm))
|
adamc@120
|
297 in
|
adamc@385
|
298 attrify (args, ft, (L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
|
adamc@120
|
299 end
|
adamc@120
|
300 | _ =>
|
adamc@120
|
301 case t of
|
adamc@185
|
302 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
|
adamc@200
|
303
|
adamc@179
|
304 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@200
|
305 | L'.TRecord ((x, t) :: xts) =>
|
adamc@200
|
306 let
|
adamc@200
|
307 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
|
adamc@200
|
308 in
|
adamc@200
|
309 foldl (fn ((x, t), (se, fm)) =>
|
adamc@200
|
310 let
|
adamc@200
|
311 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
|
adamc@200
|
312 in
|
adamc@200
|
313 ((L'.EStrcat (se,
|
adamc@200
|
314 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
|
adamc@200
|
315 se'), loc)), loc),
|
adamc@200
|
316 fm)
|
adamc@200
|
317 end) (se, fm) xts
|
adamc@200
|
318 end
|
adamc@111
|
319
|
adamc@196
|
320 | L'.TDatatype (i, ref (dk, _)) =>
|
adamc@179
|
321 let
|
adamc@179
|
322 fun makeDecl n fm =
|
adamc@179
|
323 let
|
adamc@193
|
324 val (x, _, xncs) = Env.lookupDatatype env i
|
adamc@179
|
325
|
adamc@179
|
326 val (branches, fm) =
|
adamc@179
|
327 ListUtil.foldlMap
|
adamc@179
|
328 (fn ((x, n, to), fm) =>
|
adamc@179
|
329 case to of
|
adamc@179
|
330 NONE =>
|
adamc@188
|
331 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
|
adamc@179
|
332 (L'.EPrim (Prim.String x), loc)),
|
adamc@179
|
333 fm)
|
adamc@179
|
334 | SOME t =>
|
adamc@179
|
335 let
|
adamc@182
|
336 val t = monoType env t
|
adamc@182
|
337 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
|
adamc@179
|
338 in
|
adamc@188
|
339 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
|
adamc@179
|
340 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
|
adamc@179
|
341 arg), loc)),
|
adamc@179
|
342 fm)
|
adamc@179
|
343 end)
|
adamc@179
|
344 fm xncs
|
adamc@179
|
345
|
adamc@179
|
346 val dom = tAll
|
adamc@179
|
347 val ran = (L'.TFfi ("Basis", "string"), loc)
|
adamc@179
|
348 in
|
adamc@179
|
349 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
|
adamc@179
|
350 n,
|
adamc@179
|
351 (L'.TFun (dom, ran), loc),
|
adamc@179
|
352 (L'.EAbs ("x",
|
adamc@179
|
353 dom,
|
adamc@179
|
354 ran,
|
adamc@179
|
355 (L'.ECase ((L'.ERel 0, loc),
|
adamc@179
|
356 branches,
|
adamc@182
|
357 {disc = dom,
|
adamc@182
|
358 result = ran}), loc)), loc),
|
adamc@179
|
359 "")], loc),
|
adamc@179
|
360 fm)
|
adamc@179
|
361 end
|
adamc@179
|
362
|
adamc@179
|
363 val (fm, n) = Fm.lookup fm fk i makeDecl
|
adamc@179
|
364 in
|
adamc@179
|
365 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
|
adamc@179
|
366 end
|
adamc@164
|
367
|
adamc@120
|
368 | _ => (E.errorAt loc "Don't know how to encode attribute type";
|
adamc@120
|
369 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
|
adamc@179
|
370 (dummyExp, fm))
|
adamc@120
|
371 in
|
adamc@120
|
372 fooify
|
adamc@120
|
373 end
|
adamc@120
|
374
|
adamc@179
|
375 val attrifyExp = fooifyExp Attr
|
adamc@179
|
376 val urlifyExp = fooifyExp Url
|
adamc@105
|
377
|
adamc@143
|
378 datatype 'a failable_search =
|
adamc@143
|
379 Found of 'a
|
adamc@143
|
380 | NotFound
|
adamc@143
|
381 | Error
|
adamc@143
|
382
|
adamc@153
|
383 structure St :> sig
|
adamc@153
|
384 type t
|
adamc@153
|
385
|
adamc@153
|
386 val empty : t
|
adamc@153
|
387
|
adamc@153
|
388 val radioGroup : t -> string option
|
adamc@153
|
389 val setRadioGroup : t * string -> t
|
adamc@153
|
390 end = struct
|
adamc@153
|
391
|
adamc@153
|
392 type t = {
|
adamc@153
|
393 radioGroup : string option
|
adamc@153
|
394 }
|
adamc@153
|
395
|
adamc@153
|
396 val empty = {radioGroup = NONE}
|
adamc@153
|
397
|
adamc@153
|
398 fun radioGroup (t : t) = #radioGroup t
|
adamc@153
|
399
|
adamc@153
|
400 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
|
adamc@153
|
401
|
adamc@153
|
402 end
|
adamc@153
|
403
|
adamc@186
|
404 fun monoPatCon env pc =
|
adamc@178
|
405 case pc of
|
adamc@178
|
406 L.PConVar n => L'.PConVar n
|
adamc@188
|
407 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
|
adamc@188
|
408 arg = Option.map (monoType env) arg}
|
adamc@178
|
409
|
adamc@193
|
410 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
|
adamc@193
|
411
|
adamc@193
|
412 fun monoPat env (all as (p, loc)) =
|
adamc@193
|
413 let
|
adamc@193
|
414 fun poly () =
|
adamc@193
|
415 (E.errorAt loc "Unsupported pattern";
|
adamc@193
|
416 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
|
adamc@193
|
417 dummyPat)
|
adamc@193
|
418 in
|
adamc@193
|
419 case p of
|
adamc@193
|
420 L.PWild => (L'.PWild, loc)
|
adamc@193
|
421 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
|
adamc@193
|
422 | L.PPrim p => (L'.PPrim p, loc)
|
adamc@193
|
423 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
|
adamc@288
|
424 | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc)
|
adamc@288
|
425 | L.PCon (L.Option, _, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc)
|
adamc@193
|
426 | L.PCon _ => poly ()
|
adamc@193
|
427 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
|
adamc@193
|
428 end
|
adamc@178
|
429
|
adamc@252
|
430 fun strcat loc es =
|
adamc@252
|
431 case es of
|
adamc@252
|
432 [] => (L'.EPrim (Prim.String ""), loc)
|
adamc@252
|
433 | [e] => e
|
adamc@252
|
434 | _ =>
|
adamc@252
|
435 let
|
adamc@252
|
436 val e2 = List.last es
|
adamc@252
|
437 val es = List.take (es, length es - 1)
|
adamc@252
|
438 val e1 = List.last es
|
adamc@252
|
439 val es = List.take (es, length es - 1)
|
adamc@252
|
440 in
|
adamc@252
|
441 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
|
adamc@252
|
442 (L'.EStrcat (e1, e2), loc) es
|
adamc@252
|
443 end
|
adamc@252
|
444
|
adamc@252
|
445 fun strcatComma loc es =
|
adamc@252
|
446 case es of
|
adamc@252
|
447 [] => (L'.EPrim (Prim.String ""), loc)
|
adamc@252
|
448 | [e] => e
|
adamc@252
|
449 | _ =>
|
adamc@252
|
450 let
|
adamc@252
|
451 val e1 = List.last es
|
adamc@252
|
452 val es = List.take (es, length es - 1)
|
adamc@252
|
453 in
|
adamc@252
|
454 foldr (fn (e, e') =>
|
adamc@265
|
455 case (e, e') of
|
adamc@265
|
456 ((L'.EPrim (Prim.String ""), _), _) => e'
|
adamc@265
|
457 | (_, (L'.EPrim (Prim.String ""), _)) => e
|
adamc@252
|
458 | _ =>
|
adamc@252
|
459 (L'.EStrcat (e,
|
adamc@252
|
460 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
|
adamc@252
|
461 e1 es
|
adamc@252
|
462 end
|
adamc@252
|
463
|
adamc@252
|
464 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
|
adamc@252
|
465
|
adamc@179
|
466 fun monoExp (env, st, fm) (all as (e, loc)) =
|
adamc@25
|
467 let
|
adamc@25
|
468 fun poly () =
|
adamc@25
|
469 (E.errorAt loc "Unsupported expression";
|
adamc@25
|
470 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
|
adamc@179
|
471 (dummyExp, fm))
|
adamc@25
|
472 in
|
adamc@25
|
473 case e of
|
adamc@179
|
474 L.EPrim p => ((L'.EPrim p, loc), fm)
|
adamc@179
|
475 | L.ERel n => ((L'.ERel n, loc), fm)
|
adamc@179
|
476 | L.ENamed n => ((L'.ENamed n, loc), fm)
|
adamc@193
|
477 | L.ECon (dk, pc, [], eo) =>
|
adamc@193
|
478 let
|
adamc@179
|
479 val (eo, fm) =
|
adamc@179
|
480 case eo of
|
adamc@179
|
481 NONE => (NONE, fm)
|
adamc@179
|
482 | SOME e =>
|
adamc@179
|
483 let
|
adamc@179
|
484 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
485 in
|
adamc@179
|
486 (SOME e, fm)
|
adamc@179
|
487 end
|
adamc@179
|
488 in
|
adamc@188
|
489 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
|
adamc@193
|
490 end
|
adamc@297
|
491 | L.ECon (L.Option, _, [t], NONE) =>
|
adamc@297
|
492 ((L'.ENone (monoType env t), loc), fm)
|
adamc@297
|
493 | L.ECon (L.Option, _, [t], SOME e) =>
|
adamc@297
|
494 let
|
adamc@297
|
495 val (e, fm) = monoExp (env, st, fm) e
|
adamc@297
|
496 in
|
adamc@297
|
497 ((L'.ESome (monoType env t, e), loc), fm)
|
adamc@297
|
498 end
|
adamc@193
|
499 | L.ECon _ => poly ()
|
adamc@94
|
500
|
adamc@387
|
501 | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) =>
|
adamc@387
|
502 let
|
adamc@387
|
503 val t = monoType env t
|
adamc@387
|
504 val b = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@387
|
505 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
|
adamc@387
|
506 in
|
adamc@387
|
507 ((L'.EAbs ("f", dom, dom,
|
adamc@387
|
508 (L'.ERel 0, loc)), loc), fm)
|
adamc@387
|
509 end
|
adamc@387
|
510 | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) =>
|
adamc@387
|
511 let
|
adamc@387
|
512 val t = monoType env t
|
adamc@387
|
513 val b = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@387
|
514 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
|
adamc@387
|
515 in
|
adamc@387
|
516 ((L'.EAbs ("f", dom, dom,
|
adamc@387
|
517 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
|
adamc@387
|
518 (L'.EAbs ("y", t, b,
|
adamc@387
|
519 (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc),
|
adamc@387
|
520 (L'.ERel 1, loc)), loc),
|
adamc@387
|
521 (L'.ERel 0, loc)), loc)), loc)),
|
adamc@387
|
522 loc)),
|
adamc@387
|
523 loc)),
|
adamc@387
|
524 loc), fm)
|
adamc@387
|
525 end
|
adamc@387
|
526 | L.EFfi ("Basis", "eq_int") =>
|
adamc@387
|
527 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
|
adamc@387
|
528 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@387
|
529 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
|
adamc@387
|
530 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@387
|
531 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
|
adamc@387
|
532 fm)
|
adamc@387
|
533
|
adamc@286
|
534 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
|
adamc@286
|
535 let
|
adamc@286
|
536 val t = monoType env t
|
adamc@286
|
537 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@286
|
538 in
|
adamc@286
|
539 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
|
adamc@286
|
540 (L'.ERel 0, loc)), loc), fm)
|
adamc@286
|
541 end
|
adamc@286
|
542 | L.EFfi ("Basis", "show_int") =>
|
adamc@286
|
543 ((L'.EFfi ("Basis", "intToString"), loc), fm)
|
adamc@286
|
544 | L.EFfi ("Basis", "show_float") =>
|
adamc@286
|
545 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
|
adamc@286
|
546 | L.EFfi ("Basis", "show_string") =>
|
adamc@286
|
547 let
|
adamc@286
|
548 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@286
|
549 in
|
adamc@286
|
550 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
|
adamc@286
|
551 end
|
adamc@286
|
552 | L.EFfi ("Basis", "show_bool") =>
|
adamc@286
|
553 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
|
adamc@286
|
554
|
adamc@290
|
555 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
|
adamc@290
|
556 let
|
adamc@290
|
557 val t = monoType env t
|
adamc@290
|
558 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@290
|
559 in
|
adamc@292
|
560 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
|
adamc@292
|
561 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
|
adamc@292
|
562 end
|
adamc@292
|
563 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
|
adamc@292
|
564 let
|
adamc@292
|
565 val t = monoType env t
|
adamc@292
|
566 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@292
|
567 in
|
adamc@292
|
568 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
|
adamc@292
|
569 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
|
adamc@290
|
570 end
|
adamc@290
|
571 | L.EFfi ("Basis", "read_int") =>
|
adamc@292
|
572 let
|
adamc@292
|
573 val t = (L'.TFfi ("Basis", "int"), loc)
|
adamc@292
|
574 in
|
adamc@292
|
575 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
|
adamc@292
|
576 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
|
adamc@292
|
577 loc),
|
adamc@292
|
578 fm)
|
adamc@292
|
579 end
|
adamc@290
|
580 | L.EFfi ("Basis", "read_float") =>
|
adamc@292
|
581 let
|
adamc@292
|
582 val t = (L'.TFfi ("Basis", "float"), loc)
|
adamc@292
|
583 in
|
adamc@292
|
584 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
|
adamc@292
|
585 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
|
adamc@292
|
586 loc),
|
adamc@292
|
587 fm)
|
adamc@292
|
588 end
|
adamc@290
|
589 | L.EFfi ("Basis", "read_string") =>
|
adamc@290
|
590 let
|
adamc@290
|
591 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@290
|
592 in
|
adamc@292
|
593 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
|
adamc@292
|
594 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
|
adamc@292
|
595 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
|
adamc@292
|
596 fm)
|
adamc@290
|
597 end
|
adamc@290
|
598 | L.EFfi ("Basis", "read_bool") =>
|
adamc@292
|
599 let
|
adamc@292
|
600 val t = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@292
|
601 in
|
adamc@292
|
602 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
|
adamc@292
|
603 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
|
adamc@292
|
604 loc),
|
adamc@292
|
605 fm)
|
adamc@292
|
606 end
|
adamc@290
|
607
|
adamc@251
|
608 | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
|
adamc@252
|
609 let
|
adamc@252
|
610 val t = monoType env t
|
adamc@252
|
611 in
|
adamc@252
|
612 ((L'.EAbs ("x", t,
|
adamc@252
|
613 (L'.TFun ((L'.TRecord [], loc), t), loc),
|
adamc@252
|
614 (L'.EAbs ("_", (L'.TRecord [], loc), t,
|
adamc@252
|
615 (L'.ERel 1, loc)), loc)), loc), fm)
|
adamc@252
|
616 end
|
adamc@251
|
617 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), t1), _), t2) =>
|
adamc@251
|
618 let
|
adamc@251
|
619 val t1 = monoType env t1
|
adamc@251
|
620 val t2 = monoType env t2
|
adamc@251
|
621 val un = (L'.TRecord [], loc)
|
adamc@252
|
622 val mt1 = (L'.TFun (un, t1), loc)
|
adamc@252
|
623 val mt2 = (L'.TFun (un, t2), loc)
|
adamc@251
|
624 in
|
adamc@252
|
625 ((L'.EAbs ("m1", mt1, (L'.TFun (mt1, (L'.TFun (mt2, (L'.TFun (un, un), loc)), loc)), loc),
|
adamc@252
|
626 (L'.EAbs ("m2", mt2, (L'.TFun (un, un), loc),
|
adamc@252
|
627 (L'.EAbs ("_", un, un,
|
adamc@252
|
628 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
|
adamc@252
|
629 (L'.ERecord [], loc)), loc),
|
adamc@252
|
630 (L'.EApp (
|
adamc@252
|
631 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
|
adamc@252
|
632 (L'.ERecord [], loc)),
|
adamc@252
|
633 loc)), loc)), loc)), loc)), loc),
|
adamc@251
|
634 fm)
|
adamc@251
|
635 end
|
adamc@251
|
636
|
adamc@307
|
637 | L.EFfiApp ("Basis", "dml", [e]) =>
|
adamc@307
|
638 let
|
adamc@307
|
639 val (e, fm) = monoExp (env, st, fm) e
|
adamc@307
|
640 val un = (L'.TRecord [], loc)
|
adamc@307
|
641 in
|
adamc@307
|
642 ((L'.EAbs ("_", un, un,
|
adamc@307
|
643 (L'.EDml (liftExpInExp 0 e), loc)), loc),
|
adamc@307
|
644 fm)
|
adamc@307
|
645 end
|
adamc@308
|
646
|
adamc@307
|
647 | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) =>
|
adamc@307
|
648 (case monoType env (L.TRecord fields, loc) of
|
adamc@307
|
649 (L'.TRecord fields, _) =>
|
adamc@307
|
650 let
|
adamc@307
|
651 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@307
|
652 val fields = map (fn (x, _) => (x, s)) fields
|
adamc@307
|
653 val rt = (L'.TRecord fields, loc)
|
adamc@307
|
654 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@307
|
655 in
|
adamc@307
|
656 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
|
adamc@307
|
657 (L'.EAbs ("fs", rt, s,
|
adamc@307
|
658 strcat loc [sc "INSERT INTO ",
|
adamc@307
|
659 (L'.ERel 1, loc),
|
adamc@307
|
660 sc " (",
|
adamc@311
|
661 strcatComma loc (map (fn (x, _) => sc ("uw_" ^ x)) fields),
|
adamc@307
|
662 sc ") VALUES (",
|
adamc@307
|
663 strcatComma loc (map (fn (x, _) =>
|
adamc@307
|
664 (L'.EField ((L'.ERel 0, loc),
|
adamc@307
|
665 x), loc)) fields),
|
adamc@307
|
666 sc ")"]), loc)), loc),
|
adamc@307
|
667 fm)
|
adamc@307
|
668 end
|
adamc@307
|
669 | _ => poly ())
|
adamc@307
|
670
|
adamc@308
|
671 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), changed), _), _) =>
|
adamc@308
|
672 (case monoType env (L.TRecord changed, loc) of
|
adamc@308
|
673 (L'.TRecord changed, _) =>
|
adamc@308
|
674 let
|
adamc@308
|
675 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@308
|
676 val changed = map (fn (x, _) => (x, s)) changed
|
adamc@308
|
677 val rt = (L'.TRecord changed, loc)
|
adamc@308
|
678 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@308
|
679 in
|
adamc@308
|
680 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@308
|
681 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
|
adamc@308
|
682 (L'.EAbs ("e", s, s,
|
adamc@308
|
683 strcat loc [sc "UPDATE ",
|
adamc@308
|
684 (L'.ERel 1, loc),
|
adamc@308
|
685 sc " AS T SET ",
|
adamc@308
|
686 strcatComma loc (map (fn (x, _) =>
|
adamc@311
|
687 strcat loc [sc ("uw_" ^ x
|
adamc@308
|
688 ^ " = "),
|
adamc@308
|
689 (L'.EField
|
adamc@308
|
690 ((L'.ERel 2,
|
adamc@308
|
691 loc),
|
adamc@308
|
692 x), loc)])
|
adamc@308
|
693 changed),
|
adamc@308
|
694 sc " WHERE ",
|
adamc@308
|
695 (L'.ERel 0, loc)]), loc)), loc)), loc),
|
adamc@308
|
696 fm)
|
adamc@308
|
697 end
|
adamc@308
|
698 | _ => poly ())
|
adamc@308
|
699
|
adamc@309
|
700 | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) =>
|
adamc@309
|
701 let
|
adamc@309
|
702 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@309
|
703 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@309
|
704 in
|
adamc@309
|
705 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
|
adamc@309
|
706 (L'.EAbs ("e", s, s,
|
adamc@309
|
707 strcat loc [sc "DELETE FROM ",
|
adamc@309
|
708 (L'.ERel 1, loc),
|
adamc@309
|
709 sc " AS T WHERE ",
|
adamc@309
|
710 (L'.ERel 0, loc)]), loc)), loc),
|
adamc@309
|
711 fm)
|
adamc@309
|
712 end
|
adamc@309
|
713
|
adamc@252
|
714 | L.ECApp (
|
adamc@252
|
715 (L.ECApp (
|
adamc@252
|
716 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
|
adamc@252
|
717 exps), _),
|
adamc@252
|
718 state) =>
|
adamc@252
|
719 (case monoType env (L.TRecord exps, loc) of
|
adamc@252
|
720 (L'.TRecord exps, _) =>
|
adamc@252
|
721 let
|
adamc@252
|
722 val tables = map (fn ((L.CName x, _), xts) =>
|
adamc@252
|
723 (case monoType env (L.TRecord xts, loc) of
|
adamc@252
|
724 (L'.TRecord xts, _) => SOME (x, xts)
|
adamc@252
|
725 | _ => NONE)
|
adamc@252
|
726 | _ => NONE) tables
|
adamc@252
|
727 in
|
adamc@252
|
728 if List.exists (fn x => x = NONE) tables then
|
adamc@252
|
729 poly ()
|
adamc@252
|
730 else
|
adamc@252
|
731 let
|
adamc@252
|
732 val tables = List.mapPartial (fn x => x) tables
|
adamc@252
|
733 val state = monoType env state
|
adamc@252
|
734 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
735 val un = (L'.TRecord [], loc)
|
adamc@252
|
736
|
adamc@252
|
737 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
|
adamc@252
|
738 val ft = (L'.TFun ((L'.TRecord rt, loc),
|
adamc@252
|
739 (L'.TFun (state,
|
adamc@252
|
740 (L'.TFun (un, state), loc)),
|
adamc@252
|
741 loc)), loc)
|
adamc@252
|
742
|
adamc@267
|
743 val body' = (L'.EApp (
|
adamc@267
|
744 (L'.EApp (
|
adamc@267
|
745 (L'.EApp ((L'.ERel 4, loc),
|
adamc@267
|
746 (L'.ERel 1, loc)), loc),
|
adamc@267
|
747 (L'.ERel 0, loc)), loc),
|
adamc@267
|
748 (L'.ERecord [], loc)), loc)
|
adamc@252
|
749
|
adamc@252
|
750 val body = (L'.EQuery {exps = exps,
|
adamc@252
|
751 tables = tables,
|
adamc@252
|
752 state = state,
|
adamc@252
|
753 query = (L'.ERel 3, loc),
|
adamc@252
|
754 body = body',
|
adamc@252
|
755 initial = (L'.ERel 1, loc)},
|
adamc@252
|
756 loc)
|
adamc@252
|
757 in
|
adamc@252
|
758 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
|
adamc@252
|
759 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
|
adamc@252
|
760 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
|
adamc@252
|
761 (L'.EAbs ("_", un, state,
|
adamc@252
|
762 body), loc)), loc)), loc)), loc), fm)
|
adamc@252
|
763 end
|
adamc@252
|
764 end
|
adamc@252
|
765 | _ => poly ())
|
adamc@252
|
766
|
adamc@252
|
767 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
|
adamc@252
|
768 let
|
adamc@252
|
769 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@252
|
770 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
771 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
|
adamc@252
|
772 in
|
adamc@252
|
773 ((L'.EAbs ("r",
|
adamc@252
|
774 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
|
adamc@252
|
775 s,
|
adamc@252
|
776 strcat loc [gf "Rows",
|
adamc@261
|
777 (L'.ECase (gf "OrderBy",
|
adamc@261
|
778 [((L'.PPrim (Prim.String ""), loc), sc ""),
|
adamc@261
|
779 ((L'.PWild, loc),
|
adamc@261
|
780 strcat loc [sc " ORDER BY ",
|
adamc@261
|
781 gf "OrderBy"])],
|
adamc@261
|
782 {disc = s, result = s}), loc),
|
adamc@252
|
783 gf "Limit",
|
adamc@252
|
784 gf "Offset"]), loc), fm)
|
adamc@252
|
785 end
|
adamc@252
|
786
|
adamc@252
|
787 | L.ECApp (
|
adamc@252
|
788 (L.ECApp (
|
adamc@252
|
789 (L.ECApp (
|
adamc@252
|
790 (L.ECApp (
|
adamc@252
|
791 (L.EFfi ("Basis", "sql_query1"), _),
|
adamc@252
|
792 (L.CRecord (_, tables), _)), _),
|
adamc@252
|
793 (L.CRecord (_, grouped), _)), _),
|
adamc@252
|
794 (L.CRecord (_, stables), _)), _),
|
adamc@252
|
795 sexps) =>
|
adamc@252
|
796 let
|
adamc@252
|
797 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@252
|
798 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
799 val un = (L'.TRecord [], loc)
|
adamc@252
|
800 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
|
adamc@252
|
801
|
adamc@252
|
802 fun doTables tables =
|
adamc@252
|
803 let
|
adamc@252
|
804 val tables = map (fn ((L.CName x, _), xts) =>
|
adamc@252
|
805 (case monoType env (L.TRecord xts, loc) of
|
adamc@252
|
806 (L'.TRecord xts, _) => SOME (x, xts)
|
adamc@252
|
807 | _ => NONE)
|
adamc@252
|
808 | _ => NONE) tables
|
adamc@252
|
809 in
|
adamc@252
|
810 if List.exists (fn x => x = NONE) tables then
|
adamc@252
|
811 NONE
|
adamc@252
|
812 else
|
adamc@260
|
813 let
|
adamc@260
|
814 val tables = List.mapPartial (fn x => x) tables
|
adamc@260
|
815 val tables = ListMergeSort.sort
|
adamc@260
|
816 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
|
adamc@260
|
817 tables
|
adamc@260
|
818 val tables = map (fn (x, xts) =>
|
adamc@260
|
819 (x, ListMergeSort.sort
|
adamc@260
|
820 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
|
adamc@260
|
821 xts)) tables
|
adamc@260
|
822 in
|
adamc@260
|
823 SOME tables
|
adamc@260
|
824 end
|
adamc@252
|
825 end
|
adamc@252
|
826 in
|
adamc@252
|
827 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
|
adamc@252
|
828 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
|
adamc@252
|
829 ((L'.EAbs ("r",
|
adamc@252
|
830 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
|
adamc@252
|
831 ("Where", s),
|
adamc@252
|
832 ("GroupBy", un),
|
adamc@252
|
833 ("Having", s),
|
adamc@252
|
834 ("SelectFields", un),
|
adamc@252
|
835 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
|
adamc@252
|
836 loc),
|
adamc@252
|
837 s,
|
adamc@252
|
838 strcat loc [sc "SELECT ",
|
adamc@261
|
839 strcatComma loc (map (fn (x, t) =>
|
adamc@261
|
840 strcat loc [
|
adamc@261
|
841 (L'.EField (gf "SelectExps", x), loc),
|
adamc@261
|
842 sc (" AS _" ^ x)
|
adamc@265
|
843 ]) sexps
|
adamc@265
|
844 @ map (fn (x, xts) =>
|
adamc@265
|
845 strcatComma loc
|
adamc@265
|
846 (map (fn (x', _) =>
|
adamc@311
|
847 sc (x ^ ".uw_" ^ x'))
|
adamc@265
|
848 xts)) stables),
|
adamc@252
|
849 sc " FROM ",
|
adamc@252
|
850 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
|
adamc@253
|
851 sc (" AS " ^ x)]) tables),
|
adamc@258
|
852 (L'.ECase (gf "Where",
|
adamc@258
|
853 [((L'.PPrim (Prim.String "TRUE"), loc),
|
adamc@258
|
854 sc ""),
|
adamc@258
|
855 ((L'.PWild, loc),
|
adamc@258
|
856 strcat loc [sc " WHERE ", gf "Where"])],
|
adamc@258
|
857 {disc = s,
|
adamc@258
|
858 result = s}), loc),
|
adamc@258
|
859
|
adamc@255
|
860 if List.all (fn (x, xts) =>
|
adamc@255
|
861 case List.find (fn (x', _) => x' = x) grouped of
|
adamc@255
|
862 NONE => List.null xts
|
adamc@255
|
863 | SOME (_, xts') =>
|
adamc@255
|
864 List.all (fn (x, _) =>
|
adamc@255
|
865 List.exists (fn (x', _) => x' = x)
|
adamc@255
|
866 xts') xts) tables then
|
adamc@255
|
867 sc ""
|
adamc@255
|
868 else
|
adamc@255
|
869 strcat loc [
|
adamc@255
|
870 sc " GROUP BY ",
|
adamc@255
|
871 strcatComma loc (map (fn (x, xts) =>
|
adamc@255
|
872 strcatComma loc
|
adamc@255
|
873 (map (fn (x', _) =>
|
adamc@311
|
874 sc (x ^ ".uw_" ^ x'))
|
adamc@255
|
875 xts)) grouped)
|
adamc@259
|
876 ],
|
adamc@259
|
877
|
adamc@259
|
878 (L'.ECase (gf "Having",
|
adamc@259
|
879 [((L'.PPrim (Prim.String "TRUE"), loc),
|
adamc@259
|
880 sc ""),
|
adamc@259
|
881 ((L'.PWild, loc),
|
adamc@259
|
882 strcat loc [sc " HAVING ", gf "Having"])],
|
adamc@259
|
883 {disc = s,
|
adamc@259
|
884 result = s}), loc)
|
adamc@252
|
885 ]), loc),
|
adamc@252
|
886 fm)
|
adamc@252
|
887 | _ => poly ()
|
adamc@252
|
888 end
|
adamc@252
|
889
|
adamc@252
|
890 | L.ECApp (
|
adamc@252
|
891 (L.ECApp (
|
adamc@252
|
892 (L.ECApp (
|
adamc@252
|
893 (L.ECApp (
|
adamc@252
|
894 (L.EFfi ("Basis", "sql_inject"), _),
|
adamc@252
|
895 _), _),
|
adamc@252
|
896 _), _),
|
adamc@252
|
897 _), _),
|
adamc@252
|
898 t) =>
|
adamc@252
|
899 let
|
adamc@252
|
900 val t = monoType env t
|
adamc@252
|
901 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
902 in
|
adamc@252
|
903 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
|
adamc@252
|
904 (L'.ERel 0, loc)), loc), fm)
|
adamc@252
|
905 end
|
adamc@252
|
906
|
adamc@253
|
907 | L.EFfi ("Basis", "sql_int") =>
|
adamc@253
|
908 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
909 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
910 fm)
|
adamc@253
|
911 | L.EFfi ("Basis", "sql_float") =>
|
adamc@253
|
912 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
913 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
914 fm)
|
adamc@253
|
915 | L.EFfi ("Basis", "sql_bool") =>
|
adamc@253
|
916 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
917 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
918 fm)
|
adamc@253
|
919 | L.EFfi ("Basis", "sql_string") =>
|
adamc@253
|
920 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
921 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
922 fm)
|
adamc@253
|
923
|
adamc@252
|
924 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
|
adamc@252
|
925 ((L'.ERecord [], loc), fm)
|
adamc@252
|
926 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
|
adamc@252
|
927 ((L'.ERecord [], loc), fm)
|
adamc@252
|
928
|
adamc@252
|
929 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
|
adamc@252
|
930 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@261
|
931 | L.ECApp (
|
adamc@261
|
932 (L.ECApp (
|
adamc@261
|
933 (L.ECApp (
|
adamc@261
|
934 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
|
adamc@261
|
935 _), _),
|
adamc@261
|
936 _), _),
|
adamc@261
|
937 _) =>
|
adamc@261
|
938 let
|
adamc@261
|
939 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@261
|
940 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@261
|
941 in
|
adamc@268
|
942 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@268
|
943 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
|
adamc@268
|
944 (L'.EAbs ("e2", s, s,
|
adamc@268
|
945 (L'.ECase ((L'.ERel 0, loc),
|
adamc@268
|
946 [((L'.PPrim (Prim.String ""), loc),
|
adamc@268
|
947 strcat loc [(L'.ERel 2, loc),
|
adamc@268
|
948 (L'.ERel 1, loc)]),
|
adamc@268
|
949 ((L'.PWild, loc),
|
adamc@268
|
950 strcat loc [(L'.ERel 2, loc),
|
adamc@268
|
951 (L'.ERel 1, loc),
|
adamc@268
|
952 sc ", ",
|
adamc@268
|
953 (L'.ERel 0, loc)])],
|
adamc@268
|
954 {disc = s, result = s}), loc)), loc)), loc)), loc),
|
adamc@261
|
955 fm)
|
adamc@261
|
956 end
|
adamc@252
|
957
|
adamc@252
|
958 | L.EFfi ("Basis", "sql_no_limit") =>
|
adamc@252
|
959 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@262
|
960 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
|
adamc@262
|
961 let
|
adamc@262
|
962 val (e, fm) = monoExp (env, st, fm) e
|
adamc@262
|
963 in
|
adamc@262
|
964 (strcat loc [
|
adamc@262
|
965 (L'.EPrim (Prim.String " LIMIT "), loc),
|
adamc@262
|
966 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
|
adamc@262
|
967 ],
|
adamc@262
|
968 fm)
|
adamc@262
|
969 end
|
adamc@262
|
970
|
adamc@252
|
971 | L.EFfi ("Basis", "sql_no_offset") =>
|
adamc@252
|
972 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@263
|
973 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
|
adamc@263
|
974 let
|
adamc@263
|
975 val (e, fm) = monoExp (env, st, fm) e
|
adamc@263
|
976 in
|
adamc@263
|
977 (strcat loc [
|
adamc@263
|
978 (L'.EPrim (Prim.String " OFFSET "), loc),
|
adamc@263
|
979 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
|
adamc@263
|
980 ],
|
adamc@263
|
981 fm)
|
adamc@263
|
982 end
|
adamc@253
|
983
|
adamc@253
|
984 | L.EFfi ("Basis", "sql_eq") =>
|
adamc@253
|
985 ((L'.EPrim (Prim.String "="), loc), fm)
|
adamc@253
|
986 | L.EFfi ("Basis", "sql_ne") =>
|
adamc@253
|
987 ((L'.EPrim (Prim.String "<>"), loc), fm)
|
adamc@253
|
988 | L.EFfi ("Basis", "sql_lt") =>
|
adamc@253
|
989 ((L'.EPrim (Prim.String "<"), loc), fm)
|
adamc@253
|
990 | L.EFfi ("Basis", "sql_le") =>
|
adamc@253
|
991 ((L'.EPrim (Prim.String "<="), loc), fm)
|
adamc@253
|
992 | L.EFfi ("Basis", "sql_gt") =>
|
adamc@253
|
993 ((L'.EPrim (Prim.String ">"), loc), fm)
|
adamc@253
|
994 | L.EFfi ("Basis", "sql_ge") =>
|
adamc@253
|
995 ((L'.EPrim (Prim.String ">="), loc), fm)
|
adamc@253
|
996
|
adamc@253
|
997 | L.ECApp (
|
adamc@253
|
998 (L.ECApp (
|
adamc@253
|
999 (L.ECApp (
|
adamc@253
|
1000 (L.ECApp (
|
adamc@254
|
1001 (L.ECApp (
|
adamc@264
|
1002 (L.EFfi ("Basis", "sql_unary"), _),
|
adamc@264
|
1003 _), _),
|
adamc@264
|
1004 _), _),
|
adamc@264
|
1005 _), _),
|
adamc@264
|
1006 _), _),
|
adamc@264
|
1007 _) =>
|
adamc@264
|
1008 let
|
adamc@264
|
1009 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@264
|
1010 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@264
|
1011 in
|
adamc@264
|
1012 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@264
|
1013 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@264
|
1014 strcat loc [sc "(",
|
adamc@264
|
1015 (L'.ERel 1, loc),
|
adamc@264
|
1016 sc " ",
|
adamc@264
|
1017 (L'.ERel 0, loc),
|
adamc@264
|
1018 sc ")"]), loc)), loc),
|
adamc@264
|
1019 fm)
|
adamc@264
|
1020 end
|
adamc@264
|
1021 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
|
adamc@264
|
1022
|
adamc@264
|
1023 | L.ECApp (
|
adamc@264
|
1024 (L.ECApp (
|
adamc@264
|
1025 (L.ECApp (
|
adamc@264
|
1026 (L.ECApp (
|
adamc@264
|
1027 (L.ECApp (
|
adamc@254
|
1028 (L.ECApp (
|
adamc@254
|
1029 (L.EFfi ("Basis", "sql_binary"), _),
|
adamc@254
|
1030 _), _),
|
adamc@254
|
1031 _), _),
|
adamc@254
|
1032 _), _),
|
adamc@254
|
1033 _), _),
|
adamc@254
|
1034 _), _),
|
adamc@254
|
1035 _) =>
|
adamc@254
|
1036 let
|
adamc@254
|
1037 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@254
|
1038 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@254
|
1039 in
|
adamc@254
|
1040 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@254
|
1041 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@254
|
1042 (L'.EAbs ("e2", s, s,
|
adamc@254
|
1043 strcat loc [sc "(",
|
adamc@254
|
1044 (L'.ERel 1, loc),
|
adamc@254
|
1045 sc " ",
|
adamc@254
|
1046 (L'.ERel 2, loc),
|
adamc@254
|
1047 sc " ",
|
adamc@254
|
1048 (L'.ERel 0, loc),
|
adamc@254
|
1049 sc ")"]), loc)), loc)), loc),
|
adamc@254
|
1050 fm)
|
adamc@254
|
1051 end
|
adamc@254
|
1052 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
|
adamc@254
|
1053 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
|
adamc@254
|
1054
|
adamc@254
|
1055 | L.ECApp (
|
adamc@254
|
1056 (L.ECApp (
|
adamc@254
|
1057 (L.ECApp (
|
adamc@254
|
1058 (L.ECApp (
|
adamc@253
|
1059 (L.EFfi ("Basis", "sql_comparison"), _),
|
adamc@253
|
1060 _), _),
|
adamc@253
|
1061 _), _),
|
adamc@253
|
1062 _), _),
|
adamc@253
|
1063 _) =>
|
adamc@253
|
1064 let
|
adamc@253
|
1065 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@253
|
1066 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@253
|
1067 in
|
adamc@253
|
1068 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@253
|
1069 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@253
|
1070 (L'.EAbs ("e2", s, s,
|
adamc@254
|
1071 strcat loc [sc "(",
|
adamc@254
|
1072 (L'.ERel 1, loc),
|
adamc@253
|
1073 sc " ",
|
adamc@253
|
1074 (L'.ERel 2, loc),
|
adamc@253
|
1075 sc " ",
|
adamc@254
|
1076 (L'.ERel 0, loc),
|
adamc@254
|
1077 sc ")"]), loc)), loc)), loc),
|
adamc@253
|
1078 fm)
|
adamc@253
|
1079 end
|
adamc@253
|
1080
|
adamc@253
|
1081 | L.ECApp (
|
adamc@253
|
1082 (L.ECApp (
|
adamc@253
|
1083 (L.ECApp (
|
adamc@253
|
1084 (L.ECApp (
|
adamc@253
|
1085 (L.ECApp (
|
adamc@253
|
1086 (L.ECApp (
|
adamc@253
|
1087 (L.ECApp (
|
adamc@253
|
1088 (L.EFfi ("Basis", "sql_field"), _),
|
adamc@253
|
1089 _), _),
|
adamc@253
|
1090 _), _),
|
adamc@253
|
1091 _), _),
|
adamc@253
|
1092 _), _),
|
adamc@253
|
1093 _), _),
|
adamc@253
|
1094 (L.CName tab, _)), _),
|
adamc@311
|
1095 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
|
adamc@260
|
1096
|
adamc@260
|
1097 | L.ECApp (
|
adamc@260
|
1098 (L.ECApp (
|
adamc@260
|
1099 (L.ECApp (
|
adamc@260
|
1100 (L.ECApp (
|
adamc@261
|
1101 (L.ECApp (
|
adamc@261
|
1102 (L.EFfi ("Basis", "sql_exp"), _),
|
adamc@261
|
1103 _), _),
|
adamc@261
|
1104 _), _),
|
adamc@261
|
1105 _), _),
|
adamc@261
|
1106 _), _),
|
adamc@261
|
1107 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
|
adamc@261
|
1108
|
adamc@261
|
1109 | L.ECApp (
|
adamc@261
|
1110 (L.ECApp (
|
adamc@261
|
1111 (L.ECApp (
|
adamc@261
|
1112 (L.ECApp (
|
adamc@260
|
1113 (L.EFfi ("Basis", "sql_relop"), _),
|
adamc@260
|
1114 _), _),
|
adamc@260
|
1115 _), _),
|
adamc@260
|
1116 _), _),
|
adamc@260
|
1117 _) =>
|
adamc@260
|
1118 let
|
adamc@260
|
1119 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@260
|
1120 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@260
|
1121 in
|
adamc@260
|
1122 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@260
|
1123 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@260
|
1124 (L'.EAbs ("e2", s, s,
|
adamc@260
|
1125 strcat loc [sc "((",
|
adamc@260
|
1126 (L'.ERel 1, loc),
|
adamc@260
|
1127 sc ") ",
|
adamc@260
|
1128 (L'.ERel 2, loc),
|
adamc@260
|
1129 sc " (",
|
adamc@260
|
1130 (L'.ERel 0, loc),
|
adamc@260
|
1131 sc "))"]), loc)), loc)), loc),
|
adamc@260
|
1132 fm)
|
adamc@260
|
1133 end
|
adamc@260
|
1134
|
adamc@260
|
1135 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
|
adamc@260
|
1136 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
|
adamc@260
|
1137 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
|
adamc@260
|
1138
|
adamc@265
|
1139 | L.ECApp (
|
adamc@265
|
1140 (L.ECApp (
|
adamc@265
|
1141 (L.ECApp (
|
adamc@265
|
1142 (L.EFfi ("Basis", "sql_count"), _),
|
adamc@265
|
1143 _), _),
|
adamc@265
|
1144 _), _),
|
adamc@265
|
1145 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@265
|
1146 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
|
adamc@265
|
1147 fm)
|
adamc@266
|
1148
|
adamc@266
|
1149 | L.ECApp (
|
adamc@266
|
1150 (L.ECApp (
|
adamc@266
|
1151 (L.ECApp (
|
adamc@266
|
1152 (L.ECApp (
|
adamc@266
|
1153 (L.EFfi ("Basis", "sql_aggregate"), _),
|
adamc@266
|
1154 _), _),
|
adamc@266
|
1155 _), _),
|
adamc@266
|
1156 _), _),
|
adamc@266
|
1157 _) =>
|
adamc@266
|
1158 let
|
adamc@266
|
1159 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@266
|
1160 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@266
|
1161 in
|
adamc@266
|
1162 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@266
|
1163 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@266
|
1164 strcat loc [(L'.ERel 1, loc),
|
adamc@266
|
1165 sc "(",
|
adamc@266
|
1166 (L'.ERel 0, loc),
|
adamc@266
|
1167 sc ")"]), loc)), loc),
|
adamc@266
|
1168 fm)
|
adamc@266
|
1169 end
|
adamc@266
|
1170
|
adamc@266
|
1171 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1172 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1173 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
|
adamc@266
|
1174 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1175 (L'.EPrim (Prim.String "AVG"), loc)), loc),
|
adamc@266
|
1176 fm)
|
adamc@266
|
1177 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
|
adamc@266
|
1178 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1179 (L'.EPrim (Prim.String "SUM"), loc)), loc),
|
adamc@266
|
1180 fm)
|
adamc@266
|
1181
|
adamc@266
|
1182 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1183 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1184 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1185 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
|
adamc@266
|
1186 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1187 (L'.EPrim (Prim.String "MAX"), loc)), loc),
|
adamc@266
|
1188 fm)
|
adamc@266
|
1189 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
|
adamc@266
|
1190 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1191 (L'.EPrim (Prim.String "MIN"), loc)), loc),
|
adamc@266
|
1192 fm)
|
adamc@266
|
1193
|
adamc@268
|
1194 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@268
|
1195 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
|
adamc@268
|
1196
|
adamc@338
|
1197 | L.EFfiApp ("Basis", "nextval", [e]) =>
|
adamc@338
|
1198 let
|
adamc@338
|
1199 val un = (L'.TRecord [], loc)
|
adamc@338
|
1200 val int = (L'.TFfi ("Basis", "int"), loc)
|
adamc@338
|
1201 val (e, fm) = monoExp (env, st, fm) e
|
adamc@338
|
1202 in
|
adamc@338
|
1203 ((L'.EAbs ("_", un, int,
|
adamc@338
|
1204 (L'.ENextval (liftExpInExp 0 e), loc)), loc),
|
adamc@338
|
1205 fm)
|
adamc@338
|
1206 end
|
adamc@338
|
1207
|
adamc@139
|
1208 | L.EApp (
|
adamc@139
|
1209 (L.ECApp (
|
adamc@141
|
1210 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
|
adamc@139
|
1211 _), _),
|
adamc@179
|
1212 se) =>
|
adamc@179
|
1213 let
|
adamc@179
|
1214 val (se, fm) = monoExp (env, st, fm) se
|
adamc@179
|
1215 in
|
adamc@179
|
1216 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
|
adamc@179
|
1217 end
|
adamc@179
|
1218
|
adamc@95
|
1219 | L.EApp (
|
adamc@95
|
1220 (L.EApp (
|
adamc@95
|
1221 (L.ECApp (
|
adamc@95
|
1222 (L.ECApp (
|
adamc@95
|
1223 (L.ECApp (
|
adamc@139
|
1224 (L.ECApp (
|
adamc@140
|
1225 (L.EFfi ("Basis", "join"),
|
adamc@139
|
1226 _), _), _),
|
adamc@139
|
1227 _), _),
|
adamc@95
|
1228 _), _),
|
adamc@95
|
1229 _), _),
|
adamc@95
|
1230 xml1), _),
|
adamc@179
|
1231 xml2) =>
|
adamc@179
|
1232 let
|
adamc@179
|
1233 val (xml1, fm) = monoExp (env, st, fm) xml1
|
adamc@179
|
1234 val (xml2, fm) = monoExp (env, st, fm) xml2
|
adamc@179
|
1235 in
|
adamc@179
|
1236 ((L'.EStrcat (xml1, xml2), loc), fm)
|
adamc@179
|
1237 end
|
adamc@95
|
1238
|
adamc@95
|
1239 | L.EApp (
|
adamc@95
|
1240 (L.EApp (
|
adamc@104
|
1241 (L.EApp (
|
adamc@95
|
1242 (L.ECApp (
|
adamc@104
|
1243 (L.ECApp (
|
adamc@104
|
1244 (L.ECApp (
|
adamc@104
|
1245 (L.ECApp (
|
adamc@139
|
1246 (L.ECApp (
|
adamc@139
|
1247 (L.ECApp (
|
adamc@139
|
1248 (L.ECApp (
|
adamc@139
|
1249 (L.ECApp (
|
adamc@139
|
1250 (L.EFfi ("Basis", "tag"),
|
adamc@139
|
1251 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@104
|
1252 attrs), _),
|
adamc@95
|
1253 tag), _),
|
adamc@95
|
1254 xml) =>
|
adamc@95
|
1255 let
|
adamc@140
|
1256 fun getTag' (e, _) =
|
adamc@140
|
1257 case e of
|
adamc@143
|
1258 L.EFfi ("Basis", tag) => (tag, [])
|
adamc@143
|
1259 | L.ECApp (e, t) => let
|
adamc@143
|
1260 val (tag, ts) = getTag' e
|
adamc@143
|
1261 in
|
adamc@143
|
1262 (tag, ts @ [t])
|
adamc@143
|
1263 end
|
adamc@140
|
1264 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@140
|
1265 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
1266 ("", []))
|
adamc@140
|
1267
|
adamc@95
|
1268 fun getTag (e, _) =
|
adamc@95
|
1269 case e of
|
adamc@143
|
1270 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
|
adamc@140
|
1271 | L.EApp (e, (L.ERecord [], _)) => getTag' e
|
adamc@95
|
1272 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@95
|
1273 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
1274 ("", []))
|
adamc@95
|
1275
|
adamc@143
|
1276 val (tag, targs) = getTag tag
|
adamc@95
|
1277
|
adamc@179
|
1278 val (attrs, fm) = monoExp (env, st, fm) attrs
|
adamc@104
|
1279
|
adamc@143
|
1280 fun tagStart tag =
|
adamc@104
|
1281 case #1 attrs of
|
adamc@104
|
1282 L'.ERecord xes =>
|
adamc@104
|
1283 let
|
adamc@104
|
1284 fun lowercaseFirst "" = ""
|
adamc@143
|
1285 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
|
adamc@143
|
1286 ^ String.extract (s, 1, NONE)
|
adamc@104
|
1287
|
adamc@104
|
1288 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
|
adamc@104
|
1289 in
|
adamc@179
|
1290 foldl (fn ((x, e, t), (s, fm)) =>
|
adamc@347
|
1291 case t of
|
adamc@347
|
1292 (L'.TFfi ("Basis", "bool"), _) =>
|
adamc@347
|
1293 let
|
adamc@347
|
1294 val s' = " " ^ lowercaseFirst x
|
adamc@347
|
1295 in
|
adamc@347
|
1296 ((L'.ECase (e,
|
adamc@347
|
1297 [((L'.PCon (L'.Enum,
|
adamc@347
|
1298 L'.PConFfi {mod = "Basis",
|
adamc@347
|
1299 datatyp = "bool",
|
adamc@347
|
1300 con = "True",
|
adamc@347
|
1301 arg = NONE},
|
adamc@347
|
1302 NONE), loc),
|
adamc@347
|
1303 (L'.EStrcat (s,
|
adamc@347
|
1304 (L'.EPrim (Prim.String s'), loc)), loc)),
|
adamc@347
|
1305 ((L'.PCon (L'.Enum,
|
adamc@347
|
1306 L'.PConFfi {mod = "Basis",
|
adamc@347
|
1307 datatyp = "bool",
|
adamc@347
|
1308 con = "False",
|
adamc@347
|
1309 arg = NONE},
|
adamc@347
|
1310 NONE), loc),
|
adamc@347
|
1311 s)],
|
adamc@347
|
1312 {disc = (L'.TFfi ("Basis", "bool"), loc),
|
adamc@347
|
1313 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
|
adamc@347
|
1314 fm)
|
adamc@347
|
1315 end
|
adamc@347
|
1316 | _ =>
|
adamc@347
|
1317 let
|
adamc@347
|
1318 val fooify =
|
adamc@347
|
1319 case x of
|
adamc@347
|
1320 "Href" => urlifyExp
|
adamc@347
|
1321 | "Link" => urlifyExp
|
adamc@347
|
1322 | "Action" => urlifyExp
|
adamc@347
|
1323 | _ => attrifyExp
|
adamc@120
|
1324
|
adamc@347
|
1325 val xp = " " ^ lowercaseFirst x ^ "=\""
|
adamc@179
|
1326
|
adamc@347
|
1327 val (e, fm) = fooify env fm (e, t)
|
adamc@347
|
1328 in
|
adamc@347
|
1329 ((L'.EStrcat (s,
|
adamc@347
|
1330 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
|
adamc@347
|
1331 (L'.EStrcat (e,
|
adamc@347
|
1332 (L'.EPrim (Prim.String "\""),
|
adamc@347
|
1333 loc)),
|
adamc@347
|
1334 loc)),
|
adamc@347
|
1335 loc)), loc),
|
adamc@347
|
1336 fm)
|
adamc@347
|
1337 end)
|
adamc@179
|
1338 (s, fm) xes
|
adamc@104
|
1339 end
|
adamc@143
|
1340 | _ => raise Fail "Non-record attributes!"
|
adamc@104
|
1341
|
adamc@143
|
1342 fun input typ =
|
adamc@143
|
1343 case targs of
|
adamc@155
|
1344 [_, (L.CName name, _)] =>
|
adamc@179
|
1345 let
|
adamc@179
|
1346 val (ts, fm) = tagStart "input"
|
adamc@179
|
1347 in
|
adamc@179
|
1348 ((L'.EStrcat (ts,
|
adamc@179
|
1349 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
1350 loc)), loc), fm)
|
adamc@179
|
1351 end
|
adamc@143
|
1352 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1353 raise Fail "No name passed to input tag")
|
adamc@104
|
1354
|
adamc@152
|
1355 fun normal (tag, extra) =
|
adamc@143
|
1356 let
|
adamc@179
|
1357 val (tagStart, fm) = tagStart tag
|
adamc@152
|
1358 val tagStart = case extra of
|
adamc@152
|
1359 NONE => tagStart
|
adamc@152
|
1360 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
|
adamc@152
|
1361
|
adamc@143
|
1362 fun normal () =
|
adamc@179
|
1363 let
|
adamc@179
|
1364 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1365 in
|
adamc@179
|
1366 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
|
adamc@179
|
1367 (L'.EStrcat (xml,
|
adamc@179
|
1368 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
|
adamc@179
|
1369 loc)), loc)),
|
adamc@179
|
1370 loc),
|
adamc@179
|
1371 fm)
|
adamc@179
|
1372 end
|
adamc@143
|
1373 in
|
adamc@143
|
1374 case xml of
|
adamc@143
|
1375 (L.EApp ((L.ECApp (
|
adamc@143
|
1376 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
|
adamc@143
|
1377 _), _),
|
adamc@143
|
1378 _), _),
|
adamc@143
|
1379 (L.EPrim (Prim.String s), _)), _) =>
|
adamc@143
|
1380 if CharVector.all Char.isSpace s then
|
adamc@179
|
1381 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
|
adamc@143
|
1382 else
|
adamc@143
|
1383 normal ()
|
adamc@143
|
1384 | _ => normal ()
|
adamc@143
|
1385 end
|
adamc@152
|
1386 in
|
adamc@152
|
1387 case tag of
|
adamc@179
|
1388 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
|
adamc@152
|
1389
|
adamc@152
|
1390 | "textbox" =>
|
adamc@152
|
1391 (case targs of
|
adamc@152
|
1392 [_, (L.CName name, _)] =>
|
adamc@179
|
1393 let
|
adamc@179
|
1394 val (ts, fm) = tagStart "input"
|
adamc@179
|
1395 in
|
adamc@179
|
1396 ((L'.EStrcat (ts,
|
adamc@179
|
1397 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
1398 loc)), loc), fm)
|
adamc@179
|
1399 end
|
adamc@152
|
1400 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1401 raise Fail "No name passed to textarea tag"))
|
adamc@155
|
1402 | "password" => input "password"
|
adamc@361
|
1403 | "textarea" =>
|
adamc@152
|
1404 (case targs of
|
adamc@152
|
1405 [_, (L.CName name, _)] =>
|
adamc@179
|
1406 let
|
adamc@179
|
1407 val (ts, fm) = tagStart "textarea"
|
adamc@179
|
1408 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1409 in
|
adamc@179
|
1410 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
1411 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
1412 (L'.EStrcat (xml,
|
adamc@179
|
1413 (L'.EPrim (Prim.String "</textarea>"),
|
adamc@179
|
1414 loc)), loc)),
|
adamc@179
|
1415 loc), fm)
|
adamc@179
|
1416 end
|
adamc@152
|
1417 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1418 raise Fail "No name passed to ltextarea tag"))
|
adamc@153
|
1419
|
adamc@190
|
1420 | "checkbox" => input "checkbox"
|
adamc@190
|
1421
|
adamc@153
|
1422 | "radio" =>
|
adamc@153
|
1423 (case targs of
|
adamc@153
|
1424 [_, (L.CName name, _)] =>
|
adamc@179
|
1425 monoExp (env, St.setRadioGroup (st, name), fm) xml
|
adamc@153
|
1426 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1427 raise Fail "No name passed to radio tag"))
|
adamc@153
|
1428 | "radioOption" =>
|
adamc@153
|
1429 (case St.radioGroup st of
|
adamc@153
|
1430 NONE => raise Fail "No name for radioGroup"
|
adamc@153
|
1431 | SOME name =>
|
adamc@153
|
1432 normal ("input",
|
adamc@153
|
1433 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
|
adamc@152
|
1434
|
adamc@361
|
1435 | "select" =>
|
adamc@154
|
1436 (case targs of
|
adamc@154
|
1437 [_, (L.CName name, _)] =>
|
adamc@179
|
1438 let
|
adamc@179
|
1439 val (ts, fm) = tagStart "select"
|
adamc@179
|
1440 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1441 in
|
adamc@179
|
1442 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
1443 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
1444 (L'.EStrcat (xml,
|
adamc@179
|
1445 (L'.EPrim (Prim.String "</select>"),
|
adamc@179
|
1446 loc)), loc)),
|
adamc@179
|
1447 loc),
|
adamc@179
|
1448 fm)
|
adamc@179
|
1449 end
|
adamc@154
|
1450 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@154
|
1451 raise Fail "No name passed to lselect tag"))
|
adamc@154
|
1452
|
adamc@361
|
1453 | "option" => normal ("option", NONE)
|
adamc@154
|
1454
|
adamc@325
|
1455 | "tabl" => normal ("table", NONE)
|
adamc@152
|
1456 | _ => normal (tag, NONE)
|
adamc@95
|
1457 end
|
adamc@94
|
1458
|
adamc@141
|
1459 | L.EApp ((L.ECApp (
|
adamc@361
|
1460 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
|
adamc@141
|
1461 _), _),
|
adamc@141
|
1462 xml) =>
|
adamc@143
|
1463 let
|
adamc@143
|
1464 fun findSubmit (e, _) =
|
adamc@143
|
1465 case e of
|
adamc@143
|
1466 L.EApp (
|
adamc@143
|
1467 (L.EApp (
|
adamc@143
|
1468 (L.ECApp (
|
adamc@143
|
1469 (L.ECApp (
|
adamc@143
|
1470 (L.ECApp (
|
adamc@143
|
1471 (L.ECApp (
|
adamc@143
|
1472 (L.EFfi ("Basis", "join"),
|
adamc@143
|
1473 _), _), _),
|
adamc@143
|
1474 _), _),
|
adamc@143
|
1475 _), _),
|
adamc@143
|
1476 _), _),
|
adamc@143
|
1477 xml1), _),
|
adamc@143
|
1478 xml2) => (case findSubmit xml1 of
|
adamc@143
|
1479 Error => Error
|
adamc@143
|
1480 | NotFound => findSubmit xml2
|
adamc@143
|
1481 | Found e =>
|
adamc@143
|
1482 case findSubmit xml2 of
|
adamc@143
|
1483 NotFound => Found e
|
adamc@143
|
1484 | _ => Error)
|
adamc@143
|
1485 | L.EApp (
|
adamc@143
|
1486 (L.EApp (
|
adamc@143
|
1487 (L.EApp (
|
adamc@143
|
1488 (L.ECApp (
|
adamc@143
|
1489 (L.ECApp (
|
adamc@143
|
1490 (L.ECApp (
|
adamc@143
|
1491 (L.ECApp (
|
adamc@143
|
1492 (L.ECApp (
|
adamc@143
|
1493 (L.ECApp (
|
adamc@143
|
1494 (L.ECApp (
|
adamc@143
|
1495 (L.ECApp (
|
adamc@143
|
1496 (L.EFfi ("Basis", "tag"),
|
adamc@143
|
1497 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@143
|
1498 attrs), _),
|
adamc@143
|
1499 _), _),
|
adamc@143
|
1500 xml) =>
|
adamc@143
|
1501 (case #1 attrs of
|
adamc@143
|
1502 L.ERecord xes =>
|
adamc@143
|
1503 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
|
adamc@143
|
1504 | _ => NONE) xes of
|
adamc@143
|
1505 NONE => findSubmit xml
|
adamc@143
|
1506 | SOME et =>
|
adamc@143
|
1507 case findSubmit xml of
|
adamc@143
|
1508 NotFound => Found et
|
adamc@143
|
1509 | _ => Error)
|
adamc@143
|
1510 | _ => findSubmit xml)
|
adamc@143
|
1511 | _ => NotFound
|
adamc@143
|
1512
|
adamc@143
|
1513 val (action, actionT) = case findSubmit xml of
|
adamc@143
|
1514 NotFound => raise Fail "No submit found"
|
adamc@143
|
1515 | Error => raise Fail "Not ready for multi-submit lforms yet"
|
adamc@143
|
1516 | Found et => et
|
adamc@143
|
1517
|
adamc@143
|
1518 val actionT = monoType env actionT
|
adamc@179
|
1519 val (action, fm) = monoExp (env, st, fm) action
|
adamc@179
|
1520 val (action, fm) = urlifyExp env fm (action, actionT)
|
adamc@179
|
1521 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@143
|
1522 in
|
adamc@179
|
1523 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
|
adamc@179
|
1524 (L'.EStrcat (action,
|
adamc@179
|
1525 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
|
adamc@179
|
1526 (L'.EStrcat (xml,
|
adamc@179
|
1527 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
|
adamc@179
|
1528 fm)
|
adamc@143
|
1529 end
|
adamc@141
|
1530
|
adamc@148
|
1531 | L.EApp ((L.ECApp (
|
adamc@148
|
1532 (L.ECApp (
|
adamc@148
|
1533 (L.ECApp (
|
adamc@148
|
1534 (L.ECApp (
|
adamc@148
|
1535 (L.EFfi ("Basis", "useMore"), _), _), _),
|
adamc@148
|
1536 _), _),
|
adamc@148
|
1537 _), _),
|
adamc@148
|
1538 _), _),
|
adamc@179
|
1539 xml) => monoExp (env, st, fm) xml
|
adamc@148
|
1540
|
adamc@283
|
1541 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
|
adamc@283
|
1542 let
|
adamc@283
|
1543 val t = monoType env t
|
adamc@283
|
1544 in
|
adamc@283
|
1545 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
|
adamc@283
|
1546 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
|
adamc@283
|
1547 fm)
|
adamc@283
|
1548 end
|
adamc@283
|
1549
|
adamc@179
|
1550 | L.EApp (e1, e2) =>
|
adamc@179
|
1551 let
|
adamc@179
|
1552 val (e1, fm) = monoExp (env, st, fm) e1
|
adamc@179
|
1553 val (e2, fm) = monoExp (env, st, fm) e2
|
adamc@179
|
1554 in
|
adamc@179
|
1555 ((L'.EApp (e1, e2), loc), fm)
|
adamc@179
|
1556 end
|
adamc@26
|
1557 | L.EAbs (x, dom, ran, e) =>
|
adamc@179
|
1558 let
|
adamc@179
|
1559 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
|
adamc@179
|
1560 in
|
adamc@179
|
1561 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
|
adamc@179
|
1562 end
|
adamc@25
|
1563 | L.ECApp _ => poly ()
|
adamc@25
|
1564 | L.ECAbs _ => poly ()
|
adamc@25
|
1565
|
adamc@252
|
1566 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
|
adamc@252
|
1567 | L.EFfiApp (m, x, es) =>
|
adamc@252
|
1568 let
|
adamc@252
|
1569 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
|
adamc@252
|
1570 in
|
adamc@252
|
1571 ((L'.EFfiApp (m, x, es), loc), fm)
|
adamc@252
|
1572 end
|
adamc@252
|
1573
|
adamc@179
|
1574 | L.ERecord xes =>
|
adamc@179
|
1575 let
|
adamc@179
|
1576 val (xes, fm) = ListUtil.foldlMap
|
adamc@179
|
1577 (fn ((x, e, t), fm) =>
|
adamc@179
|
1578 let
|
adamc@179
|
1579 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1580 in
|
adamc@179
|
1581 ((monoName env x,
|
adamc@179
|
1582 e,
|
adamc@179
|
1583 monoType env t), fm)
|
adamc@179
|
1584 end) fm xes
|
adamc@179
|
1585 in
|
adamc@179
|
1586 ((L'.ERecord xes, loc), fm)
|
adamc@179
|
1587 end
|
adamc@179
|
1588 | L.EField (e, x, _) =>
|
adamc@179
|
1589 let
|
adamc@179
|
1590 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1591 in
|
adamc@179
|
1592 ((L'.EField (e, monoName env x), loc), fm)
|
adamc@179
|
1593 end
|
adamc@343
|
1594 | L.EWith _ => poly ()
|
adamc@149
|
1595 | L.ECut _ => poly ()
|
adamc@73
|
1596 | L.EFold _ => poly ()
|
adamc@177
|
1597
|
adamc@182
|
1598 | L.ECase (e, pes, {disc, result}) =>
|
adamc@179
|
1599 let
|
adamc@179
|
1600 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1601 val (pes, fm) = ListUtil.foldlMap
|
adamc@179
|
1602 (fn ((p, e), fm) =>
|
adamc@179
|
1603 let
|
adamc@179
|
1604 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1605 in
|
adamc@182
|
1606 ((monoPat env p, e), fm)
|
adamc@179
|
1607 end) fm pes
|
adamc@179
|
1608 in
|
adamc@182
|
1609 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
|
adamc@179
|
1610 end
|
adamc@177
|
1611
|
adamc@179
|
1612 | L.EWrite e =>
|
adamc@179
|
1613 let
|
adamc@179
|
1614 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1615 in
|
adamc@252
|
1616 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
|
adamc@252
|
1617 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
|
adamc@179
|
1618 end
|
adamc@110
|
1619
|
adamc@179
|
1620 | L.EClosure (n, es) =>
|
adamc@179
|
1621 let
|
adamc@179
|
1622 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
|
adamc@179
|
1623 monoExp (env, st, fm) e)
|
adamc@179
|
1624 fm es
|
adamc@179
|
1625 in
|
adamc@179
|
1626 ((L'.EClosure (n, es), loc), fm)
|
adamc@179
|
1627 end
|
adamc@25
|
1628 end
|
adamc@25
|
1629
|
adamc@179
|
1630 fun monoDecl (env, fm) (all as (d, loc)) =
|
adamc@25
|
1631 let
|
adamc@25
|
1632 fun poly () =
|
adamc@25
|
1633 (E.errorAt loc "Unsupported declaration";
|
adamc@25
|
1634 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
|
adamc@25
|
1635 NONE)
|
adamc@25
|
1636 in
|
adamc@25
|
1637 case d of
|
adamc@25
|
1638 L.DCon _ => NONE
|
adamc@193
|
1639 | L.DDatatype (x, n, [], xncs) =>
|
adamc@193
|
1640 let
|
adamc@196
|
1641 val env' = Env.declBinds env all
|
adamc@196
|
1642 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
|
adamc@164
|
1643 in
|
adamc@273
|
1644 SOME (env', fm, [d])
|
adamc@193
|
1645 end
|
adamc@193
|
1646 | L.DDatatype _ => poly ()
|
adamc@179
|
1647 | L.DVal (x, n, t, e, s) =>
|
adamc@179
|
1648 let
|
adamc@179
|
1649 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
1650 in
|
adamc@179
|
1651 SOME (Env.pushENamed env x n t NONE s,
|
adamc@179
|
1652 fm,
|
adamc@273
|
1653 [(L'.DVal (x, n, monoType env t, e, s), loc)])
|
adamc@179
|
1654 end
|
adamc@128
|
1655 | L.DValRec vis =>
|
adamc@128
|
1656 let
|
adamc@128
|
1657 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
|
adamc@179
|
1658
|
adamc@179
|
1659 val (vis, fm) = ListUtil.foldlMap
|
adamc@179
|
1660 (fn ((x, n, t, e, s), fm) =>
|
adamc@179
|
1661 let
|
adamc@179
|
1662 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
1663 in
|
adamc@179
|
1664 ((x, n, monoType env t, e, s), fm)
|
adamc@179
|
1665 end)
|
adamc@179
|
1666 fm vis
|
adamc@128
|
1667 in
|
adamc@128
|
1668 SOME (env,
|
adamc@179
|
1669 fm,
|
adamc@273
|
1670 [(L'.DValRec vis, loc)])
|
adamc@128
|
1671 end
|
adamc@144
|
1672 | L.DExport (ek, n) =>
|
adamc@115
|
1673 let
|
adamc@120
|
1674 val (_, t, _, s) = Env.lookupENamed env n
|
adamc@120
|
1675
|
adamc@120
|
1676 fun unwind (t, _) =
|
adamc@120
|
1677 case t of
|
adamc@120
|
1678 L.TFun (dom, ran) => dom :: unwind ran
|
adamc@314
|
1679 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
|
adamc@314
|
1680 (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: unwind t
|
adamc@120
|
1681 | _ => []
|
adamc@120
|
1682
|
adamc@120
|
1683 val ts = map (monoType env) (unwind t)
|
adamc@115
|
1684 in
|
adamc@273
|
1685 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
|
adamc@115
|
1686 end
|
adamc@273
|
1687 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
|
adamc@251
|
1688 let
|
adamc@251
|
1689 val t = (L.CFfi ("Basis", "string"), loc)
|
adamc@251
|
1690 val t' = (L'.TFfi ("Basis", "string"), loc)
|
adamc@311
|
1691 val s = "uw_" ^ s
|
adamc@251
|
1692 val e = (L'.EPrim (Prim.String s), loc)
|
adamc@273
|
1693
|
adamc@273
|
1694 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
|
adamc@251
|
1695 in
|
adamc@251
|
1696 SOME (Env.pushENamed env x n t NONE s,
|
adamc@251
|
1697 fm,
|
adamc@273
|
1698 [(L'.DTable (s, xts), loc),
|
adamc@273
|
1699 (L'.DVal (x, n, t', e, s), loc)])
|
adamc@251
|
1700 end
|
adamc@273
|
1701 | L.DTable _ => poly ()
|
adamc@338
|
1702 | L.DSequence (x, n, s) =>
|
adamc@338
|
1703 let
|
adamc@338
|
1704 val t = (L.CFfi ("Basis", "string"), loc)
|
adamc@338
|
1705 val t' = (L'.TFfi ("Basis", "string"), loc)
|
adamc@338
|
1706 val s = "uw_" ^ s
|
adamc@338
|
1707 val e = (L'.EPrim (Prim.String s), loc)
|
adamc@338
|
1708 in
|
adamc@338
|
1709 SOME (Env.pushENamed env x n t NONE s,
|
adamc@338
|
1710 fm,
|
adamc@338
|
1711 [(L'.DSequence s, loc),
|
adamc@338
|
1712 (L'.DVal (x, n, t', e, s), loc)])
|
adamc@338
|
1713 end
|
adamc@273
|
1714 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
|
adamc@25
|
1715 end
|
adamc@25
|
1716
|
adamc@25
|
1717 fun monoize env ds =
|
adamc@25
|
1718 let
|
adamc@385
|
1719 val p = !urlPrefix
|
adamc@385
|
1720 val () =
|
adamc@385
|
1721 if p = "" then
|
adamc@385
|
1722 urlPrefix := "/"
|
adamc@385
|
1723 else if String.sub (p, size p - 1) <> #"/" then
|
adamc@385
|
1724 urlPrefix := p ^ "/"
|
adamc@385
|
1725 else
|
adamc@385
|
1726 ()
|
adamc@385
|
1727
|
adamc@179
|
1728 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
|
adamc@179
|
1729 case monoDecl (env, fm) d of
|
adamc@179
|
1730 NONE => (env, fm, ds)
|
adamc@273
|
1731 | SOME (env, fm, ds') =>
|
adamc@179
|
1732 (env,
|
adamc@179
|
1733 Fm.enter fm,
|
adamc@273
|
1734 ds' @ Fm.decls fm @ ds))
|
adamc@179
|
1735 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
|
adamc@25
|
1736 in
|
adamc@25
|
1737 rev ds
|
adamc@25
|
1738 end
|
adamc@25
|
1739
|
adamc@25
|
1740 end
|