adamc@1114
|
1 (* Copyright (c) 2008-2010, Adam Chlipala
|
adamc@29
|
2 * All rights reserved.
|
adamc@29
|
3 *
|
adamc@29
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@29
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@29
|
6 *
|
adamc@29
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@29
|
8 * this list of conditions and the following disclaimer.
|
adamc@29
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@29
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@29
|
11 * and/or other materials provided with the distribution.
|
adamc@29
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@29
|
13 * derived from this software without specific prior written permission.
|
adamc@29
|
14 *
|
adamc@29
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@29
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@29
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@29
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@29
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@29
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@29
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@29
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@29
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@29
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@29
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@29
|
26 *)
|
adamc@29
|
27
|
adamc@29
|
28 (* Pretty-printing C jr. *)
|
adamc@29
|
29
|
adamc@29
|
30 structure CjrPrint :> CJR_PRINT = struct
|
adamc@29
|
31
|
adamc@29
|
32 open Print.PD
|
adamc@29
|
33 open Print
|
adamc@29
|
34
|
adamc@29
|
35 open Cjr
|
adamc@29
|
36
|
adamc@269
|
37 val dummyt = (TRecord 0, ErrorMsg.dummySpan)
|
adamc@269
|
38
|
adamc@29
|
39 structure E = CjrEnv
|
adamc@29
|
40 structure EM = ErrorMsg
|
adamc@29
|
41
|
adamc@144
|
42 structure SK = struct
|
adamc@144
|
43 type ord_key = string
|
adamc@144
|
44 val compare = String.compare
|
adamc@144
|
45 end
|
adamc@144
|
46
|
adamc@144
|
47 structure SS = BinarySetFn(SK)
|
adamc@144
|
48 structure SM = BinaryMapFn(SK)
|
adamc@144
|
49 structure IS = IntBinarySet
|
adamc@144
|
50
|
adamc@144
|
51 structure CM = BinaryMapFn(struct
|
adamc@144
|
52 type ord_key = char
|
adamc@144
|
53 val compare = Char.compare
|
adamc@144
|
54 end)
|
adamc@144
|
55
|
adamc@29
|
56 val debug = ref false
|
adamc@29
|
57
|
adamc@196
|
58 val dummyTyp = (TDatatype (Enum, 0, ref []), ErrorMsg.dummySpan)
|
adamc@29
|
59
|
adamc@316
|
60 val ident = String.translate (fn #"'" => "PRIME"
|
adamc@316
|
61 | ch => str ch)
|
adamc@316
|
62
|
adamc@316
|
63 val p_ident = string o ident
|
adamc@316
|
64
|
adamc@463
|
65 fun isUnboxable (t : typ) =
|
adamc@463
|
66 case #1 t of
|
adamc@463
|
67 TDatatype (Default, _, _) => true
|
adamc@463
|
68 | TFfi ("Basis", "string") => true
|
adamc@463
|
69 | _ => false
|
adamc@463
|
70
|
adamc@29
|
71 fun p_typ' par env (t, loc) =
|
adamc@29
|
72 case t of
|
adamc@476
|
73 TFun (t1, t2) => (EM.errorAt loc "Function type remains";
|
adamc@476
|
74 string "<FUNCTION>")
|
adamc@29
|
75 | TRecord i => box [string "struct",
|
adamc@29
|
76 space,
|
adamc@311
|
77 string "__uws_",
|
adamc@29
|
78 string (Int.toString i)]
|
adamc@188
|
79 | TDatatype (Enum, n, _) =>
|
adamc@188
|
80 (box [string "enum",
|
adamc@188
|
81 space,
|
adamc@1257
|
82 string ("__uwe_" ^ ident (#1 (E.lookupDatatype env n)) ^ "_" ^ Int.toString n)]
|
adamc@311
|
83 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
|
adamc@198
|
84 | TDatatype (Option, n, xncs) =>
|
adamc@198
|
85 (case ListUtil.search #3 (!xncs) of
|
adamc@198
|
86 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
|
adamc@198
|
87 | SOME t =>
|
adamc@897
|
88 if isUnboxable t then
|
adamc@897
|
89 p_typ' par env t
|
adamc@897
|
90 else
|
adamc@897
|
91 box [p_typ' par env t,
|
adamc@897
|
92 string "*"])
|
adamc@188
|
93 | TDatatype (Default, n, _) =>
|
adamc@165
|
94 (box [string "struct",
|
adamc@165
|
95 space,
|
adamc@1257
|
96 string ("__uwd_" ^ ident (#1 (E.lookupDatatype env n)) ^ "_" ^ Int.toString n ^ "*")]
|
adamc@311
|
97 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
|
adamc@316
|
98 | TFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
|
adamc@288
|
99 | TOption t =>
|
adamc@463
|
100 if isUnboxable t then
|
adamc@463
|
101 p_typ' par env t
|
adamc@463
|
102 else
|
adamc@463
|
103 box [p_typ' par env t,
|
adamc@463
|
104 string "*"]
|
adamc@757
|
105 | TList (_, i) => box [string "struct",
|
adamc@757
|
106 space,
|
adamc@757
|
107 string "__uws_",
|
adamc@757
|
108 string (Int.toString i),
|
adamc@757
|
109 string "*"]
|
adamc@29
|
110
|
adamc@29
|
111 and p_typ env = p_typ' false env
|
adamc@29
|
112
|
adamc@316
|
113 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@311
|
114 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@29
|
115
|
adam@1294
|
116 fun p_enamed' env n =
|
adam@1294
|
117 "__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n
|
adam@1294
|
118 handle CjrEnv.UnboundNamed _ => "__uwn_UNBOUND_" ^ Int.toString n
|
adam@1294
|
119
|
adam@1294
|
120 fun p_enamed env n = string (p_enamed' env n)
|
adamc@109
|
121
|
adamc@182
|
122 fun p_con_named env n =
|
adamc@316
|
123 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
|
adamc@311
|
124 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
|
adamc@182
|
125
|
adamc@182
|
126 fun p_pat_preamble env (p, _) =
|
adamc@182
|
127 case p of
|
adamc@182
|
128 PWild => (box [],
|
adamc@182
|
129 env)
|
adamc@182
|
130 | PVar (x, t) => (box [p_typ env t,
|
adamc@182
|
131 space,
|
adamc@311
|
132 string "__uwr_",
|
adamc@316
|
133 p_ident x,
|
adamc@182
|
134 string "_",
|
adamc@182
|
135 string (Int.toString (E.countERels env)),
|
adamc@182
|
136 string ";",
|
adamc@182
|
137 newline],
|
adamc@196
|
138 E.pushERel env x t)
|
adamc@182
|
139 | PPrim _ => (box [], env)
|
adamc@188
|
140 | PCon (_, _, NONE) => (box [], env)
|
adamc@188
|
141 | PCon (_, _, SOME p) => p_pat_preamble env p
|
adamc@182
|
142 | PRecord xps =>
|
adamc@182
|
143 foldl (fn ((_, p, _), (pp, env)) =>
|
adamc@182
|
144 let
|
adamc@182
|
145 val (pp', env) = p_pat_preamble env p
|
adamc@182
|
146 in
|
adamc@182
|
147 (box [pp', pp], env)
|
adamc@182
|
148 end) (box [], env) xps
|
adamc@288
|
149 | PNone _ => (box [], env)
|
adamc@288
|
150 | PSome (_, p) => p_pat_preamble env p
|
adamc@182
|
151
|
adamc@182
|
152 fun p_patCon env pc =
|
adamc@182
|
153 case pc of
|
adamc@182
|
154 PConVar n => p_con_named env n
|
adamc@316
|
155 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
|
adamc@182
|
156
|
adamc@757
|
157 fun p_pat (env, exit, depth) (p, loc) =
|
adamc@182
|
158 case p of
|
adamc@182
|
159 PWild =>
|
adamc@182
|
160 (box [], env)
|
adamc@182
|
161 | PVar (x, t) =>
|
adamc@311
|
162 (box [string "__uwr_",
|
adamc@316
|
163 p_ident x,
|
adamc@182
|
164 string "_",
|
adamc@182
|
165 string (Int.toString (E.countERels env)),
|
adamc@182
|
166 space,
|
adamc@182
|
167 string "=",
|
adamc@182
|
168 space,
|
adamc@182
|
169 string "disc",
|
adamc@182
|
170 string (Int.toString depth),
|
adamc@182
|
171 string ";"],
|
adamc@182
|
172 E.pushERel env x t)
|
adamc@182
|
173 | PPrim (Prim.Int n) =>
|
adamc@182
|
174 (box [string "if",
|
adamc@182
|
175 space,
|
adamc@182
|
176 string "(disc",
|
adamc@182
|
177 string (Int.toString depth),
|
adamc@182
|
178 space,
|
adamc@182
|
179 string "!=",
|
adamc@182
|
180 space,
|
adamc@276
|
181 Prim.p_t_GCC (Prim.Int n),
|
adamc@182
|
182 string ")",
|
adamc@182
|
183 space,
|
adamc@182
|
184 exit],
|
adamc@182
|
185 env)
|
adamc@182
|
186 | PPrim (Prim.String s) =>
|
adamc@182
|
187 (box [string "if",
|
adamc@182
|
188 space,
|
adamc@182
|
189 string "(strcmp(disc",
|
adamc@182
|
190 string (Int.toString depth),
|
adamc@182
|
191 string ",",
|
adamc@182
|
192 space,
|
adamc@276
|
193 Prim.p_t_GCC (Prim.String s),
|
adamc@182
|
194 string "))",
|
adamc@182
|
195 space,
|
adamc@182
|
196 exit],
|
adamc@182
|
197 env)
|
adamc@947
|
198 | PPrim (Prim.Char ch) =>
|
adamc@947
|
199 (box [string "if",
|
adamc@947
|
200 space,
|
adamc@947
|
201 string "(disc",
|
adamc@947
|
202 string (Int.toString depth),
|
adamc@947
|
203 space,
|
adamc@947
|
204 string "!=",
|
adamc@947
|
205 space,
|
adamc@947
|
206 Prim.p_t_GCC (Prim.Char ch),
|
adamc@947
|
207 string ")",
|
adamc@947
|
208 space,
|
adamc@947
|
209 exit],
|
adamc@947
|
210 env)
|
adamc@182
|
211 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
|
adamc@182
|
212
|
adamc@188
|
213 | PCon (dk, pc, po) =>
|
adamc@182
|
214 let
|
adamc@182
|
215 val (p, env) =
|
adamc@182
|
216 case po of
|
adamc@182
|
217 NONE => (box [], env)
|
adamc@182
|
218 | SOME p =>
|
adamc@182
|
219 let
|
adamc@182
|
220 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
221
|
adamc@182
|
222 val (x, to) = case pc of
|
adamc@182
|
223 PConVar n =>
|
adamc@182
|
224 let
|
adamc@182
|
225 val (x, to, _) = E.lookupConstructor env n
|
adamc@182
|
226 in
|
adamc@316
|
227 ("uw_" ^ ident x, to)
|
adamc@182
|
228 end
|
adamc@188
|
229 | PConFfi {mod = m, con, arg, ...} =>
|
adamc@316
|
230 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
|
adamc@182
|
231
|
adamc@182
|
232 val t = case to of
|
adamc@182
|
233 NONE => raise Fail "CjrPrint: Constructor mismatch"
|
adamc@182
|
234 | SOME t => t
|
adamc@182
|
235 in
|
adamc@182
|
236 (box [string "{",
|
adamc@182
|
237 newline,
|
adamc@182
|
238 p_typ env t,
|
adamc@182
|
239 space,
|
adamc@182
|
240 string "disc",
|
adamc@182
|
241 string (Int.toString (depth + 1)),
|
adamc@182
|
242 space,
|
adamc@182
|
243 string "=",
|
adamc@182
|
244 space,
|
adamc@198
|
245 case dk of
|
adamc@198
|
246 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
|
adamc@198
|
247 | Default => box [string "disc",
|
adamc@198
|
248 string (Int.toString depth),
|
adamc@198
|
249 string "->data.",
|
adamc@198
|
250 string x]
|
adamc@198
|
251 | Option =>
|
adamc@463
|
252 if isUnboxable t then
|
adamc@463
|
253 box [string "disc",
|
adamc@463
|
254 string (Int.toString depth)]
|
adamc@463
|
255 else
|
adamc@463
|
256 box [string "*disc",
|
adamc@463
|
257 string (Int.toString depth)],
|
adamc@182
|
258 string ";",
|
adamc@182
|
259 newline,
|
adamc@182
|
260 p,
|
adamc@182
|
261 newline,
|
adamc@182
|
262 string "}"],
|
adamc@182
|
263 env)
|
adamc@182
|
264 end
|
adamc@182
|
265 in
|
adamc@182
|
266 (box [string "if",
|
adamc@182
|
267 space,
|
adamc@182
|
268 string "(disc",
|
adamc@182
|
269 string (Int.toString depth),
|
adamc@198
|
270 case (dk, po) of
|
adamc@198
|
271 (Enum, _) => box [space,
|
adamc@198
|
272 string "!=",
|
adamc@198
|
273 space,
|
adamc@198
|
274 p_patCon env pc]
|
adamc@198
|
275 | (Default, _) => box [string "->tag",
|
adamc@198
|
276 space,
|
adamc@198
|
277 string "!=",
|
adamc@198
|
278 space,
|
adamc@198
|
279 p_patCon env pc]
|
adamc@198
|
280 | (Option, NONE) => box [space,
|
adamc@198
|
281 string "!=",
|
adamc@198
|
282 space,
|
adamc@198
|
283 string "NULL"]
|
adamc@198
|
284 | (Option, SOME _) => box [space,
|
adamc@198
|
285 string "==",
|
adamc@198
|
286 space,
|
adamc@198
|
287 string "NULL"],
|
adamc@182
|
288 string ")",
|
adamc@182
|
289 space,
|
adamc@182
|
290 exit,
|
adamc@182
|
291 newline,
|
adamc@182
|
292 p],
|
adamc@182
|
293 env)
|
adamc@182
|
294 end
|
adamc@182
|
295
|
adamc@182
|
296 | PRecord xps =>
|
adamc@182
|
297 let
|
adamc@182
|
298 val (xps, env) =
|
adamc@182
|
299 ListUtil.foldlMap (fn ((x, p, t), env) =>
|
adamc@182
|
300 let
|
adamc@182
|
301 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
302
|
adamc@182
|
303 val p = box [string "{",
|
adamc@182
|
304 newline,
|
adamc@182
|
305 p_typ env t,
|
adamc@182
|
306 space,
|
adamc@182
|
307 string "disc",
|
adamc@182
|
308 string (Int.toString (depth + 1)),
|
adamc@182
|
309 space,
|
adamc@182
|
310 string "=",
|
adamc@182
|
311 space,
|
adamc@182
|
312 string "disc",
|
adamc@182
|
313 string (Int.toString depth),
|
adamc@311
|
314 string ".__uwf_",
|
adamc@316
|
315 p_ident x,
|
adamc@182
|
316 string ";",
|
adamc@182
|
317 newline,
|
adamc@182
|
318 p,
|
adamc@182
|
319 newline,
|
adamc@182
|
320 string "}"]
|
adamc@182
|
321 in
|
adamc@182
|
322 (p, env)
|
adamc@182
|
323 end) env xps
|
adamc@182
|
324 in
|
adamc@182
|
325 (p_list_sep newline (fn x => x) xps,
|
adamc@182
|
326 env)
|
adamc@182
|
327 end
|
adamc@182
|
328
|
adamc@288
|
329 | PNone t =>
|
adamc@288
|
330 (box [string "if",
|
adamc@288
|
331 space,
|
adamc@288
|
332 string "(disc",
|
adamc@288
|
333 string (Int.toString depth),
|
adamc@288
|
334 space,
|
adamc@288
|
335 string "!=",
|
adamc@288
|
336 space,
|
adamc@288
|
337 string "NULL)",
|
adamc@288
|
338 space,
|
adamc@288
|
339 exit,
|
adamc@288
|
340 newline],
|
adamc@288
|
341 env)
|
adamc@288
|
342
|
adamc@288
|
343 | PSome (t, p) =>
|
adamc@288
|
344 let
|
adamc@288
|
345 val (p, env) =
|
adamc@288
|
346 let
|
adamc@288
|
347 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@288
|
348 in
|
adamc@288
|
349 (box [string "{",
|
adamc@288
|
350 newline,
|
adamc@288
|
351 p_typ env t,
|
adamc@288
|
352 space,
|
adamc@288
|
353 string "disc",
|
adamc@288
|
354 string (Int.toString (depth + 1)),
|
adamc@288
|
355 space,
|
adamc@288
|
356 string "=",
|
adamc@288
|
357 space,
|
adamc@463
|
358 if isUnboxable t then
|
adamc@463
|
359 box [string "disc",
|
adamc@463
|
360 string (Int.toString depth)]
|
adamc@463
|
361 else
|
adamc@463
|
362 box [string "*disc",
|
adamc@463
|
363 string (Int.toString depth)],
|
adamc@288
|
364 string ";",
|
adamc@288
|
365 newline,
|
adamc@288
|
366 p,
|
adamc@288
|
367 newline,
|
adamc@288
|
368 string "}"],
|
adamc@288
|
369 env)
|
adamc@288
|
370 end
|
adamc@288
|
371 in
|
adamc@288
|
372 (box [string "if",
|
adamc@288
|
373 space,
|
adamc@288
|
374 string "(disc",
|
adamc@288
|
375 string (Int.toString depth),
|
adamc@288
|
376 space,
|
adamc@288
|
377 string "==",
|
adamc@288
|
378 space,
|
adamc@288
|
379 string "NULL)",
|
adamc@288
|
380 space,
|
adamc@288
|
381 exit,
|
adamc@288
|
382 newline,
|
adamc@288
|
383 p],
|
adamc@288
|
384 env)
|
adamc@288
|
385 end
|
adamc@288
|
386
|
adamc@182
|
387 local
|
adamc@182
|
388 val count = ref 0
|
adamc@182
|
389 in
|
adamc@182
|
390 fun newGoto () =
|
adamc@182
|
391 let
|
adamc@182
|
392 val r = !count
|
adamc@182
|
393 in
|
adamc@182
|
394 count := r + 1;
|
adamc@182
|
395 string ("L" ^ Int.toString r)
|
adamc@182
|
396 end
|
adamc@182
|
397 end
|
adamc@182
|
398
|
adamc@185
|
399 fun patConInfo env pc =
|
adamc@185
|
400 case pc of
|
adamc@185
|
401 PConVar n =>
|
adamc@185
|
402 let
|
adamc@185
|
403 val (x, _, dn) = E.lookupConstructor env n
|
adamc@185
|
404 val (dx, _) = E.lookupDatatype env dn
|
adamc@185
|
405 in
|
adamc@316
|
406 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
|
adamc@316
|
407 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
|
adamc@316
|
408 "uw_" ^ ident x)
|
adamc@185
|
409 end
|
adamc@186
|
410 | PConFfi {mod = m, datatyp, con, ...} =>
|
adamc@316
|
411 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
|
adamc@316
|
412 "uw_" ^ ident m ^ "_" ^ ident con,
|
adamc@316
|
413 "uw_" ^ ident con)
|
adamc@185
|
414
|
adamc@743
|
415 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e eLen =
|
adamc@278
|
416 case t of
|
adamc@311
|
417 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
|
adamc@311
|
418 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
|
adamc@324
|
419 | TFfi ("Basis", "string") =>
|
adamc@324
|
420 if wontLeakStrings then
|
adamc@324
|
421 e
|
adamc@324
|
422 else
|
adamc@737
|
423 box [string "uw_strdup(ctx, ", e, string ")"]
|
adamc@311
|
424 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
|
adamc@438
|
425 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
|
adamc@743
|
426 | TFfi ("Basis", "blob") => box [string "uw_Basis_stringToBlob_error(ctx, ",
|
adamc@743
|
427 e,
|
adamc@743
|
428 string ", ",
|
adamc@743
|
429 eLen,
|
adamc@743
|
430 string ")"]
|
adamc@678
|
431 | TFfi ("Basis", "channel") => box [string "uw_Basis_stringToChannel_error(ctx, ", e, string ")"]
|
adamc@682
|
432 | TFfi ("Basis", "client") => box [string "uw_Basis_stringToClient_error(ctx, ", e, string ")"]
|
adamc@467
|
433
|
adamc@278
|
434 | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
|
adamc@278
|
435 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@278
|
436 string "ERROR")
|
adamc@278
|
437
|
adamc@467
|
438 fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
|
adamc@467
|
439 case t of
|
adamc@467
|
440 TOption t =>
|
adamc@747
|
441 box [string "(PQgetisnull(res, i, ",
|
adamc@467
|
442 string (Int.toString i),
|
adamc@467
|
443 string ") ? NULL : ",
|
adamc@467
|
444 case t of
|
adamc@467
|
445 (TFfi ("Basis", "string"), _) => p_getcol wontLeakStrings env t i
|
adamc@467
|
446 | _ => box [string "({",
|
adamc@467
|
447 newline,
|
adamc@467
|
448 p_typ env t,
|
adamc@467
|
449 space,
|
adamc@467
|
450 string "*tmp = uw_malloc(ctx, sizeof(",
|
adamc@467
|
451 p_typ env t,
|
adamc@467
|
452 string "));",
|
adamc@467
|
453 newline,
|
adamc@467
|
454 string "*tmp = ",
|
adamc@467
|
455 p_getcol wontLeakStrings env t i,
|
adamc@467
|
456 string ";",
|
adamc@467
|
457 newline,
|
adamc@467
|
458 string "tmp;",
|
adamc@467
|
459 newline,
|
adamc@467
|
460 string "})"],
|
adamc@467
|
461 string ")"]
|
adamc@467
|
462 | _ =>
|
adamc@747
|
463 box [string "(PQgetisnull(res, i, ",
|
adamc@747
|
464 string (Int.toString i),
|
adamc@747
|
465 string ") ? ",
|
adamc@747
|
466 box [string "({",
|
adamc@747
|
467 p_typ env tAll,
|
adamc@747
|
468 space,
|
adamc@747
|
469 string "tmp;",
|
adamc@747
|
470 newline,
|
adamc@747
|
471 string "uw_error(ctx, FATAL, \"Unexpectedly NULL field #",
|
adamc@747
|
472 string (Int.toString i),
|
adamc@747
|
473 string "\");",
|
adamc@747
|
474 newline,
|
adamc@747
|
475 string "tmp;",
|
adamc@747
|
476 newline,
|
adamc@747
|
477 string "})"],
|
adamc@747
|
478 string " : ",
|
adamc@747
|
479 p_unsql wontLeakStrings env tAll
|
adamc@747
|
480 (box [string "PQgetvalue(res, i, ",
|
adamc@747
|
481 string (Int.toString i),
|
adamc@747
|
482 string ")"])
|
adamc@747
|
483 (box [string "PQgetlength(res, i, ",
|
adamc@747
|
484 string (Int.toString i),
|
adamc@747
|
485 string ")"]),
|
adamc@747
|
486 string ")"]
|
adamc@467
|
487
|
adamc@867
|
488 datatype sql_type = datatype Settings.sql_type
|
adamc@867
|
489 val isBlob = Settings.isBlob
|
adamc@737
|
490
|
adamc@739
|
491 fun isFile (t : typ) =
|
adamc@737
|
492 case #1 t of
|
adamc@739
|
493 TFfi ("Basis", "file") => true
|
adamc@737
|
494 | _ => false
|
adamc@737
|
495
|
adamc@1011
|
496 fun p_sql_type t = string (Settings.p_sql_ctype t)
|
adamc@282
|
497
|
adamc@282
|
498 fun getPargs (e, _) =
|
adamc@282
|
499 case e of
|
adamc@282
|
500 EPrim (Prim.String _) => []
|
adamc@282
|
501 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
|
adamc@282
|
502
|
adamc@282
|
503 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
|
adamc@282
|
504 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
|
adamc@282
|
505 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
|
adamc@282
|
506 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
|
adamc@439
|
507 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)]
|
adamc@737
|
508 | EFfiApp ("Basis", "sqlifyBlob", [e]) => [(e, Blob)]
|
adamc@678
|
509 | EFfiApp ("Basis", "sqlifyChannel", [e]) => [(e, Channel)]
|
adamc@682
|
510 | EFfiApp ("Basis", "sqlifyClient", [e]) => [(e, Client)]
|
adamc@468
|
511
|
adamc@678
|
512 | ECase (e,
|
adamc@678
|
513 [((PNone _, _),
|
adamc@678
|
514 (EPrim (Prim.String "NULL"), _)),
|
adamc@678
|
515 ((PSome (_, (PVar _, _)), _),
|
adamc@678
|
516 (EFfiApp (m, x, [(ERel 0, _)]), _))],
|
adamc@678
|
517 _) => map (fn (x, y) => (x, Nullable y)) (getPargs (EFfiApp (m, x, [e]), #2 e))
|
adamc@468
|
518
|
adamc@491
|
519 | ECase (e,
|
adamc@491
|
520 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
|
adamc@491
|
521 (EPrim (Prim.String "TRUE"), _)),
|
adamc@491
|
522 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
|
adamc@491
|
523 (EPrim (Prim.String "FALSE"), _))],
|
adamc@491
|
524 _) => [(e, Bool)]
|
adamc@282
|
525
|
adamc@282
|
526 | _ => raise Fail "CjrPrint: getPargs"
|
adamc@282
|
527
|
adamc@1324
|
528 val notLeakies = SS.fromList ["int", "float", "char", "time", "bool", "unit", "client", "channel",
|
adamc@1324
|
529 "xhtml", "page", "xbody", "css_class"]
|
adamc@1324
|
530 val notLeakies' = SS.fromList ["blob"]
|
adamc@1324
|
531
|
adamc@324
|
532 fun notLeaky env allowHeapAllocated =
|
adamc@324
|
533 let
|
adamc@638
|
534 fun nl ok (t, _) =
|
adamc@324
|
535 case t of
|
adamc@324
|
536 TFun _ => false
|
adamc@324
|
537 | TRecord n =>
|
adamc@324
|
538 let
|
adamc@324
|
539 val xts = E.lookupStruct env n
|
adamc@324
|
540 in
|
adamc@638
|
541 List.all (fn (_, t) => nl ok t) xts
|
adamc@324
|
542 end
|
adamc@638
|
543 | TDatatype (dk, n, ref cons) =>
|
adamc@638
|
544 IS.member (ok, n)
|
adamc@638
|
545 orelse
|
adamc@638
|
546 ((allowHeapAllocated orelse dk = Enum)
|
adamc@638
|
547 andalso
|
adamc@638
|
548 let
|
adamc@638
|
549 val ok' = IS.add (ok, n)
|
adamc@638
|
550 in
|
adamc@638
|
551 List.all (fn (_, _, to) => case to of
|
adamc@638
|
552 NONE => true
|
adamc@638
|
553 | SOME t => nl ok' t) cons
|
adamc@638
|
554 end)
|
adamc@1324
|
555 | TFfi ("Basis", t) => SS.member (notLeakies, t)
|
adamc@1324
|
556 orelse (allowHeapAllocated andalso SS.member (notLeakies', t))
|
adamc@1324
|
557 | TFfi _ => false
|
adamc@638
|
558 | TOption t => allowHeapAllocated andalso nl ok t
|
adamc@757
|
559 | TList (t, _) => allowHeapAllocated andalso nl ok t
|
adamc@324
|
560 in
|
adamc@638
|
561 nl IS.empty
|
adamc@324
|
562 end
|
adamc@324
|
563
|
adamc@463
|
564 fun capitalize s =
|
adamc@463
|
565 if s = "" then
|
adamc@463
|
566 ""
|
adamc@463
|
567 else
|
adamc@463
|
568 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@463
|
569
|
adamc@1023
|
570 fun unurlify fromClient env (t, loc) =
|
adamc@463
|
571 let
|
adamc@463
|
572 fun unurlify' rf t =
|
adamc@463
|
573 case t of
|
adamc@1109
|
574 TFfi ("Basis", "unit") => string "uw_Basis_unurlifyUnit(ctx, &request)"
|
adamc@1023
|
575 | TFfi ("Basis", "string") => string (if fromClient then
|
adamc@1023
|
576 "uw_Basis_unurlifyString_fromClient(ctx, &request)"
|
adamc@1023
|
577 else
|
adamc@1023
|
578 "uw_Basis_unurlifyString(ctx, &request)")
|
adamc@463
|
579 | TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
|
adamc@463
|
580
|
adamc@1109
|
581 | TRecord 0 => string "uw_Basis_unurlifyUnit(ctx, &request)"
|
adamc@463
|
582 | TRecord i =>
|
adamc@463
|
583 let
|
adamc@463
|
584 val xts = E.lookupStruct env i
|
adamc@463
|
585 in
|
adamc@463
|
586 box [string "({",
|
adamc@463
|
587 newline,
|
adamc@463
|
588 box (map (fn (x, t) =>
|
adamc@463
|
589 box [p_typ env t,
|
adamc@463
|
590 space,
|
adamc@463
|
591 string "uwr_",
|
adamc@463
|
592 string x,
|
adamc@463
|
593 space,
|
adamc@463
|
594 string "=",
|
adamc@463
|
595 space,
|
adamc@463
|
596 unurlify' rf (#1 t),
|
adamc@463
|
597 string ";",
|
adamc@463
|
598 newline]) xts),
|
adamc@463
|
599 string "struct",
|
adamc@463
|
600 space,
|
adamc@463
|
601 string "__uws_",
|
adamc@463
|
602 string (Int.toString i),
|
adamc@463
|
603 space,
|
adamc@463
|
604 string "tmp",
|
adamc@463
|
605 space,
|
adamc@463
|
606 string "=",
|
adamc@463
|
607 space,
|
adamc@463
|
608 string "{",
|
adamc@463
|
609 space,
|
adamc@463
|
610 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
|
adamc@463
|
611 string x]) xts,
|
adamc@463
|
612 space,
|
adamc@463
|
613 string "};",
|
adamc@463
|
614 newline,
|
adamc@463
|
615 string "tmp;",
|
adamc@463
|
616 newline,
|
adamc@463
|
617 string "})"]
|
adamc@463
|
618 end
|
adamc@463
|
619
|
adamc@463
|
620 | TDatatype (Enum, i, _) =>
|
adamc@463
|
621 let
|
adamc@463
|
622 val (x, xncs) = E.lookupDatatype env i
|
adamc@463
|
623
|
adamc@463
|
624 fun doEm xncs =
|
adamc@463
|
625 case xncs of
|
adamc@463
|
626 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
|
adamc@463
|
627 ^ x ^ "\"), (enum __uwe_"
|
adamc@463
|
628 ^ x ^ "_" ^ Int.toString i ^ ")0)")
|
adamc@463
|
629 | (x', n, to) :: rest =>
|
adamc@463
|
630 box [string "((!strncmp(request, \"",
|
adamc@463
|
631 string x',
|
adamc@463
|
632 string "\", ",
|
adamc@463
|
633 string (Int.toString (size x')),
|
adamc@463
|
634 string ") && (request[",
|
adamc@463
|
635 string (Int.toString (size x')),
|
adamc@463
|
636 string "] == 0 || request[",
|
adamc@463
|
637 string (Int.toString (size x')),
|
adam@1360
|
638 string "] == '/')) ? (request += ",
|
adam@1360
|
639 string (Int.toString (size x')),
|
adam@1360
|
640 string (", (*request == '/' ? ++request : NULL), __uwc_" ^ ident x' ^ "_" ^ Int.toString n ^ ")"),
|
adamc@463
|
641 space,
|
adamc@463
|
642 string ":",
|
adamc@463
|
643 space,
|
adamc@463
|
644 doEm rest,
|
adamc@463
|
645 string ")"]
|
adamc@463
|
646 in
|
adamc@463
|
647 doEm xncs
|
adamc@463
|
648 end
|
adamc@463
|
649
|
adamc@463
|
650 | TDatatype (Option, i, xncs) =>
|
adamc@463
|
651 if IS.member (rf, i) then
|
adamc@463
|
652 box [string "unurlify_",
|
adamc@463
|
653 string (Int.toString i),
|
adamc@463
|
654 string "()"]
|
adamc@463
|
655 else
|
adamc@463
|
656 let
|
adamc@463
|
657 val (x, _) = E.lookupDatatype env i
|
adamc@463
|
658
|
adamc@463
|
659 val (no_arg, has_arg, t) =
|
adamc@463
|
660 case !xncs of
|
adamc@463
|
661 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@463
|
662 (no_arg, has_arg, t)
|
adamc@463
|
663 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@463
|
664 (no_arg, has_arg, t)
|
adamc@463
|
665 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
|
adamc@463
|
666
|
adamc@463
|
667 val rf = IS.add (rf, i)
|
adamc@463
|
668 in
|
adamc@463
|
669 box [string "({",
|
adamc@463
|
670 space,
|
adamc@463
|
671 p_typ env t,
|
adamc@463
|
672 space,
|
adamc@463
|
673 string "*unurlify_",
|
adamc@463
|
674 string (Int.toString i),
|
adamc@463
|
675 string "(void) {",
|
adamc@463
|
676 newline,
|
adamc@463
|
677 box [string "return (request[0] == '/' ? ++request : request,",
|
adamc@463
|
678 newline,
|
adamc@463
|
679 string "((!strncmp(request, \"",
|
adamc@463
|
680 string no_arg,
|
adamc@463
|
681 string "\", ",
|
adamc@463
|
682 string (Int.toString (size no_arg)),
|
adamc@463
|
683 string ") && (request[",
|
adamc@463
|
684 string (Int.toString (size no_arg)),
|
adamc@463
|
685 string "] == 0 || request[",
|
adamc@463
|
686 string (Int.toString (size no_arg)),
|
adamc@463
|
687 string "] == '/')) ? (request",
|
adamc@463
|
688 space,
|
adamc@463
|
689 string "+=",
|
adamc@463
|
690 space,
|
adamc@463
|
691 string (Int.toString (size no_arg)),
|
adamc@463
|
692 string ", NULL) : ((!strncmp(request, \"",
|
adamc@463
|
693 string has_arg,
|
adamc@463
|
694 string "\", ",
|
adamc@463
|
695 string (Int.toString (size has_arg)),
|
adamc@463
|
696 string ") && (request[",
|
adamc@463
|
697 string (Int.toString (size has_arg)),
|
adamc@463
|
698 string "] == 0 || request[",
|
adamc@463
|
699 string (Int.toString (size has_arg)),
|
adamc@463
|
700 string "] == '/')) ? (request",
|
adamc@463
|
701 space,
|
adamc@463
|
702 string "+=",
|
adamc@463
|
703 space,
|
adamc@463
|
704 string (Int.toString (size has_arg)),
|
adamc@463
|
705 string ", (request[0] == '/' ? ++request : NULL), ",
|
adamc@463
|
706 newline,
|
adamc@463
|
707
|
adamc@463
|
708 if isUnboxable t then
|
adamc@463
|
709 unurlify' rf (#1 t)
|
adamc@463
|
710 else
|
adamc@463
|
711 box [string "({",
|
adamc@463
|
712 newline,
|
adamc@463
|
713 p_typ env t,
|
adamc@463
|
714 space,
|
adamc@463
|
715 string "*tmp",
|
adamc@463
|
716 space,
|
adamc@463
|
717 string "=",
|
adamc@463
|
718 space,
|
adamc@463
|
719 string "uw_malloc(ctx, sizeof(",
|
adamc@463
|
720 p_typ env t,
|
adamc@463
|
721 string "));",
|
adamc@463
|
722 newline,
|
adamc@463
|
723 string "*tmp",
|
adamc@463
|
724 space,
|
adamc@463
|
725 string "=",
|
adamc@463
|
726 space,
|
adamc@463
|
727 unurlify' rf (#1 t),
|
adamc@463
|
728 string ";",
|
adamc@463
|
729 newline,
|
adamc@463
|
730 string "tmp;",
|
adamc@463
|
731 newline,
|
adamc@463
|
732 string "})"],
|
adamc@463
|
733 string ")",
|
adamc@463
|
734 newline,
|
adamc@463
|
735 string ":",
|
adamc@463
|
736 space,
|
adamc@463
|
737 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
|
adamc@463
|
738 ^ "\"), NULL))));"),
|
adamc@463
|
739 newline],
|
adamc@463
|
740 string "}",
|
adamc@463
|
741 newline,
|
adamc@463
|
742 newline,
|
adamc@463
|
743
|
adamc@463
|
744 string "unurlify_",
|
adamc@463
|
745 string (Int.toString i),
|
adamc@463
|
746 string "();",
|
adamc@463
|
747 newline,
|
adamc@463
|
748 string "})"]
|
adamc@463
|
749 end
|
adamc@463
|
750
|
adamc@463
|
751 | TDatatype (Default, i, _) =>
|
adamc@463
|
752 if IS.member (rf, i) then
|
adamc@463
|
753 box [string "unurlify_",
|
adamc@463
|
754 string (Int.toString i),
|
adamc@463
|
755 string "()"]
|
adamc@463
|
756 else
|
adamc@463
|
757 let
|
adamc@463
|
758 val (x, xncs) = E.lookupDatatype env i
|
adamc@463
|
759
|
adamc@463
|
760 val rf = IS.add (rf, i)
|
adamc@463
|
761
|
adamc@463
|
762 fun doEm xncs =
|
adamc@463
|
763 case xncs of
|
adamc@463
|
764 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
|
adamc@463
|
765 ^ x ^ "\"), NULL)")
|
adamc@463
|
766 | (x', n, to) :: rest =>
|
adamc@463
|
767 box [string "((!strncmp(request, \"",
|
adamc@463
|
768 string x',
|
adamc@463
|
769 string "\", ",
|
adamc@463
|
770 string (Int.toString (size x')),
|
adamc@463
|
771 string ") && (request[",
|
adamc@463
|
772 string (Int.toString (size x')),
|
adamc@463
|
773 string "] == 0 || request[",
|
adamc@463
|
774 string (Int.toString (size x')),
|
adamc@463
|
775 string "] == '/')) ? ({",
|
adamc@463
|
776 newline,
|
adamc@463
|
777 string "struct",
|
adamc@463
|
778 space,
|
adamc@463
|
779 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
|
adamc@463
|
780 space,
|
adamc@463
|
781 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
|
adamc@463
|
782 string x,
|
adamc@463
|
783 string "_",
|
adamc@463
|
784 string (Int.toString i),
|
adamc@463
|
785 string "));",
|
adamc@463
|
786 newline,
|
adamc@463
|
787 string "tmp->tag",
|
adamc@463
|
788 space,
|
adamc@463
|
789 string "=",
|
adamc@463
|
790 space,
|
adamc@463
|
791 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
|
adamc@463
|
792 string ";",
|
adamc@463
|
793 newline,
|
adamc@463
|
794 string "request",
|
adamc@463
|
795 space,
|
adamc@463
|
796 string "+=",
|
adamc@463
|
797 space,
|
adamc@463
|
798 string (Int.toString (size x')),
|
adamc@463
|
799 string ";",
|
adamc@463
|
800 newline,
|
adamc@463
|
801 string "if (request[0] == '/') ++request;",
|
adamc@463
|
802 newline,
|
adamc@463
|
803 case to of
|
adamc@463
|
804 NONE => box []
|
adamc@463
|
805 | SOME (t, _) => box [string "tmp->data.uw_",
|
adamc@463
|
806 p_ident x',
|
adamc@463
|
807 space,
|
adamc@463
|
808 string "=",
|
adamc@463
|
809 space,
|
adamc@463
|
810 unurlify' rf t,
|
adamc@463
|
811 string ";",
|
adamc@463
|
812 newline],
|
adamc@463
|
813 string "tmp;",
|
adamc@463
|
814 newline,
|
adamc@463
|
815 string "})",
|
adamc@463
|
816 space,
|
adamc@463
|
817 string ":",
|
adamc@463
|
818 space,
|
adamc@463
|
819 doEm rest,
|
adamc@463
|
820 string ")"]
|
adamc@463
|
821 in
|
adamc@463
|
822 box [string "({",
|
adamc@463
|
823 space,
|
adamc@463
|
824 p_typ env (t, ErrorMsg.dummySpan),
|
adamc@463
|
825 space,
|
adamc@463
|
826 string "unurlify_",
|
adamc@463
|
827 string (Int.toString i),
|
adamc@463
|
828 string "(void) {",
|
adamc@463
|
829 newline,
|
adamc@463
|
830 box [string "return",
|
adamc@463
|
831 space,
|
adamc@463
|
832 doEm xncs,
|
adamc@463
|
833 string ";",
|
adamc@463
|
834 newline],
|
adamc@463
|
835 string "}",
|
adamc@463
|
836 newline,
|
adamc@463
|
837 newline,
|
adamc@463
|
838
|
adamc@463
|
839 string "unurlify_",
|
adamc@463
|
840 string (Int.toString i),
|
adamc@463
|
841 string "();",
|
adamc@463
|
842 newline,
|
adamc@463
|
843 string "})"]
|
adamc@463
|
844 end
|
adamc@463
|
845
|
adamc@758
|
846 | TList (t', i) =>
|
adamc@758
|
847 if IS.member (rf, i) then
|
adamc@758
|
848 box [string "unurlify_list_",
|
adamc@758
|
849 string (Int.toString i),
|
adamc@758
|
850 string "()"]
|
adamc@758
|
851 else
|
adamc@758
|
852 let
|
adamc@758
|
853 val rf = IS.add (rf, i)
|
adamc@758
|
854 in
|
adamc@758
|
855 box [string "({",
|
adamc@758
|
856 space,
|
adamc@758
|
857 p_typ env (t, loc),
|
adamc@758
|
858 space,
|
adamc@758
|
859 string "unurlify_list_",
|
adamc@758
|
860 string (Int.toString i),
|
adamc@758
|
861 string "(void) {",
|
adamc@758
|
862 newline,
|
adamc@758
|
863 box [string "return (request[0] == '/' ? ++request : request,",
|
adamc@758
|
864 newline,
|
adamc@758
|
865 string "((!strncmp(request, \"Nil\", 3) && (request[3] == 0 ",
|
adamc@758
|
866 string "|| request[3] == '/')) ? (request",
|
adamc@758
|
867 space,
|
adamc@758
|
868 string "+=",
|
adamc@758
|
869 space,
|
adam@1322
|
870 string "3, (*request == '/' ? *request++ = 0 : 0), NULL) : ((!strncmp(request, \"Cons\", 4) && (request[4] == 0 ",
|
adamc@758
|
871 string "|| request[4] == '/')) ? (request",
|
adamc@758
|
872 space,
|
adamc@758
|
873 string "+=",
|
adamc@758
|
874 space,
|
adamc@758
|
875 string "4, (request[0] == '/' ? ++request : NULL), ",
|
adamc@758
|
876 newline,
|
adamc@758
|
877
|
adamc@758
|
878 string "({",
|
adamc@758
|
879 newline,
|
adamc@758
|
880 p_typ env (t, loc),
|
adamc@758
|
881 space,
|
adamc@758
|
882 string "tmp",
|
adamc@758
|
883 space,
|
adamc@758
|
884 string "=",
|
adamc@758
|
885 space,
|
adamc@758
|
886 string "uw_malloc(ctx, sizeof(struct __uws_",
|
adamc@758
|
887 string (Int.toString i),
|
adamc@758
|
888 string "));",
|
adamc@758
|
889 newline,
|
adamc@758
|
890 string "*tmp",
|
adamc@758
|
891 space,
|
adamc@758
|
892 string "=",
|
adamc@758
|
893 space,
|
adamc@758
|
894 unurlify' rf (TRecord i),
|
adamc@758
|
895 string ";",
|
adamc@758
|
896 newline,
|
adamc@758
|
897 string "tmp;",
|
adamc@758
|
898 newline,
|
adamc@758
|
899 string "})",
|
adamc@758
|
900 string ")",
|
adamc@758
|
901 newline,
|
adamc@758
|
902 string ":",
|
adamc@758
|
903 space,
|
adam@1322
|
904 string ("(uw_error(ctx, FATAL, \"Error unurlifying list: %s\", request), NULL))));"),
|
adamc@758
|
905 newline],
|
adamc@758
|
906 string "}",
|
adamc@758
|
907 newline,
|
adamc@758
|
908 newline,
|
adamc@758
|
909
|
adamc@758
|
910 string "unurlify_list_",
|
adamc@758
|
911 string (Int.toString i),
|
adamc@758
|
912 string "();",
|
adamc@758
|
913 newline,
|
adamc@758
|
914 string "})"]
|
adamc@758
|
915 end
|
adamc@758
|
916
|
adamc@471
|
917 | TOption t =>
|
adamc@471
|
918 box [string "(request[0] == '/' ? ++request : request, ",
|
adamc@471
|
919 string "((!strncmp(request, \"None\", 4) ",
|
adamc@471
|
920 string "&& (request[4] == 0 || request[4] == '/')) ",
|
adamc@931
|
921 string "? (request += (request[4] == 0 ? 4 : 5), NULL) ",
|
adamc@471
|
922 string ": ((!strncmp(request, \"Some\", 4) ",
|
adamc@471
|
923 string "&& request[4] == '/') ",
|
adamc@471
|
924 string "? (request += 5, ",
|
adamc@471
|
925 if isUnboxable t then
|
adamc@471
|
926 unurlify' rf (#1 t)
|
adamc@471
|
927 else
|
adamc@471
|
928 box [string "({",
|
adamc@471
|
929 newline,
|
adamc@471
|
930 p_typ env t,
|
adamc@471
|
931 space,
|
adamc@471
|
932 string "*tmp",
|
adamc@471
|
933 space,
|
adamc@471
|
934 string "=",
|
adamc@471
|
935 space,
|
adamc@471
|
936 string "uw_malloc(ctx, sizeof(",
|
adamc@471
|
937 p_typ env t,
|
adamc@471
|
938 string "));",
|
adamc@471
|
939 newline,
|
adamc@471
|
940 string "*tmp",
|
adamc@471
|
941 space,
|
adamc@471
|
942 string "=",
|
adamc@471
|
943 space,
|
adamc@471
|
944 unurlify' rf (#1 t),
|
adamc@471
|
945 string ";",
|
adamc@471
|
946 newline,
|
adamc@471
|
947 string "tmp;",
|
adamc@471
|
948 newline,
|
adamc@471
|
949 string "})"],
|
adamc@471
|
950 string ") :",
|
adamc@471
|
951 space,
|
adamc@471
|
952 string "(uw_error(ctx, FATAL, \"Error unurlifying option type\"), NULL))))"]
|
adamc@471
|
953
|
adamc@463
|
954 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
|
adamc@463
|
955 space)
|
adamc@463
|
956 in
|
adamc@463
|
957 unurlify' IS.empty t
|
adamc@463
|
958 end
|
adamc@463
|
959
|
adamc@905
|
960 val urlify1 = ref 0
|
adamc@905
|
961
|
adamc@610
|
962 fun urlify env t =
|
adamc@610
|
963 let
|
adamc@905
|
964 fun urlify' rf rfl level (t as (_, loc)) =
|
adamc@610
|
965 case #1 t of
|
adamc@610
|
966 TFfi ("Basis", "unit") => box []
|
adamc@610
|
967 | TFfi (m, t) => box [string ("uw_" ^ ident m ^ "_urlify" ^ capitalize t
|
adamc@610
|
968 ^ "_w(ctx, it" ^ Int.toString level ^ ");"),
|
adamc@610
|
969 newline]
|
adamc@610
|
970
|
adamc@610
|
971 | TRecord 0 => box []
|
adamc@610
|
972 | TRecord i =>
|
adamc@610
|
973 let
|
adamc@611
|
974 fun empty (t, _) =
|
adamc@611
|
975 case t of
|
adamc@611
|
976 TFfi ("Basis", "unit") => true
|
adamc@611
|
977 | TRecord 0 => true
|
adamc@611
|
978 | TRecord j =>
|
adamc@611
|
979 List.all (fn (_, t) => empty t) (E.lookupStruct env j)
|
adamc@611
|
980 | _ => false
|
adamc@611
|
981
|
adamc@610
|
982 val xts = E.lookupStruct env i
|
adamc@611
|
983
|
adamc@613
|
984 val (blocks, _) = foldl
|
adamc@613
|
985 (fn ((x, t), (blocks, printingSinceLastSlash)) =>
|
adamc@613
|
986 let
|
adamc@613
|
987 val thisEmpty = empty t
|
adamc@613
|
988 in
|
adamc@613
|
989 if thisEmpty then
|
adamc@613
|
990 (blocks, printingSinceLastSlash)
|
adamc@613
|
991 else
|
adamc@613
|
992 (box [string "{",
|
adamc@613
|
993 newline,
|
adamc@613
|
994 p_typ env t,
|
adamc@613
|
995 space,
|
adamc@613
|
996 string ("it" ^ Int.toString (level + 1)),
|
adamc@613
|
997 space,
|
adamc@613
|
998 string "=",
|
adamc@613
|
999 space,
|
adamc@613
|
1000 string ("it" ^ Int.toString level ^ ".__uwf_" ^ x ^ ";"),
|
adamc@613
|
1001 newline,
|
adamc@613
|
1002 box (if printingSinceLastSlash then
|
adamc@613
|
1003 [string "uw_write(ctx, \"/\");",
|
adamc@613
|
1004 newline]
|
adamc@613
|
1005 else
|
adamc@613
|
1006 []),
|
adamc@905
|
1007 urlify' rf rfl (level + 1) t,
|
adamc@613
|
1008 string "}",
|
adamc@613
|
1009 newline] :: blocks,
|
adamc@613
|
1010 true)
|
adamc@613
|
1011 end)
|
adamc@613
|
1012 ([], false) xts
|
adamc@610
|
1013 in
|
adamc@613
|
1014 box (rev blocks)
|
adamc@610
|
1015 end
|
adamc@610
|
1016
|
adamc@638
|
1017 | TDatatype (Enum, i, _) =>
|
adamc@638
|
1018 let
|
adamc@610
|
1019 val (x, xncs) = E.lookupDatatype env i
|
adamc@610
|
1020
|
adamc@610
|
1021 fun doEm xncs =
|
adamc@610
|
1022 case xncs of
|
adamc@638
|
1023 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
|
adamc@638
|
1024 ^ x ^ "\");"),
|
adamc@638
|
1025 newline]
|
adamc@610
|
1026 | (x', n, to) :: rest =>
|
adamc@638
|
1027 box [string ("if (it" ^ Int.toString level
|
adamc@638
|
1028 ^ "==__uwc_" ^ ident x' ^ "_" ^ Int.toString n ^ ") {"),
|
adamc@638
|
1029 newline,
|
adamc@638
|
1030 box [string ("uw_write(ctx, \"" ^ x' ^ "\");"),
|
adamc@638
|
1031 newline],
|
adamc@638
|
1032 string "} else {",
|
adamc@638
|
1033 newline,
|
adamc@638
|
1034 box [doEm rest,
|
adamc@638
|
1035 newline],
|
adamc@638
|
1036 string "}"]
|
adamc@610
|
1037 in
|
adamc@610
|
1038 doEm xncs
|
adamc@638
|
1039 end
|
adamc@610
|
1040
|
adamc@639
|
1041 | TDatatype (Option, i, xncs) =>
|
adamc@639
|
1042 if IS.member (rf, i) then
|
adamc@639
|
1043 box [string "urlify_",
|
adamc@610
|
1044 string (Int.toString i),
|
adamc@639
|
1045 string "(it",
|
adamc@639
|
1046 string (Int.toString level),
|
adamc@639
|
1047 string ");",
|
adamc@639
|
1048 newline]
|
adamc@610
|
1049 else
|
adamc@610
|
1050 let
|
adamc@610
|
1051 val (x, _) = E.lookupDatatype env i
|
adamc@610
|
1052
|
adamc@610
|
1053 val (no_arg, has_arg, t) =
|
adamc@610
|
1054 case !xncs of
|
adamc@610
|
1055 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@610
|
1056 (no_arg, has_arg, t)
|
adamc@610
|
1057 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@610
|
1058 (no_arg, has_arg, t)
|
adamc@639
|
1059 | _ => raise Fail "CjrPrint: urlify misclassified Option datatype"
|
adamc@610
|
1060
|
adamc@610
|
1061 val rf = IS.add (rf, i)
|
adamc@610
|
1062 in
|
adamc@610
|
1063 box [string "({",
|
adamc@610
|
1064 space,
|
adamc@639
|
1065 string "void",
|
adamc@639
|
1066 space,
|
adamc@639
|
1067 string "urlify_",
|
adamc@639
|
1068 string (Int.toString i),
|
adamc@639
|
1069 string "(",
|
adamc@610
|
1070 p_typ env t,
|
adamc@610
|
1071 space,
|
adamc@639
|
1072 if isUnboxable t then
|
adamc@639
|
1073 box []
|
adamc@639
|
1074 else
|
adamc@639
|
1075 string "*",
|
adamc@639
|
1076 string "it0) {",
|
adamc@610
|
1077 newline,
|
adamc@639
|
1078 box [string "if (it0) {",
|
adamc@905
|
1079 newline,
|
adamc@639
|
1080 if isUnboxable t then
|
adamc@905
|
1081 urlify' rf rfl 0 t
|
adamc@610
|
1082 else
|
adamc@639
|
1083 box [p_typ env t,
|
adamc@610
|
1084 space,
|
adamc@639
|
1085 string "it1",
|
adamc@610
|
1086 space,
|
adamc@610
|
1087 string "=",
|
adamc@610
|
1088 space,
|
adamc@639
|
1089 string "*it0;",
|
adamc@610
|
1090 newline,
|
adamc@639
|
1091 string "uw_write(ctx, \"",
|
adamc@639
|
1092 string has_arg,
|
adamc@639
|
1093 string "/\");",
|
adamc@639
|
1094 newline,
|
adamc@905
|
1095 urlify' rf rfl 1 t,
|
adamc@610
|
1096 string ";",
|
adamc@639
|
1097 newline],
|
adamc@639
|
1098 string "} else {",
|
adamc@905
|
1099 box [newline,
|
adamc@905
|
1100 string "uw_write(ctx, \"",
|
adamc@639
|
1101 string no_arg,
|
adamc@639
|
1102 string "\");",
|
adamc@639
|
1103 newline],
|
adamc@639
|
1104 string "}",
|
adamc@610
|
1105 newline],
|
adamc@610
|
1106 string "}",
|
adamc@610
|
1107 newline,
|
adamc@610
|
1108 newline,
|
adamc@610
|
1109
|
adamc@639
|
1110 string "urlify_",
|
adamc@610
|
1111 string (Int.toString i),
|
adamc@639
|
1112 string "(it",
|
adamc@639
|
1113 string (Int.toString level),
|
adamc@639
|
1114 string ");",
|
adamc@610
|
1115 newline,
|
adamc@639
|
1116 string "});",
|
adamc@639
|
1117 newline]
|
adamc@639
|
1118 end
|
adamc@610
|
1119
|
adamc@640
|
1120 | TDatatype (Default, i, _) =>
|
adamc@640
|
1121 if IS.member (rf, i) then
|
adamc@640
|
1122 box [string "urlify_",
|
adamc@610
|
1123 string (Int.toString i),
|
adamc@640
|
1124 string "(it",
|
adamc@640
|
1125 string (Int.toString level),
|
adamc@640
|
1126 string ");",
|
adamc@640
|
1127 newline]
|
adamc@610
|
1128 else
|
adamc@610
|
1129 let
|
adamc@610
|
1130 val (x, xncs) = E.lookupDatatype env i
|
adamc@610
|
1131
|
adamc@610
|
1132 val rf = IS.add (rf, i)
|
adamc@610
|
1133
|
adamc@610
|
1134 fun doEm xncs =
|
adamc@610
|
1135 case xncs of
|
adamc@640
|
1136 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
|
adamc@640
|
1137 ^ x ^ " (%d)\", it0->data);"),
|
adamc@640
|
1138 newline]
|
adamc@610
|
1139 | (x', n, to) :: rest =>
|
adamc@640
|
1140 box [string "if",
|
adamc@610
|
1141 space,
|
adamc@640
|
1142 string "(it0->tag==__uwc_",
|
adamc@640
|
1143 string (ident x'),
|
adamc@610
|
1144 string "_",
|
adamc@640
|
1145 string (Int.toString n),
|
adamc@640
|
1146 string ") {",
|
adamc@610
|
1147 newline,
|
adamc@610
|
1148 case to of
|
adamc@640
|
1149 NONE => box [string "uw_write(ctx, \"",
|
adamc@640
|
1150 string x',
|
adamc@640
|
1151 string "\");",
|
adamc@640
|
1152 newline]
|
adamc@640
|
1153 | SOME t => box [string "uw_write(ctx, \"",
|
adamc@640
|
1154 string x',
|
adamc@640
|
1155 string "/\");",
|
adamc@640
|
1156 newline,
|
adamc@640
|
1157 p_typ env t,
|
adamc@640
|
1158 space,
|
adamc@640
|
1159 string "it1",
|
adamc@640
|
1160 space,
|
adamc@640
|
1161 string "=",
|
adamc@640
|
1162 space,
|
adamc@640
|
1163 string "it0->data.uw_",
|
adamc@640
|
1164 string x',
|
adamc@640
|
1165 string ";",
|
adamc@640
|
1166 newline,
|
adamc@905
|
1167 urlify' rf rfl 1 t,
|
adamc@640
|
1168 newline],
|
adamc@640
|
1169 string "} else {",
|
adamc@610
|
1170 newline,
|
adamc@640
|
1171 box [doEm rest,
|
adamc@640
|
1172 newline],
|
adamc@640
|
1173 string "}",
|
adamc@640
|
1174 newline]
|
adamc@610
|
1175 in
|
adamc@610
|
1176 box [string "({",
|
adamc@610
|
1177 space,
|
adamc@640
|
1178 string "void",
|
adamc@610
|
1179 space,
|
adamc@640
|
1180 string "urlify_",
|
adamc@610
|
1181 string (Int.toString i),
|
adamc@640
|
1182 string "(",
|
adamc@640
|
1183 p_typ env t,
|
adamc@640
|
1184 space,
|
adamc@640
|
1185 string "it0) {",
|
adamc@610
|
1186 newline,
|
adamc@640
|
1187 box [doEm xncs,
|
adamc@610
|
1188 newline],
|
adamc@640
|
1189 newline,
|
adamc@610
|
1190 string "}",
|
adamc@610
|
1191 newline,
|
adamc@640
|
1192
|
adamc@640
|
1193 string "urlify_",
|
adamc@640
|
1194 string (Int.toString i),
|
adamc@640
|
1195 string "(it",
|
adamc@640
|
1196 string (Int.toString level),
|
adamc@640
|
1197 string ");",
|
adamc@610
|
1198 newline,
|
adamc@640
|
1199 string "});",
|
adamc@640
|
1200 newline]
|
adamc@640
|
1201 end
|
adamc@610
|
1202
|
adamc@641
|
1203 | TOption t =>
|
adamc@641
|
1204 box [string "if (it",
|
adamc@641
|
1205 string (Int.toString level),
|
adamc@641
|
1206 string ") {",
|
adamc@641
|
1207 if isUnboxable t then
|
adamc@641
|
1208 box [string "uw_write(ctx, \"Some/\");",
|
adamc@641
|
1209 newline,
|
adamc@905
|
1210 urlify' rf rfl level t]
|
adamc@610
|
1211 else
|
adamc@641
|
1212 box [p_typ env t,
|
adamc@610
|
1213 space,
|
adamc@641
|
1214 string "it",
|
adamc@641
|
1215 string (Int.toString (level + 1)),
|
adamc@610
|
1216 space,
|
adamc@610
|
1217 string "=",
|
adamc@610
|
1218 space,
|
adamc@641
|
1219 string "*it",
|
adamc@641
|
1220 string (Int.toString level),
|
adamc@610
|
1221 string ";",
|
adamc@610
|
1222 newline,
|
adamc@641
|
1223 string "uw_write(ctx, \"Some/\");",
|
adamc@610
|
1224 newline,
|
adamc@905
|
1225 urlify' rf rfl (level + 1) t,
|
adamc@641
|
1226 string ";",
|
adamc@641
|
1227 newline],
|
adamc@641
|
1228 string "} else {",
|
adamc@905
|
1229 box [newline,
|
adamc@905
|
1230 string "uw_write(ctx, \"None\");",
|
adamc@641
|
1231 newline],
|
adamc@641
|
1232 string "}",
|
adamc@641
|
1233 newline]
|
adamc@610
|
1234
|
adamc@905
|
1235 | TList (t, i) =>
|
adamc@905
|
1236 if IS.member (rfl, i) then
|
adamc@905
|
1237 box [string "urlifyl_",
|
adamc@905
|
1238 string (Int.toString i),
|
adamc@905
|
1239 string "(it",
|
adamc@905
|
1240 string (Int.toString level),
|
adamc@905
|
1241 string ");",
|
adamc@905
|
1242 newline]
|
adamc@905
|
1243 else
|
adamc@905
|
1244 let
|
adamc@905
|
1245 val rfl = IS.add (rfl, i)
|
adamc@905
|
1246 in
|
adamc@905
|
1247 box [string "({",
|
adamc@905
|
1248 space,
|
adamc@905
|
1249 string "void",
|
adamc@905
|
1250 space,
|
adamc@905
|
1251 string "urlifyl_",
|
adamc@905
|
1252 string (Int.toString i),
|
adamc@905
|
1253 string "(struct __uws_",
|
adamc@905
|
1254 string (Int.toString i),
|
adamc@905
|
1255 space,
|
adamc@905
|
1256 string "*it0) {",
|
adamc@905
|
1257 newline,
|
adamc@905
|
1258 box [string "if (it0) {",
|
adamc@905
|
1259 newline,
|
adamc@905
|
1260 p_typ env t,
|
adamc@905
|
1261 space,
|
adamc@905
|
1262 string "it1",
|
adamc@905
|
1263 space,
|
adamc@905
|
1264 string "=",
|
adamc@905
|
1265 space,
|
adamc@905
|
1266 string "it0->__uwf_1;",
|
adamc@905
|
1267 newline,
|
adamc@905
|
1268 string "uw_write(ctx, \"Cons/\");",
|
adamc@905
|
1269 newline,
|
adamc@905
|
1270 urlify' rf rfl 1 t,
|
adamc@905
|
1271 string ";",
|
adamc@905
|
1272 newline,
|
adamc@905
|
1273 string "uw_write(ctx, \"/\");",
|
adamc@905
|
1274 newline,
|
adamc@905
|
1275 string "urlifyl_",
|
adamc@905
|
1276 string (Int.toString i),
|
adamc@905
|
1277 string "(it0->__uwf_2);",
|
adamc@905
|
1278 newline,
|
adamc@905
|
1279 string "} else {",
|
adamc@905
|
1280 newline,
|
adamc@905
|
1281 box [string "uw_write(ctx, \"Nil\");",
|
adamc@905
|
1282 newline],
|
adamc@905
|
1283 string "}",
|
adamc@905
|
1284 newline],
|
adamc@905
|
1285 string "}",
|
adamc@905
|
1286 newline,
|
adamc@905
|
1287 newline,
|
adamc@905
|
1288
|
adamc@905
|
1289 string "urlifyl_",
|
adamc@905
|
1290 string (Int.toString i),
|
adamc@905
|
1291 string "(it",
|
adamc@905
|
1292 string (Int.toString level),
|
adamc@905
|
1293 string ");",
|
adamc@905
|
1294 newline,
|
adamc@905
|
1295 string "});",
|
adamc@905
|
1296 newline]
|
adamc@905
|
1297 end
|
adamc@905
|
1298
|
adamc@610
|
1299 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL encoding function";
|
adamc@610
|
1300 space)
|
adamc@610
|
1301 in
|
adamc@905
|
1302 urlify' IS.empty IS.empty 0 t
|
adamc@610
|
1303 end
|
adamc@610
|
1304
|
adamc@867
|
1305 fun sql_type_in env (tAll as (t, loc)) =
|
adamc@867
|
1306 case t of
|
adamc@867
|
1307 TFfi ("Basis", "int") => Int
|
adamc@867
|
1308 | TFfi ("Basis", "float") => Float
|
adamc@867
|
1309 | TFfi ("Basis", "string") => String
|
adamc@1011
|
1310 | TFfi ("Basis", "char") => Char
|
adamc@867
|
1311 | TFfi ("Basis", "bool") => Bool
|
adamc@867
|
1312 | TFfi ("Basis", "time") => Time
|
adamc@867
|
1313 | TFfi ("Basis", "blob") => Blob
|
adamc@867
|
1314 | TFfi ("Basis", "channel") => Channel
|
adamc@867
|
1315 | TFfi ("Basis", "client") => Client
|
adamc@867
|
1316 | TOption t' => Nullable (sql_type_in env t')
|
adamc@867
|
1317 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
|
adamc@867
|
1318 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@867
|
1319 Int)
|
adamc@867
|
1320
|
adamc@182
|
1321 fun p_exp' par env (e, loc) =
|
adamc@29
|
1322 case e of
|
adamc@276
|
1323 EPrim p => Prim.p_t_GCC p
|
adamc@29
|
1324 | ERel n => p_rel env n
|
adamc@109
|
1325 | ENamed n => p_enamed env n
|
adamc@188
|
1326 | ECon (Enum, pc, _) => p_patCon env pc
|
adamc@198
|
1327 | ECon (Option, pc, NONE) => string "NULL"
|
adamc@198
|
1328 | ECon (Option, pc, SOME e) =>
|
adamc@198
|
1329 let
|
adamc@198
|
1330 val to = case pc of
|
adamc@198
|
1331 PConVar n => #2 (E.lookupConstructor env n)
|
adamc@198
|
1332 | PConFfi {arg, ...} => arg
|
adamc@198
|
1333
|
adamc@198
|
1334 val t = case to of
|
adamc@198
|
1335 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
|
adamc@198
|
1336 | SOME t => t
|
adamc@198
|
1337 in
|
adamc@463
|
1338 if isUnboxable t then
|
adamc@463
|
1339 p_exp' par env e
|
adamc@463
|
1340 else
|
adamc@463
|
1341 box [string "({",
|
adamc@463
|
1342 newline,
|
adamc@463
|
1343 p_typ env t,
|
adamc@463
|
1344 space,
|
adamc@463
|
1345 string "*tmp",
|
adamc@463
|
1346 space,
|
adamc@463
|
1347 string "=",
|
adamc@463
|
1348 space,
|
adamc@463
|
1349 string "uw_malloc(ctx, sizeof(",
|
adamc@463
|
1350 p_typ env t,
|
adamc@463
|
1351 string "));",
|
adamc@463
|
1352 newline,
|
adamc@463
|
1353 string "*tmp",
|
adamc@463
|
1354 space,
|
adamc@463
|
1355 string "=",
|
adamc@463
|
1356 p_exp' par env e,
|
adamc@463
|
1357 string ";",
|
adamc@463
|
1358 newline,
|
adamc@463
|
1359 string "tmp;",
|
adamc@463
|
1360 newline,
|
adamc@463
|
1361 string "})"]
|
adamc@198
|
1362 end
|
adamc@188
|
1363 | ECon (Default, pc, eo) =>
|
adamc@181
|
1364 let
|
adamc@196
|
1365 val (xd, xc, xn) = patConInfo env pc
|
adamc@181
|
1366 in
|
adamc@182
|
1367 box [string "({",
|
adamc@181
|
1368 newline,
|
adamc@181
|
1369 string "struct",
|
adamc@181
|
1370 space,
|
adamc@185
|
1371 string xd,
|
adamc@181
|
1372 space,
|
adamc@181
|
1373 string "*tmp",
|
adamc@181
|
1374 space,
|
adamc@181
|
1375 string "=",
|
adamc@181
|
1376 space,
|
adamc@311
|
1377 string "uw_malloc(ctx, sizeof(struct ",
|
adamc@185
|
1378 string xd,
|
adamc@181
|
1379 string "));",
|
adamc@181
|
1380 newline,
|
adamc@181
|
1381 string "tmp->tag",
|
adamc@181
|
1382 space,
|
adamc@181
|
1383 string "=",
|
adamc@181
|
1384 space,
|
adamc@185
|
1385 string xc,
|
adamc@181
|
1386 string ";",
|
adamc@181
|
1387 newline,
|
adamc@181
|
1388 case eo of
|
adamc@181
|
1389 NONE => box []
|
adamc@185
|
1390 | SOME e => box [string "tmp->data.",
|
adamc@196
|
1391 string xn,
|
adamc@181
|
1392 space,
|
adamc@181
|
1393 string "=",
|
adamc@181
|
1394 space,
|
adamc@181
|
1395 p_exp env e,
|
adamc@181
|
1396 string ";",
|
adamc@181
|
1397 newline],
|
adamc@181
|
1398 string "tmp;",
|
adamc@181
|
1399 newline,
|
adamc@181
|
1400 string "})"]
|
adamc@181
|
1401 end
|
adamc@297
|
1402 | ENone _ => string "NULL"
|
adamc@290
|
1403 | ESome (t, e) =>
|
adamc@463
|
1404 if isUnboxable t then
|
adamc@463
|
1405 p_exp' par env e
|
adamc@463
|
1406 else
|
adamc@463
|
1407 box [string "({",
|
adamc@463
|
1408 newline,
|
adamc@463
|
1409 p_typ env t,
|
adamc@463
|
1410 space,
|
adamc@463
|
1411 string "*tmp",
|
adamc@463
|
1412 space,
|
adamc@463
|
1413 string "=",
|
adamc@463
|
1414 space,
|
adamc@463
|
1415 string "uw_malloc(ctx, sizeof(",
|
adamc@463
|
1416 p_typ env t,
|
adamc@463
|
1417 string "));",
|
adamc@463
|
1418 newline,
|
adamc@463
|
1419 string "*tmp",
|
adamc@463
|
1420 space,
|
adamc@463
|
1421 string "=",
|
adamc@463
|
1422 p_exp' par env e,
|
adamc@463
|
1423 string ";",
|
adamc@463
|
1424 newline,
|
adamc@463
|
1425 string "tmp;",
|
adamc@463
|
1426 newline,
|
adamc@463
|
1427 string "})"]
|
adamc@109
|
1428
|
adamc@316
|
1429 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
|
adamc@283
|
1430 | EError (e, t) =>
|
adamc@283
|
1431 box [string "({",
|
adamc@283
|
1432 newline,
|
adamc@283
|
1433 p_typ env t,
|
adamc@283
|
1434 space,
|
adamc@283
|
1435 string "tmp;",
|
adamc@283
|
1436 newline,
|
adamc@311
|
1437 string "uw_error(ctx, FATAL, \"",
|
adamc@292
|
1438 string (ErrorMsg.spanToString loc),
|
adamc@292
|
1439 string ": %s\", ",
|
adamc@283
|
1440 p_exp env e,
|
adamc@283
|
1441 string ");",
|
adamc@283
|
1442 newline,
|
adamc@283
|
1443 string "tmp;",
|
adamc@283
|
1444 newline,
|
adamc@283
|
1445 string "})"]
|
adamc@741
|
1446 | EReturnBlob {blob, mimeType, t} =>
|
adamc@741
|
1447 box [string "({",
|
adamc@741
|
1448 newline,
|
adamc@741
|
1449 p_typ env t,
|
adamc@741
|
1450 space,
|
adamc@741
|
1451 string "tmp;",
|
adamc@741
|
1452 newline,
|
adamc@741
|
1453 string "uw_return_blob(ctx, ",
|
adamc@741
|
1454 p_exp env blob,
|
adamc@741
|
1455 string ", ",
|
adamc@741
|
1456 p_exp env mimeType,
|
adamc@741
|
1457 string ");",
|
adamc@741
|
1458 newline,
|
adamc@741
|
1459 string "tmp;",
|
adamc@741
|
1460 newline,
|
adamc@741
|
1461 string "})"]
|
adamc@1065
|
1462 | ERedirect (e, t) =>
|
adamc@1065
|
1463 box [string "({",
|
adamc@1065
|
1464 newline,
|
adamc@1065
|
1465 p_typ env t,
|
adamc@1065
|
1466 space,
|
adamc@1065
|
1467 string "tmp;",
|
adamc@1065
|
1468 newline,
|
adamc@1065
|
1469 string "uw_redirect(ctx, ",
|
adamc@1065
|
1470 p_exp env e,
|
adamc@1065
|
1471 string ");",
|
adamc@1065
|
1472 newline,
|
adamc@1065
|
1473 string "tmp;",
|
adamc@1065
|
1474 newline,
|
adamc@1065
|
1475 string "})"]
|
adamc@476
|
1476 | EApp ((EError (e, (TFun (_, ran), _)), loc), _) =>
|
adamc@476
|
1477 p_exp env (EError (e, ran), loc)
|
adamc@741
|
1478 | EApp ((EReturnBlob {blob, mimeType, t = (TFun (_, ran), _)}, loc), _) =>
|
adamc@741
|
1479 p_exp env (EReturnBlob {blob = blob, mimeType = mimeType, t = ran}, loc)
|
adamc@476
|
1480
|
adamc@922
|
1481 | EFfiApp ("Basis", "strcat", [e1, e2]) =>
|
adamc@922
|
1482 let
|
adamc@922
|
1483 fun flatten e =
|
adamc@922
|
1484 case #1 e of
|
adamc@922
|
1485 EFfiApp ("Basis", "strcat", [e1, e2]) => flatten e1 @ flatten e2
|
adamc@922
|
1486 | _ => [e]
|
adamc@922
|
1487 in
|
adamc@922
|
1488 case flatten e1 @ flatten e2 of
|
adamc@922
|
1489 [e1, e2] => box [string "uw_Basis_strcat(ctx, ",
|
adamc@922
|
1490 p_exp env e1,
|
adamc@922
|
1491 string ",",
|
adamc@922
|
1492 p_exp env e2,
|
adamc@922
|
1493 string ")"]
|
adamc@922
|
1494 | es => box [string "uw_Basis_mstrcat(ctx, ",
|
adamc@922
|
1495 p_list (p_exp env) es,
|
adamc@922
|
1496 string ", NULL)"]
|
adamc@922
|
1497 end
|
adamc@922
|
1498
|
adamc@765
|
1499 | EFfiApp (m, x, []) => box [string "uw_",
|
adamc@765
|
1500 p_ident m,
|
adamc@765
|
1501 string "_",
|
adamc@765
|
1502 p_ident x,
|
adamc@765
|
1503 string "(ctx)"]
|
adamc@765
|
1504
|
adamc@311
|
1505 | EFfiApp (m, x, es) => box [string "uw_",
|
adamc@316
|
1506 p_ident m,
|
adamc@53
|
1507 string "_",
|
adamc@316
|
1508 p_ident x,
|
adamc@117
|
1509 string "(ctx, ",
|
adamc@53
|
1510 p_list (p_exp env) es,
|
adamc@53
|
1511 string ")"]
|
adamc@316
|
1512 | EApp (f, args) =>
|
adamc@316
|
1513 parenIf par (box [p_exp' true env f,
|
adamc@316
|
1514 string "(ctx,",
|
adamc@316
|
1515 space,
|
adamc@316
|
1516 p_list_sep (box [string ",", space]) (p_exp env) args,
|
adamc@316
|
1517 string ")"])
|
adamc@29
|
1518
|
adamc@387
|
1519 | EUnop (s, e1) =>
|
adamc@387
|
1520 parenIf par (box [string s,
|
adamc@387
|
1521 space,
|
adamc@387
|
1522 p_exp' true env e1])
|
adamc@387
|
1523
|
adamc@387
|
1524 | EBinop (s, e1, e2) =>
|
adamc@389
|
1525 if Char.isAlpha (String.sub (s, size s - 1)) then
|
adamc@389
|
1526 box [string s,
|
adamc@390
|
1527 string "(",
|
adamc@389
|
1528 p_exp env e1,
|
adamc@389
|
1529 string ",",
|
adamc@389
|
1530 space,
|
adamc@389
|
1531 p_exp env e2,
|
adamc@389
|
1532 string ")"]
|
adamc@389
|
1533 else
|
adamc@389
|
1534 parenIf par (box [p_exp' true env e1,
|
adamc@389
|
1535 space,
|
adamc@389
|
1536 string s,
|
adamc@389
|
1537 space,
|
adamc@389
|
1538 p_exp' true env e2])
|
adamc@387
|
1539
|
adamc@29
|
1540 | ERecord (i, xes) => box [string "({",
|
adamc@29
|
1541 space,
|
adamc@29
|
1542 string "struct",
|
adamc@29
|
1543 space,
|
adamc@311
|
1544 string ("__uws_" ^ Int.toString i),
|
adamc@29
|
1545 space,
|
adamc@181
|
1546 string "tmp",
|
adamc@29
|
1547 space,
|
adamc@29
|
1548 string "=",
|
adamc@29
|
1549 space,
|
adamc@29
|
1550 string "{",
|
adamc@29
|
1551 p_list (fn (_, e) =>
|
adamc@29
|
1552 p_exp env e) xes,
|
adamc@29
|
1553 string "};",
|
adamc@29
|
1554 space,
|
adamc@181
|
1555 string "tmp;",
|
adamc@29
|
1556 space,
|
adamc@29
|
1557 string "})" ]
|
adamc@29
|
1558 | EField (e, x) =>
|
adamc@29
|
1559 box [p_exp' true env e,
|
adamc@311
|
1560 string ".__uwf_",
|
adamc@316
|
1561 p_ident x]
|
adamc@29
|
1562
|
adamc@182
|
1563 | ECase (e, pes, {disc, result}) =>
|
adamc@182
|
1564 let
|
adamc@182
|
1565 val final = newGoto ()
|
adamc@182
|
1566
|
adamc@182
|
1567 val body = foldl (fn ((p, e), body) =>
|
adamc@182
|
1568 let
|
adamc@182
|
1569 val exit = newGoto ()
|
adamc@182
|
1570 val (pr, _) = p_pat_preamble env p
|
adamc@182
|
1571 val (p, env) = p_pat (env,
|
adamc@182
|
1572 box [string "goto",
|
adamc@182
|
1573 space,
|
adamc@182
|
1574 exit,
|
adamc@182
|
1575 string ";"],
|
adamc@182
|
1576 0) p
|
adamc@182
|
1577 in
|
adamc@182
|
1578 box [body,
|
adamc@182
|
1579 box [string "{",
|
adamc@182
|
1580 newline,
|
adamc@182
|
1581 pr,
|
adamc@182
|
1582 newline,
|
adamc@182
|
1583 p,
|
adamc@182
|
1584 newline,
|
adamc@182
|
1585 string "result",
|
adamc@182
|
1586 space,
|
adamc@182
|
1587 string "=",
|
adamc@182
|
1588 space,
|
adamc@182
|
1589 p_exp env e,
|
adamc@182
|
1590 string ";",
|
adamc@182
|
1591 newline,
|
adamc@182
|
1592 string "goto",
|
adamc@182
|
1593 space,
|
adamc@182
|
1594 final,
|
adamc@182
|
1595 string ";",
|
adamc@182
|
1596 newline,
|
adamc@182
|
1597 string "}"],
|
adamc@182
|
1598 newline,
|
adamc@182
|
1599 exit,
|
adamc@182
|
1600 string ":",
|
adamc@182
|
1601 newline]
|
adamc@182
|
1602 end) (box []) pes
|
adamc@182
|
1603 in
|
adamc@182
|
1604 box [string "({",
|
adamc@182
|
1605 newline,
|
adamc@182
|
1606 p_typ env disc,
|
adamc@182
|
1607 space,
|
adamc@182
|
1608 string "disc0",
|
adamc@182
|
1609 space,
|
adamc@182
|
1610 string "=",
|
adamc@182
|
1611 space,
|
adamc@182
|
1612 p_exp env e,
|
adamc@182
|
1613 string ";",
|
adamc@182
|
1614 newline,
|
adamc@182
|
1615 p_typ env result,
|
adamc@182
|
1616 space,
|
adamc@182
|
1617 string "result;",
|
adamc@182
|
1618 newline,
|
adamc@182
|
1619 body,
|
adamc@311
|
1620 string "uw_error(ctx, FATAL, \"",
|
adamc@182
|
1621 string (ErrorMsg.spanToString loc),
|
adamc@182
|
1622 string ": pattern match failure\");",
|
adamc@182
|
1623 newline,
|
adamc@182
|
1624 final,
|
adamc@182
|
1625 string ":",
|
adamc@182
|
1626 space,
|
adamc@182
|
1627 string "result;",
|
adamc@182
|
1628 newline,
|
adamc@182
|
1629 string "})"]
|
adamc@182
|
1630 end
|
adamc@181
|
1631
|
adamc@311
|
1632 | EWrite e => box [string "(uw_write(ctx, ",
|
adamc@102
|
1633 p_exp env e,
|
adamc@311
|
1634 string "), uw_unit_v)"]
|
adamc@102
|
1635
|
adamc@106
|
1636 | ESeq (e1, e2) => box [string "(",
|
adamc@106
|
1637 p_exp env e1,
|
adamc@106
|
1638 string ",",
|
adamc@106
|
1639 space,
|
adamc@106
|
1640 p_exp env e2,
|
adamc@106
|
1641 string ")"]
|
adamc@269
|
1642 | ELet (x, t, e1, e2) => box [string "({",
|
adamc@269
|
1643 newline,
|
adamc@269
|
1644 p_typ env t,
|
adamc@269
|
1645 space,
|
adamc@311
|
1646 string "__uwr_",
|
adamc@316
|
1647 p_ident x,
|
adamc@272
|
1648 string "_",
|
adamc@272
|
1649 string (Int.toString (E.countERels env)),
|
adamc@269
|
1650 space,
|
adamc@269
|
1651 string "=",
|
adamc@269
|
1652 space,
|
adamc@269
|
1653 p_exp env e1,
|
adamc@269
|
1654 string ";",
|
adamc@269
|
1655 newline,
|
adamc@269
|
1656 p_exp (E.pushERel env x t) e2,
|
adamc@269
|
1657 string ";",
|
adamc@269
|
1658 newline,
|
adamc@269
|
1659 string "})"]
|
adamc@269
|
1660
|
adamc@282
|
1661 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
|
adamc@278
|
1662 let
|
adamc@316
|
1663 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
|
adamc@278
|
1664 val tables = ListUtil.mapConcat (fn (x, xts) =>
|
adamc@316
|
1665 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
|
adamc@278
|
1666 tables
|
adamc@638
|
1667
|
adamc@1168
|
1668 val sort = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER)
|
adamc@1168
|
1669 val outputs = sort exps @ sort tables
|
adamc@324
|
1670
|
adamc@324
|
1671 val wontLeakStrings = notLeaky env true state
|
adamc@324
|
1672 val wontLeakAnything = notLeaky env false state
|
adamc@867
|
1673
|
adamc@867
|
1674 val inputs =
|
adamc@867
|
1675 case prepared of
|
adamc@867
|
1676 NONE => []
|
adamc@867
|
1677 | SOME _ => getPargs query
|
adamc@867
|
1678
|
adamc@867
|
1679 fun doCols p_getcol =
|
adamc@867
|
1680 box [string "struct __uws_",
|
adamc@867
|
1681 string (Int.toString rnum),
|
adamc@867
|
1682 string " __uwr_r_",
|
adamc@867
|
1683 string (Int.toString (E.countERels env)),
|
adamc@867
|
1684 string ";",
|
adamc@867
|
1685 newline,
|
adamc@867
|
1686 p_typ env state,
|
adamc@867
|
1687 space,
|
adamc@867
|
1688 string "__uwr_acc_",
|
adamc@867
|
1689 string (Int.toString (E.countERels env + 1)),
|
adamc@867
|
1690 space,
|
adamc@867
|
1691 string "=",
|
adamc@867
|
1692 space,
|
adamc@867
|
1693 string "acc;",
|
adamc@867
|
1694 newline,
|
adamc@867
|
1695 newline,
|
adamc@1114
|
1696
|
adamc@1114
|
1697 if Settings.getDeadlines () then
|
adamc@1114
|
1698 box [string "uw_check_deadline(ctx);",
|
adamc@1114
|
1699 newline]
|
adamc@1114
|
1700 else
|
adamc@1114
|
1701 box [],
|
adamc@1114
|
1702
|
adamc@867
|
1703 p_list_sepi (box []) (fn i =>
|
adamc@867
|
1704 fn (proj, t) =>
|
adamc@867
|
1705 box [string "__uwr_r_",
|
adamc@867
|
1706 string (Int.toString (E.countERels env)),
|
adamc@867
|
1707 string ".",
|
adamc@867
|
1708 string proj,
|
adamc@867
|
1709 space,
|
adamc@867
|
1710 string "=",
|
adamc@867
|
1711 space,
|
adamc@880
|
1712 p_getcol {loc = loc,
|
adamc@880
|
1713 wontLeakStrings = wontLeakStrings,
|
adamc@867
|
1714 col = i,
|
adamc@867
|
1715 typ = sql_type_in env t},
|
adamc@867
|
1716 string ";",
|
adamc@867
|
1717 newline]) outputs,
|
adamc@867
|
1718 newline,
|
adamc@867
|
1719 newline,
|
adamc@867
|
1720
|
adamc@867
|
1721 string "acc",
|
adamc@867
|
1722 space,
|
adamc@867
|
1723 string "=",
|
adamc@867
|
1724 space,
|
adamc@867
|
1725 p_exp (E.pushERel
|
adamc@867
|
1726 (E.pushERel env "r" (TRecord rnum, loc))
|
adamc@867
|
1727 "acc" state)
|
adamc@867
|
1728 body,
|
adamc@867
|
1729 string ";",
|
adamc@867
|
1730 newline]
|
adamc@278
|
1731 in
|
adamc@640
|
1732 box [if wontLeakAnything then
|
adamc@704
|
1733 string "(uw_begin_region(ctx), "
|
adamc@324
|
1734 else
|
adamc@324
|
1735 box [],
|
adamc@324
|
1736 string "({",
|
adamc@278
|
1737 newline,
|
adamc@640
|
1738 p_typ env state,
|
adamc@640
|
1739 space,
|
adamc@640
|
1740 string "acc",
|
adamc@640
|
1741 space,
|
adamc@640
|
1742 string "=",
|
adamc@640
|
1743 space,
|
adamc@640
|
1744 p_exp env initial,
|
adamc@640
|
1745 string ";",
|
adamc@640
|
1746 newline,
|
adamc@867
|
1747 string "int dummy = (uw_begin_region(ctx), 0);",
|
adamc@640
|
1748 newline,
|
adamc@640
|
1749
|
adamc@282
|
1750 case prepared of
|
adamc@867
|
1751 NONE =>
|
adamc@867
|
1752 box [string "char *query = ",
|
adamc@867
|
1753 p_exp env query,
|
adamc@867
|
1754 string ";",
|
adamc@867
|
1755 newline,
|
adamc@867
|
1756 newline,
|
adamc@282
|
1757
|
adamc@867
|
1758 #query (Settings.currentDbms ())
|
adamc@867
|
1759 {loc = loc,
|
adamc@873
|
1760 cols = map (fn (_, t) => sql_type_in env t) outputs,
|
adamc@867
|
1761 doCols = doCols}]
|
adamc@879
|
1762 | SOME {id, query, nested} =>
|
adamc@867
|
1763 box [p_list_sepi newline
|
adamc@867
|
1764 (fn i => fn (e, t) =>
|
adamc@867
|
1765 box [p_sql_type t,
|
adamc@867
|
1766 space,
|
adamc@867
|
1767 string "arg",
|
adamc@867
|
1768 string (Int.toString (i + 1)),
|
adamc@867
|
1769 space,
|
adamc@867
|
1770 string "=",
|
adamc@867
|
1771 space,
|
adamc@867
|
1772 p_exp env e,
|
adamc@867
|
1773 string ";"])
|
adamc@867
|
1774 inputs,
|
adamc@867
|
1775 newline,
|
adamc@867
|
1776 newline,
|
adamc@640
|
1777
|
adamc@867
|
1778 #queryPrepared (Settings.currentDbms ())
|
adamc@867
|
1779 {loc = loc,
|
adamc@867
|
1780 id = id,
|
adamc@867
|
1781 query = query,
|
adamc@867
|
1782 inputs = map #2 inputs,
|
adamc@873
|
1783 cols = map (fn (_, t) => sql_type_in env t) outputs,
|
adamc@879
|
1784 doCols = doCols,
|
adamc@879
|
1785 nested = nested}],
|
adamc@278
|
1786 newline,
|
adamc@277
|
1787
|
adamc@324
|
1788 if wontLeakAnything then
|
adamc@324
|
1789 box [string "uw_end_region(ctx);",
|
adamc@324
|
1790 newline]
|
adamc@324
|
1791 else
|
adamc@324
|
1792 box [],
|
adamc@278
|
1793 string "acc;",
|
adamc@278
|
1794 newline,
|
adamc@704
|
1795 string "})",
|
adamc@704
|
1796 if wontLeakAnything then
|
adamc@704
|
1797 string ")"
|
adamc@704
|
1798 else
|
adamc@704
|
1799 box []]
|
adamc@278
|
1800 end
|
adamc@106
|
1801
|
adam@1293
|
1802 | EDml {dml, prepared, mode} =>
|
adam@1295
|
1803 box [string "(uw_begin_region(ctx), ({",
|
adamc@307
|
1804 newline,
|
adamc@307
|
1805 case prepared of
|
adamc@307
|
1806 NONE => box [string "char *dml = ",
|
adamc@307
|
1807 p_exp env dml,
|
adamc@307
|
1808 string ";",
|
adamc@868
|
1809 newline,
|
adamc@868
|
1810 newline,
|
adam@1293
|
1811 #dml (Settings.currentDbms ()) (loc, mode)]
|
adamc@879
|
1812 | SOME {id, dml = dml'} =>
|
adamc@307
|
1813 let
|
adamc@868
|
1814 val inputs = getPargs dml
|
adamc@307
|
1815 in
|
adamc@307
|
1816 box [p_list_sepi newline
|
adamc@307
|
1817 (fn i => fn (e, t) =>
|
adamc@307
|
1818 box [p_sql_type t,
|
adamc@307
|
1819 space,
|
adamc@307
|
1820 string "arg",
|
adamc@307
|
1821 string (Int.toString (i + 1)),
|
adamc@307
|
1822 space,
|
adamc@307
|
1823 string "=",
|
adamc@307
|
1824 space,
|
adamc@307
|
1825 p_exp env e,
|
adamc@307
|
1826 string ";"])
|
adamc@868
|
1827 inputs,
|
adamc@307
|
1828 newline,
|
adamc@307
|
1829 newline,
|
adamc@307
|
1830
|
adamc@868
|
1831 #dmlPrepared (Settings.currentDbms ()) {loc = loc,
|
adamc@868
|
1832 id = id,
|
adamc@868
|
1833 dml = dml',
|
adam@1293
|
1834 inputs = map #2 inputs,
|
adam@1293
|
1835 mode = mode}]
|
adamc@307
|
1836 end,
|
adamc@307
|
1837 newline,
|
adamc@307
|
1838 newline,
|
adamc@337
|
1839 string "uw_end_region(ctx);",
|
adamc@337
|
1840 newline,
|
adam@1293
|
1841
|
adam@1293
|
1842 case mode of
|
adam@1293
|
1843 Settings.Error => string "uw_unit_v;"
|
adam@1295
|
1844 | Settings.None => string "uw_dup_and_clear_error_message(ctx);",
|
adam@1293
|
1845
|
adamc@307
|
1846 newline,
|
adam@1295
|
1847 string "}))"]
|
adamc@307
|
1848
|
adamc@338
|
1849 | ENextval {seq, prepared} =>
|
adamc@878
|
1850 box [string "({",
|
adamc@878
|
1851 newline,
|
adamc@878
|
1852 string "uw_Basis_int n;",
|
adamc@878
|
1853 newline,
|
adamc@869
|
1854
|
adamc@878
|
1855 case prepared of
|
adamc@878
|
1856 NONE => #nextval (Settings.currentDbms ()) {loc = loc,
|
adamc@878
|
1857 seqE = p_exp env seq,
|
adamc@878
|
1858 seqName = case #1 seq of
|
adamc@878
|
1859 EPrim (Prim.String s) => SOME s
|
adamc@878
|
1860 | _ => NONE}
|
adamc@879
|
1861 | SOME {id, query} => #nextvalPrepared (Settings.currentDbms ()) {loc = loc,
|
adamc@878
|
1862 id = id,
|
adamc@878
|
1863 query = query},
|
adamc@878
|
1864 newline,
|
adamc@878
|
1865 newline,
|
adamc@869
|
1866
|
adamc@878
|
1867 string "n;",
|
adamc@878
|
1868 newline,
|
adamc@878
|
1869 string "})"]
|
adamc@338
|
1870
|
adamc@1073
|
1871 | ESetval {seq, count} =>
|
adamc@1073
|
1872 box [string "({",
|
adamc@1073
|
1873 newline,
|
adamc@1073
|
1874
|
adamc@1073
|
1875 #setval (Settings.currentDbms ()) {loc = loc,
|
adamc@1073
|
1876 seqE = p_exp env seq,
|
adamc@1073
|
1877 count = p_exp env count},
|
adamc@1073
|
1878 newline,
|
adamc@1073
|
1879 newline,
|
adamc@1073
|
1880
|
adamc@1073
|
1881 string "uw_unit_v;",
|
adamc@1073
|
1882 newline,
|
adamc@1073
|
1883 string "})"]
|
adamc@1073
|
1884
|
adamc@1112
|
1885 | EUnurlify (e, t, true) =>
|
adamc@463
|
1886 let
|
adamc@463
|
1887 fun getIt () =
|
adamc@463
|
1888 if isUnboxable t then
|
adamc@1023
|
1889 unurlify false env t
|
adamc@463
|
1890 else
|
adamc@463
|
1891 box [string "({",
|
adamc@463
|
1892 newline,
|
adamc@463
|
1893 p_typ env t,
|
adamc@463
|
1894 string " *tmp = uw_malloc(ctx, sizeof(",
|
adamc@463
|
1895 p_typ env t,
|
adamc@463
|
1896 string "));",
|
adamc@463
|
1897 newline,
|
adamc@463
|
1898 string "*tmp = ",
|
adamc@1023
|
1899 unurlify false env t,
|
adamc@463
|
1900 string ";",
|
adamc@463
|
1901 newline,
|
adamc@463
|
1902 string "tmp;",
|
adamc@463
|
1903 newline,
|
adamc@463
|
1904 string "})"]
|
adamc@463
|
1905 in
|
adamc@463
|
1906 box [string "({",
|
adamc@463
|
1907 newline,
|
adamc@737
|
1908 string "uw_Basis_string request = uw_maybe_strdup(ctx, ",
|
adamc@463
|
1909 p_exp env e,
|
adamc@492
|
1910 string ");",
|
adamc@463
|
1911 newline,
|
adamc@463
|
1912 newline,
|
adamc@463
|
1913 string "(request ? ",
|
adamc@463
|
1914 getIt (),
|
adamc@463
|
1915 string " : NULL);",
|
adamc@463
|
1916 newline,
|
adamc@463
|
1917 string "})"]
|
adamc@463
|
1918 end
|
adamc@463
|
1919
|
adamc@1112
|
1920 | EUnurlify (e, t, false) =>
|
adamc@1112
|
1921 let
|
adamc@1112
|
1922 fun getIt () =
|
adamc@1112
|
1923 if isUnboxable t then
|
adamc@1112
|
1924 unurlify false env t
|
adamc@1112
|
1925 else
|
adamc@1112
|
1926 box [string "({",
|
adamc@1112
|
1927 newline,
|
adamc@1112
|
1928 p_typ env t,
|
adamc@1112
|
1929 string " *tmp = uw_malloc(ctx, sizeof(",
|
adamc@1112
|
1930 p_typ env t,
|
adamc@1112
|
1931 string "));",
|
adamc@1112
|
1932 newline,
|
adamc@1112
|
1933 string "*tmp = ",
|
adamc@1112
|
1934 unurlify false env t,
|
adamc@1112
|
1935 string ";",
|
adamc@1112
|
1936 newline,
|
adamc@1112
|
1937 string "tmp;",
|
adamc@1112
|
1938 newline,
|
adamc@1112
|
1939 string "})"]
|
adamc@1112
|
1940 in
|
adamc@1112
|
1941 box [string "({",
|
adamc@1112
|
1942 newline,
|
adamc@1112
|
1943 string "uw_Basis_string request = uw_maybe_strdup(ctx, ",
|
adamc@1112
|
1944 p_exp env e,
|
adamc@1112
|
1945 string ");",
|
adamc@1112
|
1946 newline,
|
adamc@1112
|
1947 newline,
|
adamc@1112
|
1948 unurlify false env t,
|
adamc@1112
|
1949 string ";",
|
adamc@1112
|
1950 newline,
|
adamc@1112
|
1951 string "})"]
|
adamc@1112
|
1952 end
|
adamc@1112
|
1953
|
adamc@29
|
1954 and p_exp env = p_exp' false env
|
adamc@29
|
1955
|
adamc@1114
|
1956 fun p_fun isRec env (fx, n, args, ran, e) =
|
adamc@129
|
1957 let
|
adamc@129
|
1958 val nargs = length args
|
adamc@129
|
1959 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
|
adamc@129
|
1960 in
|
adamc@129
|
1961 box [string "static",
|
adamc@129
|
1962 space,
|
adamc@129
|
1963 p_typ env ran,
|
adamc@129
|
1964 space,
|
adamc@316
|
1965 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@129
|
1966 string "(",
|
adamc@129
|
1967 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@311
|
1968 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
|
adamc@129
|
1969 box [p_typ env dom,
|
adamc@129
|
1970 space,
|
adamc@129
|
1971 p_rel env' (nargs - i - 1)]) args),
|
adamc@129
|
1972 string ")",
|
adamc@129
|
1973 space,
|
adamc@129
|
1974 string "{",
|
adamc@129
|
1975 newline,
|
adamc@1114
|
1976 if isRec andalso Settings.getDeadlines () then
|
adamc@1114
|
1977 box [string "uw_check_deadline(ctx);",
|
adamc@1114
|
1978 newline]
|
adamc@1114
|
1979 else
|
adamc@1114
|
1980 box [],
|
adamc@638
|
1981 box [string "return(",
|
adamc@638
|
1982 p_exp env' e,
|
adamc@638
|
1983 string ");"],
|
adamc@129
|
1984 newline,
|
adamc@129
|
1985 string "}"]
|
adamc@129
|
1986 end
|
adamc@129
|
1987
|
adamc@129
|
1988 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@29
|
1989 case d of
|
adamc@29
|
1990 DStruct (n, xts) =>
|
adamc@196
|
1991 let
|
adamc@196
|
1992 val env = E.declBinds env dAll
|
adamc@196
|
1993 in
|
adamc@196
|
1994 box [string "struct",
|
adamc@196
|
1995 space,
|
adamc@311
|
1996 string ("__uws_" ^ Int.toString n),
|
adamc@196
|
1997 space,
|
adamc@196
|
1998 string "{",
|
adamc@196
|
1999 newline,
|
adamc@196
|
2000 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
|
adamc@196
|
2001 space,
|
adamc@311
|
2002 string "__uwf_",
|
adamc@316
|
2003 p_ident x,
|
adamc@196
|
2004 string ";",
|
adamc@196
|
2005 newline]) xts,
|
adamc@196
|
2006 string "};"]
|
adamc@196
|
2007 end
|
adamc@809
|
2008 | DDatatype dts =>
|
adamc@165
|
2009 let
|
adamc@809
|
2010 val dts = ListMergeSort.sort (fn ((dk1, _, _, _), (dk2, _, _, _)) =>
|
adamc@809
|
2011 dk1 = Enum andalso dk2 <> Enum) dts
|
adamc@809
|
2012
|
adamc@809
|
2013 fun p_one (Enum, x, n, xncs) =
|
adamc@809
|
2014 box [string "enum",
|
adamc@809
|
2015 space,
|
adamc@809
|
2016 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@809
|
2017 space,
|
adamc@809
|
2018 string "{",
|
adamc@809
|
2019 space,
|
adam@1298
|
2020 case xncs of
|
adam@1298
|
2021 [] => string ("__uwec_" ^ ident x ^ "_" ^ Int.toString n)
|
adam@1298
|
2022 | _ =>
|
adam@1298
|
2023 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adam@1298
|
2024 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
|
adamc@809
|
2025 space,
|
adamc@809
|
2026 string "};"]
|
adamc@809
|
2027 | p_one (Option, _, _, _) = box []
|
adamc@809
|
2028 | p_one (Default, x, n, xncs) =
|
adamc@809
|
2029 let
|
adamc@809
|
2030 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
|
adamc@809
|
2031 | (x, n, SOME t) => SOME (x, n, t)) xncs
|
adamc@809
|
2032 in
|
adamc@809
|
2033 box [string "enum",
|
adamc@809
|
2034 space,
|
adamc@809
|
2035 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@809
|
2036 space,
|
adamc@809
|
2037 string "{",
|
adamc@809
|
2038 space,
|
adamc@809
|
2039 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adamc@809
|
2040 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n))
|
adamc@809
|
2041 xncs,
|
adamc@809
|
2042 space,
|
adamc@809
|
2043 string "};",
|
adamc@809
|
2044 newline,
|
adamc@809
|
2045 newline,
|
adamc@809
|
2046 string "struct",
|
adamc@809
|
2047 space,
|
adamc@809
|
2048 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@809
|
2049 space,
|
adamc@809
|
2050 string "{",
|
adamc@809
|
2051 newline,
|
adamc@809
|
2052 string "enum",
|
adamc@809
|
2053 space,
|
adamc@809
|
2054 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@809
|
2055 space,
|
adamc@809
|
2056 string "tag;",
|
adamc@809
|
2057 newline,
|
adamc@809
|
2058 box (case xncsArgs of
|
adamc@809
|
2059 [] => []
|
adamc@809
|
2060 | _ => [string "union",
|
adamc@809
|
2061 space,
|
adamc@809
|
2062 string "{",
|
adamc@809
|
2063 newline,
|
adamc@809
|
2064 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
|
adamc@809
|
2065 space,
|
adamc@809
|
2066 string ("uw_" ^ ident x),
|
adamc@809
|
2067 string ";"]) xncsArgs,
|
adamc@809
|
2068 newline,
|
adamc@809
|
2069 string "}",
|
adamc@809
|
2070 space,
|
adamc@809
|
2071 string "data;",
|
adamc@809
|
2072 newline]),
|
adamc@809
|
2073 string "};"]
|
adamc@809
|
2074 end
|
adamc@165
|
2075 in
|
adamc@809
|
2076 p_list_sep (box []) p_one dts
|
adamc@188
|
2077 end
|
adamc@29
|
2078
|
adamc@196
|
2079 | DDatatypeForward _ => box []
|
adamc@196
|
2080
|
adamc@29
|
2081 | DVal (x, n, t, e) =>
|
adamc@29
|
2082 box [p_typ env t,
|
adamc@29
|
2083 space,
|
adamc@316
|
2084 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@29
|
2085 space,
|
adamc@29
|
2086 string "=",
|
adamc@29
|
2087 space,
|
adamc@29
|
2088 p_exp env e,
|
adamc@29
|
2089 string ";"]
|
adamc@1114
|
2090 | DFun vi => p_fun false env vi
|
adamc@129
|
2091 | DFunRec vis =>
|
adamc@29
|
2092 let
|
adamc@129
|
2093 val env = E.declBinds env dAll
|
adamc@29
|
2094 in
|
adamc@129
|
2095 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
|
adamc@129
|
2096 box [string "static",
|
adamc@129
|
2097 space,
|
adamc@129
|
2098 p_typ env ran,
|
adamc@129
|
2099 space,
|
adamc@316
|
2100 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@311
|
2101 string "(uw_context,",
|
adamc@129
|
2102 space,
|
adamc@129
|
2103 p_list_sep (box [string ",", space])
|
adamc@129
|
2104 (fn (_, dom) => p_typ env dom) args,
|
adamc@129
|
2105 string ");"]) vis,
|
adamc@29
|
2106 newline,
|
adamc@1114
|
2107 p_list_sep newline (p_fun true env) vis,
|
adamc@129
|
2108 newline]
|
adamc@29
|
2109 end
|
adamc@707
|
2110 | DTable (x, _, pk, csts) => box [string "/* SQL table ",
|
adamc@707
|
2111 string x,
|
adamc@707
|
2112 space,
|
adamc@707
|
2113 case pk of
|
adamc@707
|
2114 "" => box []
|
adamc@707
|
2115 | _ => box [string "keys",
|
adamc@707
|
2116 space,
|
adamc@707
|
2117 string pk,
|
adamc@707
|
2118 space],
|
adamc@707
|
2119 string "constraints",
|
adamc@707
|
2120 space,
|
adamc@707
|
2121 p_list (fn (x, v) => box [string x,
|
adamc@707
|
2122 space,
|
adamc@707
|
2123 string ":",
|
adamc@707
|
2124 space,
|
adamc@707
|
2125 string v]) csts,
|
adamc@707
|
2126 space,
|
adamc@707
|
2127 string " */",
|
adamc@707
|
2128 newline]
|
adamc@338
|
2129 | DSequence x => box [string "/* SQL sequence ",
|
adamc@338
|
2130 string x,
|
adamc@338
|
2131 string " */",
|
adamc@338
|
2132 newline]
|
adamc@754
|
2133 | DView (x, _, s) => box [string "/* SQL view ",
|
adamc@754
|
2134 string x,
|
adamc@754
|
2135 space,
|
adamc@754
|
2136 string "AS",
|
adamc@754
|
2137 space,
|
adamc@754
|
2138 string s,
|
adamc@754
|
2139 space,
|
adamc@754
|
2140 string " */",
|
adamc@754
|
2141 newline]
|
adamc@870
|
2142 | DDatabase _ => box []
|
adamc@870
|
2143 | DPreparedStatements _ => box []
|
adamc@282
|
2144
|
adamc@569
|
2145 | DJavaScript s => box [string "static char jslib[] = \"",
|
adam@1285
|
2146 string (String.toCString s),
|
adamc@569
|
2147 string "\";"]
|
adamc@725
|
2148 | DCookie s => box [string "/*",
|
adamc@725
|
2149 space,
|
adamc@725
|
2150 string "cookie",
|
adamc@725
|
2151 space,
|
adamc@725
|
2152 string s,
|
adamc@725
|
2153 space,
|
adamc@725
|
2154 string "*/"]
|
adamc@720
|
2155 | DStyle s => box [string "/*",
|
adamc@720
|
2156 space,
|
adamc@720
|
2157 string "style",
|
adamc@720
|
2158 space,
|
adamc@720
|
2159 string s,
|
adamc@720
|
2160 space,
|
adamc@720
|
2161 string "*/"]
|
adamc@569
|
2162
|
adamc@1075
|
2163 | DTask _ => box []
|
adam@1294
|
2164 | DOnError _ => box []
|
adamc@1073
|
2165
|
adamc@144
|
2166 datatype 'a search =
|
adamc@144
|
2167 Found of 'a
|
adamc@144
|
2168 | NotFound
|
adamc@144
|
2169 | Error
|
adamc@120
|
2170
|
adamc@467
|
2171 fun p_sqltype'' env (tAll as (t, loc)) =
|
adamc@275
|
2172 case t of
|
adamc@275
|
2173 TFfi ("Basis", "int") => "int8"
|
adamc@275
|
2174 | TFfi ("Basis", "float") => "float8"
|
adamc@275
|
2175 | TFfi ("Basis", "string") => "text"
|
adamc@275
|
2176 | TFfi ("Basis", "bool") => "bool"
|
adamc@438
|
2177 | TFfi ("Basis", "time") => "timestamp"
|
adamc@737
|
2178 | TFfi ("Basis", "blob") => "bytea"
|
adamc@682
|
2179 | TFfi ("Basis", "channel") => "int8"
|
adamc@682
|
2180 | TFfi ("Basis", "client") => "int4"
|
adamc@275
|
2181 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
|
adamc@275
|
2182 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@275
|
2183 "ERROR")
|
adamc@275
|
2184
|
adamc@467
|
2185 fun p_sqltype' env (tAll as (t, loc)) =
|
adamc@467
|
2186 case t of
|
adamc@467
|
2187 (TOption t, _) => p_sqltype'' env t
|
adamc@467
|
2188 | _ => p_sqltype'' env t ^ " NOT NULL"
|
adamc@467
|
2189
|
adamc@275
|
2190 fun p_sqltype env t = string (p_sqltype' env t)
|
adamc@101
|
2191
|
adamc@467
|
2192 fun p_sqltype_base' env t =
|
adamc@467
|
2193 case t of
|
adamc@467
|
2194 (TOption t, _) => p_sqltype'' env t
|
adamc@467
|
2195 | _ => p_sqltype'' env t
|
adamc@467
|
2196
|
adamc@467
|
2197 fun p_sqltype_base env t = string (p_sqltype_base' env t)
|
adamc@467
|
2198
|
adamc@467
|
2199 fun is_not_null t =
|
adamc@467
|
2200 case t of
|
adamc@467
|
2201 (TOption _, _) => false
|
adamc@467
|
2202 | _ => true
|
adamc@467
|
2203
|
adamc@734
|
2204 fun sigName fields =
|
adamc@734
|
2205 let
|
adamc@734
|
2206 fun inFields s = List.exists (fn (s', _) => s' = s) fields
|
adamc@734
|
2207
|
adamc@734
|
2208 fun getSigName n =
|
adamc@734
|
2209 let
|
adamc@734
|
2210 val s = "Sig" ^ Int.toString n
|
adamc@734
|
2211 in
|
adamc@734
|
2212 if inFields s then
|
adamc@734
|
2213 getSigName (n + 1)
|
adamc@734
|
2214 else
|
adamc@734
|
2215 s
|
adamc@734
|
2216 end
|
adamc@734
|
2217 in
|
adamc@734
|
2218 if inFields "Sig" then
|
adamc@734
|
2219 getSigName 0
|
adamc@734
|
2220 else
|
adamc@734
|
2221 "Sig"
|
adamc@734
|
2222 end
|
adamc@734
|
2223
|
adamc@101
|
2224 fun p_file env (ds, ps) =
|
adamc@29
|
2225 let
|
adamc@101
|
2226 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@638
|
2227 (p_decl env d,
|
adamc@638
|
2228 E.declBinds env d))
|
adamc@101
|
2229 env ds
|
adamc@144
|
2230
|
adamc@779
|
2231 fun flatFields always (t : typ) =
|
adamc@756
|
2232 case #1 t of
|
adamc@756
|
2233 TRecord i =>
|
adamc@756
|
2234 let
|
adamc@756
|
2235 val xts = E.lookupStruct env i
|
adamc@756
|
2236 in
|
adamc@779
|
2237 SOME ((always @ map #1 xts) :: List.concat (List.mapPartial (flatFields [] o #2) xts))
|
adamc@756
|
2238 end
|
adamc@759
|
2239 | TList (_, i) =>
|
adamc@759
|
2240 let
|
adamc@759
|
2241 val ts = E.lookupStruct env i
|
adamc@759
|
2242 in
|
adamc@759
|
2243 case ts of
|
adamc@779
|
2244 [("1", t'), ("2", _)] => flatFields [] t'
|
adamc@759
|
2245 | _ => raise Fail "CjrPrint: Bad struct for TList"
|
adamc@759
|
2246 end
|
adamc@756
|
2247 | _ => NONE
|
adamc@756
|
2248
|
adamc@1104
|
2249 val fields = foldl (fn ((ek, _, _, ts, _, _, _), fields) =>
|
adamc@144
|
2250 case ek of
|
adam@1347
|
2251 Action eff =>
|
adam@1347
|
2252 (case List.nth (ts, length ts - 2) of
|
adam@1347
|
2253 (TRecord i, loc) =>
|
adam@1347
|
2254 let
|
adam@1347
|
2255 val xts = E.lookupStruct env i
|
adam@1347
|
2256 val extra = case eff of
|
adam@1347
|
2257 ReadCookieWrite => [sigName xts]
|
adam@1347
|
2258 | _ => []
|
adam@1347
|
2259 in
|
adam@1347
|
2260 case flatFields extra (TRecord i, loc) of
|
adam@1347
|
2261 NONE => raise Fail "CjrPrint: flatFields impossible"
|
adam@1347
|
2262 | SOME fields' => List.revAppend (fields', fields)
|
adam@1347
|
2263 end
|
adam@1347
|
2264 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
|
adam@1347
|
2265 | _ => fields)
|
adamc@756
|
2266 [] ps
|
adamc@756
|
2267
|
adamc@756
|
2268 val fields = foldl (fn (xts, fields) =>
|
adamc@756
|
2269 let
|
adamc@756
|
2270 val xtsSet = SS.addList (SS.empty, xts)
|
adamc@756
|
2271 in
|
adamc@756
|
2272 foldl (fn (x, fields) =>
|
adamc@756
|
2273 let
|
adamc@756
|
2274 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
|
adamc@756
|
2275 in
|
adamc@756
|
2276 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
|
adamc@756
|
2277 xtsSet'))
|
adamc@756
|
2278 end) fields xts
|
adamc@756
|
2279 end)
|
adamc@756
|
2280 SM.empty fields
|
adamc@144
|
2281
|
adamc@144
|
2282 val fnums = SM.foldli (fn (x, xs, fnums) =>
|
adamc@144
|
2283 let
|
adamc@144
|
2284 val unusable = SS.foldl (fn (x', unusable) =>
|
adamc@144
|
2285 case SM.find (fnums, x') of
|
adamc@144
|
2286 NONE => unusable
|
adamc@144
|
2287 | SOME n => IS.add (unusable, n))
|
adamc@144
|
2288 IS.empty xs
|
adamc@144
|
2289
|
adamc@144
|
2290 fun findAvailable n =
|
adamc@144
|
2291 if IS.member (unusable, n) then
|
adamc@144
|
2292 findAvailable (n + 1)
|
adamc@144
|
2293 else
|
adamc@144
|
2294 n
|
adamc@144
|
2295 in
|
adamc@144
|
2296 SM.insert (fnums, x, findAvailable 0)
|
adamc@144
|
2297 end)
|
adamc@144
|
2298 SM.empty fields
|
adamc@144
|
2299
|
adamc@734
|
2300 val cookies = List.mapPartial (fn (DCookie s, _) => SOME s | _ => NONE) ds
|
adamc@734
|
2301
|
adamc@144
|
2302 fun makeSwitch (fnums, i) =
|
adamc@144
|
2303 case SM.foldl (fn (n, NotFound) => Found n
|
adamc@144
|
2304 | (n, Error) => Error
|
adamc@144
|
2305 | (n, Found n') => if n = n' then
|
adamc@144
|
2306 Found n'
|
adamc@144
|
2307 else
|
adamc@144
|
2308 Error) NotFound fnums of
|
adamc@144
|
2309 NotFound => box [string "return",
|
adamc@144
|
2310 space,
|
adamc@144
|
2311 string "-1;"]
|
adamc@144
|
2312 | Found n => box [string "return",
|
adamc@144
|
2313 space,
|
adamc@144
|
2314 string (Int.toString n),
|
adamc@144
|
2315 string ";"]
|
adamc@144
|
2316 | Error =>
|
adamc@144
|
2317 let
|
adamc@144
|
2318 val cmap = SM.foldli (fn (x, n, cmap) =>
|
adamc@144
|
2319 let
|
adamc@144
|
2320 val ch = if i < size x then
|
adamc@144
|
2321 String.sub (x, i)
|
adamc@144
|
2322 else
|
adamc@144
|
2323 chr 0
|
adamc@144
|
2324
|
adamc@144
|
2325 val fnums = case CM.find (cmap, ch) of
|
adamc@144
|
2326 NONE => SM.empty
|
adamc@144
|
2327 | SOME fnums => fnums
|
adamc@144
|
2328 val fnums = SM.insert (fnums, x, n)
|
adamc@144
|
2329 in
|
adamc@144
|
2330 CM.insert (cmap, ch, fnums)
|
adamc@144
|
2331 end)
|
adamc@144
|
2332 CM.empty fnums
|
adamc@144
|
2333
|
adamc@144
|
2334 val cmap = CM.listItemsi cmap
|
adamc@144
|
2335 in
|
adamc@144
|
2336 case cmap of
|
adamc@144
|
2337 [(_, fnums)] =>
|
adamc@144
|
2338 box [string "if",
|
adamc@144
|
2339 space,
|
adamc@144
|
2340 string "(name[",
|
adamc@144
|
2341 string (Int.toString i),
|
adamc@144
|
2342 string "]",
|
adamc@144
|
2343 space,
|
adamc@144
|
2344 string "==",
|
adamc@144
|
2345 space,
|
adamc@144
|
2346 string "0)",
|
adamc@144
|
2347 space,
|
adamc@144
|
2348 string "return",
|
adamc@144
|
2349 space,
|
adamc@144
|
2350 string "-1;",
|
adamc@144
|
2351 newline,
|
adamc@144
|
2352 makeSwitch (fnums, i+1)]
|
adamc@144
|
2353 | _ =>
|
adamc@144
|
2354 box [string "switch",
|
adamc@144
|
2355 space,
|
adamc@144
|
2356 string "(name[",
|
adamc@144
|
2357 string (Int.toString i),
|
adamc@144
|
2358 string "])",
|
adamc@144
|
2359 space,
|
adamc@144
|
2360 string "{",
|
adamc@144
|
2361 newline,
|
adamc@144
|
2362 box (map (fn (ch, fnums) =>
|
adamc@144
|
2363 box [string "case",
|
adamc@144
|
2364 space,
|
adamc@144
|
2365 if ch = chr 0 then
|
adamc@144
|
2366 string "0:"
|
adamc@144
|
2367 else
|
adamc@144
|
2368 box [string "'",
|
adamc@144
|
2369 string (Char.toString ch),
|
adamc@144
|
2370 string "':"],
|
adamc@144
|
2371 newline,
|
adamc@144
|
2372 makeSwitch (fnums, i+1),
|
adamc@144
|
2373 newline]) cmap),
|
adamc@144
|
2374 string "default:",
|
adamc@144
|
2375 newline,
|
adamc@144
|
2376 string "return",
|
adamc@144
|
2377 space,
|
adamc@144
|
2378 string "-1;",
|
adamc@144
|
2379 newline,
|
adamc@144
|
2380 string "}"]
|
adamc@144
|
2381 end
|
adamc@144
|
2382
|
adamc@756
|
2383 fun getInput (x, t) =
|
adamc@756
|
2384 let
|
adamc@756
|
2385 val n = case SM.find (fnums, x) of
|
adamc@759
|
2386 NONE => raise Fail ("CjrPrint: Can't find " ^ x ^ " in fnums")
|
adamc@756
|
2387 | SOME n => n
|
adamc@756
|
2388
|
adamc@756
|
2389 val f = case t of
|
adamc@756
|
2390 (TFfi ("Basis", "bool"), _) => "optional_"
|
adamc@756
|
2391 | _ => ""
|
adamc@756
|
2392 in
|
adamc@756
|
2393 if isFile t then
|
adamc@756
|
2394 box [string "uw_input_",
|
adamc@756
|
2395 p_ident x,
|
adamc@756
|
2396 space,
|
adamc@756
|
2397 string "=",
|
adamc@756
|
2398 space,
|
adamc@756
|
2399 string "uw_get_file_input(ctx, ",
|
adamc@756
|
2400 string (Int.toString n),
|
adamc@756
|
2401 string ");",
|
adamc@756
|
2402 newline]
|
adamc@756
|
2403 else case #1 t of
|
adamc@756
|
2404 TRecord i =>
|
adamc@756
|
2405 let
|
adamc@756
|
2406 val xts = E.lookupStruct env i
|
adamc@756
|
2407 in
|
adamc@756
|
2408 box [string "uw_enter_subform(ctx, ",
|
adamc@756
|
2409 string (Int.toString n),
|
adamc@756
|
2410 string ");",
|
adamc@756
|
2411 newline,
|
adamc@756
|
2412 string "uw_input_",
|
adamc@756
|
2413 p_ident x,
|
adamc@756
|
2414 space,
|
adamc@756
|
2415 string "=",
|
adamc@756
|
2416 space,
|
adamc@756
|
2417 string "({",
|
adamc@756
|
2418 box [p_typ env t,
|
adamc@756
|
2419 space,
|
adamc@756
|
2420 string "result;",
|
adamc@756
|
2421 newline,
|
adamc@756
|
2422 p_list_sep (box [])
|
adamc@756
|
2423 (fn (x, t) =>
|
adamc@756
|
2424 box [p_typ env t,
|
adamc@756
|
2425 space,
|
adamc@756
|
2426 string "uw_input_",
|
adamc@756
|
2427 string x,
|
adamc@756
|
2428 string ";",
|
adamc@756
|
2429 newline])
|
adamc@756
|
2430 xts,
|
adamc@756
|
2431 newline,
|
adamc@756
|
2432 p_list_sep (box []) (fn (x, t) =>
|
adamc@756
|
2433 box [getInput (x, t),
|
adamc@756
|
2434 string "result.__uwf_",
|
adamc@756
|
2435 string x,
|
adamc@756
|
2436 space,
|
adamc@756
|
2437 string "=",
|
adamc@756
|
2438 space,
|
adamc@756
|
2439 string "uw_input_",
|
adamc@756
|
2440 string x,
|
adamc@756
|
2441 string ";",
|
adamc@756
|
2442 newline])
|
adamc@756
|
2443 xts,
|
adamc@756
|
2444 newline,
|
adamc@756
|
2445 string "result;",
|
adamc@756
|
2446 newline],
|
adamc@756
|
2447 string "});",
|
adamc@756
|
2448 newline,
|
adamc@756
|
2449 string "uw_leave_subform(ctx);"]
|
adamc@756
|
2450 end
|
adamc@759
|
2451 | TList (t', i) =>
|
adamc@759
|
2452 let
|
adamc@759
|
2453 val xts = E.lookupStruct env i
|
adamc@759
|
2454 val i' = case xts of
|
adamc@759
|
2455 [("1", (TRecord i', loc)), ("2", _)] => i'
|
adamc@759
|
2456 | _ => raise Fail "CjrPrint: Bad TList record [2]"
|
adamc@759
|
2457 val xts = E.lookupStruct env i'
|
adamc@759
|
2458 in
|
adamc@759
|
2459 box [string "{",
|
adamc@759
|
2460 newline,
|
adamc@759
|
2461 string "int status;",
|
adamc@759
|
2462 newline,
|
adamc@759
|
2463 string "uw_input_",
|
adamc@759
|
2464 p_ident x,
|
adamc@759
|
2465 space,
|
adamc@759
|
2466 string "=",
|
adamc@759
|
2467 space,
|
adamc@759
|
2468 string "NULL;",
|
adamc@759
|
2469 newline,
|
adamc@759
|
2470 string "for (status = uw_enter_subforms(ctx, ",
|
adamc@759
|
2471 string (Int.toString n),
|
adamc@759
|
2472 string "); status; status = uw_next_entry(ctx)) {",
|
adamc@759
|
2473 newline,
|
adamc@759
|
2474 box [p_typ env t,
|
adamc@759
|
2475 space,
|
adamc@759
|
2476 string "result",
|
adamc@759
|
2477 space,
|
adamc@759
|
2478 string "=",
|
adamc@759
|
2479 space,
|
adamc@759
|
2480 string "uw_malloc(ctx, sizeof(struct __uws_",
|
adamc@759
|
2481 string (Int.toString i),
|
adamc@759
|
2482 string "));",
|
adamc@759
|
2483 newline,
|
adamc@759
|
2484 box [string "{",
|
adamc@759
|
2485 p_list_sep (box [])
|
adamc@759
|
2486 (fn (x, t) =>
|
adamc@759
|
2487 box [p_typ env t,
|
adamc@759
|
2488 space,
|
adamc@759
|
2489 string "uw_input_",
|
adamc@759
|
2490 string x,
|
adamc@759
|
2491 string ";",
|
adamc@759
|
2492 newline])
|
adamc@759
|
2493 xts,
|
adamc@759
|
2494 newline,
|
adamc@759
|
2495 p_list_sep (box []) (fn (x, t) =>
|
adamc@759
|
2496 box [getInput (x, t),
|
adamc@759
|
2497 string "result->__uwf_1.__uwf_",
|
adamc@759
|
2498 string x,
|
adamc@759
|
2499 space,
|
adamc@759
|
2500 string "=",
|
adamc@759
|
2501 space,
|
adamc@759
|
2502 string "uw_input_",
|
adamc@759
|
2503 string x,
|
adamc@759
|
2504 string ";",
|
adamc@759
|
2505 newline])
|
adamc@759
|
2506 xts,
|
adamc@759
|
2507 string "}",
|
adamc@759
|
2508 newline],
|
adamc@759
|
2509 newline,
|
adamc@759
|
2510 string "result->__uwf_2 = uw_input_",
|
adamc@759
|
2511 p_ident x,
|
adamc@759
|
2512 string ";",
|
adamc@759
|
2513 newline,
|
adamc@759
|
2514 string "uw_input_",
|
adamc@759
|
2515 p_ident x,
|
adamc@759
|
2516 string " = result;",
|
adamc@759
|
2517 newline],
|
adamc@759
|
2518 string "}}",
|
adamc@759
|
2519 newline]
|
adamc@759
|
2520 end
|
adamc@756
|
2521 | _ =>
|
adamc@756
|
2522 box [string "request = uw_get_",
|
adamc@756
|
2523 string f,
|
adamc@756
|
2524 string "input(ctx, ",
|
adamc@756
|
2525 string (Int.toString n),
|
adamc@756
|
2526 string ");",
|
adamc@756
|
2527 newline,
|
adamc@756
|
2528 string "if (request == NULL)",
|
adamc@756
|
2529 newline,
|
adamc@756
|
2530 box [string "uw_error(ctx, FATAL, \"Missing input ",
|
adamc@756
|
2531 string x,
|
adamc@756
|
2532 string "\");"],
|
adamc@756
|
2533 newline,
|
adamc@756
|
2534 string "uw_input_",
|
adamc@756
|
2535 p_ident x,
|
adamc@756
|
2536 space,
|
adamc@756
|
2537 string "=",
|
adamc@756
|
2538 space,
|
adamc@1023
|
2539 unurlify true env t,
|
adamc@756
|
2540 string ";",
|
adamc@756
|
2541 newline]
|
adamc@756
|
2542 end
|
adamc@756
|
2543
|
adamc@1104
|
2544 fun p_page (ek, s, n, ts, ran, side, tellSig) =
|
adamc@144
|
2545 let
|
adamc@734
|
2546 val (ts, defInputs, inputsVar, fields) =
|
adamc@144
|
2547 case ek of
|
adam@1347
|
2548 Core.Action _ =>
|
adam@1347
|
2549 (case List.nth (ts, length ts - 2) of
|
adam@1347
|
2550 (TRecord i, _) =>
|
adam@1347
|
2551 let
|
adam@1347
|
2552 val xts = E.lookupStruct env i
|
adam@1347
|
2553 in
|
adam@1347
|
2554 (List.take (ts, length ts - 2),
|
adam@1347
|
2555 box [box (map (fn (x, t) => box [p_typ env t,
|
adam@1347
|
2556 space,
|
adam@1347
|
2557 string "uw_input_",
|
adam@1347
|
2558 p_ident x,
|
adam@1347
|
2559 string ";",
|
adam@1347
|
2560 newline]) xts),
|
adam@1347
|
2561 newline,
|
adam@1347
|
2562 box (map getInput xts),
|
adam@1347
|
2563 string "struct __uws_",
|
adam@1347
|
2564 string (Int.toString i),
|
adam@1347
|
2565 space,
|
adam@1347
|
2566 string "uw_inputs",
|
adam@1347
|
2567 space,
|
adam@1347
|
2568 string "= {",
|
adam@1347
|
2569 newline,
|
adam@1347
|
2570 box (map (fn (x, _) => box [string "uw_input_",
|
adam@1347
|
2571 p_ident x,
|
adam@1347
|
2572 string ",",
|
adam@1347
|
2573 newline]) xts),
|
adam@1347
|
2574 string "};",
|
adam@1347
|
2575 newline],
|
adam@1347
|
2576 box [string ",",
|
adam@1347
|
2577 space,
|
adam@1347
|
2578 string "uw_inputs"],
|
adam@1347
|
2579 SOME xts)
|
adam@1347
|
2580 end
|
adamc@144
|
2581
|
adam@1347
|
2582 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record")
|
adam@1347
|
2583 | _ => (List.take (ts, length ts - 1), string "", string "", NONE)
|
adamc@734
|
2584
|
adamc@734
|
2585 fun couldWrite ek =
|
adamc@734
|
2586 case ek of
|
adamc@734
|
2587 Link => false
|
adamc@735
|
2588 | Action ef => ef = ReadCookieWrite
|
adamc@735
|
2589 | Rpc ef => ef = ReadCookieWrite
|
adam@1347
|
2590 | Extern ef => ef = ReadCookieWrite
|
adamc@863
|
2591
|
adamc@863
|
2592 val s =
|
adamc@863
|
2593 case Settings.getUrlPrefix () of
|
adamc@863
|
2594 "" => s
|
adamc@863
|
2595 | "/" => s
|
adamc@863
|
2596 | prefix =>
|
adamc@863
|
2597 if size s > 0 andalso String.sub (s, 0) = #"/" then
|
adamc@863
|
2598 prefix ^ String.extract (s, 1, NONE)
|
adamc@863
|
2599 else
|
adamc@863
|
2600 prefix ^ s
|
adamc@144
|
2601 in
|
adamc@735
|
2602 box [string "if (!strncmp(request, \"",
|
adam@1285
|
2603 string (String.toCString s),
|
adamc@735
|
2604 string "\", ",
|
adamc@735
|
2605 string (Int.toString (size s)),
|
adamc@735
|
2606 string ") && (request[",
|
adamc@735
|
2607 string (Int.toString (size s)),
|
adamc@735
|
2608 string "] == 0 || request[",
|
adamc@735
|
2609 string (Int.toString (size s)),
|
adamc@735
|
2610 string "] == '/')) {",
|
adamc@735
|
2611 newline,
|
adamc@735
|
2612 string "request += ",
|
adamc@735
|
2613 string (Int.toString (size s)),
|
adamc@735
|
2614 string ";",
|
adamc@735
|
2615 newline,
|
adamc@735
|
2616 string "if (*request == '/') ++request;",
|
adamc@735
|
2617 newline,
|
adamc@735
|
2618 if couldWrite ek then
|
adamc@734
|
2619 box [string "{",
|
adamc@734
|
2620 newline,
|
adamc@734
|
2621 string "uw_Basis_string sig = ",
|
adamc@734
|
2622 case fields of
|
adamc@734
|
2623 NONE => string "uw_Basis_requestHeader(ctx, \"UrWeb-Sig\")"
|
adamc@734
|
2624 | SOME fields =>
|
adamc@734
|
2625 case SM.find (fnums, sigName fields) of
|
adamc@734
|
2626 NONE => raise Fail "CjrPrint: sig name wasn't assigned a number"
|
adamc@734
|
2627 | SOME inum =>
|
adamc@734
|
2628 string ("uw_get_input(ctx, " ^ Int.toString inum ^ ")"),
|
adamc@734
|
2629 string ";",
|
adamc@734
|
2630 newline,
|
adamc@734
|
2631 string "if (sig == NULL) uw_error(ctx, FATAL, \"Missing cookie signature\");",
|
adamc@734
|
2632 newline,
|
adamc@734
|
2633 string "if (strcmp(sig, uw_cookie_sig(ctx)))",
|
adamc@734
|
2634 newline,
|
adamc@734
|
2635 box [string "uw_error(ctx, FATAL, \"Wrong cookie signature\");",
|
adamc@734
|
2636 newline],
|
adamc@734
|
2637 string "}",
|
adamc@734
|
2638 newline]
|
adamc@734
|
2639 else
|
adamc@734
|
2640 box [],
|
adamc@609
|
2641 box (case ek of
|
adamc@731
|
2642 Core.Rpc _ => [string "uw_write_header(ctx, \"Content-type: text/plain\\r\\n\");",
|
adamc@731
|
2643 newline]
|
adamc@609
|
2644 | _ => [string "uw_write_header(ctx, \"Content-type: text/html\\r\\n\");",
|
adamc@609
|
2645 newline,
|
adamc@609
|
2646 string "uw_write_header(ctx, \"Content-script-type: text/javascript\\r\\n\");",
|
adamc@609
|
2647 newline,
|
adamc@804
|
2648 string "uw_write(ctx, begin_xhtml);",
|
adamc@643
|
2649 newline,
|
adamc@643
|
2650 string "uw_set_script_header(ctx, \"",
|
adamc@812
|
2651 let
|
adamc@812
|
2652 val scripts =
|
adamc@812
|
2653 case side of
|
adamc@693
|
2654 ServerOnly => ""
|
adamc@1111
|
2655 | _ =>
|
adamc@1111
|
2656 let
|
adamc@1111
|
2657 val scripts =
|
adamc@1111
|
2658 "<script src=\\\""
|
adamc@1111
|
2659 ^ OS.Path.joinDirFile {dir = Settings.getUrlPrefix (),
|
adamc@1111
|
2660 file = "app.js"}
|
adamc@1111
|
2661 ^ "\\\"></script>\\n"
|
adamc@1111
|
2662 in
|
adamc@1111
|
2663 foldl (fn (x, scripts) =>
|
adamc@1111
|
2664 scripts
|
adamc@1111
|
2665 ^ "<script src=\\\"" ^ x ^ "\\\"></script>\\n")
|
adamc@1111
|
2666 scripts (Settings.getScripts ())
|
adamc@1111
|
2667 end
|
adamc@812
|
2668 in
|
adamc@812
|
2669 string scripts
|
adamc@812
|
2670 end,
|
adamc@643
|
2671 string "\");",
|
adamc@609
|
2672 newline]),
|
adamc@1038
|
2673 string "uw_set_needs_push(ctx, ",
|
adamc@1038
|
2674 string (case side of
|
adamc@1038
|
2675 ServerAndPullAndPush => "1"
|
adamc@1038
|
2676 | _ => "0"),
|
adamc@1038
|
2677 string ");",
|
adamc@1038
|
2678 newline,
|
adamc@736
|
2679 string "uw_set_needs_sig(ctx, ",
|
adamc@1104
|
2680 string (if tellSig then
|
adamc@736
|
2681 "1"
|
adamc@736
|
2682 else
|
adamc@736
|
2683 "0"),
|
adamc@736
|
2684 string ");",
|
adamc@736
|
2685 newline,
|
adamc@682
|
2686 string "uw_login(ctx);",
|
adamc@682
|
2687 newline,
|
adamc@144
|
2688 box [string "{",
|
adamc@144
|
2689 newline,
|
adamc@144
|
2690 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
|
adamc@144
|
2691 space,
|
adamc@144
|
2692 string "arg",
|
adamc@144
|
2693 string (Int.toString i),
|
adamc@144
|
2694 space,
|
adamc@144
|
2695 string "=",
|
adamc@144
|
2696 space,
|
adam@1347
|
2697 case #1 t of
|
adam@1347
|
2698 TFfi ("Basis", "postBody") => string "uw_getPostBody(ctx)"
|
adam@1347
|
2699 | _ => unurlify false env t,
|
adamc@144
|
2700 string ";",
|
adamc@144
|
2701 newline]) ts),
|
adamc@144
|
2702 defInputs,
|
adamc@609
|
2703 box (case ek of
|
adamc@731
|
2704 Core.Rpc _ => [p_typ env ran,
|
adamc@731
|
2705 space,
|
adamc@731
|
2706 string "it0",
|
adamc@731
|
2707 space,
|
adamc@731
|
2708 string "=",
|
adamc@731
|
2709 space]
|
adamc@609
|
2710 | _ => []),
|
adamc@144
|
2711 p_enamed env n,
|
adamc@144
|
2712 string "(",
|
adamc@144
|
2713 p_list_sep (box [string ",", space])
|
adamc@144
|
2714 (fn x => x)
|
adamc@272
|
2715 (string "ctx"
|
adamc@280
|
2716 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
|
adamc@144
|
2717 inputsVar,
|
adamc@311
|
2718 string ", uw_unit_v);",
|
adamc@144
|
2719 newline,
|
adamc@609
|
2720 box (case ek of
|
adamc@731
|
2721 Core.Rpc _ => [urlify env ran]
|
adamc@609
|
2722 | _ => [string "uw_write(ctx, \"</html>\");",
|
adamc@609
|
2723 newline]),
|
adamc@144
|
2724 string "return;",
|
adamc@144
|
2725 newline,
|
adamc@144
|
2726 string "}",
|
adamc@144
|
2727 newline,
|
adamc@144
|
2728 string "}"]
|
adamc@144
|
2729 ]
|
adamc@144
|
2730 end
|
adamc@144
|
2731
|
adamc@144
|
2732 val pds' = map p_page ps
|
adamc@275
|
2733
|
adamc@870
|
2734 val hasDb = ref false
|
adamc@870
|
2735 val tables = ref []
|
adamc@872
|
2736 val views = ref []
|
adamc@870
|
2737 val sequences = ref []
|
adamc@870
|
2738 val dbstring = ref ""
|
adamc@870
|
2739 val expunge = ref 0
|
adamc@870
|
2740 val initialize = ref 0
|
adamc@870
|
2741 val prepped = ref []
|
adamc@275
|
2742
|
adamc@870
|
2743 val () = app (fn d =>
|
adamc@870
|
2744 case #1 d of
|
adamc@870
|
2745 DDatabase {name = x, expunge = y, initialize = z} => (hasDb := true;
|
adamc@870
|
2746 dbstring := x;
|
adamc@870
|
2747 expunge := y;
|
adamc@870
|
2748 initialize := z)
|
adamc@870
|
2749 | DTable (s, xts, _, _) => tables := (s, map (fn (x, t) =>
|
adamc@870
|
2750 (x, sql_type_in env t)) xts) :: !tables
|
adamc@872
|
2751 | DView (s, xts, _) => views := (s, map (fn (x, t) =>
|
adamc@872
|
2752 (x, sql_type_in env t)) xts) :: !views
|
adamc@870
|
2753 | DSequence s => sequences := s :: !sequences
|
adamc@870
|
2754 | DPreparedStatements ss => prepped := ss
|
adamc@870
|
2755 | _ => ()) ds
|
adamc@377
|
2756
|
adamc@870
|
2757 val hasDb = !hasDb
|
adamc@734
|
2758
|
adamc@734
|
2759 val cookies = List.mapPartial (fn (DCookie s, _) => SOME s | _ => NONE) ds
|
adamc@734
|
2760
|
adamc@734
|
2761 val cookieCode = foldl (fn (cookie, acc) =>
|
adamc@734
|
2762 SOME (case acc of
|
adamc@734
|
2763 NONE => string ("uw_unnull(uw_Basis_get_cookie(ctx, \""
|
adamc@734
|
2764 ^ cookie ^ "\"))")
|
adamc@734
|
2765 | SOME acc => box [string ("uw_Basis_strcat(ctx, uw_unnull(uw_Basis_get_cookie(ctx, \""
|
adamc@734
|
2766 ^ cookie ^ "\")), uw_Basis_strcat(ctx, \"/\", "),
|
adamc@734
|
2767 acc,
|
adamc@734
|
2768 string "))"]))
|
adamc@734
|
2769 NONE cookies
|
adamc@770
|
2770
|
adamc@770
|
2771 fun makeChecker (name, rules : Settings.rule list) =
|
adamc@1094
|
2772 box [string "static int ",
|
adamc@770
|
2773 string name,
|
adamc@770
|
2774 string "(const char *s) {",
|
adamc@770
|
2775 newline,
|
adamc@770
|
2776 box [p_list_sep (box [])
|
adamc@770
|
2777 (fn rule =>
|
adamc@770
|
2778 box [string "if (!str",
|
adamc@770
|
2779 case #kind rule of
|
adamc@770
|
2780 Settings.Exact => box [string "cmp(s, \"",
|
adam@1285
|
2781 string (String.toCString (#pattern rule)),
|
adamc@770
|
2782 string "\"))"]
|
adamc@770
|
2783 | Settings.Prefix => box [string "ncmp(s, \"",
|
adam@1285
|
2784 string (String.toCString (#pattern rule)),
|
adamc@770
|
2785 string "\", ",
|
adamc@770
|
2786 string (Int.toString (size (#pattern rule))),
|
adamc@770
|
2787 string "))"],
|
adamc@770
|
2788 string " return ",
|
adamc@770
|
2789 string (case #action rule of
|
adamc@770
|
2790 Settings.Allow => "1"
|
adamc@770
|
2791 | Settings.Deny => "0"),
|
adamc@770
|
2792 string ";",
|
adamc@770
|
2793 newline]) rules,
|
adamc@770
|
2794 string "return 0;",
|
adamc@770
|
2795 newline],
|
adamc@770
|
2796 string "}",
|
adamc@770
|
2797 newline]
|
adamc@1073
|
2798
|
adam@1348
|
2799 val initializers = List.mapPartial (fn (DTask (Initialize, x1, x2, e), _) => SOME (x1, x2, e) | _ => NONE) ds
|
adam@1348
|
2800 val expungers = List.mapPartial (fn (DTask (ClientLeaves, x1, x2, e), _) => SOME (x1, x2, e) | _ => NONE) ds
|
adam@1349
|
2801 val periodics = List.mapPartial (fn (DTask (Periodic n, x1, x2, e), _) => SOME (n, x1, x2, e) | _ => NONE) ds
|
adamc@1263
|
2802
|
adam@1294
|
2803 val onError = ListUtil.search (fn (DOnError n, _) => SOME n | _ => NONE) ds
|
adam@1294
|
2804
|
adamc@1263
|
2805 val now = Time.now ()
|
adamc@1263
|
2806 val nowD = Date.fromTimeUniv now
|
adamc@1263
|
2807 val rfcFmt = "%a, %d %b %Y %H:%M:%S"
|
adamc@29
|
2808 in
|
adamc@1263
|
2809 box [string "#include \"",
|
adamc@1263
|
2810 string (OS.Path.joinDirFile {dir = Config.includ,
|
adamc@1263
|
2811 file = "config.h"}),
|
adamc@1263
|
2812 string "\"",
|
adamc@1263
|
2813 newline,
|
adamc@1263
|
2814 string "#include <stdio.h>",
|
adamc@144
|
2815 newline,
|
adamc@144
|
2816 string "#include <stdlib.h>",
|
adamc@144
|
2817 newline,
|
adamc@272
|
2818 string "#include <string.h>",
|
adamc@272
|
2819 newline,
|
adamc@390
|
2820 string "#include <math.h>",
|
adamc@390
|
2821 newline,
|
adamc@1263
|
2822 string "#include <time.h>",
|
adamc@1263
|
2823 newline,
|
adamc@432
|
2824 if hasDb then
|
adamc@866
|
2825 box [string ("#include <" ^ #header (Settings.currentDbms ()) ^ ">"),
|
adamc@432
|
2826 newline]
|
adamc@432
|
2827 else
|
adamc@432
|
2828 box [],
|
adamc@764
|
2829 p_list_sep (box []) (fn s => box [string "#include \"",
|
adamc@764
|
2830 string s,
|
adamc@764
|
2831 string "\"",
|
adamc@764
|
2832 newline]) (Settings.getHeaders ()),
|
adamc@378
|
2833 string "#include \"",
|
adamc@378
|
2834 string (OS.Path.joinDirFile {dir = Config.includ,
|
adamc@378
|
2835 file = "urweb.h"}),
|
adamc@378
|
2836 string "\"",
|
adamc@101
|
2837 newline,
|
adamc@101
|
2838 newline,
|
adamc@804
|
2839
|
adam@1307
|
2840 box [string "static void uw_setup_limits() {",
|
adam@1307
|
2841 newline,
|
adam@1332
|
2842 case Settings.getMinHeap () of
|
adam@1332
|
2843 0 => box []
|
adam@1332
|
2844 | n => box [string "uw_min_heap",
|
adam@1332
|
2845 space,
|
adam@1332
|
2846 string "=",
|
adam@1332
|
2847 space,
|
adam@1332
|
2848 string (Int.toString n),
|
adam@1332
|
2849 string ";",
|
adam@1332
|
2850 newline,
|
adam@1332
|
2851 newline],
|
adam@1307
|
2852 box [p_list_sep (box []) (fn (class, num) =>
|
adam@1307
|
2853 let
|
adam@1307
|
2854 val num = case class of
|
adam@1307
|
2855 "page" => Int.max (2048, num)
|
adam@1307
|
2856 | _ => num
|
adam@1307
|
2857 in
|
adam@1307
|
2858 box [string ("uw_" ^ class ^ "_max"),
|
adam@1307
|
2859 space,
|
adam@1307
|
2860 string "=",
|
adam@1307
|
2861 space,
|
adam@1307
|
2862 string (Int.toString num),
|
adam@1307
|
2863 string ";",
|
adam@1307
|
2864 newline]
|
adam@1307
|
2865 end) (Settings.limits ())],
|
adam@1307
|
2866 string "}",
|
adam@1307
|
2867 newline,
|
adam@1307
|
2868 newline],
|
adam@1307
|
2869
|
adamc@1164
|
2870 #code (Settings.currentProtocol ()) (),
|
adamc@1164
|
2871
|
adamc@870
|
2872 if hasDb then
|
adamc@870
|
2873 #init (Settings.currentDbms ()) {dbstring = !dbstring,
|
adamc@870
|
2874 prepared = !prepped,
|
adamc@870
|
2875 tables = !tables,
|
adamc@872
|
2876 views = !views,
|
adamc@870
|
2877 sequences = !sequences}
|
adamc@870
|
2878 else
|
adam@1307
|
2879 box [string "static void uw_client_init(void) { };",
|
adamc@891
|
2880 newline,
|
adam@1307
|
2881 string "static void uw_db_init(uw_context ctx) { };",
|
adamc@870
|
2882 newline,
|
adam@1307
|
2883 string "static int uw_db_begin(uw_context ctx) { return 0; };",
|
adamc@870
|
2884 newline,
|
adam@1307
|
2885 string "static void uw_db_close(uw_context ctx) { };",
|
adamc@1094
|
2886 newline,
|
adam@1307
|
2887 string "static int uw_db_commit(uw_context ctx) { return 0; };",
|
adamc@870
|
2888 newline,
|
adam@1307
|
2889 string "static int uw_db_rollback(uw_context ctx) { return 0; };"],
|
adamc@870
|
2890 newline,
|
adamc@870
|
2891 newline,
|
adamc@870
|
2892
|
adam@1349
|
2893 box (ListUtil.mapi (fn (i, (_, x1, x2, e)) =>
|
adam@1349
|
2894 box [string "static void uw_periodic",
|
adam@1349
|
2895 string (Int.toString i),
|
adam@1349
|
2896 string "(uw_context ctx) {",
|
adam@1349
|
2897 newline,
|
adam@1349
|
2898 box [string "uw_unit __uwr_",
|
adam@1349
|
2899 string x1,
|
adam@1349
|
2900 string "_0 = uw_unit_v, __uwr_",
|
adam@1349
|
2901 string x2,
|
adam@1349
|
2902 string "_1 = uw_unit_v;",
|
adam@1349
|
2903 newline,
|
adam@1349
|
2904 p_exp (E.pushERel (E.pushERel env x1 dummyt) x2 dummyt) e,
|
adam@1349
|
2905 string ";",
|
adam@1349
|
2906 newline],
|
adam@1349
|
2907 string "}",
|
adam@1349
|
2908 newline,
|
adam@1349
|
2909 newline]) periodics),
|
adam@1349
|
2910
|
adam@1349
|
2911 string "static uw_periodic my_periodics[] = {",
|
adam@1349
|
2912 box (ListUtil.mapi (fn (i, (n, _, _, _)) =>
|
adam@1349
|
2913 box [string "{uw_periodic",
|
adam@1349
|
2914 string (Int.toString i),
|
adam@1349
|
2915 string ",",
|
adam@1349
|
2916 space,
|
adam@1349
|
2917 string (Int64.toString n),
|
adam@1349
|
2918 string "},"]) periodics),
|
adam@1349
|
2919 string "{NULL}};",
|
adam@1349
|
2920 newline,
|
adam@1349
|
2921 newline,
|
adam@1349
|
2922
|
adamc@804
|
2923 string "static const char begin_xhtml[] = \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\" ?>\\n<!DOCTYPE html PUBLIC \\\"-//W3C//DTD XHTML 1.0 Transitional//EN\\\" \\\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\\">\\n<html xmlns=\\\"http://www.w3.org/1999/xhtml\\\" xml:lang=\\\"en\\\" lang=\\\"en\\\">\";",
|
adamc@804
|
2924 newline,
|
adamc@804
|
2925 newline,
|
adamc@804
|
2926
|
adamc@101
|
2927 p_list_sep newline (fn x => x) pds,
|
adamc@101
|
2928 newline,
|
adamc@144
|
2929 newline,
|
adamc@1094
|
2930 string "static int uw_input_num(const char *name) {",
|
adamc@144
|
2931 newline,
|
adamc@144
|
2932 makeSwitch (fnums, 0),
|
adamc@144
|
2933 string "}",
|
adamc@144
|
2934 newline,
|
adamc@144
|
2935 newline,
|
adamc@770
|
2936
|
adamc@770
|
2937 makeChecker ("uw_check_url", Settings.getUrlRules ()),
|
adamc@770
|
2938 newline,
|
adamc@770
|
2939
|
adamc@770
|
2940 makeChecker ("uw_check_mime", Settings.getMimeRules ()),
|
adamc@770
|
2941 newline,
|
adamc@734
|
2942
|
adamc@734
|
2943 string "extern void uw_sign(const char *in, char *out);",
|
adamc@734
|
2944 newline,
|
adamc@734
|
2945 string "extern int uw_hash_blocksize;",
|
adamc@734
|
2946 newline,
|
adamc@1094
|
2947 string "static uw_Basis_string uw_cookie_sig(uw_context ctx) {",
|
adamc@734
|
2948 newline,
|
adamc@734
|
2949 box [string "uw_Basis_string r = uw_malloc(ctx, uw_hash_blocksize);",
|
adamc@734
|
2950 newline,
|
adamc@734
|
2951 string "uw_sign(",
|
adamc@734
|
2952 case cookieCode of
|
adamc@734
|
2953 NONE => string "\"\""
|
adamc@734
|
2954 | SOME code => code,
|
adamc@734
|
2955 string ", r);",
|
adamc@734
|
2956 newline,
|
adamc@734
|
2957 string "return uw_Basis_makeSigString(ctx, r);",
|
adamc@734
|
2958 newline],
|
adamc@734
|
2959 string "}",
|
adamc@734
|
2960 newline,
|
adamc@734
|
2961 newline,
|
adamc@734
|
2962
|
adamc@1094
|
2963 string "static void uw_handle(uw_context ctx, char *request) {",
|
adamc@101
|
2964 newline,
|
adamc@863
|
2965 string "if (!strcmp(request, \"",
|
adamc@863
|
2966 string (OS.Path.joinDirFile {dir = Settings.getUrlPrefix (),
|
adamc@863
|
2967 file = "app.js"}),
|
adamc@863
|
2968 string "\")) {",
|
adamc@569
|
2969 newline,
|
adamc@1263
|
2970 box [string "uw_Basis_string ims = uw_Basis_requestHeader(ctx, \"If-modified-since\");",
|
adamc@1263
|
2971 newline,
|
adamc@1263
|
2972 string ("if (ims && !strcmp(ims, \"" ^ Date.fmt rfcFmt nowD ^ "\")) {"),
|
adamc@1263
|
2973 newline,
|
adamc@1263
|
2974 box [string "uw_clear_headers(ctx);",
|
adamc@1263
|
2975 newline,
|
adam@1320
|
2976 string "uw_write_header(ctx, uw_supports_direct_status ? \"HTTP/1.1 304 Not Modified\\r\\n\" : \"Status: 304 Not Modified\\r\\n\");",
|
adamc@1263
|
2977 newline,
|
adamc@1263
|
2978 string "return;",
|
adamc@1263
|
2979 newline],
|
adamc@1263
|
2980 string "}",
|
adamc@1263
|
2981 newline,
|
adamc@1263
|
2982 newline,
|
adamc@1263
|
2983 string "uw_write_header(ctx, \"Content-type: text/javascript\\r\\n\");",
|
adamc@1263
|
2984 newline,
|
adamc@1263
|
2985 string ("uw_write_header(ctx, \"Last-modified: " ^ Date.fmt rfcFmt nowD ^ "\\r\\n\");"),
|
adamc@569
|
2986 newline,
|
adamc@569
|
2987 string "uw_write(ctx, jslib);",
|
adamc@569
|
2988 newline,
|
adamc@569
|
2989 string "return;",
|
adamc@569
|
2990 newline],
|
adamc@569
|
2991 string "}",
|
adamc@569
|
2992 newline,
|
adamc@101
|
2993 p_list_sep newline (fn x => x) pds',
|
adamc@101
|
2994 newline,
|
adamc@1110
|
2995 string "uw_clear_headers(ctx);",
|
adamc@1110
|
2996 newline,
|
adamc@1110
|
2997 string "uw_write_header(ctx, \"HTTP/1.1 404 Not Found\\r\\nContent-type: text/plain\\r\\n\");",
|
adamc@1110
|
2998 newline,
|
adamc@1110
|
2999 string "uw_write(ctx, \"Not Found\");",
|
adamc@387
|
3000 newline,
|
adamc@101
|
3001 string "}",
|
adamc@275
|
3002 newline,
|
adamc@275
|
3003 newline,
|
adamc@870
|
3004
|
adam@1348
|
3005 box [string "static void uw_expunger(uw_context ctx, uw_Basis_client cli) {",
|
adam@1348
|
3006 newline,
|
adam@1348
|
3007
|
adam@1348
|
3008 p_list_sep (box []) (fn (x1, x2, e) => box [string "({",
|
adam@1348
|
3009 newline,
|
adam@1348
|
3010 string "uw_Basis_client __uwr_",
|
adam@1348
|
3011 string x1,
|
adam@1348
|
3012 string "_0 = cli;",
|
adam@1348
|
3013 newline,
|
adam@1348
|
3014 string "uw_unit __uwr_",
|
adam@1348
|
3015 string x2,
|
adam@1348
|
3016 string "_1 = uw_unit_v;",
|
adam@1348
|
3017 newline,
|
adam@1348
|
3018 p_exp (E.pushERel (E.pushERel env x1 (TFfi ("Basis", "client"), ErrorMsg.dummySpan))
|
adam@1348
|
3019 x2 dummyt) e,
|
adam@1348
|
3020 string ";",
|
adam@1348
|
3021 newline,
|
adam@1348
|
3022 string "});",
|
adam@1348
|
3023 newline]) expungers,
|
adam@1348
|
3024
|
adam@1348
|
3025 if hasDb then
|
adamc@870
|
3026 box [p_enamed env (!expunge),
|
adamc@870
|
3027 string "(ctx, cli);",
|
adam@1348
|
3028 newline]
|
adam@1348
|
3029 else
|
adam@1348
|
3030 box [],
|
adam@1348
|
3031 string "}"],
|
adamc@870
|
3032
|
adam@1348
|
3033 newline,
|
adam@1348
|
3034 string "static void uw_initializer(uw_context ctx) {",
|
adam@1348
|
3035 newline,
|
adam@1348
|
3036 box [p_list_sep (box []) (fn (x1, x2, e) => box [string "({",
|
adam@1348
|
3037 newline,
|
adam@1348
|
3038 string "uw_unit __uwr_",
|
adam@1348
|
3039 string x1,
|
adam@1348
|
3040 string "_0 = uw_unit_v, __uwr_",
|
adam@1348
|
3041 string x2,
|
adam@1348
|
3042 string "_1 = uw_unit_v;",
|
adam@1348
|
3043 newline,
|
adam@1348
|
3044 p_exp (E.pushERel (E.pushERel env x1 dummyt) x2 dummyt) e,
|
adam@1348
|
3045 string ";",
|
adam@1348
|
3046 newline,
|
adam@1348
|
3047 string "});",
|
adam@1348
|
3048 newline]) initializers,
|
adam@1348
|
3049 if hasDb then
|
adam@1348
|
3050 box [p_enamed env (!initialize),
|
adamc@870
|
3051 string "(ctx, uw_unit_v);",
|
adam@1348
|
3052 newline]
|
adam@1348
|
3053 else
|
adam@1348
|
3054 box []],
|
adam@1348
|
3055 string "}",
|
adam@1348
|
3056 newline,
|
adamc@1094
|
3057
|
adam@1294
|
3058 case onError of
|
adam@1294
|
3059 NONE => box []
|
adam@1294
|
3060 | SOME n => box [string "static void uw_onError(uw_context ctx, char *msg) {",
|
adam@1294
|
3061 newline,
|
adam@1294
|
3062 box [string "uw_write(ctx, ",
|
adam@1294
|
3063 p_enamed env n,
|
adam@1294
|
3064 string "(ctx, msg, uw_unit_v));",
|
adam@1294
|
3065 newline],
|
adam@1294
|
3066 string "}",
|
adam@1294
|
3067 newline,
|
adam@1294
|
3068 newline],
|
adam@1294
|
3069
|
adamc@1094
|
3070 string "uw_app uw_application = {",
|
adamc@1094
|
3071 p_list_sep (box [string ",", newline]) string
|
adamc@1094
|
3072 [Int.toString (SM.foldl Int.max 0 fnums + 1),
|
adamc@1094
|
3073 Int.toString (Settings.getTimeout ()),
|
adamc@1094
|
3074 "\"" ^ Settings.getUrlPrefix () ^ "\"",
|
adamc@1094
|
3075 "uw_client_init", "uw_initializer", "uw_expunger",
|
adamc@1094
|
3076 "uw_db_init", "uw_db_begin", "uw_db_commit", "uw_db_rollback", "uw_db_close",
|
adamc@1094
|
3077 "uw_handle",
|
adam@1294
|
3078 "uw_input_num", "uw_cookie_sig", "uw_check_url", "uw_check_mime",
|
adam@1349
|
3079 case onError of NONE => "NULL" | SOME _ => "uw_onError", "my_periodics"],
|
adamc@1094
|
3080 string "};",
|
adamc@1094
|
3081 newline]
|
adamc@29
|
3082 end
|
adamc@29
|
3083
|
adamc@274
|
3084 fun p_sql env (ds, _) =
|
adamc@274
|
3085 let
|
adamc@274
|
3086 val (pps, _) = ListUtil.foldlMap
|
adamc@274
|
3087 (fn (dAll as (d, _), env) =>
|
adamc@274
|
3088 let
|
adamc@274
|
3089 val pp = case d of
|
adamc@707
|
3090 DTable (s, xts, pk, csts) =>
|
adamc@274
|
3091 box [string "CREATE TABLE ",
|
adamc@274
|
3092 string s,
|
adamc@274
|
3093 string "(",
|
adamc@274
|
3094 p_list (fn (x, t) =>
|
adamc@874
|
3095 let
|
adamc@874
|
3096 val t = sql_type_in env t
|
adamc@874
|
3097 in
|
adamc@874
|
3098 box [string "uw_",
|
adamc@874
|
3099 string (CharVector.map Char.toLower x),
|
adamc@874
|
3100 space,
|
adamc@874
|
3101 string (#p_sql_type (Settings.currentDbms ()) t),
|
adamc@874
|
3102 case t of
|
adamc@874
|
3103 Nullable _ => box []
|
adamc@874
|
3104 | _ => string " NOT NULL"]
|
adamc@874
|
3105 end) xts,
|
adamc@707
|
3106 case (pk, csts) of
|
adamc@707
|
3107 ("", []) => box []
|
adamc@707
|
3108 | _ => string ",",
|
adamc@704
|
3109 cut,
|
adamc@707
|
3110 case pk of
|
adamc@707
|
3111 "" => box []
|
adamc@707
|
3112 | _ => box [string "PRIMARY",
|
adamc@707
|
3113 space,
|
adamc@707
|
3114 string "KEY",
|
adamc@707
|
3115 space,
|
adamc@707
|
3116 string "(",
|
adamc@707
|
3117 string pk,
|
adamc@707
|
3118 string ")",
|
adamc@707
|
3119 case csts of
|
adamc@707
|
3120 [] => box []
|
adamc@707
|
3121 | _ => string ",",
|
adamc@707
|
3122 newline],
|
adamc@704
|
3123 p_list_sep (box [string ",", newline])
|
adamc@704
|
3124 (fn (x, c) =>
|
adamc@704
|
3125 box [string "CONSTRAINT",
|
adamc@704
|
3126 space,
|
adamc@704
|
3127 string s,
|
adamc@704
|
3128 string "_",
|
adamc@704
|
3129 string x,
|
adamc@704
|
3130 space,
|
adamc@704
|
3131 string c]) csts,
|
adamc@704
|
3132 newline,
|
adamc@274
|
3133 string ");",
|
adamc@274
|
3134 newline,
|
adamc@274
|
3135 newline]
|
adamc@338
|
3136 | DSequence s =>
|
adamc@877
|
3137 box [string (#createSequence (Settings.currentDbms ()) s),
|
adamc@338
|
3138 string ";",
|
adamc@338
|
3139 newline,
|
adamc@338
|
3140 newline]
|
adamc@754
|
3141 | DView (s, xts, q) =>
|
adamc@754
|
3142 box [string "CREATE VIEW",
|
adamc@754
|
3143 space,
|
adamc@754
|
3144 string s,
|
adamc@754
|
3145 space,
|
adamc@754
|
3146 string "AS",
|
adamc@754
|
3147 space,
|
adamc@754
|
3148 string q,
|
adamc@754
|
3149 string ";",
|
adamc@754
|
3150 newline,
|
adamc@754
|
3151 newline]
|
adamc@274
|
3152 | _ => box []
|
adamc@274
|
3153 in
|
adamc@274
|
3154 (pp, E.declBinds env dAll)
|
adamc@274
|
3155 end)
|
adamc@274
|
3156 env ds
|
adamc@274
|
3157 in
|
adamc@882
|
3158 box (string (#sqlPrefix (Settings.currentDbms ())) :: pps)
|
adamc@274
|
3159 end
|
adamc@274
|
3160
|
adamc@29
|
3161 end
|