adamc@643
|
1 (* Copyright (c) 2008-2009, 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@311
|
82 string ("__uwe_" ^ #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@198
|
88 case #1 t of
|
adamc@198
|
89 TDatatype _ => p_typ' par env t
|
adamc@199
|
90 | TFfi ("Basis", "string") => p_typ' par env t
|
adamc@198
|
91 | _ => box [p_typ' par env t,
|
adamc@198
|
92 string "*"])
|
adamc@188
|
93 | TDatatype (Default, n, _) =>
|
adamc@165
|
94 (box [string "struct",
|
adamc@165
|
95 space,
|
adamc@311
|
96 string ("__uwd_" ^ #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@29
|
105
|
adamc@29
|
106 and p_typ env = p_typ' false env
|
adamc@29
|
107
|
adamc@316
|
108 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@311
|
109 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@29
|
110
|
adamc@109
|
111 fun p_enamed env n =
|
adamc@316
|
112 string ("__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n)
|
adamc@311
|
113 handle CjrEnv.UnboundNamed _ => string ("__uwn_UNBOUND_" ^ Int.toString n)
|
adamc@109
|
114
|
adamc@182
|
115 fun p_con_named env n =
|
adamc@316
|
116 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
|
adamc@311
|
117 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
|
adamc@182
|
118
|
adamc@182
|
119 fun p_pat_preamble env (p, _) =
|
adamc@182
|
120 case p of
|
adamc@182
|
121 PWild => (box [],
|
adamc@182
|
122 env)
|
adamc@182
|
123 | PVar (x, t) => (box [p_typ env t,
|
adamc@182
|
124 space,
|
adamc@311
|
125 string "__uwr_",
|
adamc@316
|
126 p_ident x,
|
adamc@182
|
127 string "_",
|
adamc@182
|
128 string (Int.toString (E.countERels env)),
|
adamc@182
|
129 string ";",
|
adamc@182
|
130 newline],
|
adamc@196
|
131 E.pushERel env x t)
|
adamc@182
|
132 | PPrim _ => (box [], env)
|
adamc@188
|
133 | PCon (_, _, NONE) => (box [], env)
|
adamc@188
|
134 | PCon (_, _, SOME p) => p_pat_preamble env p
|
adamc@182
|
135 | PRecord xps =>
|
adamc@182
|
136 foldl (fn ((_, p, _), (pp, env)) =>
|
adamc@182
|
137 let
|
adamc@182
|
138 val (pp', env) = p_pat_preamble env p
|
adamc@182
|
139 in
|
adamc@182
|
140 (box [pp', pp], env)
|
adamc@182
|
141 end) (box [], env) xps
|
adamc@288
|
142 | PNone _ => (box [], env)
|
adamc@288
|
143 | PSome (_, p) => p_pat_preamble env p
|
adamc@182
|
144
|
adamc@182
|
145 fun p_patCon env pc =
|
adamc@182
|
146 case pc of
|
adamc@182
|
147 PConVar n => p_con_named env n
|
adamc@316
|
148 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
|
adamc@182
|
149
|
adamc@182
|
150 fun p_pat (env, exit, depth) (p, _) =
|
adamc@182
|
151 case p of
|
adamc@182
|
152 PWild =>
|
adamc@182
|
153 (box [], env)
|
adamc@182
|
154 | PVar (x, t) =>
|
adamc@311
|
155 (box [string "__uwr_",
|
adamc@316
|
156 p_ident x,
|
adamc@182
|
157 string "_",
|
adamc@182
|
158 string (Int.toString (E.countERels env)),
|
adamc@182
|
159 space,
|
adamc@182
|
160 string "=",
|
adamc@182
|
161 space,
|
adamc@182
|
162 string "disc",
|
adamc@182
|
163 string (Int.toString depth),
|
adamc@182
|
164 string ";"],
|
adamc@182
|
165 E.pushERel env x t)
|
adamc@182
|
166 | PPrim (Prim.Int n) =>
|
adamc@182
|
167 (box [string "if",
|
adamc@182
|
168 space,
|
adamc@182
|
169 string "(disc",
|
adamc@182
|
170 string (Int.toString depth),
|
adamc@182
|
171 space,
|
adamc@182
|
172 string "!=",
|
adamc@182
|
173 space,
|
adamc@276
|
174 Prim.p_t_GCC (Prim.Int n),
|
adamc@182
|
175 string ")",
|
adamc@182
|
176 space,
|
adamc@182
|
177 exit],
|
adamc@182
|
178 env)
|
adamc@182
|
179 | PPrim (Prim.String s) =>
|
adamc@182
|
180 (box [string "if",
|
adamc@182
|
181 space,
|
adamc@182
|
182 string "(strcmp(disc",
|
adamc@182
|
183 string (Int.toString depth),
|
adamc@182
|
184 string ",",
|
adamc@182
|
185 space,
|
adamc@276
|
186 Prim.p_t_GCC (Prim.String s),
|
adamc@182
|
187 string "))",
|
adamc@182
|
188 space,
|
adamc@182
|
189 exit],
|
adamc@182
|
190 env)
|
adamc@182
|
191 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
|
adamc@182
|
192
|
adamc@188
|
193 | PCon (dk, pc, po) =>
|
adamc@182
|
194 let
|
adamc@182
|
195 val (p, env) =
|
adamc@182
|
196 case po of
|
adamc@182
|
197 NONE => (box [], env)
|
adamc@182
|
198 | SOME p =>
|
adamc@182
|
199 let
|
adamc@182
|
200 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
201
|
adamc@182
|
202 val (x, to) = case pc of
|
adamc@182
|
203 PConVar n =>
|
adamc@182
|
204 let
|
adamc@182
|
205 val (x, to, _) = E.lookupConstructor env n
|
adamc@182
|
206 in
|
adamc@316
|
207 ("uw_" ^ ident x, to)
|
adamc@182
|
208 end
|
adamc@188
|
209 | PConFfi {mod = m, con, arg, ...} =>
|
adamc@316
|
210 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
|
adamc@182
|
211
|
adamc@182
|
212 val t = case to of
|
adamc@182
|
213 NONE => raise Fail "CjrPrint: Constructor mismatch"
|
adamc@182
|
214 | SOME t => t
|
adamc@182
|
215 in
|
adamc@182
|
216 (box [string "{",
|
adamc@182
|
217 newline,
|
adamc@182
|
218 p_typ env t,
|
adamc@182
|
219 space,
|
adamc@182
|
220 string "disc",
|
adamc@182
|
221 string (Int.toString (depth + 1)),
|
adamc@182
|
222 space,
|
adamc@182
|
223 string "=",
|
adamc@182
|
224 space,
|
adamc@198
|
225 case dk of
|
adamc@198
|
226 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
|
adamc@198
|
227 | Default => box [string "disc",
|
adamc@198
|
228 string (Int.toString depth),
|
adamc@198
|
229 string "->data.",
|
adamc@198
|
230 string x]
|
adamc@198
|
231 | Option =>
|
adamc@463
|
232 if isUnboxable t then
|
adamc@463
|
233 box [string "disc",
|
adamc@463
|
234 string (Int.toString depth)]
|
adamc@463
|
235 else
|
adamc@463
|
236 box [string "*disc",
|
adamc@463
|
237 string (Int.toString depth)],
|
adamc@182
|
238 string ";",
|
adamc@182
|
239 newline,
|
adamc@182
|
240 p,
|
adamc@182
|
241 newline,
|
adamc@182
|
242 string "}"],
|
adamc@182
|
243 env)
|
adamc@182
|
244 end
|
adamc@182
|
245 in
|
adamc@182
|
246 (box [string "if",
|
adamc@182
|
247 space,
|
adamc@182
|
248 string "(disc",
|
adamc@182
|
249 string (Int.toString depth),
|
adamc@198
|
250 case (dk, po) of
|
adamc@198
|
251 (Enum, _) => box [space,
|
adamc@198
|
252 string "!=",
|
adamc@198
|
253 space,
|
adamc@198
|
254 p_patCon env pc]
|
adamc@198
|
255 | (Default, _) => box [string "->tag",
|
adamc@198
|
256 space,
|
adamc@198
|
257 string "!=",
|
adamc@198
|
258 space,
|
adamc@198
|
259 p_patCon env pc]
|
adamc@198
|
260 | (Option, NONE) => box [space,
|
adamc@198
|
261 string "!=",
|
adamc@198
|
262 space,
|
adamc@198
|
263 string "NULL"]
|
adamc@198
|
264 | (Option, SOME _) => box [space,
|
adamc@198
|
265 string "==",
|
adamc@198
|
266 space,
|
adamc@198
|
267 string "NULL"],
|
adamc@182
|
268 string ")",
|
adamc@182
|
269 space,
|
adamc@182
|
270 exit,
|
adamc@182
|
271 newline,
|
adamc@182
|
272 p],
|
adamc@182
|
273 env)
|
adamc@182
|
274 end
|
adamc@182
|
275
|
adamc@182
|
276 | PRecord xps =>
|
adamc@182
|
277 let
|
adamc@182
|
278 val (xps, env) =
|
adamc@182
|
279 ListUtil.foldlMap (fn ((x, p, t), env) =>
|
adamc@182
|
280 let
|
adamc@182
|
281 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
282
|
adamc@182
|
283 val p = box [string "{",
|
adamc@182
|
284 newline,
|
adamc@182
|
285 p_typ env t,
|
adamc@182
|
286 space,
|
adamc@182
|
287 string "disc",
|
adamc@182
|
288 string (Int.toString (depth + 1)),
|
adamc@182
|
289 space,
|
adamc@182
|
290 string "=",
|
adamc@182
|
291 space,
|
adamc@182
|
292 string "disc",
|
adamc@182
|
293 string (Int.toString depth),
|
adamc@311
|
294 string ".__uwf_",
|
adamc@316
|
295 p_ident x,
|
adamc@182
|
296 string ";",
|
adamc@182
|
297 newline,
|
adamc@182
|
298 p,
|
adamc@182
|
299 newline,
|
adamc@182
|
300 string "}"]
|
adamc@182
|
301 in
|
adamc@182
|
302 (p, env)
|
adamc@182
|
303 end) env xps
|
adamc@182
|
304 in
|
adamc@182
|
305 (p_list_sep newline (fn x => x) xps,
|
adamc@182
|
306 env)
|
adamc@182
|
307 end
|
adamc@182
|
308
|
adamc@288
|
309 | PNone t =>
|
adamc@288
|
310 (box [string "if",
|
adamc@288
|
311 space,
|
adamc@288
|
312 string "(disc",
|
adamc@288
|
313 string (Int.toString depth),
|
adamc@288
|
314 space,
|
adamc@288
|
315 string "!=",
|
adamc@288
|
316 space,
|
adamc@288
|
317 string "NULL)",
|
adamc@288
|
318 space,
|
adamc@288
|
319 exit,
|
adamc@288
|
320 newline],
|
adamc@288
|
321 env)
|
adamc@288
|
322
|
adamc@288
|
323 | PSome (t, p) =>
|
adamc@288
|
324 let
|
adamc@288
|
325 val (p, env) =
|
adamc@288
|
326 let
|
adamc@288
|
327 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@288
|
328 in
|
adamc@288
|
329 (box [string "{",
|
adamc@288
|
330 newline,
|
adamc@288
|
331 p_typ env t,
|
adamc@288
|
332 space,
|
adamc@288
|
333 string "disc",
|
adamc@288
|
334 string (Int.toString (depth + 1)),
|
adamc@288
|
335 space,
|
adamc@288
|
336 string "=",
|
adamc@288
|
337 space,
|
adamc@463
|
338 if isUnboxable t then
|
adamc@463
|
339 box [string "disc",
|
adamc@463
|
340 string (Int.toString depth)]
|
adamc@463
|
341 else
|
adamc@463
|
342 box [string "*disc",
|
adamc@463
|
343 string (Int.toString depth)],
|
adamc@288
|
344 string ";",
|
adamc@288
|
345 newline,
|
adamc@288
|
346 p,
|
adamc@288
|
347 newline,
|
adamc@288
|
348 string "}"],
|
adamc@288
|
349 env)
|
adamc@288
|
350 end
|
adamc@288
|
351 in
|
adamc@288
|
352 (box [string "if",
|
adamc@288
|
353 space,
|
adamc@288
|
354 string "(disc",
|
adamc@288
|
355 string (Int.toString depth),
|
adamc@288
|
356 space,
|
adamc@288
|
357 string "==",
|
adamc@288
|
358 space,
|
adamc@288
|
359 string "NULL)",
|
adamc@288
|
360 space,
|
adamc@288
|
361 exit,
|
adamc@288
|
362 newline,
|
adamc@288
|
363 p],
|
adamc@288
|
364 env)
|
adamc@288
|
365 end
|
adamc@288
|
366
|
adamc@182
|
367 local
|
adamc@182
|
368 val count = ref 0
|
adamc@182
|
369 in
|
adamc@182
|
370 fun newGoto () =
|
adamc@182
|
371 let
|
adamc@182
|
372 val r = !count
|
adamc@182
|
373 in
|
adamc@182
|
374 count := r + 1;
|
adamc@182
|
375 string ("L" ^ Int.toString r)
|
adamc@182
|
376 end
|
adamc@182
|
377 end
|
adamc@182
|
378
|
adamc@185
|
379 fun patConInfo env pc =
|
adamc@185
|
380 case pc of
|
adamc@185
|
381 PConVar n =>
|
adamc@185
|
382 let
|
adamc@185
|
383 val (x, _, dn) = E.lookupConstructor env n
|
adamc@185
|
384 val (dx, _) = E.lookupDatatype env dn
|
adamc@185
|
385 in
|
adamc@316
|
386 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
|
adamc@316
|
387 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
|
adamc@316
|
388 "uw_" ^ ident x)
|
adamc@185
|
389 end
|
adamc@186
|
390 | PConFfi {mod = m, datatyp, con, ...} =>
|
adamc@316
|
391 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
|
adamc@316
|
392 "uw_" ^ ident m ^ "_" ^ ident con,
|
adamc@316
|
393 "uw_" ^ ident con)
|
adamc@185
|
394
|
adamc@324
|
395 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e =
|
adamc@278
|
396 case t of
|
adamc@311
|
397 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
|
adamc@311
|
398 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
|
adamc@324
|
399 | TFfi ("Basis", "string") =>
|
adamc@324
|
400 if wontLeakStrings then
|
adamc@324
|
401 e
|
adamc@324
|
402 else
|
adamc@324
|
403 box [string "uw_Basis_strdup(ctx, ", e, string ")"]
|
adamc@311
|
404 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
|
adamc@438
|
405 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
|
adamc@678
|
406 | TFfi ("Basis", "channel") => box [string "uw_Basis_stringToChannel_error(ctx, ", e, string ")"]
|
adamc@467
|
407
|
adamc@278
|
408 | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
|
adamc@278
|
409 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@278
|
410 string "ERROR")
|
adamc@278
|
411
|
adamc@467
|
412 fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
|
adamc@467
|
413 case t of
|
adamc@467
|
414 TOption t =>
|
adamc@467
|
415 box [string "(PQgetisnull (res, i, ",
|
adamc@467
|
416 string (Int.toString i),
|
adamc@467
|
417 string ") ? NULL : ",
|
adamc@467
|
418 case t of
|
adamc@467
|
419 (TFfi ("Basis", "string"), _) => p_getcol wontLeakStrings env t i
|
adamc@467
|
420 | _ => box [string "({",
|
adamc@467
|
421 newline,
|
adamc@467
|
422 p_typ env t,
|
adamc@467
|
423 space,
|
adamc@467
|
424 string "*tmp = uw_malloc(ctx, sizeof(",
|
adamc@467
|
425 p_typ env t,
|
adamc@467
|
426 string "));",
|
adamc@467
|
427 newline,
|
adamc@467
|
428 string "*tmp = ",
|
adamc@467
|
429 p_getcol wontLeakStrings env t i,
|
adamc@467
|
430 string ";",
|
adamc@467
|
431 newline,
|
adamc@467
|
432 string "tmp;",
|
adamc@467
|
433 newline,
|
adamc@467
|
434 string "})"],
|
adamc@467
|
435 string ")"]
|
adamc@467
|
436
|
adamc@467
|
437 | _ =>
|
adamc@467
|
438 p_unsql wontLeakStrings env tAll
|
adamc@467
|
439 (box [string "PQgetvalue(res, i, ",
|
adamc@467
|
440 string (Int.toString i),
|
adamc@467
|
441 string ")"])
|
adamc@467
|
442
|
adamc@282
|
443 datatype sql_type =
|
adamc@282
|
444 Int
|
adamc@282
|
445 | Float
|
adamc@282
|
446 | String
|
adamc@282
|
447 | Bool
|
adamc@439
|
448 | Time
|
adamc@678
|
449 | Channel
|
adamc@467
|
450 | Nullable of sql_type
|
adamc@282
|
451
|
adamc@467
|
452 fun p_sql_type' t =
|
adamc@467
|
453 case t of
|
adamc@467
|
454 Int => "uw_Basis_int"
|
adamc@467
|
455 | Float => "uw_Basis_float"
|
adamc@467
|
456 | String => "uw_Basis_string"
|
adamc@467
|
457 | Bool => "uw_Basis_bool"
|
adamc@467
|
458 | Time => "uw_Basis_time"
|
adamc@678
|
459 | Channel => "uw_Basis_channel"
|
adamc@467
|
460 | Nullable String => "uw_Basis_string"
|
adamc@467
|
461 | Nullable t => p_sql_type' t ^ "*"
|
adamc@467
|
462
|
adamc@467
|
463 fun p_sql_type t = string (p_sql_type' t)
|
adamc@282
|
464
|
adamc@282
|
465 fun getPargs (e, _) =
|
adamc@282
|
466 case e of
|
adamc@282
|
467 EPrim (Prim.String _) => []
|
adamc@282
|
468 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
|
adamc@282
|
469
|
adamc@282
|
470 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
|
adamc@282
|
471 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
|
adamc@282
|
472 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
|
adamc@282
|
473 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
|
adamc@439
|
474 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)]
|
adamc@678
|
475 | EFfiApp ("Basis", "sqlifyChannel", [e]) => [(e, Channel)]
|
adamc@468
|
476
|
adamc@678
|
477 | ECase (e,
|
adamc@678
|
478 [((PNone _, _),
|
adamc@678
|
479 (EPrim (Prim.String "NULL"), _)),
|
adamc@678
|
480 ((PSome (_, (PVar _, _)), _),
|
adamc@678
|
481 (EFfiApp (m, x, [(ERel 0, _)]), _))],
|
adamc@678
|
482 _) => map (fn (x, y) => (x, Nullable y)) (getPargs (EFfiApp (m, x, [e]), #2 e))
|
adamc@468
|
483
|
adamc@491
|
484 | ECase (e,
|
adamc@491
|
485 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
|
adamc@491
|
486 (EPrim (Prim.String "TRUE"), _)),
|
adamc@491
|
487 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
|
adamc@491
|
488 (EPrim (Prim.String "FALSE"), _))],
|
adamc@491
|
489 _) => [(e, Bool)]
|
adamc@282
|
490
|
adamc@282
|
491 | _ => raise Fail "CjrPrint: getPargs"
|
adamc@282
|
492
|
adamc@282
|
493 fun p_ensql t e =
|
adamc@282
|
494 case t of
|
adamc@311
|
495 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
|
adamc@311
|
496 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
|
adamc@282
|
497 | String => e
|
adamc@295
|
498 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
|
adamc@439
|
499 | Time => box [string "uw_Basis_sqlifyTime(ctx, ", e, string ")"]
|
adamc@678
|
500 | Channel => box [string "uw_Basis_attrifyChannel(ctx, ", e, string ")"]
|
adamc@467
|
501 | Nullable String => e
|
adamc@467
|
502 | Nullable t => box [string "(",
|
adamc@467
|
503 e,
|
adamc@467
|
504 string " == NULL ? NULL : ",
|
adamc@467
|
505 p_ensql t (box [string "*", e]),
|
adamc@467
|
506 string ")"]
|
adamc@282
|
507
|
adamc@324
|
508 fun notLeaky env allowHeapAllocated =
|
adamc@324
|
509 let
|
adamc@638
|
510 fun nl ok (t, _) =
|
adamc@324
|
511 case t of
|
adamc@324
|
512 TFun _ => false
|
adamc@324
|
513 | TRecord n =>
|
adamc@324
|
514 let
|
adamc@324
|
515 val xts = E.lookupStruct env n
|
adamc@324
|
516 in
|
adamc@638
|
517 List.all (fn (_, t) => nl ok t) xts
|
adamc@324
|
518 end
|
adamc@638
|
519 | TDatatype (dk, n, ref cons) =>
|
adamc@638
|
520 IS.member (ok, n)
|
adamc@638
|
521 orelse
|
adamc@638
|
522 ((allowHeapAllocated orelse dk = Enum)
|
adamc@638
|
523 andalso
|
adamc@638
|
524 let
|
adamc@638
|
525 val ok' = IS.add (ok, n)
|
adamc@638
|
526 in
|
adamc@638
|
527 List.all (fn (_, _, to) => case to of
|
adamc@638
|
528 NONE => true
|
adamc@638
|
529 | SOME t => nl ok' t) cons
|
adamc@638
|
530 end)
|
adamc@324
|
531 | TFfi ("Basis", "string") => false
|
adamc@324
|
532 | TFfi _ => true
|
adamc@638
|
533 | TOption t => allowHeapAllocated andalso nl ok t
|
adamc@324
|
534 in
|
adamc@638
|
535 nl IS.empty
|
adamc@324
|
536 end
|
adamc@324
|
537
|
adamc@463
|
538 fun capitalize s =
|
adamc@463
|
539 if s = "" then
|
adamc@463
|
540 ""
|
adamc@463
|
541 else
|
adamc@463
|
542 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@463
|
543
|
adamc@463
|
544 fun unurlify env (t, loc) =
|
adamc@463
|
545 let
|
adamc@463
|
546 fun unurlify' rf t =
|
adamc@463
|
547 case t of
|
adamc@463
|
548 TFfi ("Basis", "unit") => string ("uw_unit_v")
|
adamc@463
|
549 | TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
|
adamc@463
|
550
|
adamc@463
|
551 | TRecord 0 => string "uw_unit_v"
|
adamc@463
|
552 | TRecord i =>
|
adamc@463
|
553 let
|
adamc@463
|
554 val xts = E.lookupStruct env i
|
adamc@463
|
555 in
|
adamc@463
|
556 box [string "({",
|
adamc@463
|
557 newline,
|
adamc@463
|
558 box (map (fn (x, t) =>
|
adamc@463
|
559 box [p_typ env t,
|
adamc@463
|
560 space,
|
adamc@463
|
561 string "uwr_",
|
adamc@463
|
562 string x,
|
adamc@463
|
563 space,
|
adamc@463
|
564 string "=",
|
adamc@463
|
565 space,
|
adamc@463
|
566 unurlify' rf (#1 t),
|
adamc@463
|
567 string ";",
|
adamc@463
|
568 newline]) xts),
|
adamc@463
|
569 string "struct",
|
adamc@463
|
570 space,
|
adamc@463
|
571 string "__uws_",
|
adamc@463
|
572 string (Int.toString i),
|
adamc@463
|
573 space,
|
adamc@463
|
574 string "tmp",
|
adamc@463
|
575 space,
|
adamc@463
|
576 string "=",
|
adamc@463
|
577 space,
|
adamc@463
|
578 string "{",
|
adamc@463
|
579 space,
|
adamc@463
|
580 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
|
adamc@463
|
581 string x]) xts,
|
adamc@463
|
582 space,
|
adamc@463
|
583 string "};",
|
adamc@463
|
584 newline,
|
adamc@463
|
585 string "tmp;",
|
adamc@463
|
586 newline,
|
adamc@463
|
587 string "})"]
|
adamc@463
|
588 end
|
adamc@463
|
589
|
adamc@463
|
590 | TDatatype (Enum, i, _) =>
|
adamc@463
|
591 let
|
adamc@463
|
592 val (x, xncs) = E.lookupDatatype env i
|
adamc@463
|
593
|
adamc@463
|
594 fun doEm xncs =
|
adamc@463
|
595 case xncs of
|
adamc@463
|
596 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
|
adamc@463
|
597 ^ x ^ "\"), (enum __uwe_"
|
adamc@463
|
598 ^ x ^ "_" ^ Int.toString i ^ ")0)")
|
adamc@463
|
599 | (x', n, to) :: rest =>
|
adamc@463
|
600 box [string "((!strncmp(request, \"",
|
adamc@463
|
601 string x',
|
adamc@463
|
602 string "\", ",
|
adamc@463
|
603 string (Int.toString (size x')),
|
adamc@463
|
604 string ") && (request[",
|
adamc@463
|
605 string (Int.toString (size x')),
|
adamc@463
|
606 string "] == 0 || request[",
|
adamc@463
|
607 string (Int.toString (size x')),
|
adamc@463
|
608 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
|
adamc@463
|
609 space,
|
adamc@463
|
610 string ":",
|
adamc@463
|
611 space,
|
adamc@463
|
612 doEm rest,
|
adamc@463
|
613 string ")"]
|
adamc@463
|
614 in
|
adamc@463
|
615 doEm xncs
|
adamc@463
|
616 end
|
adamc@463
|
617
|
adamc@463
|
618 | TDatatype (Option, i, xncs) =>
|
adamc@463
|
619 if IS.member (rf, i) then
|
adamc@463
|
620 box [string "unurlify_",
|
adamc@463
|
621 string (Int.toString i),
|
adamc@463
|
622 string "()"]
|
adamc@463
|
623 else
|
adamc@463
|
624 let
|
adamc@463
|
625 val (x, _) = E.lookupDatatype env i
|
adamc@463
|
626
|
adamc@463
|
627 val (no_arg, has_arg, t) =
|
adamc@463
|
628 case !xncs of
|
adamc@463
|
629 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@463
|
630 (no_arg, has_arg, t)
|
adamc@463
|
631 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@463
|
632 (no_arg, has_arg, t)
|
adamc@463
|
633 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
|
adamc@463
|
634
|
adamc@463
|
635 val rf = IS.add (rf, i)
|
adamc@463
|
636 in
|
adamc@463
|
637 box [string "({",
|
adamc@463
|
638 space,
|
adamc@463
|
639 p_typ env t,
|
adamc@463
|
640 space,
|
adamc@463
|
641 string "*unurlify_",
|
adamc@463
|
642 string (Int.toString i),
|
adamc@463
|
643 string "(void) {",
|
adamc@463
|
644 newline,
|
adamc@463
|
645 box [string "return (request[0] == '/' ? ++request : request,",
|
adamc@463
|
646 newline,
|
adamc@463
|
647 string "((!strncmp(request, \"",
|
adamc@463
|
648 string no_arg,
|
adamc@463
|
649 string "\", ",
|
adamc@463
|
650 string (Int.toString (size no_arg)),
|
adamc@463
|
651 string ") && (request[",
|
adamc@463
|
652 string (Int.toString (size no_arg)),
|
adamc@463
|
653 string "] == 0 || request[",
|
adamc@463
|
654 string (Int.toString (size no_arg)),
|
adamc@463
|
655 string "] == '/')) ? (request",
|
adamc@463
|
656 space,
|
adamc@463
|
657 string "+=",
|
adamc@463
|
658 space,
|
adamc@463
|
659 string (Int.toString (size no_arg)),
|
adamc@463
|
660 string ", NULL) : ((!strncmp(request, \"",
|
adamc@463
|
661 string has_arg,
|
adamc@463
|
662 string "\", ",
|
adamc@463
|
663 string (Int.toString (size has_arg)),
|
adamc@463
|
664 string ") && (request[",
|
adamc@463
|
665 string (Int.toString (size has_arg)),
|
adamc@463
|
666 string "] == 0 || request[",
|
adamc@463
|
667 string (Int.toString (size has_arg)),
|
adamc@463
|
668 string "] == '/')) ? (request",
|
adamc@463
|
669 space,
|
adamc@463
|
670 string "+=",
|
adamc@463
|
671 space,
|
adamc@463
|
672 string (Int.toString (size has_arg)),
|
adamc@463
|
673 string ", (request[0] == '/' ? ++request : NULL), ",
|
adamc@463
|
674 newline,
|
adamc@463
|
675
|
adamc@463
|
676 if isUnboxable t then
|
adamc@463
|
677 unurlify' rf (#1 t)
|
adamc@463
|
678 else
|
adamc@463
|
679 box [string "({",
|
adamc@463
|
680 newline,
|
adamc@463
|
681 p_typ env t,
|
adamc@463
|
682 space,
|
adamc@463
|
683 string "*tmp",
|
adamc@463
|
684 space,
|
adamc@463
|
685 string "=",
|
adamc@463
|
686 space,
|
adamc@463
|
687 string "uw_malloc(ctx, sizeof(",
|
adamc@463
|
688 p_typ env t,
|
adamc@463
|
689 string "));",
|
adamc@463
|
690 newline,
|
adamc@463
|
691 string "*tmp",
|
adamc@463
|
692 space,
|
adamc@463
|
693 string "=",
|
adamc@463
|
694 space,
|
adamc@463
|
695 unurlify' rf (#1 t),
|
adamc@463
|
696 string ";",
|
adamc@463
|
697 newline,
|
adamc@463
|
698 string "tmp;",
|
adamc@463
|
699 newline,
|
adamc@463
|
700 string "})"],
|
adamc@463
|
701 string ")",
|
adamc@463
|
702 newline,
|
adamc@463
|
703 string ":",
|
adamc@463
|
704 space,
|
adamc@463
|
705 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
|
adamc@463
|
706 ^ "\"), NULL))));"),
|
adamc@463
|
707 newline],
|
adamc@463
|
708 string "}",
|
adamc@463
|
709 newline,
|
adamc@463
|
710 newline,
|
adamc@463
|
711
|
adamc@463
|
712 string "unurlify_",
|
adamc@463
|
713 string (Int.toString i),
|
adamc@463
|
714 string "();",
|
adamc@463
|
715 newline,
|
adamc@463
|
716 string "})"]
|
adamc@463
|
717 end
|
adamc@463
|
718
|
adamc@463
|
719 | TDatatype (Default, i, _) =>
|
adamc@463
|
720 if IS.member (rf, i) then
|
adamc@463
|
721 box [string "unurlify_",
|
adamc@463
|
722 string (Int.toString i),
|
adamc@463
|
723 string "()"]
|
adamc@463
|
724 else
|
adamc@463
|
725 let
|
adamc@463
|
726 val (x, xncs) = E.lookupDatatype env i
|
adamc@463
|
727
|
adamc@463
|
728 val rf = IS.add (rf, i)
|
adamc@463
|
729
|
adamc@463
|
730 fun doEm xncs =
|
adamc@463
|
731 case xncs of
|
adamc@463
|
732 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
|
adamc@463
|
733 ^ x ^ "\"), NULL)")
|
adamc@463
|
734 | (x', n, to) :: rest =>
|
adamc@463
|
735 box [string "((!strncmp(request, \"",
|
adamc@463
|
736 string x',
|
adamc@463
|
737 string "\", ",
|
adamc@463
|
738 string (Int.toString (size x')),
|
adamc@463
|
739 string ") && (request[",
|
adamc@463
|
740 string (Int.toString (size x')),
|
adamc@463
|
741 string "] == 0 || request[",
|
adamc@463
|
742 string (Int.toString (size x')),
|
adamc@463
|
743 string "] == '/')) ? ({",
|
adamc@463
|
744 newline,
|
adamc@463
|
745 string "struct",
|
adamc@463
|
746 space,
|
adamc@463
|
747 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
|
adamc@463
|
748 space,
|
adamc@463
|
749 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
|
adamc@463
|
750 string x,
|
adamc@463
|
751 string "_",
|
adamc@463
|
752 string (Int.toString i),
|
adamc@463
|
753 string "));",
|
adamc@463
|
754 newline,
|
adamc@463
|
755 string "tmp->tag",
|
adamc@463
|
756 space,
|
adamc@463
|
757 string "=",
|
adamc@463
|
758 space,
|
adamc@463
|
759 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
|
adamc@463
|
760 string ";",
|
adamc@463
|
761 newline,
|
adamc@463
|
762 string "request",
|
adamc@463
|
763 space,
|
adamc@463
|
764 string "+=",
|
adamc@463
|
765 space,
|
adamc@463
|
766 string (Int.toString (size x')),
|
adamc@463
|
767 string ";",
|
adamc@463
|
768 newline,
|
adamc@463
|
769 string "if (request[0] == '/') ++request;",
|
adamc@463
|
770 newline,
|
adamc@463
|
771 case to of
|
adamc@463
|
772 NONE => box []
|
adamc@463
|
773 | SOME (t, _) => box [string "tmp->data.uw_",
|
adamc@463
|
774 p_ident x',
|
adamc@463
|
775 space,
|
adamc@463
|
776 string "=",
|
adamc@463
|
777 space,
|
adamc@463
|
778 unurlify' rf t,
|
adamc@463
|
779 string ";",
|
adamc@463
|
780 newline],
|
adamc@463
|
781 string "tmp;",
|
adamc@463
|
782 newline,
|
adamc@463
|
783 string "})",
|
adamc@463
|
784 space,
|
adamc@463
|
785 string ":",
|
adamc@463
|
786 space,
|
adamc@463
|
787 doEm rest,
|
adamc@463
|
788 string ")"]
|
adamc@463
|
789 in
|
adamc@463
|
790 box [string "({",
|
adamc@463
|
791 space,
|
adamc@463
|
792 p_typ env (t, ErrorMsg.dummySpan),
|
adamc@463
|
793 space,
|
adamc@463
|
794 string "unurlify_",
|
adamc@463
|
795 string (Int.toString i),
|
adamc@463
|
796 string "(void) {",
|
adamc@463
|
797 newline,
|
adamc@463
|
798 box [string "return",
|
adamc@463
|
799 space,
|
adamc@463
|
800 doEm xncs,
|
adamc@463
|
801 string ";",
|
adamc@463
|
802 newline],
|
adamc@463
|
803 string "}",
|
adamc@463
|
804 newline,
|
adamc@463
|
805 newline,
|
adamc@463
|
806
|
adamc@463
|
807 string "unurlify_",
|
adamc@463
|
808 string (Int.toString i),
|
adamc@463
|
809 string "();",
|
adamc@463
|
810 newline,
|
adamc@463
|
811 string "})"]
|
adamc@463
|
812 end
|
adamc@463
|
813
|
adamc@471
|
814 | TOption t =>
|
adamc@471
|
815 box [string "(request[0] == '/' ? ++request : request, ",
|
adamc@471
|
816 string "((!strncmp(request, \"None\", 4) ",
|
adamc@471
|
817 string "&& (request[4] == 0 || request[4] == '/')) ",
|
adamc@471
|
818 string "? (request += 4, NULL) ",
|
adamc@471
|
819 string ": ((!strncmp(request, \"Some\", 4) ",
|
adamc@471
|
820 string "&& request[4] == '/') ",
|
adamc@471
|
821 string "? (request += 5, ",
|
adamc@471
|
822 if isUnboxable t then
|
adamc@471
|
823 unurlify' rf (#1 t)
|
adamc@471
|
824 else
|
adamc@471
|
825 box [string "({",
|
adamc@471
|
826 newline,
|
adamc@471
|
827 p_typ env t,
|
adamc@471
|
828 space,
|
adamc@471
|
829 string "*tmp",
|
adamc@471
|
830 space,
|
adamc@471
|
831 string "=",
|
adamc@471
|
832 space,
|
adamc@471
|
833 string "uw_malloc(ctx, sizeof(",
|
adamc@471
|
834 p_typ env t,
|
adamc@471
|
835 string "));",
|
adamc@471
|
836 newline,
|
adamc@471
|
837 string "*tmp",
|
adamc@471
|
838 space,
|
adamc@471
|
839 string "=",
|
adamc@471
|
840 space,
|
adamc@471
|
841 unurlify' rf (#1 t),
|
adamc@471
|
842 string ";",
|
adamc@471
|
843 newline,
|
adamc@471
|
844 string "tmp;",
|
adamc@471
|
845 newline,
|
adamc@471
|
846 string "})"],
|
adamc@471
|
847 string ") :",
|
adamc@471
|
848 space,
|
adamc@471
|
849 string "(uw_error(ctx, FATAL, \"Error unurlifying option type\"), NULL))))"]
|
adamc@471
|
850
|
adamc@463
|
851 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
|
adamc@463
|
852 space)
|
adamc@463
|
853 in
|
adamc@463
|
854 unurlify' IS.empty t
|
adamc@463
|
855 end
|
adamc@463
|
856
|
adamc@610
|
857 fun urlify env t =
|
adamc@610
|
858 let
|
adamc@610
|
859 fun urlify' rf level (t as (_, loc)) =
|
adamc@610
|
860 case #1 t of
|
adamc@610
|
861 TFfi ("Basis", "unit") => box []
|
adamc@610
|
862 | TFfi (m, t) => box [string ("uw_" ^ ident m ^ "_urlify" ^ capitalize t
|
adamc@610
|
863 ^ "_w(ctx, it" ^ Int.toString level ^ ");"),
|
adamc@610
|
864 newline]
|
adamc@610
|
865
|
adamc@610
|
866 | TRecord 0 => box []
|
adamc@610
|
867 | TRecord i =>
|
adamc@610
|
868 let
|
adamc@611
|
869 fun empty (t, _) =
|
adamc@611
|
870 case t of
|
adamc@611
|
871 TFfi ("Basis", "unit") => true
|
adamc@611
|
872 | TRecord 0 => true
|
adamc@611
|
873 | TRecord j =>
|
adamc@611
|
874 List.all (fn (_, t) => empty t) (E.lookupStruct env j)
|
adamc@611
|
875 | _ => false
|
adamc@611
|
876
|
adamc@610
|
877 val xts = E.lookupStruct env i
|
adamc@611
|
878
|
adamc@613
|
879 val (blocks, _) = foldl
|
adamc@613
|
880 (fn ((x, t), (blocks, printingSinceLastSlash)) =>
|
adamc@613
|
881 let
|
adamc@613
|
882 val thisEmpty = empty t
|
adamc@613
|
883 in
|
adamc@613
|
884 if thisEmpty then
|
adamc@613
|
885 (blocks, printingSinceLastSlash)
|
adamc@613
|
886 else
|
adamc@613
|
887 (box [string "{",
|
adamc@613
|
888 newline,
|
adamc@613
|
889 p_typ env t,
|
adamc@613
|
890 space,
|
adamc@613
|
891 string ("it" ^ Int.toString (level + 1)),
|
adamc@613
|
892 space,
|
adamc@613
|
893 string "=",
|
adamc@613
|
894 space,
|
adamc@613
|
895 string ("it" ^ Int.toString level ^ ".__uwf_" ^ x ^ ";"),
|
adamc@613
|
896 newline,
|
adamc@613
|
897 box (if printingSinceLastSlash then
|
adamc@613
|
898 [string "uw_write(ctx, \"/\");",
|
adamc@613
|
899 newline]
|
adamc@613
|
900 else
|
adamc@613
|
901 []),
|
adamc@613
|
902 urlify' rf (level + 1) t,
|
adamc@613
|
903 string "}",
|
adamc@613
|
904 newline] :: blocks,
|
adamc@613
|
905 true)
|
adamc@613
|
906 end)
|
adamc@613
|
907 ([], false) xts
|
adamc@610
|
908 in
|
adamc@613
|
909 box (rev blocks)
|
adamc@610
|
910 end
|
adamc@610
|
911
|
adamc@638
|
912 | TDatatype (Enum, i, _) =>
|
adamc@638
|
913 let
|
adamc@610
|
914 val (x, xncs) = E.lookupDatatype env i
|
adamc@610
|
915
|
adamc@610
|
916 fun doEm xncs =
|
adamc@610
|
917 case xncs of
|
adamc@638
|
918 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
|
adamc@638
|
919 ^ x ^ "\");"),
|
adamc@638
|
920 newline]
|
adamc@610
|
921 | (x', n, to) :: rest =>
|
adamc@638
|
922 box [string ("if (it" ^ Int.toString level
|
adamc@638
|
923 ^ "==__uwc_" ^ ident x' ^ "_" ^ Int.toString n ^ ") {"),
|
adamc@638
|
924 newline,
|
adamc@638
|
925 box [string ("uw_write(ctx, \"" ^ x' ^ "\");"),
|
adamc@638
|
926 newline],
|
adamc@638
|
927 string "} else {",
|
adamc@638
|
928 newline,
|
adamc@638
|
929 box [doEm rest,
|
adamc@638
|
930 newline],
|
adamc@638
|
931 string "}"]
|
adamc@610
|
932 in
|
adamc@610
|
933 doEm xncs
|
adamc@638
|
934 end
|
adamc@610
|
935
|
adamc@639
|
936 | TDatatype (Option, i, xncs) =>
|
adamc@639
|
937 if IS.member (rf, i) then
|
adamc@639
|
938 box [string "urlify_",
|
adamc@610
|
939 string (Int.toString i),
|
adamc@639
|
940 string "(it",
|
adamc@639
|
941 string (Int.toString level),
|
adamc@639
|
942 string ");",
|
adamc@639
|
943 newline]
|
adamc@610
|
944 else
|
adamc@610
|
945 let
|
adamc@610
|
946 val (x, _) = E.lookupDatatype env i
|
adamc@610
|
947
|
adamc@610
|
948 val (no_arg, has_arg, t) =
|
adamc@610
|
949 case !xncs of
|
adamc@610
|
950 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@610
|
951 (no_arg, has_arg, t)
|
adamc@610
|
952 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@610
|
953 (no_arg, has_arg, t)
|
adamc@639
|
954 | _ => raise Fail "CjrPrint: urlify misclassified Option datatype"
|
adamc@610
|
955
|
adamc@610
|
956 val rf = IS.add (rf, i)
|
adamc@610
|
957 in
|
adamc@610
|
958 box [string "({",
|
adamc@610
|
959 space,
|
adamc@639
|
960 string "void",
|
adamc@639
|
961 space,
|
adamc@639
|
962 string "urlify_",
|
adamc@639
|
963 string (Int.toString i),
|
adamc@639
|
964 string "(",
|
adamc@610
|
965 p_typ env t,
|
adamc@610
|
966 space,
|
adamc@639
|
967 if isUnboxable t then
|
adamc@639
|
968 box []
|
adamc@639
|
969 else
|
adamc@639
|
970 string "*",
|
adamc@639
|
971 string "it0) {",
|
adamc@610
|
972 newline,
|
adamc@639
|
973 box [string "if (it0) {",
|
adamc@639
|
974 if isUnboxable t then
|
adamc@639
|
975 urlify' rf 0 t
|
adamc@610
|
976 else
|
adamc@639
|
977 box [p_typ env t,
|
adamc@610
|
978 space,
|
adamc@639
|
979 string "it1",
|
adamc@610
|
980 space,
|
adamc@610
|
981 string "=",
|
adamc@610
|
982 space,
|
adamc@639
|
983 string "*it0;",
|
adamc@610
|
984 newline,
|
adamc@639
|
985 string "uw_write(ctx, \"",
|
adamc@639
|
986 string has_arg,
|
adamc@639
|
987 string "/\");",
|
adamc@639
|
988 newline,
|
adamc@639
|
989 urlify' rf 1 t,
|
adamc@610
|
990 string ";",
|
adamc@639
|
991 newline],
|
adamc@639
|
992 string "} else {",
|
adamc@639
|
993 box [string "uw_write(ctx, \"",
|
adamc@639
|
994 string no_arg,
|
adamc@639
|
995 string "\");",
|
adamc@639
|
996 newline],
|
adamc@639
|
997 string "}",
|
adamc@610
|
998 newline],
|
adamc@610
|
999 string "}",
|
adamc@610
|
1000 newline,
|
adamc@610
|
1001 newline,
|
adamc@610
|
1002
|
adamc@639
|
1003 string "urlify_",
|
adamc@610
|
1004 string (Int.toString i),
|
adamc@639
|
1005 string "(it",
|
adamc@639
|
1006 string (Int.toString level),
|
adamc@639
|
1007 string ");",
|
adamc@610
|
1008 newline,
|
adamc@639
|
1009 string "});",
|
adamc@639
|
1010 newline]
|
adamc@639
|
1011 end
|
adamc@610
|
1012
|
adamc@640
|
1013 | TDatatype (Default, i, _) =>
|
adamc@640
|
1014 if IS.member (rf, i) then
|
adamc@640
|
1015 box [string "urlify_",
|
adamc@610
|
1016 string (Int.toString i),
|
adamc@640
|
1017 string "(it",
|
adamc@640
|
1018 string (Int.toString level),
|
adamc@640
|
1019 string ");",
|
adamc@640
|
1020 newline]
|
adamc@610
|
1021 else
|
adamc@610
|
1022 let
|
adamc@610
|
1023 val (x, xncs) = E.lookupDatatype env i
|
adamc@610
|
1024
|
adamc@610
|
1025 val rf = IS.add (rf, i)
|
adamc@610
|
1026
|
adamc@610
|
1027 fun doEm xncs =
|
adamc@610
|
1028 case xncs of
|
adamc@640
|
1029 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
|
adamc@640
|
1030 ^ x ^ " (%d)\", it0->data);"),
|
adamc@640
|
1031 newline]
|
adamc@610
|
1032 | (x', n, to) :: rest =>
|
adamc@640
|
1033 box [string "if",
|
adamc@610
|
1034 space,
|
adamc@640
|
1035 string "(it0->tag==__uwc_",
|
adamc@640
|
1036 string (ident x'),
|
adamc@610
|
1037 string "_",
|
adamc@640
|
1038 string (Int.toString n),
|
adamc@640
|
1039 string ") {",
|
adamc@610
|
1040 newline,
|
adamc@610
|
1041 case to of
|
adamc@640
|
1042 NONE => box [string "uw_write(ctx, \"",
|
adamc@640
|
1043 string x',
|
adamc@640
|
1044 string "\");",
|
adamc@640
|
1045 newline]
|
adamc@640
|
1046 | SOME t => box [string "uw_write(ctx, \"",
|
adamc@640
|
1047 string x',
|
adamc@640
|
1048 string "/\");",
|
adamc@640
|
1049 newline,
|
adamc@640
|
1050 p_typ env t,
|
adamc@640
|
1051 space,
|
adamc@640
|
1052 string "it1",
|
adamc@640
|
1053 space,
|
adamc@640
|
1054 string "=",
|
adamc@640
|
1055 space,
|
adamc@640
|
1056 string "it0->data.uw_",
|
adamc@640
|
1057 string x',
|
adamc@640
|
1058 string ";",
|
adamc@640
|
1059 newline,
|
adamc@640
|
1060 urlify' rf 1 t,
|
adamc@640
|
1061 newline],
|
adamc@640
|
1062 string "} else {",
|
adamc@610
|
1063 newline,
|
adamc@640
|
1064 box [doEm rest,
|
adamc@640
|
1065 newline],
|
adamc@640
|
1066 string "}",
|
adamc@640
|
1067 newline]
|
adamc@610
|
1068 in
|
adamc@610
|
1069 box [string "({",
|
adamc@610
|
1070 space,
|
adamc@640
|
1071 string "void",
|
adamc@610
|
1072 space,
|
adamc@640
|
1073 string "urlify_",
|
adamc@610
|
1074 string (Int.toString i),
|
adamc@640
|
1075 string "(",
|
adamc@640
|
1076 p_typ env t,
|
adamc@640
|
1077 space,
|
adamc@640
|
1078 string "it0) {",
|
adamc@610
|
1079 newline,
|
adamc@640
|
1080 box [doEm xncs,
|
adamc@610
|
1081 newline],
|
adamc@640
|
1082 newline,
|
adamc@610
|
1083 string "}",
|
adamc@610
|
1084 newline,
|
adamc@640
|
1085
|
adamc@640
|
1086 string "urlify_",
|
adamc@640
|
1087 string (Int.toString i),
|
adamc@640
|
1088 string "(it",
|
adamc@640
|
1089 string (Int.toString level),
|
adamc@640
|
1090 string ");",
|
adamc@610
|
1091 newline,
|
adamc@640
|
1092 string "});",
|
adamc@640
|
1093 newline]
|
adamc@640
|
1094 end
|
adamc@610
|
1095
|
adamc@641
|
1096 | TOption t =>
|
adamc@641
|
1097 box [string "if (it",
|
adamc@641
|
1098 string (Int.toString level),
|
adamc@641
|
1099 string ") {",
|
adamc@641
|
1100 if isUnboxable t then
|
adamc@641
|
1101 box [string "uw_write(ctx, \"Some/\");",
|
adamc@641
|
1102 newline,
|
adamc@641
|
1103 urlify' rf level t]
|
adamc@610
|
1104 else
|
adamc@641
|
1105 box [p_typ env t,
|
adamc@610
|
1106 space,
|
adamc@641
|
1107 string "it",
|
adamc@641
|
1108 string (Int.toString (level + 1)),
|
adamc@610
|
1109 space,
|
adamc@610
|
1110 string "=",
|
adamc@610
|
1111 space,
|
adamc@641
|
1112 string "*it",
|
adamc@641
|
1113 string (Int.toString level),
|
adamc@610
|
1114 string ";",
|
adamc@610
|
1115 newline,
|
adamc@641
|
1116 string "uw_write(ctx, \"Some/\");",
|
adamc@610
|
1117 newline,
|
adamc@641
|
1118 urlify' rf (level + 1) t,
|
adamc@641
|
1119 string ";",
|
adamc@641
|
1120 newline],
|
adamc@641
|
1121 string "} else {",
|
adamc@641
|
1122 box [string "uw_write(ctx, \"None\");",
|
adamc@641
|
1123 newline],
|
adamc@641
|
1124 string "}",
|
adamc@641
|
1125 newline]
|
adamc@610
|
1126
|
adamc@610
|
1127 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL encoding function";
|
adamc@610
|
1128 space)
|
adamc@610
|
1129 in
|
adamc@610
|
1130 urlify' IS.empty 0 t
|
adamc@610
|
1131 end
|
adamc@610
|
1132
|
adamc@673
|
1133 val timeout = ref 0
|
adamc@673
|
1134
|
adamc@182
|
1135 fun p_exp' par env (e, loc) =
|
adamc@29
|
1136 case e of
|
adamc@276
|
1137 EPrim p => Prim.p_t_GCC p
|
adamc@29
|
1138 | ERel n => p_rel env n
|
adamc@109
|
1139 | ENamed n => p_enamed env n
|
adamc@188
|
1140 | ECon (Enum, pc, _) => p_patCon env pc
|
adamc@198
|
1141 | ECon (Option, pc, NONE) => string "NULL"
|
adamc@198
|
1142 | ECon (Option, pc, SOME e) =>
|
adamc@198
|
1143 let
|
adamc@198
|
1144 val to = case pc of
|
adamc@198
|
1145 PConVar n => #2 (E.lookupConstructor env n)
|
adamc@198
|
1146 | PConFfi {arg, ...} => arg
|
adamc@198
|
1147
|
adamc@198
|
1148 val t = case to of
|
adamc@198
|
1149 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
|
adamc@198
|
1150 | SOME t => t
|
adamc@198
|
1151 in
|
adamc@463
|
1152 if isUnboxable t then
|
adamc@463
|
1153 p_exp' par env e
|
adamc@463
|
1154 else
|
adamc@463
|
1155 box [string "({",
|
adamc@463
|
1156 newline,
|
adamc@463
|
1157 p_typ env t,
|
adamc@463
|
1158 space,
|
adamc@463
|
1159 string "*tmp",
|
adamc@463
|
1160 space,
|
adamc@463
|
1161 string "=",
|
adamc@463
|
1162 space,
|
adamc@463
|
1163 string "uw_malloc(ctx, sizeof(",
|
adamc@463
|
1164 p_typ env t,
|
adamc@463
|
1165 string "));",
|
adamc@463
|
1166 newline,
|
adamc@463
|
1167 string "*tmp",
|
adamc@463
|
1168 space,
|
adamc@463
|
1169 string "=",
|
adamc@463
|
1170 p_exp' par env e,
|
adamc@463
|
1171 string ";",
|
adamc@463
|
1172 newline,
|
adamc@463
|
1173 string "tmp;",
|
adamc@463
|
1174 newline,
|
adamc@463
|
1175 string "})"]
|
adamc@198
|
1176 end
|
adamc@188
|
1177 | ECon (Default, pc, eo) =>
|
adamc@181
|
1178 let
|
adamc@196
|
1179 val (xd, xc, xn) = patConInfo env pc
|
adamc@181
|
1180 in
|
adamc@182
|
1181 box [string "({",
|
adamc@181
|
1182 newline,
|
adamc@181
|
1183 string "struct",
|
adamc@181
|
1184 space,
|
adamc@185
|
1185 string xd,
|
adamc@181
|
1186 space,
|
adamc@181
|
1187 string "*tmp",
|
adamc@181
|
1188 space,
|
adamc@181
|
1189 string "=",
|
adamc@181
|
1190 space,
|
adamc@311
|
1191 string "uw_malloc(ctx, sizeof(struct ",
|
adamc@185
|
1192 string xd,
|
adamc@181
|
1193 string "));",
|
adamc@181
|
1194 newline,
|
adamc@181
|
1195 string "tmp->tag",
|
adamc@181
|
1196 space,
|
adamc@181
|
1197 string "=",
|
adamc@181
|
1198 space,
|
adamc@185
|
1199 string xc,
|
adamc@181
|
1200 string ";",
|
adamc@181
|
1201 newline,
|
adamc@181
|
1202 case eo of
|
adamc@181
|
1203 NONE => box []
|
adamc@185
|
1204 | SOME e => box [string "tmp->data.",
|
adamc@196
|
1205 string xn,
|
adamc@181
|
1206 space,
|
adamc@181
|
1207 string "=",
|
adamc@181
|
1208 space,
|
adamc@181
|
1209 p_exp env e,
|
adamc@181
|
1210 string ";",
|
adamc@181
|
1211 newline],
|
adamc@181
|
1212 string "tmp;",
|
adamc@181
|
1213 newline,
|
adamc@181
|
1214 string "})"]
|
adamc@181
|
1215 end
|
adamc@297
|
1216 | ENone _ => string "NULL"
|
adamc@290
|
1217 | ESome (t, e) =>
|
adamc@463
|
1218 if isUnboxable t then
|
adamc@463
|
1219 p_exp' par env e
|
adamc@463
|
1220 else
|
adamc@463
|
1221 box [string "({",
|
adamc@463
|
1222 newline,
|
adamc@463
|
1223 p_typ env t,
|
adamc@463
|
1224 space,
|
adamc@463
|
1225 string "*tmp",
|
adamc@463
|
1226 space,
|
adamc@463
|
1227 string "=",
|
adamc@463
|
1228 space,
|
adamc@463
|
1229 string "uw_malloc(ctx, sizeof(",
|
adamc@463
|
1230 p_typ env t,
|
adamc@463
|
1231 string "));",
|
adamc@463
|
1232 newline,
|
adamc@463
|
1233 string "*tmp",
|
adamc@463
|
1234 space,
|
adamc@463
|
1235 string "=",
|
adamc@463
|
1236 p_exp' par env e,
|
adamc@463
|
1237 string ";",
|
adamc@463
|
1238 newline,
|
adamc@463
|
1239 string "tmp;",
|
adamc@463
|
1240 newline,
|
adamc@463
|
1241 string "})"]
|
adamc@109
|
1242
|
adamc@316
|
1243 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
|
adamc@283
|
1244 | EError (e, t) =>
|
adamc@283
|
1245 box [string "({",
|
adamc@283
|
1246 newline,
|
adamc@283
|
1247 p_typ env t,
|
adamc@283
|
1248 space,
|
adamc@283
|
1249 string "tmp;",
|
adamc@283
|
1250 newline,
|
adamc@311
|
1251 string "uw_error(ctx, FATAL, \"",
|
adamc@292
|
1252 string (ErrorMsg.spanToString loc),
|
adamc@292
|
1253 string ": %s\", ",
|
adamc@283
|
1254 p_exp env e,
|
adamc@283
|
1255 string ");",
|
adamc@283
|
1256 newline,
|
adamc@283
|
1257 string "tmp;",
|
adamc@283
|
1258 newline,
|
adamc@283
|
1259 string "})"]
|
adamc@476
|
1260 | EApp ((EError (e, (TFun (_, ran), _)), loc), _) =>
|
adamc@476
|
1261 p_exp env (EError (e, ran), loc)
|
adamc@476
|
1262
|
adamc@311
|
1263 | EFfiApp (m, x, es) => box [string "uw_",
|
adamc@316
|
1264 p_ident m,
|
adamc@53
|
1265 string "_",
|
adamc@316
|
1266 p_ident x,
|
adamc@117
|
1267 string "(ctx, ",
|
adamc@53
|
1268 p_list (p_exp env) es,
|
adamc@53
|
1269 string ")"]
|
adamc@316
|
1270 | EApp (f, args) =>
|
adamc@316
|
1271 parenIf par (box [p_exp' true env f,
|
adamc@316
|
1272 string "(ctx,",
|
adamc@316
|
1273 space,
|
adamc@316
|
1274 p_list_sep (box [string ",", space]) (p_exp env) args,
|
adamc@316
|
1275 string ")"])
|
adamc@29
|
1276
|
adamc@387
|
1277 | EUnop (s, e1) =>
|
adamc@387
|
1278 parenIf par (box [string s,
|
adamc@387
|
1279 space,
|
adamc@387
|
1280 p_exp' true env e1])
|
adamc@387
|
1281
|
adamc@387
|
1282 | EBinop (s, e1, e2) =>
|
adamc@389
|
1283 if Char.isAlpha (String.sub (s, size s - 1)) then
|
adamc@389
|
1284 box [string s,
|
adamc@390
|
1285 string "(",
|
adamc@389
|
1286 p_exp env e1,
|
adamc@389
|
1287 string ",",
|
adamc@389
|
1288 space,
|
adamc@389
|
1289 p_exp env e2,
|
adamc@389
|
1290 string ")"]
|
adamc@389
|
1291 else
|
adamc@389
|
1292 parenIf par (box [p_exp' true env e1,
|
adamc@389
|
1293 space,
|
adamc@389
|
1294 string s,
|
adamc@389
|
1295 space,
|
adamc@389
|
1296 p_exp' true env e2])
|
adamc@387
|
1297
|
adamc@29
|
1298 | ERecord (i, xes) => box [string "({",
|
adamc@29
|
1299 space,
|
adamc@29
|
1300 string "struct",
|
adamc@29
|
1301 space,
|
adamc@311
|
1302 string ("__uws_" ^ Int.toString i),
|
adamc@29
|
1303 space,
|
adamc@181
|
1304 string "tmp",
|
adamc@29
|
1305 space,
|
adamc@29
|
1306 string "=",
|
adamc@29
|
1307 space,
|
adamc@29
|
1308 string "{",
|
adamc@29
|
1309 p_list (fn (_, e) =>
|
adamc@29
|
1310 p_exp env e) xes,
|
adamc@29
|
1311 string "};",
|
adamc@29
|
1312 space,
|
adamc@181
|
1313 string "tmp;",
|
adamc@29
|
1314 space,
|
adamc@29
|
1315 string "})" ]
|
adamc@29
|
1316 | EField (e, x) =>
|
adamc@29
|
1317 box [p_exp' true env e,
|
adamc@311
|
1318 string ".__uwf_",
|
adamc@316
|
1319 p_ident x]
|
adamc@29
|
1320
|
adamc@182
|
1321 | ECase (e, pes, {disc, result}) =>
|
adamc@182
|
1322 let
|
adamc@182
|
1323 val final = newGoto ()
|
adamc@182
|
1324
|
adamc@182
|
1325 val body = foldl (fn ((p, e), body) =>
|
adamc@182
|
1326 let
|
adamc@182
|
1327 val exit = newGoto ()
|
adamc@182
|
1328 val (pr, _) = p_pat_preamble env p
|
adamc@182
|
1329 val (p, env) = p_pat (env,
|
adamc@182
|
1330 box [string "goto",
|
adamc@182
|
1331 space,
|
adamc@182
|
1332 exit,
|
adamc@182
|
1333 string ";"],
|
adamc@182
|
1334 0) p
|
adamc@182
|
1335 in
|
adamc@182
|
1336 box [body,
|
adamc@182
|
1337 box [string "{",
|
adamc@182
|
1338 newline,
|
adamc@182
|
1339 pr,
|
adamc@182
|
1340 newline,
|
adamc@182
|
1341 p,
|
adamc@182
|
1342 newline,
|
adamc@182
|
1343 string "result",
|
adamc@182
|
1344 space,
|
adamc@182
|
1345 string "=",
|
adamc@182
|
1346 space,
|
adamc@182
|
1347 p_exp env e,
|
adamc@182
|
1348 string ";",
|
adamc@182
|
1349 newline,
|
adamc@182
|
1350 string "goto",
|
adamc@182
|
1351 space,
|
adamc@182
|
1352 final,
|
adamc@182
|
1353 string ";",
|
adamc@182
|
1354 newline,
|
adamc@182
|
1355 string "}"],
|
adamc@182
|
1356 newline,
|
adamc@182
|
1357 exit,
|
adamc@182
|
1358 string ":",
|
adamc@182
|
1359 newline]
|
adamc@182
|
1360 end) (box []) pes
|
adamc@182
|
1361 in
|
adamc@182
|
1362 box [string "({",
|
adamc@182
|
1363 newline,
|
adamc@182
|
1364 p_typ env disc,
|
adamc@182
|
1365 space,
|
adamc@182
|
1366 string "disc0",
|
adamc@182
|
1367 space,
|
adamc@182
|
1368 string "=",
|
adamc@182
|
1369 space,
|
adamc@182
|
1370 p_exp env e,
|
adamc@182
|
1371 string ";",
|
adamc@182
|
1372 newline,
|
adamc@182
|
1373 p_typ env result,
|
adamc@182
|
1374 space,
|
adamc@182
|
1375 string "result;",
|
adamc@182
|
1376 newline,
|
adamc@182
|
1377 body,
|
adamc@311
|
1378 string "uw_error(ctx, FATAL, \"",
|
adamc@182
|
1379 string (ErrorMsg.spanToString loc),
|
adamc@182
|
1380 string ": pattern match failure\");",
|
adamc@182
|
1381 newline,
|
adamc@182
|
1382 final,
|
adamc@182
|
1383 string ":",
|
adamc@182
|
1384 space,
|
adamc@182
|
1385 string "result;",
|
adamc@182
|
1386 newline,
|
adamc@182
|
1387 string "})"]
|
adamc@182
|
1388 end
|
adamc@181
|
1389
|
adamc@311
|
1390 | EWrite e => box [string "(uw_write(ctx, ",
|
adamc@102
|
1391 p_exp env e,
|
adamc@311
|
1392 string "), uw_unit_v)"]
|
adamc@102
|
1393
|
adamc@106
|
1394 | ESeq (e1, e2) => box [string "(",
|
adamc@106
|
1395 p_exp env e1,
|
adamc@106
|
1396 string ",",
|
adamc@106
|
1397 space,
|
adamc@106
|
1398 p_exp env e2,
|
adamc@106
|
1399 string ")"]
|
adamc@269
|
1400 | ELet (x, t, e1, e2) => box [string "({",
|
adamc@269
|
1401 newline,
|
adamc@269
|
1402 p_typ env t,
|
adamc@269
|
1403 space,
|
adamc@311
|
1404 string "__uwr_",
|
adamc@316
|
1405 p_ident x,
|
adamc@272
|
1406 string "_",
|
adamc@272
|
1407 string (Int.toString (E.countERels env)),
|
adamc@269
|
1408 space,
|
adamc@269
|
1409 string "=",
|
adamc@269
|
1410 space,
|
adamc@269
|
1411 p_exp env e1,
|
adamc@269
|
1412 string ";",
|
adamc@269
|
1413 newline,
|
adamc@269
|
1414 p_exp (E.pushERel env x t) e2,
|
adamc@269
|
1415 string ";",
|
adamc@269
|
1416 newline,
|
adamc@269
|
1417 string "})"]
|
adamc@269
|
1418
|
adamc@282
|
1419 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
|
adamc@278
|
1420 let
|
adamc@316
|
1421 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
|
adamc@278
|
1422 val tables = ListUtil.mapConcat (fn (x, xts) =>
|
adamc@316
|
1423 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
|
adamc@278
|
1424 tables
|
adamc@638
|
1425
|
adamc@278
|
1426 val outputs = exps @ tables
|
adamc@325
|
1427 val outputs = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER) outputs
|
adamc@324
|
1428
|
adamc@324
|
1429 val wontLeakStrings = notLeaky env true state
|
adamc@324
|
1430 val wontLeakAnything = notLeaky env false state
|
adamc@278
|
1431 in
|
adamc@640
|
1432 box [if wontLeakAnything then
|
adamc@331
|
1433 string "uw_begin_region(ctx), "
|
adamc@324
|
1434 else
|
adamc@324
|
1435 box [],
|
adamc@324
|
1436 string "({",
|
adamc@278
|
1437 newline,
|
adamc@311
|
1438 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@278
|
1439 newline,
|
adamc@640
|
1440 p_typ env state,
|
adamc@640
|
1441 space,
|
adamc@640
|
1442 string "acc",
|
adamc@640
|
1443 space,
|
adamc@640
|
1444 string "=",
|
adamc@640
|
1445 space,
|
adamc@640
|
1446 p_exp env initial,
|
adamc@640
|
1447 string ";",
|
adamc@640
|
1448 newline,
|
adamc@640
|
1449 string "int n, i, dummy = (uw_begin_region(ctx), 0);",
|
adamc@640
|
1450 newline,
|
adamc@640
|
1451
|
adamc@282
|
1452 case prepared of
|
adamc@282
|
1453 NONE => box [string "char *query = ",
|
adamc@282
|
1454 p_exp env query,
|
adamc@282
|
1455 string ";",
|
adamc@282
|
1456 newline]
|
adamc@282
|
1457 | SOME _ =>
|
adamc@282
|
1458 let
|
adamc@282
|
1459 val ets = getPargs query
|
adamc@282
|
1460 in
|
adamc@282
|
1461 box [p_list_sepi newline
|
adamc@282
|
1462 (fn i => fn (e, t) =>
|
adamc@282
|
1463 box [p_sql_type t,
|
adamc@282
|
1464 space,
|
adamc@282
|
1465 string "arg",
|
adamc@282
|
1466 string (Int.toString (i + 1)),
|
adamc@282
|
1467 space,
|
adamc@282
|
1468 string "=",
|
adamc@282
|
1469 space,
|
adamc@282
|
1470 p_exp env e,
|
adamc@282
|
1471 string ";"])
|
adamc@282
|
1472 ets,
|
adamc@282
|
1473 newline,
|
adamc@282
|
1474 newline,
|
adamc@282
|
1475
|
adamc@282
|
1476 string "const char *paramValues[] = { ",
|
adamc@282
|
1477 p_list_sepi (box [string ",", space])
|
adamc@282
|
1478 (fn i => fn (_, t) => p_ensql t (box [string "arg",
|
adamc@282
|
1479 string (Int.toString (i + 1))]))
|
adamc@282
|
1480 ets,
|
adamc@282
|
1481 string " };",
|
adamc@282
|
1482 newline,
|
adamc@282
|
1483 newline]
|
adamc@282
|
1484 end,
|
adamc@640
|
1485
|
adamc@282
|
1486 string "PGresult *res = ",
|
adamc@282
|
1487 case prepared of
|
adamc@295
|
1488 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@311
|
1489 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@282
|
1490 string (Int.toString n),
|
adamc@282
|
1491 string "\", ",
|
adamc@282
|
1492 string (Int.toString (length (getPargs query))),
|
adamc@295
|
1493 string ", paramValues, NULL, NULL, 0);"],
|
adamc@278
|
1494 newline,
|
adamc@278
|
1495 newline,
|
adamc@277
|
1496
|
adamc@311
|
1497 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@278
|
1498 newline,
|
adamc@278
|
1499 newline,
|
adamc@277
|
1500
|
adamc@278
|
1501 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@278
|
1502 newline,
|
adamc@278
|
1503 box [string "PQclear(res);",
|
adamc@278
|
1504 newline,
|
adamc@311
|
1505 string "uw_error(ctx, FATAL, \"",
|
adamc@278
|
1506 string (ErrorMsg.spanToString loc),
|
adamc@282
|
1507 string ": Query failed:\\n%s\\n%s\", ",
|
adamc@282
|
1508 case prepared of
|
adamc@282
|
1509 NONE => string "query"
|
adamc@282
|
1510 | SOME _ => p_exp env query,
|
adamc@282
|
1511 string ", PQerrorMessage(conn));",
|
adamc@278
|
1512 newline],
|
adamc@278
|
1513 string "}",
|
adamc@278
|
1514 newline,
|
adamc@278
|
1515 newline,
|
adamc@277
|
1516
|
adamc@323
|
1517 string "uw_end_region(ctx);",
|
adamc@323
|
1518 newline,
|
adamc@425
|
1519 string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",
|
adamc@425
|
1520 newline,
|
adamc@278
|
1521 string "n = PQntuples(res);",
|
adamc@278
|
1522 newline,
|
adamc@278
|
1523 string "for (i = 0; i < n; ++i) {",
|
adamc@278
|
1524 newline,
|
adamc@278
|
1525 box [string "struct",
|
adamc@278
|
1526 space,
|
adamc@311
|
1527 string "__uws_",
|
adamc@278
|
1528 string (Int.toString rnum),
|
adamc@278
|
1529 space,
|
adamc@311
|
1530 string "__uwr_r_",
|
adamc@278
|
1531 string (Int.toString (E.countERels env)),
|
adamc@278
|
1532 string ";",
|
adamc@278
|
1533 newline,
|
adamc@278
|
1534 p_typ env state,
|
adamc@278
|
1535 space,
|
adamc@311
|
1536 string "__uwr_acc_",
|
adamc@278
|
1537 string (Int.toString (E.countERels env + 1)),
|
adamc@278
|
1538 space,
|
adamc@278
|
1539 string "=",
|
adamc@278
|
1540 space,
|
adamc@278
|
1541 string "acc;",
|
adamc@278
|
1542 newline,
|
adamc@278
|
1543 newline,
|
adamc@278
|
1544
|
adamc@278
|
1545 p_list_sepi (box []) (fn i =>
|
adamc@278
|
1546 fn (proj, t) =>
|
adamc@311
|
1547 box [string "__uwr_r_",
|
adamc@278
|
1548 string (Int.toString (E.countERels env)),
|
adamc@278
|
1549 string ".",
|
adamc@278
|
1550 string proj,
|
adamc@278
|
1551 space,
|
adamc@278
|
1552 string "=",
|
adamc@278
|
1553 space,
|
adamc@467
|
1554 p_getcol wontLeakStrings env t i,
|
adamc@278
|
1555 string ";",
|
adamc@278
|
1556 newline]) outputs,
|
adamc@278
|
1557
|
adamc@278
|
1558 newline,
|
adamc@278
|
1559 newline,
|
adamc@278
|
1560
|
adamc@278
|
1561 string "acc",
|
adamc@278
|
1562 space,
|
adamc@278
|
1563 string "=",
|
adamc@278
|
1564 space,
|
adamc@278
|
1565 p_exp (E.pushERel
|
adamc@278
|
1566 (E.pushERel env "r" (TRecord rnum, loc))
|
adamc@278
|
1567 "acc" state)
|
adamc@278
|
1568 body,
|
adamc@278
|
1569 string ";",
|
adamc@278
|
1570 newline],
|
adamc@278
|
1571 string "}",
|
adamc@278
|
1572 newline,
|
adamc@278
|
1573 newline,
|
adamc@425
|
1574 string "uw_pop_cleanup(ctx);",
|
adamc@278
|
1575 newline,
|
adamc@324
|
1576 if wontLeakAnything then
|
adamc@324
|
1577 box [string "uw_end_region(ctx);",
|
adamc@324
|
1578 newline]
|
adamc@324
|
1579 else
|
adamc@324
|
1580 box [],
|
adamc@278
|
1581 string "acc;",
|
adamc@278
|
1582 newline,
|
adamc@640
|
1583 string "})"]
|
adamc@278
|
1584 end
|
adamc@106
|
1585
|
adamc@307
|
1586 | EDml {dml, prepared} =>
|
adamc@337
|
1587 box [string "(uw_begin_region(ctx), ({",
|
adamc@307
|
1588 newline,
|
adamc@311
|
1589 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@307
|
1590 newline,
|
adamc@307
|
1591 case prepared of
|
adamc@307
|
1592 NONE => box [string "char *dml = ",
|
adamc@307
|
1593 p_exp env dml,
|
adamc@307
|
1594 string ";",
|
adamc@307
|
1595 newline]
|
adamc@307
|
1596 | SOME _ =>
|
adamc@307
|
1597 let
|
adamc@307
|
1598 val ets = getPargs dml
|
adamc@307
|
1599 in
|
adamc@307
|
1600 box [p_list_sepi newline
|
adamc@307
|
1601 (fn i => fn (e, t) =>
|
adamc@307
|
1602 box [p_sql_type t,
|
adamc@307
|
1603 space,
|
adamc@307
|
1604 string "arg",
|
adamc@307
|
1605 string (Int.toString (i + 1)),
|
adamc@307
|
1606 space,
|
adamc@307
|
1607 string "=",
|
adamc@307
|
1608 space,
|
adamc@307
|
1609 p_exp env e,
|
adamc@307
|
1610 string ";"])
|
adamc@307
|
1611 ets,
|
adamc@307
|
1612 newline,
|
adamc@307
|
1613 newline,
|
adamc@307
|
1614
|
adamc@307
|
1615 string "const char *paramValues[] = { ",
|
adamc@307
|
1616 p_list_sepi (box [string ",", space])
|
adamc@307
|
1617 (fn i => fn (_, t) => p_ensql t (box [string "arg",
|
adamc@307
|
1618 string (Int.toString (i + 1))]))
|
adamc@307
|
1619 ets,
|
adamc@307
|
1620 string " };",
|
adamc@307
|
1621 newline,
|
adamc@307
|
1622 newline]
|
adamc@307
|
1623 end,
|
adamc@307
|
1624 newline,
|
adamc@307
|
1625 newline,
|
adamc@307
|
1626 string "PGresult *res = ",
|
adamc@307
|
1627 case prepared of
|
adamc@307
|
1628 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@311
|
1629 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@307
|
1630 string (Int.toString n),
|
adamc@307
|
1631 string "\", ",
|
adamc@307
|
1632 string (Int.toString (length (getPargs dml))),
|
adamc@307
|
1633 string ", paramValues, NULL, NULL, 0);"],
|
adamc@307
|
1634 newline,
|
adamc@307
|
1635 newline,
|
adamc@307
|
1636
|
adamc@311
|
1637 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
|
adamc@307
|
1638 newline,
|
adamc@307
|
1639 newline,
|
adamc@307
|
1640
|
adamc@307
|
1641 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@307
|
1642 newline,
|
adamc@307
|
1643 box [string "PQclear(res);",
|
adamc@307
|
1644 newline,
|
adamc@311
|
1645 string "uw_error(ctx, FATAL, \"",
|
adamc@307
|
1646 string (ErrorMsg.spanToString loc),
|
adamc@307
|
1647 string ": DML failed:\\n%s\\n%s\", ",
|
adamc@307
|
1648 case prepared of
|
adamc@307
|
1649 NONE => string "dml"
|
adamc@307
|
1650 | SOME _ => p_exp env dml,
|
adamc@307
|
1651 string ", PQerrorMessage(conn));",
|
adamc@307
|
1652 newline],
|
adamc@307
|
1653 string "}",
|
adamc@307
|
1654 newline,
|
adamc@307
|
1655 newline,
|
adamc@307
|
1656
|
adamc@307
|
1657 string "PQclear(res);",
|
adamc@307
|
1658 newline,
|
adamc@337
|
1659 string "uw_end_region(ctx);",
|
adamc@337
|
1660 newline,
|
adamc@311
|
1661 string "uw_unit_v;",
|
adamc@307
|
1662 newline,
|
adamc@337
|
1663 string "}))"]
|
adamc@307
|
1664
|
adamc@338
|
1665 | ENextval {seq, prepared} =>
|
adamc@338
|
1666 let
|
adamc@486
|
1667 val query = case seq of
|
adamc@486
|
1668 (EPrim (Prim.String s), loc) =>
|
adamc@486
|
1669 (EPrim (Prim.String ("SELECT NEXTVAL('" ^ s ^ "')")), loc)
|
adamc@486
|
1670 | _ =>
|
adamc@486
|
1671 let
|
adamc@486
|
1672 val query = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
|
adamc@486
|
1673 in
|
adamc@486
|
1674 (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), query]), loc)
|
adamc@486
|
1675 end
|
adamc@338
|
1676 in
|
adamc@338
|
1677 box [string "(uw_begin_region(ctx), ",
|
adamc@338
|
1678 string "({",
|
adamc@338
|
1679 newline,
|
adamc@338
|
1680 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@338
|
1681 newline,
|
adamc@338
|
1682 case prepared of
|
adamc@338
|
1683 NONE => box [string "char *query = ",
|
adamc@338
|
1684 p_exp env query,
|
adamc@338
|
1685 string ";",
|
adamc@338
|
1686 newline]
|
adamc@338
|
1687 | SOME _ =>
|
adamc@338
|
1688 box [],
|
adamc@338
|
1689 newline,
|
adamc@338
|
1690 string "PGresult *res = ",
|
adamc@338
|
1691 case prepared of
|
adamc@338
|
1692 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@338
|
1693 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@338
|
1694 string (Int.toString n),
|
adamc@338
|
1695 string "\", 0, NULL, NULL, NULL, 0);"],
|
adamc@338
|
1696 newline,
|
adamc@338
|
1697 string "uw_Basis_int n;",
|
adamc@338
|
1698 newline,
|
adamc@338
|
1699 newline,
|
adamc@338
|
1700
|
adamc@338
|
1701 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
|
adamc@338
|
1702 newline,
|
adamc@338
|
1703 newline,
|
adamc@338
|
1704
|
adamc@338
|
1705 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@338
|
1706 newline,
|
adamc@338
|
1707 box [string "PQclear(res);",
|
adamc@338
|
1708 newline,
|
adamc@338
|
1709 string "uw_error(ctx, FATAL, \"",
|
adamc@338
|
1710 string (ErrorMsg.spanToString loc),
|
adamc@338
|
1711 string ": Query failed:\\n%s\\n%s\", ",
|
adamc@338
|
1712 case prepared of
|
adamc@338
|
1713 NONE => string "query"
|
adamc@338
|
1714 | SOME _ => p_exp env query,
|
adamc@338
|
1715 string ", PQerrorMessage(conn));",
|
adamc@338
|
1716 newline],
|
adamc@338
|
1717 string "}",
|
adamc@338
|
1718 newline,
|
adamc@338
|
1719 newline,
|
adamc@338
|
1720
|
adamc@338
|
1721 string "uw_end_region(ctx);",
|
adamc@338
|
1722 newline,
|
adamc@338
|
1723 string "n = PQntuples(res);",
|
adamc@338
|
1724 newline,
|
adamc@338
|
1725 string "if (n != 1) {",
|
adamc@338
|
1726 newline,
|
adamc@338
|
1727 box [string "PQclear(res);",
|
adamc@338
|
1728 newline,
|
adamc@338
|
1729 string "uw_error(ctx, FATAL, \"",
|
adamc@338
|
1730 string (ErrorMsg.spanToString loc),
|
adamc@338
|
1731 string ": Wrong number of result rows:\\n%s\\n%s\", ",
|
adamc@338
|
1732 case prepared of
|
adamc@338
|
1733 NONE => string "query"
|
adamc@338
|
1734 | SOME _ => p_exp env query,
|
adamc@338
|
1735 string ", PQerrorMessage(conn));",
|
adamc@338
|
1736 newline],
|
adamc@338
|
1737 string "}",
|
adamc@338
|
1738 newline,
|
adamc@338
|
1739 newline,
|
adamc@338
|
1740
|
adamc@338
|
1741 string "n = ",
|
adamc@338
|
1742 p_unsql true env (TFfi ("Basis", "int"), loc)
|
adamc@338
|
1743 (string "PQgetvalue(res, 0, 0)"),
|
adamc@338
|
1744 string ";",
|
adamc@338
|
1745 newline,
|
adamc@338
|
1746 string "PQclear(res);",
|
adamc@338
|
1747 newline,
|
adamc@338
|
1748 string "n;",
|
adamc@338
|
1749 newline,
|
adamc@338
|
1750 string "}))"]
|
adamc@338
|
1751 end
|
adamc@338
|
1752
|
adamc@463
|
1753 | EUnurlify (e, t) =>
|
adamc@463
|
1754 let
|
adamc@463
|
1755 fun getIt () =
|
adamc@463
|
1756 if isUnboxable t then
|
adamc@463
|
1757 unurlify env t
|
adamc@463
|
1758 else
|
adamc@463
|
1759 box [string "({",
|
adamc@463
|
1760 newline,
|
adamc@463
|
1761 p_typ env t,
|
adamc@463
|
1762 string " *tmp = uw_malloc(ctx, sizeof(",
|
adamc@463
|
1763 p_typ env t,
|
adamc@463
|
1764 string "));",
|
adamc@463
|
1765 newline,
|
adamc@463
|
1766 string "*tmp = ",
|
adamc@463
|
1767 unurlify env t,
|
adamc@463
|
1768 string ";",
|
adamc@463
|
1769 newline,
|
adamc@463
|
1770 string "tmp;",
|
adamc@463
|
1771 newline,
|
adamc@463
|
1772 string "})"]
|
adamc@463
|
1773 in
|
adamc@463
|
1774 box [string "({",
|
adamc@463
|
1775 newline,
|
adamc@493
|
1776 string "uw_Basis_string request = uw_Basis_maybe_strdup(ctx, ",
|
adamc@463
|
1777 p_exp env e,
|
adamc@492
|
1778 string ");",
|
adamc@463
|
1779 newline,
|
adamc@463
|
1780 newline,
|
adamc@463
|
1781 string "(request ? ",
|
adamc@463
|
1782 getIt (),
|
adamc@463
|
1783 string " : NULL);",
|
adamc@463
|
1784 newline,
|
adamc@463
|
1785 string "})"]
|
adamc@463
|
1786 end
|
adamc@463
|
1787
|
adamc@29
|
1788 and p_exp env = p_exp' false env
|
adamc@29
|
1789
|
adamc@129
|
1790 fun p_fun env (fx, n, args, ran, e) =
|
adamc@129
|
1791 let
|
adamc@129
|
1792 val nargs = length args
|
adamc@129
|
1793 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
|
adamc@129
|
1794 in
|
adamc@129
|
1795 box [string "static",
|
adamc@129
|
1796 space,
|
adamc@129
|
1797 p_typ env ran,
|
adamc@129
|
1798 space,
|
adamc@316
|
1799 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@129
|
1800 string "(",
|
adamc@129
|
1801 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@311
|
1802 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
|
adamc@129
|
1803 box [p_typ env dom,
|
adamc@129
|
1804 space,
|
adamc@129
|
1805 p_rel env' (nargs - i - 1)]) args),
|
adamc@129
|
1806 string ")",
|
adamc@129
|
1807 space,
|
adamc@129
|
1808 string "{",
|
adamc@129
|
1809 newline,
|
adamc@638
|
1810 box [string "return(",
|
adamc@638
|
1811 p_exp env' e,
|
adamc@638
|
1812 string ");"],
|
adamc@129
|
1813 newline,
|
adamc@129
|
1814 string "}"]
|
adamc@129
|
1815 end
|
adamc@129
|
1816
|
adamc@129
|
1817 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@29
|
1818 case d of
|
adamc@29
|
1819 DStruct (n, xts) =>
|
adamc@196
|
1820 let
|
adamc@196
|
1821 val env = E.declBinds env dAll
|
adamc@196
|
1822 in
|
adamc@196
|
1823 box [string "struct",
|
adamc@196
|
1824 space,
|
adamc@311
|
1825 string ("__uws_" ^ Int.toString n),
|
adamc@196
|
1826 space,
|
adamc@196
|
1827 string "{",
|
adamc@196
|
1828 newline,
|
adamc@196
|
1829 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
|
adamc@196
|
1830 space,
|
adamc@311
|
1831 string "__uwf_",
|
adamc@316
|
1832 p_ident x,
|
adamc@196
|
1833 string ";",
|
adamc@196
|
1834 newline]) xts,
|
adamc@196
|
1835 string "};"]
|
adamc@196
|
1836 end
|
adamc@188
|
1837 | DDatatype (Enum, x, n, xncs) =>
|
adamc@188
|
1838 box [string "enum",
|
adamc@188
|
1839 space,
|
adamc@316
|
1840 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@188
|
1841 space,
|
adamc@188
|
1842 string "{",
|
adamc@188
|
1843 space,
|
adamc@316
|
1844 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adamc@316
|
1845 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
|
adamc@188
|
1846 space,
|
adamc@188
|
1847 string "};"]
|
adamc@198
|
1848 | DDatatype (Option, _, _, _) => box []
|
adamc@188
|
1849 | DDatatype (Default, x, n, xncs) =>
|
adamc@165
|
1850 let
|
adamc@165
|
1851 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
|
adamc@165
|
1852 | (x, n, SOME t) => SOME (x, n, t)) xncs
|
adamc@165
|
1853 in
|
adamc@165
|
1854 box [string "enum",
|
adamc@165
|
1855 space,
|
adamc@316
|
1856 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1857 space,
|
adamc@165
|
1858 string "{",
|
adamc@165
|
1859 space,
|
adamc@316
|
1860 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adamc@316
|
1861 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
|
adamc@165
|
1862 space,
|
adamc@165
|
1863 string "};",
|
adamc@165
|
1864 newline,
|
adamc@165
|
1865 newline,
|
adamc@165
|
1866 string "struct",
|
adamc@165
|
1867 space,
|
adamc@316
|
1868 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1869 space,
|
adamc@165
|
1870 string "{",
|
adamc@165
|
1871 newline,
|
adamc@165
|
1872 string "enum",
|
adamc@165
|
1873 space,
|
adamc@316
|
1874 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1875 space,
|
adamc@165
|
1876 string "tag;",
|
adamc@165
|
1877 newline,
|
adamc@165
|
1878 box (case xncsArgs of
|
adamc@165
|
1879 [] => []
|
adamc@165
|
1880 | _ => [string "union",
|
adamc@165
|
1881 space,
|
adamc@165
|
1882 string "{",
|
adamc@165
|
1883 newline,
|
adamc@165
|
1884 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
|
adamc@165
|
1885 space,
|
adamc@316
|
1886 string ("uw_" ^ ident x),
|
adamc@165
|
1887 string ";"]) xncsArgs,
|
adamc@165
|
1888 newline,
|
adamc@165
|
1889 string "}",
|
adamc@165
|
1890 space,
|
adamc@165
|
1891 string "data;",
|
adamc@165
|
1892 newline]),
|
adamc@165
|
1893 string "};"]
|
adamc@188
|
1894 end
|
adamc@29
|
1895
|
adamc@196
|
1896 | DDatatypeForward _ => box []
|
adamc@196
|
1897
|
adamc@29
|
1898 | DVal (x, n, t, e) =>
|
adamc@29
|
1899 box [p_typ env t,
|
adamc@29
|
1900 space,
|
adamc@316
|
1901 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@29
|
1902 space,
|
adamc@29
|
1903 string "=",
|
adamc@29
|
1904 space,
|
adamc@29
|
1905 p_exp env e,
|
adamc@29
|
1906 string ";"]
|
adamc@129
|
1907 | DFun vi => p_fun env vi
|
adamc@129
|
1908 | DFunRec vis =>
|
adamc@29
|
1909 let
|
adamc@129
|
1910 val env = E.declBinds env dAll
|
adamc@29
|
1911 in
|
adamc@129
|
1912 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
|
adamc@129
|
1913 box [string "static",
|
adamc@129
|
1914 space,
|
adamc@129
|
1915 p_typ env ran,
|
adamc@129
|
1916 space,
|
adamc@316
|
1917 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@311
|
1918 string "(uw_context,",
|
adamc@129
|
1919 space,
|
adamc@129
|
1920 p_list_sep (box [string ",", space])
|
adamc@129
|
1921 (fn (_, dom) => p_typ env dom) args,
|
adamc@129
|
1922 string ");"]) vis,
|
adamc@29
|
1923 newline,
|
adamc@129
|
1924 p_list_sep newline (p_fun env) vis,
|
adamc@129
|
1925 newline]
|
adamc@29
|
1926 end
|
adamc@273
|
1927 | DTable (x, _) => box [string "/* SQL table ",
|
adamc@273
|
1928 string x,
|
adamc@273
|
1929 string " */",
|
adamc@273
|
1930 newline]
|
adamc@338
|
1931 | DSequence x => box [string "/* SQL sequence ",
|
adamc@338
|
1932 string x,
|
adamc@338
|
1933 string " */",
|
adamc@338
|
1934 newline]
|
adamc@311
|
1935 | DDatabase s => box [string "static void uw_db_validate(uw_context);",
|
adamc@272
|
1936 newline,
|
adamc@311
|
1937 string "static void uw_db_prepare(uw_context);",
|
adamc@282
|
1938 newline,
|
adamc@275
|
1939 newline,
|
adamc@311
|
1940 string "void uw_db_init(uw_context ctx) {",
|
adamc@273
|
1941 newline,
|
adamc@272
|
1942 string "PGconn *conn = PQconnectdb(\"",
|
adamc@272
|
1943 string (String.toString s),
|
adamc@272
|
1944 string "\");",
|
adamc@272
|
1945 newline,
|
adamc@311
|
1946 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
1947 string "\"libpq can't allocate a connection.\");",
|
adamc@272
|
1948 newline,
|
adamc@272
|
1949 string "if (PQstatus(conn) != CONNECTION_OK) {",
|
adamc@272
|
1950 newline,
|
adamc@272
|
1951 box [string "char msg[1024];",
|
adamc@272
|
1952 newline,
|
adamc@272
|
1953 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@272
|
1954 newline,
|
adamc@272
|
1955 string "msg[1023] = 0;",
|
adamc@272
|
1956 newline,
|
adamc@272
|
1957 string "PQfinish(conn);",
|
adamc@272
|
1958 newline,
|
adamc@311
|
1959 string "uw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
1960 string "\"Connection to Postgres server failed: %s\", msg);"],
|
adamc@272
|
1961 newline,
|
adamc@272
|
1962 string "}",
|
adamc@272
|
1963 newline,
|
adamc@311
|
1964 string "uw_set_db(ctx, conn);",
|
adamc@272
|
1965 newline,
|
adamc@311
|
1966 string "uw_db_validate(ctx);",
|
adamc@275
|
1967 newline,
|
adamc@311
|
1968 string "uw_db_prepare(ctx);",
|
adamc@282
|
1969 newline,
|
adamc@272
|
1970 string "}",
|
adamc@272
|
1971 newline,
|
adamc@272
|
1972 newline,
|
adamc@311
|
1973 string "void uw_db_close(uw_context ctx) {",
|
adamc@272
|
1974 newline,
|
adamc@311
|
1975 string "PQfinish(uw_get_db(ctx));",
|
adamc@272
|
1976 newline,
|
adamc@272
|
1977 string "}",
|
adamc@424
|
1978 newline,
|
adamc@424
|
1979 newline,
|
adamc@424
|
1980
|
adamc@424
|
1981 string "int uw_db_begin(uw_context ctx) {",
|
adamc@424
|
1982 newline,
|
adamc@424
|
1983 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@424
|
1984 newline,
|
adamc@424
|
1985 string "PGresult *res = PQexec(conn, \"BEGIN\");",
|
adamc@424
|
1986 newline,
|
adamc@424
|
1987 newline,
|
adamc@424
|
1988 string "if (res == NULL) return 1;",
|
adamc@424
|
1989 newline,
|
adamc@424
|
1990 newline,
|
adamc@424
|
1991 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@424
|
1992 box [string "PQclear(res);",
|
adamc@424
|
1993 newline,
|
adamc@424
|
1994 string "return 1;",
|
adamc@424
|
1995 newline],
|
adamc@424
|
1996 string "}",
|
adamc@424
|
1997 newline,
|
adamc@424
|
1998 string "return 0;",
|
adamc@424
|
1999 newline,
|
adamc@424
|
2000 string "}",
|
adamc@424
|
2001 newline,
|
adamc@424
|
2002 newline,
|
adamc@424
|
2003
|
adamc@424
|
2004 string "int uw_db_commit(uw_context ctx) {",
|
adamc@424
|
2005 newline,
|
adamc@424
|
2006 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@424
|
2007 newline,
|
adamc@424
|
2008 string "PGresult *res = PQexec(conn, \"COMMIT\");",
|
adamc@424
|
2009 newline,
|
adamc@424
|
2010 newline,
|
adamc@424
|
2011 string "if (res == NULL) return 1;",
|
adamc@424
|
2012 newline,
|
adamc@424
|
2013 newline,
|
adamc@424
|
2014 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@424
|
2015 box [string "PQclear(res);",
|
adamc@424
|
2016 newline,
|
adamc@424
|
2017 string "return 1;",
|
adamc@424
|
2018 newline],
|
adamc@424
|
2019 string "}",
|
adamc@424
|
2020 newline,
|
adamc@424
|
2021 string "return 0;",
|
adamc@424
|
2022 newline,
|
adamc@424
|
2023 string "}",
|
adamc@424
|
2024 newline,
|
adamc@424
|
2025 newline,
|
adamc@424
|
2026
|
adamc@424
|
2027 string "int uw_db_rollback(uw_context ctx) {",
|
adamc@424
|
2028 newline,
|
adamc@424
|
2029 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@424
|
2030 newline,
|
adamc@424
|
2031 string "PGresult *res = PQexec(conn, \"ROLLBACK\");",
|
adamc@424
|
2032 newline,
|
adamc@424
|
2033 newline,
|
adamc@424
|
2034 string "if (res == NULL) return 1;",
|
adamc@424
|
2035 newline,
|
adamc@424
|
2036 newline,
|
adamc@424
|
2037 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@424
|
2038 box [string "PQclear(res);",
|
adamc@424
|
2039 newline,
|
adamc@424
|
2040 string "return 1;",
|
adamc@424
|
2041 newline],
|
adamc@424
|
2042 string "}",
|
adamc@424
|
2043 newline,
|
adamc@424
|
2044 string "return 0;",
|
adamc@424
|
2045 newline,
|
adamc@424
|
2046 string "}",
|
adamc@272
|
2047 newline]
|
adamc@29
|
2048
|
adamc@467
|
2049 | DPreparedStatements [] =>
|
adamc@467
|
2050 box [string "static void uw_db_prepare(uw_context ctx) {",
|
adamc@467
|
2051 newline,
|
adamc@467
|
2052 string "}"]
|
adamc@282
|
2053 | DPreparedStatements ss =>
|
adamc@311
|
2054 box [string "static void uw_db_prepare(uw_context ctx) {",
|
adamc@282
|
2055 newline,
|
adamc@311
|
2056 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@282
|
2057 newline,
|
adamc@282
|
2058 string "PGresult *res;",
|
adamc@282
|
2059 newline,
|
adamc@282
|
2060 newline,
|
adamc@282
|
2061
|
adamc@282
|
2062 p_list_sepi newline (fn i => fn (s, n) =>
|
adamc@311
|
2063 box [string "res = PQprepare(conn, \"uw",
|
adamc@282
|
2064 string (Int.toString i),
|
adamc@282
|
2065 string "\", \"",
|
adamc@282
|
2066 string (String.toString s),
|
adamc@282
|
2067 string "\", ",
|
adamc@282
|
2068 string (Int.toString n),
|
adamc@282
|
2069 string ", NULL);",
|
adamc@282
|
2070 newline,
|
adamc@282
|
2071 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@282
|
2072 newline,
|
adamc@282
|
2073 box [string "char msg[1024];",
|
adamc@282
|
2074 newline,
|
adamc@282
|
2075 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@282
|
2076 newline,
|
adamc@282
|
2077 string "msg[1023] = 0;",
|
adamc@282
|
2078 newline,
|
adamc@282
|
2079 string "PQclear(res);",
|
adamc@282
|
2080 newline,
|
adamc@282
|
2081 string "PQfinish(conn);",
|
adamc@282
|
2082 newline,
|
adamc@311
|
2083 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
|
adamc@282
|
2084 string (String.toString s),
|
adamc@282
|
2085 string "\\n%s\", msg);",
|
adamc@282
|
2086 newline],
|
adamc@282
|
2087 string "}",
|
adamc@282
|
2088 newline,
|
adamc@282
|
2089 string "PQclear(res);",
|
adamc@282
|
2090 newline])
|
adamc@282
|
2091 ss,
|
adamc@282
|
2092
|
adamc@282
|
2093 string "}"]
|
adamc@282
|
2094
|
adamc@569
|
2095 | DJavaScript s => box [string "static char jslib[] = \"",
|
adamc@569
|
2096 string (String.toString s),
|
adamc@569
|
2097 string "\";"]
|
adamc@569
|
2098
|
adamc@144
|
2099 datatype 'a search =
|
adamc@144
|
2100 Found of 'a
|
adamc@144
|
2101 | NotFound
|
adamc@144
|
2102 | Error
|
adamc@120
|
2103
|
adamc@467
|
2104 fun p_sqltype'' env (tAll as (t, loc)) =
|
adamc@275
|
2105 case t of
|
adamc@275
|
2106 TFfi ("Basis", "int") => "int8"
|
adamc@275
|
2107 | TFfi ("Basis", "float") => "float8"
|
adamc@275
|
2108 | TFfi ("Basis", "string") => "text"
|
adamc@275
|
2109 | TFfi ("Basis", "bool") => "bool"
|
adamc@438
|
2110 | TFfi ("Basis", "time") => "timestamp"
|
adamc@678
|
2111 | TFfi ("Basis", "channel") => "int4"
|
adamc@275
|
2112 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
|
adamc@275
|
2113 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@275
|
2114 "ERROR")
|
adamc@275
|
2115
|
adamc@467
|
2116 fun p_sqltype' env (tAll as (t, loc)) =
|
adamc@467
|
2117 case t of
|
adamc@467
|
2118 (TOption t, _) => p_sqltype'' env t
|
adamc@467
|
2119 | _ => p_sqltype'' env t ^ " NOT NULL"
|
adamc@467
|
2120
|
adamc@275
|
2121 fun p_sqltype env t = string (p_sqltype' env t)
|
adamc@101
|
2122
|
adamc@467
|
2123 fun p_sqltype_base' env t =
|
adamc@467
|
2124 case t of
|
adamc@467
|
2125 (TOption t, _) => p_sqltype'' env t
|
adamc@467
|
2126 | _ => p_sqltype'' env t
|
adamc@467
|
2127
|
adamc@467
|
2128 fun p_sqltype_base env t = string (p_sqltype_base' env t)
|
adamc@467
|
2129
|
adamc@467
|
2130 fun is_not_null t =
|
adamc@467
|
2131 case t of
|
adamc@467
|
2132 (TOption _, _) => false
|
adamc@467
|
2133 | _ => true
|
adamc@467
|
2134
|
adamc@101
|
2135 fun p_file env (ds, ps) =
|
adamc@29
|
2136 let
|
adamc@101
|
2137 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@638
|
2138 (p_decl env d,
|
adamc@638
|
2139 E.declBinds env d))
|
adamc@101
|
2140 env ds
|
adamc@144
|
2141
|
adamc@643
|
2142 val fields = foldl (fn ((ek, _, _, ts, _, _), fields) =>
|
adamc@144
|
2143 case ek of
|
adamc@144
|
2144 Core.Link => fields
|
adamc@608
|
2145 | Core.Rpc => fields
|
adamc@144
|
2146 | Core.Action =>
|
adamc@280
|
2147 case List.nth (ts, length ts - 2) of
|
adamc@144
|
2148 (TRecord i, _) =>
|
adamc@144
|
2149 let
|
adamc@144
|
2150 val xts = E.lookupStruct env i
|
adamc@144
|
2151 val xtsSet = SS.addList (SS.empty, map #1 xts)
|
adamc@144
|
2152 in
|
adamc@144
|
2153 foldl (fn ((x, _), fields) =>
|
adamc@144
|
2154 let
|
adamc@144
|
2155 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
|
adamc@144
|
2156 in
|
adamc@144
|
2157 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
|
adamc@144
|
2158 xtsSet'))
|
adamc@144
|
2159 end) fields xts
|
adamc@144
|
2160 end
|
adamc@144
|
2161 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
|
adamc@144
|
2162 SM.empty ps
|
adamc@144
|
2163
|
adamc@144
|
2164 val fnums = SM.foldli (fn (x, xs, fnums) =>
|
adamc@144
|
2165 let
|
adamc@144
|
2166 val unusable = SS.foldl (fn (x', unusable) =>
|
adamc@144
|
2167 case SM.find (fnums, x') of
|
adamc@144
|
2168 NONE => unusable
|
adamc@144
|
2169 | SOME n => IS.add (unusable, n))
|
adamc@144
|
2170 IS.empty xs
|
adamc@144
|
2171
|
adamc@144
|
2172 fun findAvailable n =
|
adamc@144
|
2173 if IS.member (unusable, n) then
|
adamc@144
|
2174 findAvailable (n + 1)
|
adamc@144
|
2175 else
|
adamc@144
|
2176 n
|
adamc@144
|
2177 in
|
adamc@144
|
2178 SM.insert (fnums, x, findAvailable 0)
|
adamc@144
|
2179 end)
|
adamc@144
|
2180 SM.empty fields
|
adamc@144
|
2181
|
adamc@144
|
2182 fun makeSwitch (fnums, i) =
|
adamc@144
|
2183 case SM.foldl (fn (n, NotFound) => Found n
|
adamc@144
|
2184 | (n, Error) => Error
|
adamc@144
|
2185 | (n, Found n') => if n = n' then
|
adamc@144
|
2186 Found n'
|
adamc@144
|
2187 else
|
adamc@144
|
2188 Error) NotFound fnums of
|
adamc@144
|
2189 NotFound => box [string "return",
|
adamc@144
|
2190 space,
|
adamc@144
|
2191 string "-1;"]
|
adamc@144
|
2192 | Found n => box [string "return",
|
adamc@144
|
2193 space,
|
adamc@144
|
2194 string (Int.toString n),
|
adamc@144
|
2195 string ";"]
|
adamc@144
|
2196 | Error =>
|
adamc@144
|
2197 let
|
adamc@144
|
2198 val cmap = SM.foldli (fn (x, n, cmap) =>
|
adamc@144
|
2199 let
|
adamc@144
|
2200 val ch = if i < size x then
|
adamc@144
|
2201 String.sub (x, i)
|
adamc@144
|
2202 else
|
adamc@144
|
2203 chr 0
|
adamc@144
|
2204
|
adamc@144
|
2205 val fnums = case CM.find (cmap, ch) of
|
adamc@144
|
2206 NONE => SM.empty
|
adamc@144
|
2207 | SOME fnums => fnums
|
adamc@144
|
2208 val fnums = SM.insert (fnums, x, n)
|
adamc@144
|
2209 in
|
adamc@144
|
2210 CM.insert (cmap, ch, fnums)
|
adamc@144
|
2211 end)
|
adamc@144
|
2212 CM.empty fnums
|
adamc@144
|
2213
|
adamc@144
|
2214 val cmap = CM.listItemsi cmap
|
adamc@144
|
2215 in
|
adamc@144
|
2216 case cmap of
|
adamc@144
|
2217 [(_, fnums)] =>
|
adamc@144
|
2218 box [string "if",
|
adamc@144
|
2219 space,
|
adamc@144
|
2220 string "(name[",
|
adamc@144
|
2221 string (Int.toString i),
|
adamc@144
|
2222 string "]",
|
adamc@144
|
2223 space,
|
adamc@144
|
2224 string "==",
|
adamc@144
|
2225 space,
|
adamc@144
|
2226 string "0)",
|
adamc@144
|
2227 space,
|
adamc@144
|
2228 string "return",
|
adamc@144
|
2229 space,
|
adamc@144
|
2230 string "-1;",
|
adamc@144
|
2231 newline,
|
adamc@144
|
2232 makeSwitch (fnums, i+1)]
|
adamc@144
|
2233 | _ =>
|
adamc@144
|
2234 box [string "switch",
|
adamc@144
|
2235 space,
|
adamc@144
|
2236 string "(name[",
|
adamc@144
|
2237 string (Int.toString i),
|
adamc@144
|
2238 string "])",
|
adamc@144
|
2239 space,
|
adamc@144
|
2240 string "{",
|
adamc@144
|
2241 newline,
|
adamc@144
|
2242 box (map (fn (ch, fnums) =>
|
adamc@144
|
2243 box [string "case",
|
adamc@144
|
2244 space,
|
adamc@144
|
2245 if ch = chr 0 then
|
adamc@144
|
2246 string "0:"
|
adamc@144
|
2247 else
|
adamc@144
|
2248 box [string "'",
|
adamc@144
|
2249 string (Char.toString ch),
|
adamc@144
|
2250 string "':"],
|
adamc@144
|
2251 newline,
|
adamc@144
|
2252 makeSwitch (fnums, i+1),
|
adamc@144
|
2253 newline]) cmap),
|
adamc@144
|
2254 string "default:",
|
adamc@144
|
2255 newline,
|
adamc@144
|
2256 string "return",
|
adamc@144
|
2257 space,
|
adamc@144
|
2258 string "-1;",
|
adamc@144
|
2259 newline,
|
adamc@144
|
2260 string "}"]
|
adamc@144
|
2261 end
|
adamc@144
|
2262
|
adamc@643
|
2263 fun p_page (ek, s, n, ts, ran, side) =
|
adamc@144
|
2264 let
|
adamc@144
|
2265 val (ts, defInputs, inputsVar) =
|
adamc@144
|
2266 case ek of
|
adamc@314
|
2267 Core.Link => (List.take (ts, length ts - 1), string "", string "")
|
adamc@608
|
2268 | Core.Rpc => (List.take (ts, length ts - 1), string "", string "")
|
adamc@144
|
2269 | Core.Action =>
|
adamc@280
|
2270 case List.nth (ts, length ts - 2) of
|
adamc@144
|
2271 (TRecord i, _) =>
|
adamc@144
|
2272 let
|
adamc@144
|
2273 val xts = E.lookupStruct env i
|
adamc@144
|
2274 in
|
adamc@280
|
2275 (List.take (ts, length ts - 2),
|
adamc@144
|
2276 box [box (map (fn (x, t) => box [p_typ env t,
|
adamc@144
|
2277 space,
|
adamc@311
|
2278 string "uw_input_",
|
adamc@316
|
2279 p_ident x,
|
adamc@144
|
2280 string ";",
|
adamc@144
|
2281 newline]) xts),
|
adamc@144
|
2282 newline,
|
adamc@144
|
2283 box (map (fn (x, t) =>
|
adamc@144
|
2284 let
|
adamc@144
|
2285 val n = case SM.find (fnums, x) of
|
adamc@144
|
2286 NONE => raise Fail "CjrPrint: Can't find in fnums"
|
adamc@144
|
2287 | SOME n => n
|
adamc@190
|
2288
|
adamc@190
|
2289 val f = case t of
|
adamc@190
|
2290 (TFfi ("Basis", "bool"), _) => "optional_"
|
adamc@190
|
2291 | _ => ""
|
adamc@144
|
2292 in
|
adamc@311
|
2293 box [string "request = uw_get_",
|
adamc@190
|
2294 string f,
|
adamc@190
|
2295 string "input(ctx, ",
|
adamc@144
|
2296 string (Int.toString n),
|
adamc@144
|
2297 string ");",
|
adamc@144
|
2298 newline,
|
adamc@433
|
2299 string "if (request == NULL)",
|
adamc@144
|
2300 newline,
|
adamc@433
|
2301 box [string "uw_error(ctx, FATAL, \"Missing input ",
|
adamc@144
|
2302 string x,
|
adamc@433
|
2303 string "\");"],
|
adamc@144
|
2304 newline,
|
adamc@311
|
2305 string "uw_input_",
|
adamc@316
|
2306 p_ident x,
|
adamc@144
|
2307 space,
|
adamc@144
|
2308 string "=",
|
adamc@144
|
2309 space,
|
adamc@463
|
2310 unurlify env t,
|
adamc@144
|
2311 string ";",
|
adamc@144
|
2312 newline]
|
adamc@144
|
2313 end) xts),
|
adamc@311
|
2314 string "struct __uws_",
|
adamc@144
|
2315 string (Int.toString i),
|
adamc@144
|
2316 space,
|
adamc@311
|
2317 string "uw_inputs",
|
adamc@144
|
2318 space,
|
adamc@144
|
2319 string "= {",
|
adamc@144
|
2320 newline,
|
adamc@311
|
2321 box (map (fn (x, _) => box [string "uw_input_",
|
adamc@316
|
2322 p_ident x,
|
adamc@144
|
2323 string ",",
|
adamc@144
|
2324 newline]) xts),
|
adamc@144
|
2325 string "};",
|
adamc@144
|
2326 newline],
|
adamc@144
|
2327 box [string ",",
|
adamc@144
|
2328 space,
|
adamc@311
|
2329 string "uw_inputs"])
|
adamc@144
|
2330 end
|
adamc@144
|
2331
|
adamc@144
|
2332 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
|
adamc@144
|
2333 in
|
adamc@144
|
2334 box [string "if (!strncmp(request, \"",
|
adamc@144
|
2335 string (String.toString s),
|
adamc@144
|
2336 string "\", ",
|
adamc@144
|
2337 string (Int.toString (size s)),
|
adamc@198
|
2338 string ") && (request[",
|
adamc@198
|
2339 string (Int.toString (size s)),
|
adamc@198
|
2340 string "] == 0 || request[",
|
adamc@198
|
2341 string (Int.toString (size s)),
|
adamc@198
|
2342 string "] == '/')) {",
|
adamc@144
|
2343 newline,
|
adamc@144
|
2344 string "request += ",
|
adamc@144
|
2345 string (Int.toString (size s)),
|
adamc@144
|
2346 string ";",
|
adamc@144
|
2347 newline,
|
adamc@144
|
2348 string "if (*request == '/') ++request;",
|
adamc@144
|
2349 newline,
|
adamc@609
|
2350 box (case ek of
|
adamc@610
|
2351 Core.Rpc => [string "uw_write_header(ctx, \"Content-type: text/plain\\r\\n\");",
|
adamc@610
|
2352 newline]
|
adamc@609
|
2353 | _ => [string "uw_write_header(ctx, \"Content-type: text/html\\r\\n\");",
|
adamc@609
|
2354 newline,
|
adamc@609
|
2355 string "uw_write_header(ctx, \"Content-script-type: text/javascript\\r\\n\");",
|
adamc@609
|
2356 newline,
|
adamc@609
|
2357 string "uw_write(ctx, \"<html>\");",
|
adamc@643
|
2358 newline,
|
adamc@643
|
2359 string "uw_set_script_header(ctx, \"",
|
adamc@643
|
2360 string (case side of
|
adamc@645
|
2361 ServerAndClient => "<script src=\\\""
|
adamc@645
|
2362 ^ OS.Path.joinDirFile {dir = !Monoize.urlPrefix,
|
adamc@645
|
2363 file = "app.js"}
|
adamc@645
|
2364 ^ "\\\"></script>\\n"
|
adamc@643
|
2365 | ServerOnly => ""),
|
adamc@643
|
2366 string "\");",
|
adamc@667
|
2367 string "uw_set_url_prefix(ctx, \"",
|
adamc@667
|
2368 string (!Monoize.urlPrefix),
|
adamc@667
|
2369 string "\");",
|
adamc@609
|
2370 newline]),
|
adamc@144
|
2371 box [string "{",
|
adamc@144
|
2372 newline,
|
adamc@144
|
2373 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
|
adamc@144
|
2374 space,
|
adamc@144
|
2375 string "arg",
|
adamc@144
|
2376 string (Int.toString i),
|
adamc@144
|
2377 space,
|
adamc@144
|
2378 string "=",
|
adamc@144
|
2379 space,
|
adamc@463
|
2380 unurlify env t,
|
adamc@144
|
2381 string ";",
|
adamc@144
|
2382 newline]) ts),
|
adamc@144
|
2383 defInputs,
|
adamc@609
|
2384 box (case ek of
|
adamc@609
|
2385 Core.Rpc => [p_typ env ran,
|
adamc@609
|
2386 space,
|
adamc@610
|
2387 string "it0",
|
adamc@609
|
2388 space,
|
adamc@609
|
2389 string "=",
|
adamc@609
|
2390 space]
|
adamc@609
|
2391 | _ => []),
|
adamc@144
|
2392 p_enamed env n,
|
adamc@144
|
2393 string "(",
|
adamc@144
|
2394 p_list_sep (box [string ",", space])
|
adamc@144
|
2395 (fn x => x)
|
adamc@272
|
2396 (string "ctx"
|
adamc@280
|
2397 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
|
adamc@144
|
2398 inputsVar,
|
adamc@311
|
2399 string ", uw_unit_v);",
|
adamc@144
|
2400 newline,
|
adamc@609
|
2401 box (case ek of
|
adamc@610
|
2402 Core.Rpc => [urlify env ran]
|
adamc@609
|
2403 | _ => [string "uw_write(ctx, \"</html>\");",
|
adamc@609
|
2404 newline]),
|
adamc@144
|
2405 string "return;",
|
adamc@144
|
2406 newline,
|
adamc@144
|
2407 string "}",
|
adamc@144
|
2408 newline,
|
adamc@144
|
2409 string "}"]
|
adamc@144
|
2410 ]
|
adamc@144
|
2411 end
|
adamc@144
|
2412
|
adamc@144
|
2413 val pds' = map p_page ps
|
adamc@275
|
2414
|
adamc@275
|
2415 val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts)
|
adamc@275
|
2416 | _ => NONE) ds
|
adamc@377
|
2417 val sequences = List.mapPartial (fn (DSequence s, _) => SOME s
|
adamc@377
|
2418 | _ => NONE) ds
|
adamc@275
|
2419
|
adamc@275
|
2420 val validate =
|
adamc@311
|
2421 box [string "static void uw_db_validate(uw_context ctx) {",
|
adamc@275
|
2422 newline,
|
adamc@311
|
2423 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@275
|
2424 newline,
|
adamc@275
|
2425 string "PGresult *res;",
|
adamc@275
|
2426 newline,
|
adamc@275
|
2427 newline,
|
adamc@275
|
2428 p_list_sep newline
|
adamc@275
|
2429 (fn (s, xts) =>
|
adamc@275
|
2430 let
|
adamc@377
|
2431 val sl = CharVector.map Char.toLower s
|
adamc@377
|
2432
|
adamc@275
|
2433 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
|
adamc@377
|
2434 ^ sl ^ "'"
|
adamc@275
|
2435
|
adamc@275
|
2436 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@377
|
2437 sl,
|
adamc@275
|
2438 "') AND (",
|
adamc@275
|
2439 String.concatWith " OR "
|
adamc@275
|
2440 (map (fn (x, t) =>
|
adamc@311
|
2441 String.concat ["(attname = 'uw_",
|
adamc@275
|
2442 CharVector.map
|
adamc@316
|
2443 Char.toLower (ident x),
|
adamc@275
|
2444 "' AND atttypid = (SELECT oid FROM pg_type",
|
adamc@275
|
2445 " WHERE typname = '",
|
adamc@467
|
2446 p_sqltype_base' env t,
|
adamc@467
|
2447 "') AND attnotnull = ",
|
adamc@467
|
2448 if is_not_null t then
|
adamc@467
|
2449 "TRUE"
|
adamc@467
|
2450 else
|
adamc@467
|
2451 "FALSE",
|
adamc@467
|
2452 ")"]) xts),
|
adamc@275
|
2453 ")"]
|
adamc@275
|
2454
|
adamc@275
|
2455 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@377
|
2456 sl,
|
adamc@325
|
2457 "') AND attname LIKE 'uw_%'"]
|
adamc@275
|
2458 in
|
adamc@275
|
2459 box [string "res = PQexec(conn, \"",
|
adamc@275
|
2460 string q,
|
adamc@275
|
2461 string "\");",
|
adamc@275
|
2462 newline,
|
adamc@275
|
2463 newline,
|
adamc@275
|
2464 string "if (res == NULL) {",
|
adamc@275
|
2465 newline,
|
adamc@275
|
2466 box [string "PQfinish(conn);",
|
adamc@275
|
2467 newline,
|
adamc@311
|
2468 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
2469 newline],
|
adamc@275
|
2470 string "}",
|
adamc@275
|
2471 newline,
|
adamc@275
|
2472 newline,
|
adamc@275
|
2473 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
2474 newline,
|
adamc@275
|
2475 box [string "char msg[1024];",
|
adamc@275
|
2476 newline,
|
adamc@275
|
2477 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
2478 newline,
|
adamc@275
|
2479 string "msg[1023] = 0;",
|
adamc@275
|
2480 newline,
|
adamc@275
|
2481 string "PQclear(res);",
|
adamc@275
|
2482 newline,
|
adamc@275
|
2483 string "PQfinish(conn);",
|
adamc@275
|
2484 newline,
|
adamc@311
|
2485 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
2486 string q,
|
adamc@275
|
2487 string "\\n%s\", msg);",
|
adamc@275
|
2488 newline],
|
adamc@275
|
2489 string "}",
|
adamc@275
|
2490 newline,
|
adamc@275
|
2491 newline,
|
adamc@275
|
2492 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@275
|
2493 newline,
|
adamc@275
|
2494 box [string "PQclear(res);",
|
adamc@275
|
2495 newline,
|
adamc@275
|
2496 string "PQfinish(conn);",
|
adamc@275
|
2497 newline,
|
adamc@311
|
2498 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
2499 string s,
|
adamc@275
|
2500 string "' does not exist.\");",
|
adamc@275
|
2501 newline],
|
adamc@275
|
2502 string "}",
|
adamc@275
|
2503 newline,
|
adamc@275
|
2504 newline,
|
adamc@275
|
2505 string "PQclear(res);",
|
adamc@275
|
2506 newline,
|
adamc@275
|
2507
|
adamc@275
|
2508 string "res = PQexec(conn, \"",
|
adamc@275
|
2509 string q',
|
adamc@275
|
2510 string "\");",
|
adamc@275
|
2511 newline,
|
adamc@275
|
2512 newline,
|
adamc@275
|
2513 string "if (res == NULL) {",
|
adamc@275
|
2514 newline,
|
adamc@275
|
2515 box [string "PQfinish(conn);",
|
adamc@275
|
2516 newline,
|
adamc@311
|
2517 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
2518 newline],
|
adamc@275
|
2519 string "}",
|
adamc@275
|
2520 newline,
|
adamc@275
|
2521 newline,
|
adamc@275
|
2522 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
2523 newline,
|
adamc@275
|
2524 box [string "char msg[1024];",
|
adamc@275
|
2525 newline,
|
adamc@275
|
2526 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
2527 newline,
|
adamc@275
|
2528 string "msg[1023] = 0;",
|
adamc@275
|
2529 newline,
|
adamc@275
|
2530 string "PQclear(res);",
|
adamc@275
|
2531 newline,
|
adamc@275
|
2532 string "PQfinish(conn);",
|
adamc@275
|
2533 newline,
|
adamc@311
|
2534 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
2535 string q',
|
adamc@275
|
2536 string "\\n%s\", msg);",
|
adamc@275
|
2537 newline],
|
adamc@275
|
2538 string "}",
|
adamc@275
|
2539 newline,
|
adamc@275
|
2540 newline,
|
adamc@275
|
2541 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
2542 string (Int.toString (length xts)),
|
adamc@275
|
2543 string "\")) {",
|
adamc@275
|
2544 newline,
|
adamc@275
|
2545 box [string "PQclear(res);",
|
adamc@275
|
2546 newline,
|
adamc@275
|
2547 string "PQfinish(conn);",
|
adamc@275
|
2548 newline,
|
adamc@311
|
2549 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
2550 string s,
|
adamc@275
|
2551 string "' has the wrong column types.\");",
|
adamc@275
|
2552 newline],
|
adamc@275
|
2553 string "}",
|
adamc@275
|
2554 newline,
|
adamc@275
|
2555 newline,
|
adamc@275
|
2556 string "PQclear(res);",
|
adamc@275
|
2557 newline,
|
adamc@275
|
2558 newline,
|
adamc@275
|
2559
|
adamc@275
|
2560 string "res = PQexec(conn, \"",
|
adamc@275
|
2561 string q'',
|
adamc@275
|
2562 string "\");",
|
adamc@275
|
2563 newline,
|
adamc@275
|
2564 newline,
|
adamc@275
|
2565 string "if (res == NULL) {",
|
adamc@275
|
2566 newline,
|
adamc@275
|
2567 box [string "PQfinish(conn);",
|
adamc@275
|
2568 newline,
|
adamc@311
|
2569 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
2570 newline],
|
adamc@275
|
2571 string "}",
|
adamc@275
|
2572 newline,
|
adamc@275
|
2573 newline,
|
adamc@275
|
2574 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
2575 newline,
|
adamc@275
|
2576 box [string "char msg[1024];",
|
adamc@275
|
2577 newline,
|
adamc@275
|
2578 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
2579 newline,
|
adamc@275
|
2580 string "msg[1023] = 0;",
|
adamc@275
|
2581 newline,
|
adamc@275
|
2582 string "PQclear(res);",
|
adamc@275
|
2583 newline,
|
adamc@275
|
2584 string "PQfinish(conn);",
|
adamc@275
|
2585 newline,
|
adamc@311
|
2586 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
2587 string q'',
|
adamc@275
|
2588 string "\\n%s\", msg);",
|
adamc@275
|
2589 newline],
|
adamc@275
|
2590 string "}",
|
adamc@275
|
2591 newline,
|
adamc@275
|
2592 newline,
|
adamc@275
|
2593 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
2594 string (Int.toString (length xts)),
|
adamc@275
|
2595 string "\")) {",
|
adamc@275
|
2596 newline,
|
adamc@275
|
2597 box [string "PQclear(res);",
|
adamc@275
|
2598 newline,
|
adamc@275
|
2599 string "PQfinish(conn);",
|
adamc@275
|
2600 newline,
|
adamc@311
|
2601 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
2602 string s,
|
adamc@275
|
2603 string "' has extra columns.\");",
|
adamc@275
|
2604 newline],
|
adamc@275
|
2605 string "}",
|
adamc@275
|
2606 newline,
|
adamc@275
|
2607 newline,
|
adamc@275
|
2608 string "PQclear(res);",
|
adamc@275
|
2609 newline]
|
adamc@275
|
2610 end) tables,
|
adamc@377
|
2611
|
adamc@377
|
2612 p_list_sep newline
|
adamc@377
|
2613 (fn s =>
|
adamc@377
|
2614 let
|
adamc@377
|
2615 val sl = CharVector.map Char.toLower s
|
adamc@377
|
2616
|
adamc@377
|
2617 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
|
adamc@377
|
2618 ^ sl ^ "' AND relkind = 'S'"
|
adamc@377
|
2619 in
|
adamc@377
|
2620 box [string "res = PQexec(conn, \"",
|
adamc@377
|
2621 string q,
|
adamc@377
|
2622 string "\");",
|
adamc@377
|
2623 newline,
|
adamc@377
|
2624 newline,
|
adamc@377
|
2625 string "if (res == NULL) {",
|
adamc@377
|
2626 newline,
|
adamc@377
|
2627 box [string "PQfinish(conn);",
|
adamc@377
|
2628 newline,
|
adamc@377
|
2629 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@377
|
2630 newline],
|
adamc@377
|
2631 string "}",
|
adamc@377
|
2632 newline,
|
adamc@377
|
2633 newline,
|
adamc@377
|
2634 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@377
|
2635 newline,
|
adamc@377
|
2636 box [string "char msg[1024];",
|
adamc@377
|
2637 newline,
|
adamc@377
|
2638 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@377
|
2639 newline,
|
adamc@377
|
2640 string "msg[1023] = 0;",
|
adamc@377
|
2641 newline,
|
adamc@377
|
2642 string "PQclear(res);",
|
adamc@377
|
2643 newline,
|
adamc@377
|
2644 string "PQfinish(conn);",
|
adamc@377
|
2645 newline,
|
adamc@377
|
2646 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@377
|
2647 string q,
|
adamc@377
|
2648 string "\\n%s\", msg);",
|
adamc@377
|
2649 newline],
|
adamc@377
|
2650 string "}",
|
adamc@377
|
2651 newline,
|
adamc@377
|
2652 newline,
|
adamc@377
|
2653 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@377
|
2654 newline,
|
adamc@377
|
2655 box [string "PQclear(res);",
|
adamc@377
|
2656 newline,
|
adamc@377
|
2657 string "PQfinish(conn);",
|
adamc@377
|
2658 newline,
|
adamc@377
|
2659 string "uw_error(ctx, FATAL, \"Sequence '",
|
adamc@377
|
2660 string s,
|
adamc@377
|
2661 string "' does not exist.\");",
|
adamc@377
|
2662 newline],
|
adamc@377
|
2663 string "}",
|
adamc@377
|
2664 newline,
|
adamc@377
|
2665 newline,
|
adamc@377
|
2666 string "PQclear(res);",
|
adamc@377
|
2667 newline]
|
adamc@377
|
2668 end) sequences,
|
adamc@377
|
2669
|
adamc@275
|
2670 string "}"]
|
adamc@432
|
2671
|
adamc@432
|
2672 val hasDb = List.exists (fn (DDatabase _, _) => true | _ => false) ds
|
adamc@29
|
2673 in
|
adamc@144
|
2674 box [string "#include <stdio.h>",
|
adamc@144
|
2675 newline,
|
adamc@144
|
2676 string "#include <stdlib.h>",
|
adamc@144
|
2677 newline,
|
adamc@272
|
2678 string "#include <string.h>",
|
adamc@272
|
2679 newline,
|
adamc@390
|
2680 string "#include <math.h>",
|
adamc@390
|
2681 newline,
|
adamc@432
|
2682 if hasDb then
|
adamc@432
|
2683 box [string "#include <postgresql/libpq-fe.h>",
|
adamc@432
|
2684 newline]
|
adamc@432
|
2685 else
|
adamc@432
|
2686 box [],
|
adamc@144
|
2687 newline,
|
adamc@378
|
2688 string "#include \"",
|
adamc@378
|
2689 string (OS.Path.joinDirFile {dir = Config.includ,
|
adamc@378
|
2690 file = "urweb.h"}),
|
adamc@378
|
2691 string "\"",
|
adamc@101
|
2692 newline,
|
adamc@101
|
2693 newline,
|
adamc@101
|
2694 p_list_sep newline (fn x => x) pds,
|
adamc@101
|
2695 newline,
|
adamc@311
|
2696 string "int uw_inputs_len = ",
|
adamc@144
|
2697 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
|
adamc@144
|
2698 string ";",
|
adamc@144
|
2699 newline,
|
adamc@673
|
2700 string "int uw_timeout = ",
|
adamc@673
|
2701 string (Int.toString (!timeout)),
|
adamc@673
|
2702 string ";",
|
adamc@673
|
2703 newline,
|
adamc@144
|
2704 newline,
|
adamc@311
|
2705 string "int uw_input_num(char *name) {",
|
adamc@144
|
2706 newline,
|
adamc@144
|
2707 makeSwitch (fnums, 0),
|
adamc@144
|
2708 string "}",
|
adamc@144
|
2709 newline,
|
adamc@144
|
2710 newline,
|
adamc@311
|
2711 string "void uw_handle(uw_context ctx, char *request) {",
|
adamc@101
|
2712 newline,
|
adamc@569
|
2713 string "if (!strcmp(request, \"/app.js\")) {",
|
adamc@569
|
2714 newline,
|
adamc@569
|
2715 box [string "uw_write_header(ctx, \"Content-type: text/javascript\\r\\n\");",
|
adamc@569
|
2716 newline,
|
adamc@569
|
2717 string "uw_write(ctx, jslib);",
|
adamc@569
|
2718 newline,
|
adamc@569
|
2719 string "return;",
|
adamc@569
|
2720 newline],
|
adamc@569
|
2721 string "}",
|
adamc@569
|
2722 newline,
|
adamc@101
|
2723 p_list_sep newline (fn x => x) pds',
|
adamc@101
|
2724 newline,
|
adamc@387
|
2725 string "uw_error(ctx, FATAL, \"Unknown page\");",
|
adamc@387
|
2726 newline,
|
adamc@101
|
2727 string "}",
|
adamc@275
|
2728 newline,
|
adamc@275
|
2729 newline,
|
adamc@432
|
2730 if hasDb then
|
adamc@432
|
2731 validate
|
adamc@432
|
2732 else
|
adamc@432
|
2733 box [],
|
adamc@376
|
2734 newline,
|
adamc@376
|
2735 if List.exists (fn (DDatabase _, _) => true | _ => false) ds then
|
adamc@376
|
2736 box []
|
adamc@376
|
2737 else
|
adamc@376
|
2738 box [newline,
|
adamc@376
|
2739 string "void uw_db_init(uw_context ctx) { };",
|
adamc@424
|
2740 newline,
|
adamc@424
|
2741 string "int uw_db_begin(uw_context ctx) { return 0; };",
|
adamc@424
|
2742 newline,
|
adamc@424
|
2743 string "int uw_db_commit(uw_context ctx) { return 0; };",
|
adamc@424
|
2744 newline,
|
adamc@424
|
2745 string "int uw_db_rollback(uw_context ctx) { return 0; };",
|
adamc@376
|
2746 newline]]
|
adamc@29
|
2747 end
|
adamc@29
|
2748
|
adamc@274
|
2749 fun p_sql env (ds, _) =
|
adamc@274
|
2750 let
|
adamc@274
|
2751 val (pps, _) = ListUtil.foldlMap
|
adamc@274
|
2752 (fn (dAll as (d, _), env) =>
|
adamc@274
|
2753 let
|
adamc@274
|
2754 val pp = case d of
|
adamc@274
|
2755 DTable (s, xts) =>
|
adamc@274
|
2756 box [string "CREATE TABLE ",
|
adamc@274
|
2757 string s,
|
adamc@274
|
2758 string "(",
|
adamc@274
|
2759 p_list (fn (x, t) =>
|
adamc@311
|
2760 box [string "uw_",
|
adamc@275
|
2761 string (CharVector.map Char.toLower x),
|
adamc@274
|
2762 space,
|
adamc@467
|
2763 p_sqltype env (t, ErrorMsg.dummySpan)]) xts,
|
adamc@274
|
2764 string ");",
|
adamc@274
|
2765 newline,
|
adamc@274
|
2766 newline]
|
adamc@338
|
2767 | DSequence s =>
|
adamc@338
|
2768 box [string "CREATE SEQUENCE ",
|
adamc@338
|
2769 string s,
|
adamc@338
|
2770 string ";",
|
adamc@338
|
2771 newline,
|
adamc@338
|
2772 newline]
|
adamc@274
|
2773 | _ => box []
|
adamc@274
|
2774 in
|
adamc@274
|
2775 (pp, E.declBinds env dAll)
|
adamc@274
|
2776 end)
|
adamc@274
|
2777 env ds
|
adamc@274
|
2778 in
|
adamc@274
|
2779 box pps
|
adamc@274
|
2780 end
|
adamc@274
|
2781
|
adamc@29
|
2782 end
|