adamc@29
|
1 (* Copyright (c) 2008, 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@29
|
65 fun p_typ' par env (t, loc) =
|
adamc@29
|
66 case t of
|
adamc@269
|
67 TFun (t1, t2) => parenIf par (box [p_typ' true env t2,
|
adamc@109
|
68 space,
|
adamc@109
|
69 string "(*)",
|
adamc@109
|
70 space,
|
adamc@109
|
71 string "(",
|
adamc@109
|
72 p_typ env t1,
|
adamc@109
|
73 string ")"])
|
adamc@29
|
74 | TRecord i => box [string "struct",
|
adamc@29
|
75 space,
|
adamc@311
|
76 string "__uws_",
|
adamc@29
|
77 string (Int.toString i)]
|
adamc@188
|
78 | TDatatype (Enum, n, _) =>
|
adamc@188
|
79 (box [string "enum",
|
adamc@188
|
80 space,
|
adamc@311
|
81 string ("__uwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
|
adamc@311
|
82 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
|
adamc@198
|
83 | TDatatype (Option, n, xncs) =>
|
adamc@198
|
84 (case ListUtil.search #3 (!xncs) of
|
adamc@198
|
85 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
|
adamc@198
|
86 | SOME t =>
|
adamc@198
|
87 case #1 t of
|
adamc@198
|
88 TDatatype _ => p_typ' par env t
|
adamc@199
|
89 | TFfi ("Basis", "string") => p_typ' par env t
|
adamc@198
|
90 | _ => box [p_typ' par env t,
|
adamc@198
|
91 string "*"])
|
adamc@188
|
92 | TDatatype (Default, n, _) =>
|
adamc@165
|
93 (box [string "struct",
|
adamc@165
|
94 space,
|
adamc@311
|
95 string ("__uwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
|
adamc@311
|
96 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
|
adamc@316
|
97 | TFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
|
adamc@288
|
98 | TOption t =>
|
adamc@288
|
99 (case #1 t of
|
adamc@288
|
100 TDatatype _ => p_typ' par env t
|
adamc@288
|
101 | TFfi ("Basis", "string") => p_typ' par env t
|
adamc@288
|
102 | _ => box [p_typ' par env t,
|
adamc@288
|
103 string "*"])
|
adamc@29
|
104
|
adamc@29
|
105 and p_typ env = p_typ' false env
|
adamc@29
|
106
|
adamc@316
|
107 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@311
|
108 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@29
|
109
|
adamc@109
|
110 fun p_enamed env n =
|
adamc@316
|
111 string ("__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n)
|
adamc@311
|
112 handle CjrEnv.UnboundNamed _ => string ("__uwn_UNBOUND_" ^ Int.toString n)
|
adamc@109
|
113
|
adamc@182
|
114 fun p_con_named env n =
|
adamc@316
|
115 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
|
adamc@311
|
116 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
|
adamc@182
|
117
|
adamc@182
|
118 fun p_pat_preamble env (p, _) =
|
adamc@182
|
119 case p of
|
adamc@182
|
120 PWild => (box [],
|
adamc@182
|
121 env)
|
adamc@182
|
122 | PVar (x, t) => (box [p_typ env t,
|
adamc@182
|
123 space,
|
adamc@311
|
124 string "__uwr_",
|
adamc@316
|
125 p_ident x,
|
adamc@182
|
126 string "_",
|
adamc@182
|
127 string (Int.toString (E.countERels env)),
|
adamc@182
|
128 string ";",
|
adamc@182
|
129 newline],
|
adamc@196
|
130 E.pushERel env x t)
|
adamc@182
|
131 | PPrim _ => (box [], env)
|
adamc@188
|
132 | PCon (_, _, NONE) => (box [], env)
|
adamc@188
|
133 | PCon (_, _, SOME p) => p_pat_preamble env p
|
adamc@182
|
134 | PRecord xps =>
|
adamc@182
|
135 foldl (fn ((_, p, _), (pp, env)) =>
|
adamc@182
|
136 let
|
adamc@182
|
137 val (pp', env) = p_pat_preamble env p
|
adamc@182
|
138 in
|
adamc@182
|
139 (box [pp', pp], env)
|
adamc@182
|
140 end) (box [], env) xps
|
adamc@288
|
141 | PNone _ => (box [], env)
|
adamc@288
|
142 | PSome (_, p) => p_pat_preamble env p
|
adamc@182
|
143
|
adamc@182
|
144 fun p_patCon env pc =
|
adamc@182
|
145 case pc of
|
adamc@182
|
146 PConVar n => p_con_named env n
|
adamc@316
|
147 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
|
adamc@182
|
148
|
adamc@182
|
149 fun p_pat (env, exit, depth) (p, _) =
|
adamc@182
|
150 case p of
|
adamc@182
|
151 PWild =>
|
adamc@182
|
152 (box [], env)
|
adamc@182
|
153 | PVar (x, t) =>
|
adamc@311
|
154 (box [string "__uwr_",
|
adamc@316
|
155 p_ident x,
|
adamc@182
|
156 string "_",
|
adamc@182
|
157 string (Int.toString (E.countERels env)),
|
adamc@182
|
158 space,
|
adamc@182
|
159 string "=",
|
adamc@182
|
160 space,
|
adamc@182
|
161 string "disc",
|
adamc@182
|
162 string (Int.toString depth),
|
adamc@182
|
163 string ";"],
|
adamc@182
|
164 E.pushERel env x t)
|
adamc@182
|
165 | PPrim (Prim.Int n) =>
|
adamc@182
|
166 (box [string "if",
|
adamc@182
|
167 space,
|
adamc@182
|
168 string "(disc",
|
adamc@182
|
169 string (Int.toString depth),
|
adamc@182
|
170 space,
|
adamc@182
|
171 string "!=",
|
adamc@182
|
172 space,
|
adamc@276
|
173 Prim.p_t_GCC (Prim.Int n),
|
adamc@182
|
174 string ")",
|
adamc@182
|
175 space,
|
adamc@182
|
176 exit],
|
adamc@182
|
177 env)
|
adamc@182
|
178 | PPrim (Prim.String s) =>
|
adamc@182
|
179 (box [string "if",
|
adamc@182
|
180 space,
|
adamc@182
|
181 string "(strcmp(disc",
|
adamc@182
|
182 string (Int.toString depth),
|
adamc@182
|
183 string ",",
|
adamc@182
|
184 space,
|
adamc@276
|
185 Prim.p_t_GCC (Prim.String s),
|
adamc@182
|
186 string "))",
|
adamc@182
|
187 space,
|
adamc@182
|
188 exit],
|
adamc@182
|
189 env)
|
adamc@182
|
190 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
|
adamc@182
|
191
|
adamc@188
|
192 | PCon (dk, pc, po) =>
|
adamc@182
|
193 let
|
adamc@182
|
194 val (p, env) =
|
adamc@182
|
195 case po of
|
adamc@182
|
196 NONE => (box [], env)
|
adamc@182
|
197 | SOME p =>
|
adamc@182
|
198 let
|
adamc@182
|
199 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
200
|
adamc@182
|
201 val (x, to) = case pc of
|
adamc@182
|
202 PConVar n =>
|
adamc@182
|
203 let
|
adamc@182
|
204 val (x, to, _) = E.lookupConstructor env n
|
adamc@182
|
205 in
|
adamc@316
|
206 ("uw_" ^ ident x, to)
|
adamc@182
|
207 end
|
adamc@188
|
208 | PConFfi {mod = m, con, arg, ...} =>
|
adamc@316
|
209 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
|
adamc@182
|
210
|
adamc@182
|
211 val t = case to of
|
adamc@182
|
212 NONE => raise Fail "CjrPrint: Constructor mismatch"
|
adamc@182
|
213 | SOME t => t
|
adamc@182
|
214 in
|
adamc@182
|
215 (box [string "{",
|
adamc@182
|
216 newline,
|
adamc@182
|
217 p_typ env t,
|
adamc@182
|
218 space,
|
adamc@182
|
219 string "disc",
|
adamc@182
|
220 string (Int.toString (depth + 1)),
|
adamc@182
|
221 space,
|
adamc@182
|
222 string "=",
|
adamc@182
|
223 space,
|
adamc@198
|
224 case dk of
|
adamc@198
|
225 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
|
adamc@198
|
226 | Default => box [string "disc",
|
adamc@198
|
227 string (Int.toString depth),
|
adamc@198
|
228 string "->data.",
|
adamc@198
|
229 string x]
|
adamc@198
|
230 | Option =>
|
adamc@198
|
231 case #1 t of
|
adamc@198
|
232 TDatatype _ => box [string "disc",
|
adamc@198
|
233 string (Int.toString depth)]
|
adamc@199
|
234 | TFfi ("Basis", "string") => box [string "disc",
|
adamc@199
|
235 string (Int.toString depth)]
|
adamc@198
|
236 | _ => box [string "*disc",
|
adamc@198
|
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@288
|
338 case #1 t of
|
adamc@288
|
339 TDatatype _ => box [string "disc",
|
adamc@288
|
340 string (Int.toString depth)]
|
adamc@288
|
341 | TFfi ("Basis", "string") => box [string "disc",
|
adamc@288
|
342 string (Int.toString depth)]
|
adamc@288
|
343 | _ => box [string "*disc",
|
adamc@288
|
344 string (Int.toString depth)],
|
adamc@288
|
345 string ";",
|
adamc@288
|
346 newline,
|
adamc@288
|
347 p,
|
adamc@288
|
348 newline,
|
adamc@288
|
349 string "}"],
|
adamc@288
|
350 env)
|
adamc@288
|
351 end
|
adamc@288
|
352 in
|
adamc@288
|
353 (box [string "if",
|
adamc@288
|
354 space,
|
adamc@288
|
355 string "(disc",
|
adamc@288
|
356 string (Int.toString depth),
|
adamc@288
|
357 space,
|
adamc@288
|
358 string "==",
|
adamc@288
|
359 space,
|
adamc@288
|
360 string "NULL)",
|
adamc@288
|
361 space,
|
adamc@288
|
362 exit,
|
adamc@288
|
363 newline,
|
adamc@288
|
364 p],
|
adamc@288
|
365 env)
|
adamc@288
|
366 end
|
adamc@288
|
367
|
adamc@182
|
368 local
|
adamc@182
|
369 val count = ref 0
|
adamc@182
|
370 in
|
adamc@182
|
371 fun newGoto () =
|
adamc@182
|
372 let
|
adamc@182
|
373 val r = !count
|
adamc@182
|
374 in
|
adamc@182
|
375 count := r + 1;
|
adamc@182
|
376 string ("L" ^ Int.toString r)
|
adamc@182
|
377 end
|
adamc@182
|
378 end
|
adamc@182
|
379
|
adamc@185
|
380 fun patConInfo env pc =
|
adamc@185
|
381 case pc of
|
adamc@185
|
382 PConVar n =>
|
adamc@185
|
383 let
|
adamc@185
|
384 val (x, _, dn) = E.lookupConstructor env n
|
adamc@185
|
385 val (dx, _) = E.lookupDatatype env dn
|
adamc@185
|
386 in
|
adamc@316
|
387 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
|
adamc@316
|
388 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
|
adamc@316
|
389 "uw_" ^ ident x)
|
adamc@185
|
390 end
|
adamc@186
|
391 | PConFfi {mod = m, datatyp, con, ...} =>
|
adamc@316
|
392 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
|
adamc@316
|
393 "uw_" ^ ident m ^ "_" ^ ident con,
|
adamc@316
|
394 "uw_" ^ ident con)
|
adamc@185
|
395
|
adamc@324
|
396 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e =
|
adamc@278
|
397 case t of
|
adamc@311
|
398 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
|
adamc@311
|
399 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
|
adamc@324
|
400 | TFfi ("Basis", "string") =>
|
adamc@324
|
401 if wontLeakStrings then
|
adamc@324
|
402 e
|
adamc@324
|
403 else
|
adamc@324
|
404 box [string "uw_Basis_strdup(ctx, ", e, string ")"]
|
adamc@311
|
405 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
|
adamc@278
|
406 | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
|
adamc@278
|
407 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@278
|
408 string "ERROR")
|
adamc@278
|
409
|
adamc@282
|
410 datatype sql_type =
|
adamc@282
|
411 Int
|
adamc@282
|
412 | Float
|
adamc@282
|
413 | String
|
adamc@282
|
414 | Bool
|
adamc@282
|
415
|
adamc@282
|
416 fun p_sql_type t =
|
adamc@282
|
417 string (case t of
|
adamc@311
|
418 Int => "uw_Basis_int"
|
adamc@311
|
419 | Float => "uw_Basis_float"
|
adamc@311
|
420 | String => "uw_Basis_string"
|
adamc@311
|
421 | Bool => "uw_Basis_bool")
|
adamc@282
|
422
|
adamc@282
|
423 fun getPargs (e, _) =
|
adamc@282
|
424 case e of
|
adamc@282
|
425 EPrim (Prim.String _) => []
|
adamc@282
|
426 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
|
adamc@282
|
427
|
adamc@282
|
428 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
|
adamc@282
|
429 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
|
adamc@282
|
430 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
|
adamc@282
|
431 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
|
adamc@322
|
432 | ECase (e, _, _) => [(e, Bool)]
|
adamc@282
|
433
|
adamc@282
|
434 | _ => raise Fail "CjrPrint: getPargs"
|
adamc@282
|
435
|
adamc@282
|
436 fun p_ensql t e =
|
adamc@282
|
437 case t of
|
adamc@311
|
438 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
|
adamc@311
|
439 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
|
adamc@282
|
440 | String => e
|
adamc@295
|
441 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
|
adamc@282
|
442
|
adamc@282
|
443 fun p_ensql_len t e =
|
adamc@282
|
444 case t of
|
adamc@311
|
445 Int => string "sizeof(uw_Basis_int)"
|
adamc@311
|
446 | Float => string "sizeof(uw_Basis_float)"
|
adamc@282
|
447 | String => box [string "strlen(", e, string ")"]
|
adamc@311
|
448 | Bool => string "sizeof(uw_Basis_bool)"
|
adamc@282
|
449
|
adamc@324
|
450 fun notLeaky env allowHeapAllocated =
|
adamc@324
|
451 let
|
adamc@324
|
452 fun nl (t, _) =
|
adamc@324
|
453 case t of
|
adamc@324
|
454 TFun _ => false
|
adamc@324
|
455 | TRecord n =>
|
adamc@324
|
456 let
|
adamc@324
|
457 val xts = E.lookupStruct env n
|
adamc@324
|
458 in
|
adamc@324
|
459 List.all (fn (_, t) => nl t) xts
|
adamc@324
|
460 end
|
adamc@324
|
461 | TDatatype (dk, _, ref cons) =>
|
adamc@324
|
462 (allowHeapAllocated orelse dk = Enum)
|
adamc@324
|
463 andalso List.all (fn (_, _, to) => case to of
|
adamc@324
|
464 NONE => true
|
adamc@324
|
465 | SOME t => nl t) cons
|
adamc@324
|
466 | TFfi ("Basis", "string") => false
|
adamc@324
|
467 | TFfi _ => true
|
adamc@324
|
468 | TOption t => allowHeapAllocated andalso nl t
|
adamc@324
|
469 in
|
adamc@324
|
470 nl
|
adamc@324
|
471 end
|
adamc@324
|
472
|
adamc@182
|
473 fun p_exp' par env (e, loc) =
|
adamc@29
|
474 case e of
|
adamc@276
|
475 EPrim p => Prim.p_t_GCC p
|
adamc@29
|
476 | ERel n => p_rel env n
|
adamc@109
|
477 | ENamed n => p_enamed env n
|
adamc@188
|
478 | ECon (Enum, pc, _) => p_patCon env pc
|
adamc@198
|
479 | ECon (Option, pc, NONE) => string "NULL"
|
adamc@198
|
480 | ECon (Option, pc, SOME e) =>
|
adamc@198
|
481 let
|
adamc@198
|
482 val to = case pc of
|
adamc@198
|
483 PConVar n => #2 (E.lookupConstructor env n)
|
adamc@198
|
484 | PConFfi {arg, ...} => arg
|
adamc@198
|
485
|
adamc@198
|
486 val t = case to of
|
adamc@198
|
487 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
|
adamc@198
|
488 | SOME t => t
|
adamc@198
|
489 in
|
adamc@198
|
490 case #1 t of
|
adamc@198
|
491 TDatatype _ => p_exp' par env e
|
adamc@199
|
492 | TFfi ("Basis", "string") => p_exp' par env e
|
adamc@198
|
493 | _ => box [string "({",
|
adamc@198
|
494 newline,
|
adamc@198
|
495 p_typ env t,
|
adamc@198
|
496 space,
|
adamc@198
|
497 string "*tmp",
|
adamc@198
|
498 space,
|
adamc@198
|
499 string "=",
|
adamc@198
|
500 space,
|
adamc@311
|
501 string "uw_malloc(ctx, sizeof(",
|
adamc@198
|
502 p_typ env t,
|
adamc@198
|
503 string "));",
|
adamc@198
|
504 newline,
|
adamc@198
|
505 string "*tmp",
|
adamc@198
|
506 space,
|
adamc@198
|
507 string "=",
|
adamc@198
|
508 p_exp' par env e,
|
adamc@198
|
509 string ";",
|
adamc@198
|
510 newline,
|
adamc@198
|
511 string "tmp;",
|
adamc@198
|
512 newline,
|
adamc@198
|
513 string "})"]
|
adamc@198
|
514 end
|
adamc@188
|
515 | ECon (Default, pc, eo) =>
|
adamc@181
|
516 let
|
adamc@196
|
517 val (xd, xc, xn) = patConInfo env pc
|
adamc@181
|
518 in
|
adamc@182
|
519 box [string "({",
|
adamc@181
|
520 newline,
|
adamc@181
|
521 string "struct",
|
adamc@181
|
522 space,
|
adamc@185
|
523 string xd,
|
adamc@181
|
524 space,
|
adamc@181
|
525 string "*tmp",
|
adamc@181
|
526 space,
|
adamc@181
|
527 string "=",
|
adamc@181
|
528 space,
|
adamc@311
|
529 string "uw_malloc(ctx, sizeof(struct ",
|
adamc@185
|
530 string xd,
|
adamc@181
|
531 string "));",
|
adamc@181
|
532 newline,
|
adamc@181
|
533 string "tmp->tag",
|
adamc@181
|
534 space,
|
adamc@181
|
535 string "=",
|
adamc@181
|
536 space,
|
adamc@185
|
537 string xc,
|
adamc@181
|
538 string ";",
|
adamc@181
|
539 newline,
|
adamc@181
|
540 case eo of
|
adamc@181
|
541 NONE => box []
|
adamc@185
|
542 | SOME e => box [string "tmp->data.",
|
adamc@196
|
543 string xn,
|
adamc@181
|
544 space,
|
adamc@181
|
545 string "=",
|
adamc@181
|
546 space,
|
adamc@181
|
547 p_exp env e,
|
adamc@181
|
548 string ";",
|
adamc@181
|
549 newline],
|
adamc@181
|
550 string "tmp;",
|
adamc@181
|
551 newline,
|
adamc@181
|
552 string "})"]
|
adamc@181
|
553 end
|
adamc@297
|
554 | ENone _ => string "NULL"
|
adamc@290
|
555 | ESome (t, e) =>
|
adamc@290
|
556 (case #1 t of
|
adamc@290
|
557 TDatatype _ => p_exp' par env e
|
adamc@290
|
558 | TFfi ("Basis", "string") => p_exp' par env e
|
adamc@290
|
559 | _ => box [string "({",
|
adamc@290
|
560 newline,
|
adamc@290
|
561 p_typ env t,
|
adamc@290
|
562 space,
|
adamc@290
|
563 string "*tmp",
|
adamc@290
|
564 space,
|
adamc@290
|
565 string "=",
|
adamc@290
|
566 space,
|
adamc@311
|
567 string "uw_malloc(ctx, sizeof(",
|
adamc@290
|
568 p_typ env t,
|
adamc@290
|
569 string "));",
|
adamc@290
|
570 newline,
|
adamc@290
|
571 string "*tmp",
|
adamc@290
|
572 space,
|
adamc@290
|
573 string "=",
|
adamc@290
|
574 p_exp' par env e,
|
adamc@290
|
575 string ";",
|
adamc@290
|
576 newline,
|
adamc@290
|
577 string "tmp;",
|
adamc@290
|
578 newline,
|
adamc@290
|
579 string "})"])
|
adamc@109
|
580
|
adamc@316
|
581 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
|
adamc@283
|
582 | EError (e, t) =>
|
adamc@283
|
583 box [string "({",
|
adamc@283
|
584 newline,
|
adamc@283
|
585 p_typ env t,
|
adamc@283
|
586 space,
|
adamc@283
|
587 string "tmp;",
|
adamc@283
|
588 newline,
|
adamc@311
|
589 string "uw_error(ctx, FATAL, \"",
|
adamc@292
|
590 string (ErrorMsg.spanToString loc),
|
adamc@292
|
591 string ": %s\", ",
|
adamc@283
|
592 p_exp env e,
|
adamc@283
|
593 string ");",
|
adamc@283
|
594 newline,
|
adamc@283
|
595 string "tmp;",
|
adamc@283
|
596 newline,
|
adamc@283
|
597 string "})"]
|
adamc@311
|
598 | EFfiApp (m, x, es) => box [string "uw_",
|
adamc@316
|
599 p_ident m,
|
adamc@53
|
600 string "_",
|
adamc@316
|
601 p_ident x,
|
adamc@117
|
602 string "(ctx, ",
|
adamc@53
|
603 p_list (p_exp env) es,
|
adamc@53
|
604 string ")"]
|
adamc@316
|
605 | EApp (f, args) =>
|
adamc@316
|
606 parenIf par (box [p_exp' true env f,
|
adamc@316
|
607 string "(ctx,",
|
adamc@316
|
608 space,
|
adamc@316
|
609 p_list_sep (box [string ",", space]) (p_exp env) args,
|
adamc@316
|
610 string ")"])
|
adamc@29
|
611
|
adamc@29
|
612 | ERecord (i, xes) => box [string "({",
|
adamc@29
|
613 space,
|
adamc@29
|
614 string "struct",
|
adamc@29
|
615 space,
|
adamc@311
|
616 string ("__uws_" ^ Int.toString i),
|
adamc@29
|
617 space,
|
adamc@181
|
618 string "tmp",
|
adamc@29
|
619 space,
|
adamc@29
|
620 string "=",
|
adamc@29
|
621 space,
|
adamc@29
|
622 string "{",
|
adamc@29
|
623 p_list (fn (_, e) =>
|
adamc@29
|
624 p_exp env e) xes,
|
adamc@29
|
625 string "};",
|
adamc@29
|
626 space,
|
adamc@181
|
627 string "tmp;",
|
adamc@29
|
628 space,
|
adamc@29
|
629 string "})" ]
|
adamc@29
|
630 | EField (e, x) =>
|
adamc@29
|
631 box [p_exp' true env e,
|
adamc@311
|
632 string ".__uwf_",
|
adamc@316
|
633 p_ident x]
|
adamc@29
|
634
|
adamc@182
|
635 | ECase (e, pes, {disc, result}) =>
|
adamc@182
|
636 let
|
adamc@182
|
637 val final = newGoto ()
|
adamc@182
|
638
|
adamc@182
|
639 val body = foldl (fn ((p, e), body) =>
|
adamc@182
|
640 let
|
adamc@182
|
641 val exit = newGoto ()
|
adamc@182
|
642 val (pr, _) = p_pat_preamble env p
|
adamc@182
|
643 val (p, env) = p_pat (env,
|
adamc@182
|
644 box [string "goto",
|
adamc@182
|
645 space,
|
adamc@182
|
646 exit,
|
adamc@182
|
647 string ";"],
|
adamc@182
|
648 0) p
|
adamc@182
|
649 in
|
adamc@182
|
650 box [body,
|
adamc@182
|
651 box [string "{",
|
adamc@182
|
652 newline,
|
adamc@182
|
653 pr,
|
adamc@182
|
654 newline,
|
adamc@182
|
655 p,
|
adamc@182
|
656 newline,
|
adamc@182
|
657 string "result",
|
adamc@182
|
658 space,
|
adamc@182
|
659 string "=",
|
adamc@182
|
660 space,
|
adamc@182
|
661 p_exp env e,
|
adamc@182
|
662 string ";",
|
adamc@182
|
663 newline,
|
adamc@182
|
664 string "goto",
|
adamc@182
|
665 space,
|
adamc@182
|
666 final,
|
adamc@182
|
667 string ";",
|
adamc@182
|
668 newline,
|
adamc@182
|
669 string "}"],
|
adamc@182
|
670 newline,
|
adamc@182
|
671 exit,
|
adamc@182
|
672 string ":",
|
adamc@182
|
673 newline]
|
adamc@182
|
674 end) (box []) pes
|
adamc@182
|
675 in
|
adamc@182
|
676 box [string "({",
|
adamc@182
|
677 newline,
|
adamc@182
|
678 p_typ env disc,
|
adamc@182
|
679 space,
|
adamc@182
|
680 string "disc0",
|
adamc@182
|
681 space,
|
adamc@182
|
682 string "=",
|
adamc@182
|
683 space,
|
adamc@182
|
684 p_exp env e,
|
adamc@182
|
685 string ";",
|
adamc@182
|
686 newline,
|
adamc@182
|
687 p_typ env result,
|
adamc@182
|
688 space,
|
adamc@182
|
689 string "result;",
|
adamc@182
|
690 newline,
|
adamc@182
|
691 body,
|
adamc@311
|
692 string "uw_error(ctx, FATAL, \"",
|
adamc@182
|
693 string (ErrorMsg.spanToString loc),
|
adamc@182
|
694 string ": pattern match failure\");",
|
adamc@182
|
695 newline,
|
adamc@182
|
696 final,
|
adamc@182
|
697 string ":",
|
adamc@182
|
698 space,
|
adamc@182
|
699 string "result;",
|
adamc@182
|
700 newline,
|
adamc@182
|
701 string "})"]
|
adamc@182
|
702 end
|
adamc@181
|
703
|
adamc@311
|
704 | EWrite e => box [string "(uw_write(ctx, ",
|
adamc@102
|
705 p_exp env e,
|
adamc@311
|
706 string "), uw_unit_v)"]
|
adamc@102
|
707
|
adamc@106
|
708 | ESeq (e1, e2) => box [string "(",
|
adamc@106
|
709 p_exp env e1,
|
adamc@106
|
710 string ",",
|
adamc@106
|
711 space,
|
adamc@106
|
712 p_exp env e2,
|
adamc@106
|
713 string ")"]
|
adamc@269
|
714 | ELet (x, t, e1, e2) => box [string "({",
|
adamc@269
|
715 newline,
|
adamc@269
|
716 p_typ env t,
|
adamc@269
|
717 space,
|
adamc@311
|
718 string "__uwr_",
|
adamc@316
|
719 p_ident x,
|
adamc@272
|
720 string "_",
|
adamc@272
|
721 string (Int.toString (E.countERels env)),
|
adamc@269
|
722 space,
|
adamc@269
|
723 string "=",
|
adamc@269
|
724 space,
|
adamc@269
|
725 p_exp env e1,
|
adamc@269
|
726 string ";",
|
adamc@269
|
727 newline,
|
adamc@269
|
728 p_exp (E.pushERel env x t) e2,
|
adamc@269
|
729 string ";",
|
adamc@269
|
730 newline,
|
adamc@269
|
731 string "})"]
|
adamc@269
|
732
|
adamc@282
|
733 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
|
adamc@278
|
734 let
|
adamc@316
|
735 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
|
adamc@278
|
736 val tables = ListUtil.mapConcat (fn (x, xts) =>
|
adamc@316
|
737 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
|
adamc@278
|
738 tables
|
adamc@278
|
739
|
adamc@278
|
740 val outputs = exps @ tables
|
adamc@324
|
741
|
adamc@324
|
742 val wontLeakStrings = notLeaky env true state
|
adamc@324
|
743 val wontLeakAnything = notLeaky env false state
|
adamc@278
|
744 in
|
adamc@324
|
745 box [string "(uw_begin_region(ctx), ",
|
adamc@324
|
746 if wontLeakAnything then
|
adamc@324
|
747 string "uw_begin_regio(ctx), "
|
adamc@324
|
748 else
|
adamc@324
|
749 box [],
|
adamc@324
|
750 string "({",
|
adamc@278
|
751 newline,
|
adamc@311
|
752 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@278
|
753 newline,
|
adamc@282
|
754 case prepared of
|
adamc@282
|
755 NONE => box [string "char *query = ",
|
adamc@282
|
756 p_exp env query,
|
adamc@282
|
757 string ";",
|
adamc@282
|
758 newline]
|
adamc@282
|
759 | SOME _ =>
|
adamc@282
|
760 let
|
adamc@282
|
761 val ets = getPargs query
|
adamc@282
|
762 in
|
adamc@282
|
763 box [p_list_sepi newline
|
adamc@282
|
764 (fn i => fn (e, t) =>
|
adamc@282
|
765 box [p_sql_type t,
|
adamc@282
|
766 space,
|
adamc@282
|
767 string "arg",
|
adamc@282
|
768 string (Int.toString (i + 1)),
|
adamc@282
|
769 space,
|
adamc@282
|
770 string "=",
|
adamc@282
|
771 space,
|
adamc@282
|
772 p_exp env e,
|
adamc@282
|
773 string ";"])
|
adamc@282
|
774 ets,
|
adamc@282
|
775 newline,
|
adamc@282
|
776 newline,
|
adamc@282
|
777
|
adamc@282
|
778 string "const char *paramValues[] = { ",
|
adamc@282
|
779 p_list_sepi (box [string ",", space])
|
adamc@282
|
780 (fn i => fn (_, t) => p_ensql t (box [string "arg",
|
adamc@282
|
781 string (Int.toString (i + 1))]))
|
adamc@282
|
782 ets,
|
adamc@282
|
783 string " };",
|
adamc@282
|
784 newline,
|
adamc@282
|
785 newline]
|
adamc@282
|
786 end,
|
adamc@278
|
787 string "int n, i;",
|
adamc@278
|
788 newline,
|
adamc@278
|
789 p_typ env state,
|
adamc@278
|
790 space,
|
adamc@278
|
791 string "acc",
|
adamc@278
|
792 space,
|
adamc@278
|
793 string "=",
|
adamc@278
|
794 space,
|
adamc@278
|
795 p_exp env initial,
|
adamc@278
|
796 string ";",
|
adamc@278
|
797 newline,
|
adamc@282
|
798 string "PGresult *res = ",
|
adamc@282
|
799 case prepared of
|
adamc@295
|
800 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@311
|
801 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@282
|
802 string (Int.toString n),
|
adamc@282
|
803 string "\", ",
|
adamc@282
|
804 string (Int.toString (length (getPargs query))),
|
adamc@295
|
805 string ", paramValues, NULL, NULL, 0);"],
|
adamc@278
|
806 newline,
|
adamc@278
|
807 newline,
|
adamc@277
|
808
|
adamc@311
|
809 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@278
|
810 newline,
|
adamc@278
|
811 newline,
|
adamc@277
|
812
|
adamc@278
|
813 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@278
|
814 newline,
|
adamc@278
|
815 box [string "PQclear(res);",
|
adamc@278
|
816 newline,
|
adamc@311
|
817 string "uw_error(ctx, FATAL, \"",
|
adamc@278
|
818 string (ErrorMsg.spanToString loc),
|
adamc@282
|
819 string ": Query failed:\\n%s\\n%s\", ",
|
adamc@282
|
820 case prepared of
|
adamc@282
|
821 NONE => string "query"
|
adamc@282
|
822 | SOME _ => p_exp env query,
|
adamc@282
|
823 string ", PQerrorMessage(conn));",
|
adamc@278
|
824 newline],
|
adamc@278
|
825 string "}",
|
adamc@278
|
826 newline,
|
adamc@278
|
827 newline,
|
adamc@277
|
828
|
adamc@323
|
829 string "uw_end_region(ctx);",
|
adamc@323
|
830 newline,
|
adamc@278
|
831 string "n = PQntuples(res);",
|
adamc@278
|
832 newline,
|
adamc@278
|
833 string "for (i = 0; i < n; ++i) {",
|
adamc@278
|
834 newline,
|
adamc@278
|
835 box [string "struct",
|
adamc@278
|
836 space,
|
adamc@311
|
837 string "__uws_",
|
adamc@278
|
838 string (Int.toString rnum),
|
adamc@278
|
839 space,
|
adamc@311
|
840 string "__uwr_r_",
|
adamc@278
|
841 string (Int.toString (E.countERels env)),
|
adamc@278
|
842 string ";",
|
adamc@278
|
843 newline,
|
adamc@278
|
844 p_typ env state,
|
adamc@278
|
845 space,
|
adamc@311
|
846 string "__uwr_acc_",
|
adamc@278
|
847 string (Int.toString (E.countERels env + 1)),
|
adamc@278
|
848 space,
|
adamc@278
|
849 string "=",
|
adamc@278
|
850 space,
|
adamc@278
|
851 string "acc;",
|
adamc@278
|
852 newline,
|
adamc@278
|
853 newline,
|
adamc@278
|
854
|
adamc@278
|
855 p_list_sepi (box []) (fn i =>
|
adamc@278
|
856 fn (proj, t) =>
|
adamc@311
|
857 box [string "__uwr_r_",
|
adamc@278
|
858 string (Int.toString (E.countERels env)),
|
adamc@278
|
859 string ".",
|
adamc@278
|
860 string proj,
|
adamc@278
|
861 space,
|
adamc@278
|
862 string "=",
|
adamc@278
|
863 space,
|
adamc@324
|
864 p_unsql wontLeakStrings env t
|
adamc@278
|
865 (box [string "PQgetvalue(res, i, ",
|
adamc@278
|
866 string (Int.toString i),
|
adamc@278
|
867 string ")"]),
|
adamc@278
|
868 string ";",
|
adamc@278
|
869 newline]) outputs,
|
adamc@278
|
870
|
adamc@278
|
871 newline,
|
adamc@278
|
872 newline,
|
adamc@278
|
873
|
adamc@278
|
874 string "acc",
|
adamc@278
|
875 space,
|
adamc@278
|
876 string "=",
|
adamc@278
|
877 space,
|
adamc@278
|
878 p_exp (E.pushERel
|
adamc@278
|
879 (E.pushERel env "r" (TRecord rnum, loc))
|
adamc@278
|
880 "acc" state)
|
adamc@278
|
881 body,
|
adamc@278
|
882 string ";",
|
adamc@278
|
883 newline],
|
adamc@278
|
884 string "}",
|
adamc@278
|
885 newline,
|
adamc@278
|
886 newline,
|
adamc@278
|
887 string "PQclear(res);",
|
adamc@278
|
888 newline,
|
adamc@324
|
889 if wontLeakAnything then
|
adamc@324
|
890 box [string "uw_end_region(ctx);",
|
adamc@324
|
891 newline]
|
adamc@324
|
892 else
|
adamc@324
|
893 box [],
|
adamc@278
|
894 string "acc;",
|
adamc@278
|
895 newline,
|
adamc@323
|
896 string "}))"]
|
adamc@278
|
897 end
|
adamc@106
|
898
|
adamc@307
|
899 | EDml {dml, prepared} =>
|
adamc@307
|
900 box [string "({",
|
adamc@307
|
901 newline,
|
adamc@311
|
902 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@307
|
903 newline,
|
adamc@307
|
904 case prepared of
|
adamc@307
|
905 NONE => box [string "char *dml = ",
|
adamc@307
|
906 p_exp env dml,
|
adamc@307
|
907 string ";",
|
adamc@307
|
908 newline]
|
adamc@307
|
909 | SOME _ =>
|
adamc@307
|
910 let
|
adamc@307
|
911 val ets = getPargs dml
|
adamc@307
|
912 in
|
adamc@307
|
913 box [p_list_sepi newline
|
adamc@307
|
914 (fn i => fn (e, t) =>
|
adamc@307
|
915 box [p_sql_type t,
|
adamc@307
|
916 space,
|
adamc@307
|
917 string "arg",
|
adamc@307
|
918 string (Int.toString (i + 1)),
|
adamc@307
|
919 space,
|
adamc@307
|
920 string "=",
|
adamc@307
|
921 space,
|
adamc@307
|
922 p_exp env e,
|
adamc@307
|
923 string ";"])
|
adamc@307
|
924 ets,
|
adamc@307
|
925 newline,
|
adamc@307
|
926 newline,
|
adamc@307
|
927
|
adamc@307
|
928 string "const char *paramValues[] = { ",
|
adamc@307
|
929 p_list_sepi (box [string ",", space])
|
adamc@307
|
930 (fn i => fn (_, t) => p_ensql t (box [string "arg",
|
adamc@307
|
931 string (Int.toString (i + 1))]))
|
adamc@307
|
932 ets,
|
adamc@307
|
933 string " };",
|
adamc@307
|
934 newline,
|
adamc@307
|
935 newline]
|
adamc@307
|
936 end,
|
adamc@307
|
937 newline,
|
adamc@307
|
938 newline,
|
adamc@307
|
939 string "PGresult *res = ",
|
adamc@307
|
940 case prepared of
|
adamc@307
|
941 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@311
|
942 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@307
|
943 string (Int.toString n),
|
adamc@307
|
944 string "\", ",
|
adamc@307
|
945 string (Int.toString (length (getPargs dml))),
|
adamc@307
|
946 string ", paramValues, NULL, NULL, 0);"],
|
adamc@307
|
947 newline,
|
adamc@307
|
948 newline,
|
adamc@307
|
949
|
adamc@311
|
950 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
|
adamc@307
|
951 newline,
|
adamc@307
|
952 newline,
|
adamc@307
|
953
|
adamc@307
|
954 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@307
|
955 newline,
|
adamc@307
|
956 box [string "PQclear(res);",
|
adamc@307
|
957 newline,
|
adamc@311
|
958 string "uw_error(ctx, FATAL, \"",
|
adamc@307
|
959 string (ErrorMsg.spanToString loc),
|
adamc@307
|
960 string ": DML failed:\\n%s\\n%s\", ",
|
adamc@307
|
961 case prepared of
|
adamc@307
|
962 NONE => string "dml"
|
adamc@307
|
963 | SOME _ => p_exp env dml,
|
adamc@307
|
964 string ", PQerrorMessage(conn));",
|
adamc@307
|
965 newline],
|
adamc@307
|
966 string "}",
|
adamc@307
|
967 newline,
|
adamc@307
|
968 newline,
|
adamc@307
|
969
|
adamc@307
|
970 string "PQclear(res);",
|
adamc@307
|
971 newline,
|
adamc@311
|
972 string "uw_unit_v;",
|
adamc@307
|
973 newline,
|
adamc@307
|
974 string "})"]
|
adamc@307
|
975
|
adamc@29
|
976 and p_exp env = p_exp' false env
|
adamc@29
|
977
|
adamc@129
|
978 fun p_fun env (fx, n, args, ran, e) =
|
adamc@129
|
979 let
|
adamc@129
|
980 val nargs = length args
|
adamc@129
|
981 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
|
adamc@129
|
982 in
|
adamc@129
|
983 box [string "static",
|
adamc@129
|
984 space,
|
adamc@129
|
985 p_typ env ran,
|
adamc@129
|
986 space,
|
adamc@316
|
987 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@129
|
988 string "(",
|
adamc@129
|
989 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@311
|
990 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
|
adamc@129
|
991 box [p_typ env dom,
|
adamc@129
|
992 space,
|
adamc@129
|
993 p_rel env' (nargs - i - 1)]) args),
|
adamc@129
|
994 string ")",
|
adamc@129
|
995 space,
|
adamc@129
|
996 string "{",
|
adamc@129
|
997 newline,
|
adamc@129
|
998 box[string "return(",
|
adamc@129
|
999 p_exp env' e,
|
adamc@129
|
1000 string ");"],
|
adamc@129
|
1001 newline,
|
adamc@129
|
1002 string "}"]
|
adamc@129
|
1003 end
|
adamc@129
|
1004
|
adamc@129
|
1005 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@29
|
1006 case d of
|
adamc@29
|
1007 DStruct (n, xts) =>
|
adamc@196
|
1008 let
|
adamc@196
|
1009 val env = E.declBinds env dAll
|
adamc@196
|
1010 in
|
adamc@196
|
1011 box [string "struct",
|
adamc@196
|
1012 space,
|
adamc@311
|
1013 string ("__uws_" ^ Int.toString n),
|
adamc@196
|
1014 space,
|
adamc@196
|
1015 string "{",
|
adamc@196
|
1016 newline,
|
adamc@196
|
1017 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
|
adamc@196
|
1018 space,
|
adamc@311
|
1019 string "__uwf_",
|
adamc@316
|
1020 p_ident x,
|
adamc@196
|
1021 string ";",
|
adamc@196
|
1022 newline]) xts,
|
adamc@196
|
1023 string "};"]
|
adamc@196
|
1024 end
|
adamc@188
|
1025 | DDatatype (Enum, x, n, xncs) =>
|
adamc@188
|
1026 box [string "enum",
|
adamc@188
|
1027 space,
|
adamc@316
|
1028 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@188
|
1029 space,
|
adamc@188
|
1030 string "{",
|
adamc@188
|
1031 space,
|
adamc@316
|
1032 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adamc@316
|
1033 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
|
adamc@188
|
1034 space,
|
adamc@188
|
1035 string "};"]
|
adamc@198
|
1036 | DDatatype (Option, _, _, _) => box []
|
adamc@188
|
1037 | DDatatype (Default, x, n, xncs) =>
|
adamc@165
|
1038 let
|
adamc@165
|
1039 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
|
adamc@165
|
1040 | (x, n, SOME t) => SOME (x, n, t)) xncs
|
adamc@165
|
1041 in
|
adamc@165
|
1042 box [string "enum",
|
adamc@165
|
1043 space,
|
adamc@316
|
1044 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1045 space,
|
adamc@165
|
1046 string "{",
|
adamc@165
|
1047 space,
|
adamc@316
|
1048 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adamc@316
|
1049 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
|
adamc@165
|
1050 space,
|
adamc@165
|
1051 string "};",
|
adamc@165
|
1052 newline,
|
adamc@165
|
1053 newline,
|
adamc@165
|
1054 string "struct",
|
adamc@165
|
1055 space,
|
adamc@316
|
1056 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1057 space,
|
adamc@165
|
1058 string "{",
|
adamc@165
|
1059 newline,
|
adamc@165
|
1060 string "enum",
|
adamc@165
|
1061 space,
|
adamc@316
|
1062 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1063 space,
|
adamc@165
|
1064 string "tag;",
|
adamc@165
|
1065 newline,
|
adamc@165
|
1066 box (case xncsArgs of
|
adamc@165
|
1067 [] => []
|
adamc@165
|
1068 | _ => [string "union",
|
adamc@165
|
1069 space,
|
adamc@165
|
1070 string "{",
|
adamc@165
|
1071 newline,
|
adamc@165
|
1072 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
|
adamc@165
|
1073 space,
|
adamc@316
|
1074 string ("uw_" ^ ident x),
|
adamc@165
|
1075 string ";"]) xncsArgs,
|
adamc@165
|
1076 newline,
|
adamc@165
|
1077 string "}",
|
adamc@165
|
1078 space,
|
adamc@165
|
1079 string "data;",
|
adamc@165
|
1080 newline]),
|
adamc@165
|
1081 string "};"]
|
adamc@188
|
1082 end
|
adamc@29
|
1083
|
adamc@196
|
1084 | DDatatypeForward _ => box []
|
adamc@196
|
1085
|
adamc@29
|
1086 | DVal (x, n, t, e) =>
|
adamc@29
|
1087 box [p_typ env t,
|
adamc@29
|
1088 space,
|
adamc@316
|
1089 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@29
|
1090 space,
|
adamc@29
|
1091 string "=",
|
adamc@29
|
1092 space,
|
adamc@29
|
1093 p_exp env e,
|
adamc@29
|
1094 string ";"]
|
adamc@129
|
1095 | DFun vi => p_fun env vi
|
adamc@129
|
1096 | DFunRec vis =>
|
adamc@29
|
1097 let
|
adamc@129
|
1098 val env = E.declBinds env dAll
|
adamc@29
|
1099 in
|
adamc@129
|
1100 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
|
adamc@129
|
1101 box [string "static",
|
adamc@129
|
1102 space,
|
adamc@129
|
1103 p_typ env ran,
|
adamc@129
|
1104 space,
|
adamc@316
|
1105 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@311
|
1106 string "(uw_context,",
|
adamc@129
|
1107 space,
|
adamc@129
|
1108 p_list_sep (box [string ",", space])
|
adamc@129
|
1109 (fn (_, dom) => p_typ env dom) args,
|
adamc@129
|
1110 string ");"]) vis,
|
adamc@29
|
1111 newline,
|
adamc@129
|
1112 p_list_sep newline (p_fun env) vis,
|
adamc@129
|
1113 newline]
|
adamc@29
|
1114 end
|
adamc@273
|
1115 | DTable (x, _) => box [string "/* SQL table ",
|
adamc@273
|
1116 string x,
|
adamc@273
|
1117 string " */",
|
adamc@273
|
1118 newline]
|
adamc@311
|
1119 | DDatabase s => box [string "static void uw_db_validate(uw_context);",
|
adamc@272
|
1120 newline,
|
adamc@311
|
1121 string "static void uw_db_prepare(uw_context);",
|
adamc@282
|
1122 newline,
|
adamc@275
|
1123 newline,
|
adamc@311
|
1124 string "void uw_db_init(uw_context ctx) {",
|
adamc@273
|
1125 newline,
|
adamc@272
|
1126 string "PGconn *conn = PQconnectdb(\"",
|
adamc@272
|
1127 string (String.toString s),
|
adamc@272
|
1128 string "\");",
|
adamc@272
|
1129 newline,
|
adamc@311
|
1130 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
1131 string "\"libpq can't allocate a connection.\");",
|
adamc@272
|
1132 newline,
|
adamc@272
|
1133 string "if (PQstatus(conn) != CONNECTION_OK) {",
|
adamc@272
|
1134 newline,
|
adamc@272
|
1135 box [string "char msg[1024];",
|
adamc@272
|
1136 newline,
|
adamc@272
|
1137 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@272
|
1138 newline,
|
adamc@272
|
1139 string "msg[1023] = 0;",
|
adamc@272
|
1140 newline,
|
adamc@272
|
1141 string "PQfinish(conn);",
|
adamc@272
|
1142 newline,
|
adamc@311
|
1143 string "uw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
1144 string "\"Connection to Postgres server failed: %s\", msg);"],
|
adamc@272
|
1145 newline,
|
adamc@272
|
1146 string "}",
|
adamc@272
|
1147 newline,
|
adamc@311
|
1148 string "uw_set_db(ctx, conn);",
|
adamc@272
|
1149 newline,
|
adamc@311
|
1150 string "uw_db_validate(ctx);",
|
adamc@275
|
1151 newline,
|
adamc@311
|
1152 string "uw_db_prepare(ctx);",
|
adamc@282
|
1153 newline,
|
adamc@272
|
1154 string "}",
|
adamc@272
|
1155 newline,
|
adamc@272
|
1156 newline,
|
adamc@311
|
1157 string "void uw_db_close(uw_context ctx) {",
|
adamc@272
|
1158 newline,
|
adamc@311
|
1159 string "PQfinish(uw_get_db(ctx));",
|
adamc@272
|
1160 newline,
|
adamc@272
|
1161 string "}",
|
adamc@272
|
1162 newline]
|
adamc@29
|
1163
|
adamc@282
|
1164 | DPreparedStatements ss =>
|
adamc@311
|
1165 box [string "static void uw_db_prepare(uw_context ctx) {",
|
adamc@282
|
1166 newline,
|
adamc@311
|
1167 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@282
|
1168 newline,
|
adamc@282
|
1169 string "PGresult *res;",
|
adamc@282
|
1170 newline,
|
adamc@282
|
1171 newline,
|
adamc@282
|
1172
|
adamc@282
|
1173 p_list_sepi newline (fn i => fn (s, n) =>
|
adamc@311
|
1174 box [string "res = PQprepare(conn, \"uw",
|
adamc@282
|
1175 string (Int.toString i),
|
adamc@282
|
1176 string "\", \"",
|
adamc@282
|
1177 string (String.toString s),
|
adamc@282
|
1178 string "\", ",
|
adamc@282
|
1179 string (Int.toString n),
|
adamc@282
|
1180 string ", NULL);",
|
adamc@282
|
1181 newline,
|
adamc@282
|
1182 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@282
|
1183 newline,
|
adamc@282
|
1184 box [string "char msg[1024];",
|
adamc@282
|
1185 newline,
|
adamc@282
|
1186 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@282
|
1187 newline,
|
adamc@282
|
1188 string "msg[1023] = 0;",
|
adamc@282
|
1189 newline,
|
adamc@282
|
1190 string "PQclear(res);",
|
adamc@282
|
1191 newline,
|
adamc@282
|
1192 string "PQfinish(conn);",
|
adamc@282
|
1193 newline,
|
adamc@311
|
1194 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
|
adamc@282
|
1195 string (String.toString s),
|
adamc@282
|
1196 string "\\n%s\", msg);",
|
adamc@282
|
1197 newline],
|
adamc@282
|
1198 string "}",
|
adamc@282
|
1199 newline,
|
adamc@282
|
1200 string "PQclear(res);",
|
adamc@282
|
1201 newline])
|
adamc@282
|
1202 ss,
|
adamc@282
|
1203
|
adamc@282
|
1204 string "}"]
|
adamc@282
|
1205
|
adamc@144
|
1206 datatype 'a search =
|
adamc@144
|
1207 Found of 'a
|
adamc@144
|
1208 | NotFound
|
adamc@144
|
1209 | Error
|
adamc@120
|
1210
|
adamc@275
|
1211 fun p_sqltype' env (tAll as (t, loc)) =
|
adamc@275
|
1212 case t of
|
adamc@275
|
1213 TFfi ("Basis", "int") => "int8"
|
adamc@275
|
1214 | TFfi ("Basis", "float") => "float8"
|
adamc@275
|
1215 | TFfi ("Basis", "string") => "text"
|
adamc@275
|
1216 | TFfi ("Basis", "bool") => "bool"
|
adamc@275
|
1217 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
|
adamc@275
|
1218 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@275
|
1219 "ERROR")
|
adamc@275
|
1220
|
adamc@275
|
1221 fun p_sqltype env t = string (p_sqltype' env t)
|
adamc@101
|
1222
|
adamc@101
|
1223 fun p_file env (ds, ps) =
|
adamc@29
|
1224 let
|
adamc@101
|
1225 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
1226 (p_decl env d,
|
adamc@31
|
1227 E.declBinds env d))
|
adamc@101
|
1228 env ds
|
adamc@144
|
1229
|
adamc@144
|
1230 val fields = foldl (fn ((ek, _, _, ts), fields) =>
|
adamc@144
|
1231 case ek of
|
adamc@144
|
1232 Core.Link => fields
|
adamc@144
|
1233 | Core.Action =>
|
adamc@280
|
1234 case List.nth (ts, length ts - 2) of
|
adamc@144
|
1235 (TRecord i, _) =>
|
adamc@144
|
1236 let
|
adamc@144
|
1237 val xts = E.lookupStruct env i
|
adamc@144
|
1238 val xtsSet = SS.addList (SS.empty, map #1 xts)
|
adamc@144
|
1239 in
|
adamc@144
|
1240 foldl (fn ((x, _), fields) =>
|
adamc@144
|
1241 let
|
adamc@144
|
1242 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
|
adamc@144
|
1243 in
|
adamc@144
|
1244 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
|
adamc@144
|
1245 xtsSet'))
|
adamc@144
|
1246 end) fields xts
|
adamc@144
|
1247 end
|
adamc@144
|
1248 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
|
adamc@144
|
1249 SM.empty ps
|
adamc@144
|
1250
|
adamc@144
|
1251 val fnums = SM.foldli (fn (x, xs, fnums) =>
|
adamc@144
|
1252 let
|
adamc@144
|
1253 val unusable = SS.foldl (fn (x', unusable) =>
|
adamc@144
|
1254 case SM.find (fnums, x') of
|
adamc@144
|
1255 NONE => unusable
|
adamc@144
|
1256 | SOME n => IS.add (unusable, n))
|
adamc@144
|
1257 IS.empty xs
|
adamc@144
|
1258
|
adamc@144
|
1259 fun findAvailable n =
|
adamc@144
|
1260 if IS.member (unusable, n) then
|
adamc@144
|
1261 findAvailable (n + 1)
|
adamc@144
|
1262 else
|
adamc@144
|
1263 n
|
adamc@144
|
1264 in
|
adamc@144
|
1265 SM.insert (fnums, x, findAvailable 0)
|
adamc@144
|
1266 end)
|
adamc@144
|
1267 SM.empty fields
|
adamc@144
|
1268
|
adamc@144
|
1269 fun makeSwitch (fnums, i) =
|
adamc@144
|
1270 case SM.foldl (fn (n, NotFound) => Found n
|
adamc@144
|
1271 | (n, Error) => Error
|
adamc@144
|
1272 | (n, Found n') => if n = n' then
|
adamc@144
|
1273 Found n'
|
adamc@144
|
1274 else
|
adamc@144
|
1275 Error) NotFound fnums of
|
adamc@144
|
1276 NotFound => box [string "return",
|
adamc@144
|
1277 space,
|
adamc@144
|
1278 string "-1;"]
|
adamc@144
|
1279 | Found n => box [string "return",
|
adamc@144
|
1280 space,
|
adamc@144
|
1281 string (Int.toString n),
|
adamc@144
|
1282 string ";"]
|
adamc@144
|
1283 | Error =>
|
adamc@144
|
1284 let
|
adamc@144
|
1285 val cmap = SM.foldli (fn (x, n, cmap) =>
|
adamc@144
|
1286 let
|
adamc@144
|
1287 val ch = if i < size x then
|
adamc@144
|
1288 String.sub (x, i)
|
adamc@144
|
1289 else
|
adamc@144
|
1290 chr 0
|
adamc@144
|
1291
|
adamc@144
|
1292 val fnums = case CM.find (cmap, ch) of
|
adamc@144
|
1293 NONE => SM.empty
|
adamc@144
|
1294 | SOME fnums => fnums
|
adamc@144
|
1295 val fnums = SM.insert (fnums, x, n)
|
adamc@144
|
1296 in
|
adamc@144
|
1297 CM.insert (cmap, ch, fnums)
|
adamc@144
|
1298 end)
|
adamc@144
|
1299 CM.empty fnums
|
adamc@144
|
1300
|
adamc@144
|
1301 val cmap = CM.listItemsi cmap
|
adamc@144
|
1302 in
|
adamc@144
|
1303 case cmap of
|
adamc@144
|
1304 [(_, fnums)] =>
|
adamc@144
|
1305 box [string "if",
|
adamc@144
|
1306 space,
|
adamc@144
|
1307 string "(name[",
|
adamc@144
|
1308 string (Int.toString i),
|
adamc@144
|
1309 string "]",
|
adamc@144
|
1310 space,
|
adamc@144
|
1311 string "==",
|
adamc@144
|
1312 space,
|
adamc@144
|
1313 string "0)",
|
adamc@144
|
1314 space,
|
adamc@144
|
1315 string "return",
|
adamc@144
|
1316 space,
|
adamc@144
|
1317 string "-1;",
|
adamc@144
|
1318 newline,
|
adamc@144
|
1319 makeSwitch (fnums, i+1)]
|
adamc@144
|
1320 | _ =>
|
adamc@144
|
1321 box [string "switch",
|
adamc@144
|
1322 space,
|
adamc@144
|
1323 string "(name[",
|
adamc@144
|
1324 string (Int.toString i),
|
adamc@144
|
1325 string "])",
|
adamc@144
|
1326 space,
|
adamc@144
|
1327 string "{",
|
adamc@144
|
1328 newline,
|
adamc@144
|
1329 box (map (fn (ch, fnums) =>
|
adamc@144
|
1330 box [string "case",
|
adamc@144
|
1331 space,
|
adamc@144
|
1332 if ch = chr 0 then
|
adamc@144
|
1333 string "0:"
|
adamc@144
|
1334 else
|
adamc@144
|
1335 box [string "'",
|
adamc@144
|
1336 string (Char.toString ch),
|
adamc@144
|
1337 string "':"],
|
adamc@144
|
1338 newline,
|
adamc@144
|
1339 makeSwitch (fnums, i+1),
|
adamc@144
|
1340 newline]) cmap),
|
adamc@144
|
1341 string "default:",
|
adamc@144
|
1342 newline,
|
adamc@144
|
1343 string "return",
|
adamc@144
|
1344 space,
|
adamc@144
|
1345 string "-1;",
|
adamc@144
|
1346 newline,
|
adamc@144
|
1347 string "}"]
|
adamc@144
|
1348 end
|
adamc@144
|
1349
|
adamc@186
|
1350 fun capitalize s =
|
adamc@186
|
1351 if s = "" then
|
adamc@186
|
1352 ""
|
adamc@186
|
1353 else
|
adamc@186
|
1354 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@186
|
1355
|
adamc@144
|
1356 fun unurlify (t, loc) =
|
adamc@144
|
1357 case t of
|
adamc@316
|
1358 TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
|
adamc@144
|
1359
|
adamc@311
|
1360 | TRecord 0 => string "uw_unit_v"
|
adamc@144
|
1361 | TRecord i =>
|
adamc@144
|
1362 let
|
adamc@144
|
1363 val xts = E.lookupStruct env i
|
adamc@144
|
1364 in
|
adamc@144
|
1365 box [string "({",
|
adamc@144
|
1366 newline,
|
adamc@144
|
1367 box (map (fn (x, t) =>
|
adamc@144
|
1368 box [p_typ env t,
|
adamc@144
|
1369 space,
|
adamc@144
|
1370 string x,
|
adamc@144
|
1371 space,
|
adamc@144
|
1372 string "=",
|
adamc@144
|
1373 space,
|
adamc@144
|
1374 unurlify t,
|
adamc@144
|
1375 string ";",
|
adamc@144
|
1376 newline]) xts),
|
adamc@144
|
1377 string "struct",
|
adamc@144
|
1378 space,
|
adamc@311
|
1379 string "__uws_",
|
adamc@144
|
1380 string (Int.toString i),
|
adamc@144
|
1381 space,
|
adamc@181
|
1382 string "tmp",
|
adamc@144
|
1383 space,
|
adamc@144
|
1384 string "=",
|
adamc@144
|
1385 space,
|
adamc@144
|
1386 string "{",
|
adamc@144
|
1387 space,
|
adamc@144
|
1388 p_list_sep (box [string ",", space]) (fn (x, _) => string x) xts,
|
adamc@144
|
1389 space,
|
adamc@144
|
1390 string "};",
|
adamc@144
|
1391 newline,
|
adamc@181
|
1392 string "tmp;",
|
adamc@144
|
1393 newline,
|
adamc@144
|
1394 string "})"]
|
adamc@144
|
1395 end
|
adamc@144
|
1396
|
adamc@188
|
1397 | TDatatype (Enum, i, _) =>
|
adamc@188
|
1398 let
|
adamc@188
|
1399 val (x, xncs) = E.lookupDatatype env i
|
adamc@188
|
1400
|
adamc@188
|
1401 fun doEm xncs =
|
adamc@188
|
1402 case xncs of
|
adamc@311
|
1403 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), (enum __uwe_"
|
adamc@188
|
1404 ^ x ^ "_" ^ Int.toString i ^ ")0)")
|
adamc@188
|
1405 | (x', n, to) :: rest =>
|
adamc@188
|
1406 box [string "((!strncmp(request, \"",
|
adamc@188
|
1407 string x',
|
adamc@188
|
1408 string "\", ",
|
adamc@188
|
1409 string (Int.toString (size x')),
|
adamc@188
|
1410 string ") && (request[",
|
adamc@188
|
1411 string (Int.toString (size x')),
|
adamc@188
|
1412 string "] == 0 || request[",
|
adamc@188
|
1413 string (Int.toString (size x')),
|
adamc@316
|
1414 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
|
adamc@188
|
1415 space,
|
adamc@188
|
1416 string ":",
|
adamc@188
|
1417 space,
|
adamc@188
|
1418 doEm rest,
|
adamc@188
|
1419 string ")"]
|
adamc@188
|
1420 in
|
adamc@188
|
1421 doEm xncs
|
adamc@188
|
1422 end
|
adamc@188
|
1423
|
adamc@198
|
1424 | TDatatype (Option, i, xncs) =>
|
adamc@198
|
1425 let
|
adamc@198
|
1426 val (x, _) = E.lookupDatatype env i
|
adamc@198
|
1427
|
adamc@198
|
1428 val (no_arg, has_arg, t) =
|
adamc@198
|
1429 case !xncs of
|
adamc@198
|
1430 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@198
|
1431 (no_arg, has_arg, t)
|
adamc@198
|
1432 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@198
|
1433 (no_arg, has_arg, t)
|
adamc@198
|
1434 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
|
adamc@198
|
1435 in
|
adamc@198
|
1436 box [string "(request[0] == '/' ? ++request : request,",
|
adamc@198
|
1437 newline,
|
adamc@198
|
1438 string "((!strncmp(request, \"",
|
adamc@198
|
1439 string no_arg,
|
adamc@198
|
1440 string "\", ",
|
adamc@198
|
1441 string (Int.toString (size no_arg)),
|
adamc@198
|
1442 string ") && (request[",
|
adamc@198
|
1443 string (Int.toString (size no_arg)),
|
adamc@198
|
1444 string "] == 0 || request[",
|
adamc@198
|
1445 string (Int.toString (size no_arg)),
|
adamc@198
|
1446 string "] == '/')) ? (request",
|
adamc@198
|
1447 space,
|
adamc@198
|
1448 string "+=",
|
adamc@198
|
1449 space,
|
adamc@198
|
1450 string (Int.toString (size no_arg)),
|
adamc@198
|
1451 string ", NULL) : ((!strncmp(request, \"",
|
adamc@198
|
1452 string has_arg,
|
adamc@198
|
1453 string "\", ",
|
adamc@198
|
1454 string (Int.toString (size has_arg)),
|
adamc@198
|
1455 string ") && (request[",
|
adamc@198
|
1456 string (Int.toString (size has_arg)),
|
adamc@198
|
1457 string "] == 0 || request[",
|
adamc@198
|
1458 string (Int.toString (size has_arg)),
|
adamc@198
|
1459 string "] == '/')) ? (request",
|
adamc@198
|
1460 space,
|
adamc@198
|
1461 string "+=",
|
adamc@198
|
1462 space,
|
adamc@198
|
1463 string (Int.toString (size has_arg)),
|
adamc@200
|
1464 string ", (request[0] == '/' ? ++request : NULL), ",
|
adamc@200
|
1465 newline,
|
adamc@198
|
1466
|
adamc@198
|
1467 case #1 t of
|
adamc@198
|
1468 TDatatype _ => unurlify t
|
adamc@199
|
1469 | TFfi ("Basis", "string") => unurlify t
|
adamc@198
|
1470 | _ => box [string "({",
|
adamc@198
|
1471 newline,
|
adamc@198
|
1472 p_typ env t,
|
adamc@198
|
1473 space,
|
adamc@198
|
1474 string "*tmp",
|
adamc@198
|
1475 space,
|
adamc@198
|
1476 string "=",
|
adamc@198
|
1477 space,
|
adamc@311
|
1478 string "uw_malloc(ctx, sizeof(",
|
adamc@198
|
1479 p_typ env t,
|
adamc@198
|
1480 string "));",
|
adamc@198
|
1481 newline,
|
adamc@198
|
1482 string "*tmp",
|
adamc@198
|
1483 space,
|
adamc@198
|
1484 string "=",
|
adamc@198
|
1485 space,
|
adamc@198
|
1486 unurlify t,
|
adamc@198
|
1487 string ";",
|
adamc@198
|
1488 newline,
|
adamc@198
|
1489 string "tmp;",
|
adamc@198
|
1490 newline,
|
adamc@198
|
1491 string "})"],
|
adamc@198
|
1492 string ")",
|
adamc@198
|
1493 newline,
|
adamc@198
|
1494 string ":",
|
adamc@198
|
1495 space,
|
adamc@311
|
1496 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL))))")]
|
adamc@198
|
1497 end
|
adamc@198
|
1498
|
adamc@188
|
1499 | TDatatype (Default, i, _) =>
|
adamc@166
|
1500 let
|
adamc@166
|
1501 val (x, xncs) = E.lookupDatatype env i
|
adamc@166
|
1502
|
adamc@166
|
1503 fun doEm xncs =
|
adamc@166
|
1504 case xncs of
|
adamc@311
|
1505 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL)")
|
adamc@167
|
1506 | (x', n, to) :: rest =>
|
adamc@167
|
1507 box [string "((!strncmp(request, \"",
|
adamc@167
|
1508 string x',
|
adamc@167
|
1509 string "\", ",
|
adamc@167
|
1510 string (Int.toString (size x')),
|
adamc@167
|
1511 string ") && (request[",
|
adamc@167
|
1512 string (Int.toString (size x')),
|
adamc@167
|
1513 string "] == 0 || request[",
|
adamc@167
|
1514 string (Int.toString (size x')),
|
adamc@167
|
1515 string "] == '/')) ? ({",
|
adamc@166
|
1516 newline,
|
adamc@167
|
1517 string "struct",
|
adamc@167
|
1518 space,
|
adamc@316
|
1519 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
|
adamc@166
|
1520 space,
|
adamc@311
|
1521 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
|
adamc@167
|
1522 string x,
|
adamc@167
|
1523 string "_",
|
adamc@167
|
1524 string (Int.toString i),
|
adamc@167
|
1525 string "));",
|
adamc@166
|
1526 newline,
|
adamc@181
|
1527 string "tmp->tag",
|
adamc@166
|
1528 space,
|
adamc@166
|
1529 string "=",
|
adamc@166
|
1530 space,
|
adamc@316
|
1531 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
|
adamc@166
|
1532 string ";",
|
adamc@166
|
1533 newline,
|
adamc@166
|
1534 string "request",
|
adamc@166
|
1535 space,
|
adamc@166
|
1536 string "+=",
|
adamc@166
|
1537 space,
|
adamc@167
|
1538 string (Int.toString (size x')),
|
adamc@166
|
1539 string ";",
|
adamc@166
|
1540 newline,
|
adamc@200
|
1541 string "if (request[0] == '/') ++request;",
|
adamc@200
|
1542 newline,
|
adamc@166
|
1543 case to of
|
adamc@166
|
1544 NONE => box []
|
adamc@311
|
1545 | SOME t => box [string "tmp->data.uw_",
|
adamc@316
|
1546 p_ident x',
|
adamc@166
|
1547 space,
|
adamc@166
|
1548 string "=",
|
adamc@166
|
1549 space,
|
adamc@166
|
1550 unurlify t,
|
adamc@166
|
1551 string ";",
|
adamc@166
|
1552 newline],
|
adamc@181
|
1553 string "tmp;",
|
adamc@166
|
1554 newline,
|
adamc@166
|
1555 string "})",
|
adamc@166
|
1556 space,
|
adamc@166
|
1557 string ":",
|
adamc@166
|
1558 space,
|
adamc@166
|
1559 doEm rest,
|
adamc@166
|
1560 string ")"]
|
adamc@166
|
1561 in
|
adamc@166
|
1562 doEm xncs
|
adamc@166
|
1563 end
|
adamc@166
|
1564
|
adamc@144
|
1565 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
|
adamc@144
|
1566 space)
|
adamc@144
|
1567
|
adamc@144
|
1568
|
adamc@144
|
1569 fun p_page (ek, s, n, ts) =
|
adamc@144
|
1570 let
|
adamc@144
|
1571 val (ts, defInputs, inputsVar) =
|
adamc@144
|
1572 case ek of
|
adamc@314
|
1573 Core.Link => (List.take (ts, length ts - 1), string "", string "")
|
adamc@144
|
1574 | Core.Action =>
|
adamc@280
|
1575 case List.nth (ts, length ts - 2) of
|
adamc@144
|
1576 (TRecord i, _) =>
|
adamc@144
|
1577 let
|
adamc@144
|
1578 val xts = E.lookupStruct env i
|
adamc@144
|
1579 in
|
adamc@280
|
1580 (List.take (ts, length ts - 2),
|
adamc@144
|
1581 box [box (map (fn (x, t) => box [p_typ env t,
|
adamc@144
|
1582 space,
|
adamc@311
|
1583 string "uw_input_",
|
adamc@316
|
1584 p_ident x,
|
adamc@144
|
1585 string ";",
|
adamc@144
|
1586 newline]) xts),
|
adamc@144
|
1587 newline,
|
adamc@144
|
1588 box (map (fn (x, t) =>
|
adamc@144
|
1589 let
|
adamc@144
|
1590 val n = case SM.find (fnums, x) of
|
adamc@144
|
1591 NONE => raise Fail "CjrPrint: Can't find in fnums"
|
adamc@144
|
1592 | SOME n => n
|
adamc@190
|
1593
|
adamc@190
|
1594 val f = case t of
|
adamc@190
|
1595 (TFfi ("Basis", "bool"), _) => "optional_"
|
adamc@190
|
1596 | _ => ""
|
adamc@144
|
1597 in
|
adamc@311
|
1598 box [string "request = uw_get_",
|
adamc@190
|
1599 string f,
|
adamc@190
|
1600 string "input(ctx, ",
|
adamc@144
|
1601 string (Int.toString n),
|
adamc@144
|
1602 string ");",
|
adamc@144
|
1603 newline,
|
adamc@144
|
1604 string "if (request == NULL) {",
|
adamc@144
|
1605 newline,
|
adamc@144
|
1606 box [string "printf(\"Missing input ",
|
adamc@144
|
1607 string x,
|
adamc@144
|
1608 string "\\n\");",
|
adamc@144
|
1609 newline,
|
adamc@144
|
1610 string "exit(1);"],
|
adamc@144
|
1611 newline,
|
adamc@144
|
1612 string "}",
|
adamc@144
|
1613 newline,
|
adamc@311
|
1614 string "uw_input_",
|
adamc@316
|
1615 p_ident x,
|
adamc@144
|
1616 space,
|
adamc@144
|
1617 string "=",
|
adamc@144
|
1618 space,
|
adamc@144
|
1619 unurlify t,
|
adamc@144
|
1620 string ";",
|
adamc@144
|
1621 newline]
|
adamc@144
|
1622 end) xts),
|
adamc@311
|
1623 string "struct __uws_",
|
adamc@144
|
1624 string (Int.toString i),
|
adamc@144
|
1625 space,
|
adamc@311
|
1626 string "uw_inputs",
|
adamc@144
|
1627 space,
|
adamc@144
|
1628 string "= {",
|
adamc@144
|
1629 newline,
|
adamc@311
|
1630 box (map (fn (x, _) => box [string "uw_input_",
|
adamc@316
|
1631 p_ident x,
|
adamc@144
|
1632 string ",",
|
adamc@144
|
1633 newline]) xts),
|
adamc@144
|
1634 string "};",
|
adamc@144
|
1635 newline],
|
adamc@144
|
1636 box [string ",",
|
adamc@144
|
1637 space,
|
adamc@311
|
1638 string "uw_inputs"])
|
adamc@144
|
1639 end
|
adamc@144
|
1640
|
adamc@144
|
1641 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
|
adamc@144
|
1642 in
|
adamc@144
|
1643 box [string "if (!strncmp(request, \"",
|
adamc@144
|
1644 string (String.toString s),
|
adamc@144
|
1645 string "\", ",
|
adamc@144
|
1646 string (Int.toString (size s)),
|
adamc@198
|
1647 string ") && (request[",
|
adamc@198
|
1648 string (Int.toString (size s)),
|
adamc@198
|
1649 string "] == 0 || request[",
|
adamc@198
|
1650 string (Int.toString (size s)),
|
adamc@198
|
1651 string "] == '/')) {",
|
adamc@144
|
1652 newline,
|
adamc@144
|
1653 string "request += ",
|
adamc@144
|
1654 string (Int.toString (size s)),
|
adamc@144
|
1655 string ";",
|
adamc@144
|
1656 newline,
|
adamc@144
|
1657 string "if (*request == '/') ++request;",
|
adamc@144
|
1658 newline,
|
adamc@144
|
1659 box [string "{",
|
adamc@144
|
1660 newline,
|
adamc@144
|
1661 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
|
adamc@144
|
1662 space,
|
adamc@144
|
1663 string "arg",
|
adamc@144
|
1664 string (Int.toString i),
|
adamc@144
|
1665 space,
|
adamc@144
|
1666 string "=",
|
adamc@144
|
1667 space,
|
adamc@144
|
1668 unurlify t,
|
adamc@144
|
1669 string ";",
|
adamc@144
|
1670 newline]) ts),
|
adamc@144
|
1671 defInputs,
|
adamc@144
|
1672 p_enamed env n,
|
adamc@144
|
1673 string "(",
|
adamc@144
|
1674 p_list_sep (box [string ",", space])
|
adamc@144
|
1675 (fn x => x)
|
adamc@272
|
1676 (string "ctx"
|
adamc@280
|
1677 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
|
adamc@144
|
1678 inputsVar,
|
adamc@311
|
1679 string ", uw_unit_v);",
|
adamc@144
|
1680 newline,
|
adamc@144
|
1681 string "return;",
|
adamc@144
|
1682 newline,
|
adamc@144
|
1683 string "}",
|
adamc@144
|
1684 newline,
|
adamc@144
|
1685 string "}"]
|
adamc@144
|
1686 ]
|
adamc@144
|
1687 end
|
adamc@144
|
1688
|
adamc@144
|
1689 val pds' = map p_page ps
|
adamc@275
|
1690
|
adamc@275
|
1691 val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts)
|
adamc@275
|
1692 | _ => NONE) ds
|
adamc@275
|
1693
|
adamc@275
|
1694 val validate =
|
adamc@311
|
1695 box [string "static void uw_db_validate(uw_context ctx) {",
|
adamc@275
|
1696 newline,
|
adamc@311
|
1697 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@275
|
1698 newline,
|
adamc@275
|
1699 string "PGresult *res;",
|
adamc@275
|
1700 newline,
|
adamc@275
|
1701 newline,
|
adamc@275
|
1702 p_list_sep newline
|
adamc@275
|
1703 (fn (s, xts) =>
|
adamc@275
|
1704 let
|
adamc@275
|
1705 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
|
adamc@275
|
1706 ^ s ^ "'"
|
adamc@275
|
1707
|
adamc@275
|
1708 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@275
|
1709 s,
|
adamc@275
|
1710 "') AND (",
|
adamc@275
|
1711 String.concatWith " OR "
|
adamc@275
|
1712 (map (fn (x, t) =>
|
adamc@311
|
1713 String.concat ["(attname = 'uw_",
|
adamc@275
|
1714 CharVector.map
|
adamc@316
|
1715 Char.toLower (ident x),
|
adamc@275
|
1716 "' AND atttypid = (SELECT oid FROM pg_type",
|
adamc@275
|
1717 " WHERE typname = '",
|
adamc@275
|
1718 p_sqltype' env t,
|
adamc@275
|
1719 "'))"]) xts),
|
adamc@275
|
1720 ")"]
|
adamc@275
|
1721
|
adamc@275
|
1722 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@275
|
1723 s,
|
adamc@275
|
1724 "') AND attnum >= 0"]
|
adamc@275
|
1725 in
|
adamc@275
|
1726 box [string "res = PQexec(conn, \"",
|
adamc@275
|
1727 string q,
|
adamc@275
|
1728 string "\");",
|
adamc@275
|
1729 newline,
|
adamc@275
|
1730 newline,
|
adamc@275
|
1731 string "if (res == NULL) {",
|
adamc@275
|
1732 newline,
|
adamc@275
|
1733 box [string "PQfinish(conn);",
|
adamc@275
|
1734 newline,
|
adamc@311
|
1735 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1736 newline],
|
adamc@275
|
1737 string "}",
|
adamc@275
|
1738 newline,
|
adamc@275
|
1739 newline,
|
adamc@275
|
1740 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1741 newline,
|
adamc@275
|
1742 box [string "char msg[1024];",
|
adamc@275
|
1743 newline,
|
adamc@275
|
1744 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1745 newline,
|
adamc@275
|
1746 string "msg[1023] = 0;",
|
adamc@275
|
1747 newline,
|
adamc@275
|
1748 string "PQclear(res);",
|
adamc@275
|
1749 newline,
|
adamc@275
|
1750 string "PQfinish(conn);",
|
adamc@275
|
1751 newline,
|
adamc@311
|
1752 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1753 string q,
|
adamc@275
|
1754 string "\\n%s\", msg);",
|
adamc@275
|
1755 newline],
|
adamc@275
|
1756 string "}",
|
adamc@275
|
1757 newline,
|
adamc@275
|
1758 newline,
|
adamc@275
|
1759 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@275
|
1760 newline,
|
adamc@275
|
1761 box [string "PQclear(res);",
|
adamc@275
|
1762 newline,
|
adamc@275
|
1763 string "PQfinish(conn);",
|
adamc@275
|
1764 newline,
|
adamc@311
|
1765 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1766 string s,
|
adamc@275
|
1767 string "' does not exist.\");",
|
adamc@275
|
1768 newline],
|
adamc@275
|
1769 string "}",
|
adamc@275
|
1770 newline,
|
adamc@275
|
1771 newline,
|
adamc@275
|
1772 string "PQclear(res);",
|
adamc@275
|
1773 newline,
|
adamc@275
|
1774
|
adamc@275
|
1775 string "res = PQexec(conn, \"",
|
adamc@275
|
1776 string q',
|
adamc@275
|
1777 string "\");",
|
adamc@275
|
1778 newline,
|
adamc@275
|
1779 newline,
|
adamc@275
|
1780 string "if (res == NULL) {",
|
adamc@275
|
1781 newline,
|
adamc@275
|
1782 box [string "PQfinish(conn);",
|
adamc@275
|
1783 newline,
|
adamc@311
|
1784 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1785 newline],
|
adamc@275
|
1786 string "}",
|
adamc@275
|
1787 newline,
|
adamc@275
|
1788 newline,
|
adamc@275
|
1789 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1790 newline,
|
adamc@275
|
1791 box [string "char msg[1024];",
|
adamc@275
|
1792 newline,
|
adamc@275
|
1793 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1794 newline,
|
adamc@275
|
1795 string "msg[1023] = 0;",
|
adamc@275
|
1796 newline,
|
adamc@275
|
1797 string "PQclear(res);",
|
adamc@275
|
1798 newline,
|
adamc@275
|
1799 string "PQfinish(conn);",
|
adamc@275
|
1800 newline,
|
adamc@311
|
1801 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1802 string q',
|
adamc@275
|
1803 string "\\n%s\", msg);",
|
adamc@275
|
1804 newline],
|
adamc@275
|
1805 string "}",
|
adamc@275
|
1806 newline,
|
adamc@275
|
1807 newline,
|
adamc@275
|
1808 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
1809 string (Int.toString (length xts)),
|
adamc@275
|
1810 string "\")) {",
|
adamc@275
|
1811 newline,
|
adamc@275
|
1812 box [string "PQclear(res);",
|
adamc@275
|
1813 newline,
|
adamc@275
|
1814 string "PQfinish(conn);",
|
adamc@275
|
1815 newline,
|
adamc@311
|
1816 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1817 string s,
|
adamc@275
|
1818 string "' has the wrong column types.\");",
|
adamc@275
|
1819 newline],
|
adamc@275
|
1820 string "}",
|
adamc@275
|
1821 newline,
|
adamc@275
|
1822 newline,
|
adamc@275
|
1823 string "PQclear(res);",
|
adamc@275
|
1824 newline,
|
adamc@275
|
1825 newline,
|
adamc@275
|
1826
|
adamc@275
|
1827 string "res = PQexec(conn, \"",
|
adamc@275
|
1828 string q'',
|
adamc@275
|
1829 string "\");",
|
adamc@275
|
1830 newline,
|
adamc@275
|
1831 newline,
|
adamc@275
|
1832 string "if (res == NULL) {",
|
adamc@275
|
1833 newline,
|
adamc@275
|
1834 box [string "PQfinish(conn);",
|
adamc@275
|
1835 newline,
|
adamc@311
|
1836 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1837 newline],
|
adamc@275
|
1838 string "}",
|
adamc@275
|
1839 newline,
|
adamc@275
|
1840 newline,
|
adamc@275
|
1841 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1842 newline,
|
adamc@275
|
1843 box [string "char msg[1024];",
|
adamc@275
|
1844 newline,
|
adamc@275
|
1845 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1846 newline,
|
adamc@275
|
1847 string "msg[1023] = 0;",
|
adamc@275
|
1848 newline,
|
adamc@275
|
1849 string "PQclear(res);",
|
adamc@275
|
1850 newline,
|
adamc@275
|
1851 string "PQfinish(conn);",
|
adamc@275
|
1852 newline,
|
adamc@311
|
1853 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1854 string q'',
|
adamc@275
|
1855 string "\\n%s\", msg);",
|
adamc@275
|
1856 newline],
|
adamc@275
|
1857 string "}",
|
adamc@275
|
1858 newline,
|
adamc@275
|
1859 newline,
|
adamc@275
|
1860 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
1861 string (Int.toString (length xts)),
|
adamc@275
|
1862 string "\")) {",
|
adamc@275
|
1863 newline,
|
adamc@275
|
1864 box [string "PQclear(res);",
|
adamc@275
|
1865 newline,
|
adamc@275
|
1866 string "PQfinish(conn);",
|
adamc@275
|
1867 newline,
|
adamc@311
|
1868 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1869 string s,
|
adamc@275
|
1870 string "' has extra columns.\");",
|
adamc@275
|
1871 newline],
|
adamc@275
|
1872 string "}",
|
adamc@275
|
1873 newline,
|
adamc@275
|
1874 newline,
|
adamc@275
|
1875 string "PQclear(res);",
|
adamc@275
|
1876 newline]
|
adamc@275
|
1877 end) tables,
|
adamc@275
|
1878 string "}"]
|
adamc@29
|
1879 in
|
adamc@144
|
1880 box [string "#include <stdio.h>",
|
adamc@144
|
1881 newline,
|
adamc@144
|
1882 string "#include <stdlib.h>",
|
adamc@144
|
1883 newline,
|
adamc@272
|
1884 string "#include <string.h>",
|
adamc@272
|
1885 newline,
|
adamc@272
|
1886 string "#include <postgresql/libpq-fe.h>",
|
adamc@272
|
1887 newline,
|
adamc@144
|
1888 newline,
|
adamc@244
|
1889 string "#include \"urweb.h\"",
|
adamc@101
|
1890 newline,
|
adamc@101
|
1891 newline,
|
adamc@101
|
1892 p_list_sep newline (fn x => x) pds,
|
adamc@101
|
1893 newline,
|
adamc@311
|
1894 string "int uw_inputs_len = ",
|
adamc@144
|
1895 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
|
adamc@144
|
1896 string ";",
|
adamc@144
|
1897 newline,
|
adamc@144
|
1898 newline,
|
adamc@311
|
1899 string "int uw_input_num(char *name) {",
|
adamc@144
|
1900 newline,
|
adamc@144
|
1901 makeSwitch (fnums, 0),
|
adamc@144
|
1902 string "}",
|
adamc@144
|
1903 newline,
|
adamc@144
|
1904 newline,
|
adamc@311
|
1905 string "void uw_handle(uw_context ctx, char *request) {",
|
adamc@101
|
1906 newline,
|
adamc@101
|
1907 p_list_sep newline (fn x => x) pds',
|
adamc@101
|
1908 newline,
|
adamc@101
|
1909 string "}",
|
adamc@275
|
1910 newline,
|
adamc@275
|
1911 newline,
|
adamc@275
|
1912 validate,
|
adamc@101
|
1913 newline]
|
adamc@29
|
1914 end
|
adamc@29
|
1915
|
adamc@274
|
1916 fun p_sql env (ds, _) =
|
adamc@274
|
1917 let
|
adamc@274
|
1918 val (pps, _) = ListUtil.foldlMap
|
adamc@274
|
1919 (fn (dAll as (d, _), env) =>
|
adamc@274
|
1920 let
|
adamc@274
|
1921 val pp = case d of
|
adamc@274
|
1922 DTable (s, xts) =>
|
adamc@274
|
1923 box [string "CREATE TABLE ",
|
adamc@274
|
1924 string s,
|
adamc@274
|
1925 string "(",
|
adamc@274
|
1926 p_list (fn (x, t) =>
|
adamc@311
|
1927 box [string "uw_",
|
adamc@275
|
1928 string (CharVector.map Char.toLower x),
|
adamc@274
|
1929 space,
|
adamc@274
|
1930 p_sqltype env t,
|
adamc@274
|
1931 space,
|
adamc@274
|
1932 string "NOT",
|
adamc@274
|
1933 space,
|
adamc@274
|
1934 string "NULL"]) xts,
|
adamc@274
|
1935 string ");",
|
adamc@274
|
1936 newline,
|
adamc@274
|
1937 newline]
|
adamc@274
|
1938 | _ => box []
|
adamc@274
|
1939 in
|
adamc@274
|
1940 (pp, E.declBinds env dAll)
|
adamc@274
|
1941 end)
|
adamc@274
|
1942 env ds
|
adamc@274
|
1943 in
|
adamc@274
|
1944 box pps
|
adamc@274
|
1945 end
|
adamc@274
|
1946
|
adamc@29
|
1947 end
|