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@387
|
612 | EUnop (s, e1) =>
|
adamc@387
|
613 parenIf par (box [string s,
|
adamc@387
|
614 space,
|
adamc@387
|
615 p_exp' true env e1])
|
adamc@387
|
616
|
adamc@387
|
617 | EBinop (s, e1, e2) =>
|
adamc@389
|
618 if Char.isAlpha (String.sub (s, size s - 1)) then
|
adamc@389
|
619 box [string s,
|
adamc@390
|
620 string "(",
|
adamc@389
|
621 p_exp env e1,
|
adamc@389
|
622 string ",",
|
adamc@389
|
623 space,
|
adamc@389
|
624 p_exp env e2,
|
adamc@389
|
625 string ")"]
|
adamc@389
|
626 else
|
adamc@389
|
627 parenIf par (box [p_exp' true env e1,
|
adamc@389
|
628 space,
|
adamc@389
|
629 string s,
|
adamc@389
|
630 space,
|
adamc@389
|
631 p_exp' true env e2])
|
adamc@387
|
632
|
adamc@29
|
633 | ERecord (i, xes) => box [string "({",
|
adamc@29
|
634 space,
|
adamc@29
|
635 string "struct",
|
adamc@29
|
636 space,
|
adamc@311
|
637 string ("__uws_" ^ Int.toString i),
|
adamc@29
|
638 space,
|
adamc@181
|
639 string "tmp",
|
adamc@29
|
640 space,
|
adamc@29
|
641 string "=",
|
adamc@29
|
642 space,
|
adamc@29
|
643 string "{",
|
adamc@29
|
644 p_list (fn (_, e) =>
|
adamc@29
|
645 p_exp env e) xes,
|
adamc@29
|
646 string "};",
|
adamc@29
|
647 space,
|
adamc@181
|
648 string "tmp;",
|
adamc@29
|
649 space,
|
adamc@29
|
650 string "})" ]
|
adamc@29
|
651 | EField (e, x) =>
|
adamc@29
|
652 box [p_exp' true env e,
|
adamc@311
|
653 string ".__uwf_",
|
adamc@316
|
654 p_ident x]
|
adamc@29
|
655
|
adamc@182
|
656 | ECase (e, pes, {disc, result}) =>
|
adamc@182
|
657 let
|
adamc@182
|
658 val final = newGoto ()
|
adamc@182
|
659
|
adamc@182
|
660 val body = foldl (fn ((p, e), body) =>
|
adamc@182
|
661 let
|
adamc@182
|
662 val exit = newGoto ()
|
adamc@182
|
663 val (pr, _) = p_pat_preamble env p
|
adamc@182
|
664 val (p, env) = p_pat (env,
|
adamc@182
|
665 box [string "goto",
|
adamc@182
|
666 space,
|
adamc@182
|
667 exit,
|
adamc@182
|
668 string ";"],
|
adamc@182
|
669 0) p
|
adamc@182
|
670 in
|
adamc@182
|
671 box [body,
|
adamc@182
|
672 box [string "{",
|
adamc@182
|
673 newline,
|
adamc@182
|
674 pr,
|
adamc@182
|
675 newline,
|
adamc@182
|
676 p,
|
adamc@182
|
677 newline,
|
adamc@182
|
678 string "result",
|
adamc@182
|
679 space,
|
adamc@182
|
680 string "=",
|
adamc@182
|
681 space,
|
adamc@182
|
682 p_exp env e,
|
adamc@182
|
683 string ";",
|
adamc@182
|
684 newline,
|
adamc@182
|
685 string "goto",
|
adamc@182
|
686 space,
|
adamc@182
|
687 final,
|
adamc@182
|
688 string ";",
|
adamc@182
|
689 newline,
|
adamc@182
|
690 string "}"],
|
adamc@182
|
691 newline,
|
adamc@182
|
692 exit,
|
adamc@182
|
693 string ":",
|
adamc@182
|
694 newline]
|
adamc@182
|
695 end) (box []) pes
|
adamc@182
|
696 in
|
adamc@182
|
697 box [string "({",
|
adamc@182
|
698 newline,
|
adamc@182
|
699 p_typ env disc,
|
adamc@182
|
700 space,
|
adamc@182
|
701 string "disc0",
|
adamc@182
|
702 space,
|
adamc@182
|
703 string "=",
|
adamc@182
|
704 space,
|
adamc@182
|
705 p_exp env e,
|
adamc@182
|
706 string ";",
|
adamc@182
|
707 newline,
|
adamc@182
|
708 p_typ env result,
|
adamc@182
|
709 space,
|
adamc@182
|
710 string "result;",
|
adamc@182
|
711 newline,
|
adamc@182
|
712 body,
|
adamc@311
|
713 string "uw_error(ctx, FATAL, \"",
|
adamc@182
|
714 string (ErrorMsg.spanToString loc),
|
adamc@182
|
715 string ": pattern match failure\");",
|
adamc@182
|
716 newline,
|
adamc@182
|
717 final,
|
adamc@182
|
718 string ":",
|
adamc@182
|
719 space,
|
adamc@182
|
720 string "result;",
|
adamc@182
|
721 newline,
|
adamc@182
|
722 string "})"]
|
adamc@182
|
723 end
|
adamc@181
|
724
|
adamc@311
|
725 | EWrite e => box [string "(uw_write(ctx, ",
|
adamc@102
|
726 p_exp env e,
|
adamc@311
|
727 string "), uw_unit_v)"]
|
adamc@102
|
728
|
adamc@106
|
729 | ESeq (e1, e2) => box [string "(",
|
adamc@106
|
730 p_exp env e1,
|
adamc@106
|
731 string ",",
|
adamc@106
|
732 space,
|
adamc@106
|
733 p_exp env e2,
|
adamc@106
|
734 string ")"]
|
adamc@269
|
735 | ELet (x, t, e1, e2) => box [string "({",
|
adamc@269
|
736 newline,
|
adamc@269
|
737 p_typ env t,
|
adamc@269
|
738 space,
|
adamc@311
|
739 string "__uwr_",
|
adamc@316
|
740 p_ident x,
|
adamc@272
|
741 string "_",
|
adamc@272
|
742 string (Int.toString (E.countERels env)),
|
adamc@269
|
743 space,
|
adamc@269
|
744 string "=",
|
adamc@269
|
745 space,
|
adamc@269
|
746 p_exp env e1,
|
adamc@269
|
747 string ";",
|
adamc@269
|
748 newline,
|
adamc@269
|
749 p_exp (E.pushERel env x t) e2,
|
adamc@269
|
750 string ";",
|
adamc@269
|
751 newline,
|
adamc@269
|
752 string "})"]
|
adamc@269
|
753
|
adamc@282
|
754 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
|
adamc@278
|
755 let
|
adamc@316
|
756 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
|
adamc@278
|
757 val tables = ListUtil.mapConcat (fn (x, xts) =>
|
adamc@316
|
758 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
|
adamc@278
|
759 tables
|
adamc@278
|
760
|
adamc@278
|
761 val outputs = exps @ tables
|
adamc@325
|
762 val outputs = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER) outputs
|
adamc@324
|
763
|
adamc@324
|
764 val wontLeakStrings = notLeaky env true state
|
adamc@324
|
765 val wontLeakAnything = notLeaky env false state
|
adamc@278
|
766 in
|
adamc@324
|
767 box [string "(uw_begin_region(ctx), ",
|
adamc@324
|
768 if wontLeakAnything then
|
adamc@331
|
769 string "uw_begin_region(ctx), "
|
adamc@324
|
770 else
|
adamc@324
|
771 box [],
|
adamc@324
|
772 string "({",
|
adamc@278
|
773 newline,
|
adamc@311
|
774 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@278
|
775 newline,
|
adamc@282
|
776 case prepared of
|
adamc@282
|
777 NONE => box [string "char *query = ",
|
adamc@282
|
778 p_exp env query,
|
adamc@282
|
779 string ";",
|
adamc@282
|
780 newline]
|
adamc@282
|
781 | SOME _ =>
|
adamc@282
|
782 let
|
adamc@282
|
783 val ets = getPargs query
|
adamc@282
|
784 in
|
adamc@282
|
785 box [p_list_sepi newline
|
adamc@282
|
786 (fn i => fn (e, t) =>
|
adamc@282
|
787 box [p_sql_type t,
|
adamc@282
|
788 space,
|
adamc@282
|
789 string "arg",
|
adamc@282
|
790 string (Int.toString (i + 1)),
|
adamc@282
|
791 space,
|
adamc@282
|
792 string "=",
|
adamc@282
|
793 space,
|
adamc@282
|
794 p_exp env e,
|
adamc@282
|
795 string ";"])
|
adamc@282
|
796 ets,
|
adamc@282
|
797 newline,
|
adamc@282
|
798 newline,
|
adamc@282
|
799
|
adamc@282
|
800 string "const char *paramValues[] = { ",
|
adamc@282
|
801 p_list_sepi (box [string ",", space])
|
adamc@282
|
802 (fn i => fn (_, t) => p_ensql t (box [string "arg",
|
adamc@282
|
803 string (Int.toString (i + 1))]))
|
adamc@282
|
804 ets,
|
adamc@282
|
805 string " };",
|
adamc@282
|
806 newline,
|
adamc@282
|
807 newline]
|
adamc@282
|
808 end,
|
adamc@278
|
809 string "int n, i;",
|
adamc@278
|
810 newline,
|
adamc@278
|
811 p_typ env state,
|
adamc@278
|
812 space,
|
adamc@278
|
813 string "acc",
|
adamc@278
|
814 space,
|
adamc@278
|
815 string "=",
|
adamc@278
|
816 space,
|
adamc@278
|
817 p_exp env initial,
|
adamc@278
|
818 string ";",
|
adamc@278
|
819 newline,
|
adamc@282
|
820 string "PGresult *res = ",
|
adamc@282
|
821 case prepared of
|
adamc@295
|
822 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@311
|
823 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@282
|
824 string (Int.toString n),
|
adamc@282
|
825 string "\", ",
|
adamc@282
|
826 string (Int.toString (length (getPargs query))),
|
adamc@295
|
827 string ", paramValues, NULL, NULL, 0);"],
|
adamc@278
|
828 newline,
|
adamc@278
|
829 newline,
|
adamc@277
|
830
|
adamc@311
|
831 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@278
|
832 newline,
|
adamc@278
|
833 newline,
|
adamc@277
|
834
|
adamc@278
|
835 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@278
|
836 newline,
|
adamc@278
|
837 box [string "PQclear(res);",
|
adamc@278
|
838 newline,
|
adamc@311
|
839 string "uw_error(ctx, FATAL, \"",
|
adamc@278
|
840 string (ErrorMsg.spanToString loc),
|
adamc@282
|
841 string ": Query failed:\\n%s\\n%s\", ",
|
adamc@282
|
842 case prepared of
|
adamc@282
|
843 NONE => string "query"
|
adamc@282
|
844 | SOME _ => p_exp env query,
|
adamc@282
|
845 string ", PQerrorMessage(conn));",
|
adamc@278
|
846 newline],
|
adamc@278
|
847 string "}",
|
adamc@278
|
848 newline,
|
adamc@278
|
849 newline,
|
adamc@277
|
850
|
adamc@323
|
851 string "uw_end_region(ctx);",
|
adamc@323
|
852 newline,
|
adamc@278
|
853 string "n = PQntuples(res);",
|
adamc@278
|
854 newline,
|
adamc@278
|
855 string "for (i = 0; i < n; ++i) {",
|
adamc@278
|
856 newline,
|
adamc@278
|
857 box [string "struct",
|
adamc@278
|
858 space,
|
adamc@311
|
859 string "__uws_",
|
adamc@278
|
860 string (Int.toString rnum),
|
adamc@278
|
861 space,
|
adamc@311
|
862 string "__uwr_r_",
|
adamc@278
|
863 string (Int.toString (E.countERels env)),
|
adamc@278
|
864 string ";",
|
adamc@278
|
865 newline,
|
adamc@278
|
866 p_typ env state,
|
adamc@278
|
867 space,
|
adamc@311
|
868 string "__uwr_acc_",
|
adamc@278
|
869 string (Int.toString (E.countERels env + 1)),
|
adamc@278
|
870 space,
|
adamc@278
|
871 string "=",
|
adamc@278
|
872 space,
|
adamc@278
|
873 string "acc;",
|
adamc@278
|
874 newline,
|
adamc@278
|
875 newline,
|
adamc@278
|
876
|
adamc@278
|
877 p_list_sepi (box []) (fn i =>
|
adamc@278
|
878 fn (proj, t) =>
|
adamc@311
|
879 box [string "__uwr_r_",
|
adamc@278
|
880 string (Int.toString (E.countERels env)),
|
adamc@278
|
881 string ".",
|
adamc@278
|
882 string proj,
|
adamc@278
|
883 space,
|
adamc@278
|
884 string "=",
|
adamc@278
|
885 space,
|
adamc@324
|
886 p_unsql wontLeakStrings env t
|
adamc@278
|
887 (box [string "PQgetvalue(res, i, ",
|
adamc@278
|
888 string (Int.toString i),
|
adamc@278
|
889 string ")"]),
|
adamc@278
|
890 string ";",
|
adamc@278
|
891 newline]) outputs,
|
adamc@278
|
892
|
adamc@278
|
893 newline,
|
adamc@278
|
894 newline,
|
adamc@278
|
895
|
adamc@278
|
896 string "acc",
|
adamc@278
|
897 space,
|
adamc@278
|
898 string "=",
|
adamc@278
|
899 space,
|
adamc@278
|
900 p_exp (E.pushERel
|
adamc@278
|
901 (E.pushERel env "r" (TRecord rnum, loc))
|
adamc@278
|
902 "acc" state)
|
adamc@278
|
903 body,
|
adamc@278
|
904 string ";",
|
adamc@278
|
905 newline],
|
adamc@278
|
906 string "}",
|
adamc@278
|
907 newline,
|
adamc@278
|
908 newline,
|
adamc@278
|
909 string "PQclear(res);",
|
adamc@278
|
910 newline,
|
adamc@324
|
911 if wontLeakAnything then
|
adamc@324
|
912 box [string "uw_end_region(ctx);",
|
adamc@324
|
913 newline]
|
adamc@324
|
914 else
|
adamc@324
|
915 box [],
|
adamc@278
|
916 string "acc;",
|
adamc@278
|
917 newline,
|
adamc@323
|
918 string "}))"]
|
adamc@278
|
919 end
|
adamc@106
|
920
|
adamc@307
|
921 | EDml {dml, prepared} =>
|
adamc@337
|
922 box [string "(uw_begin_region(ctx), ({",
|
adamc@307
|
923 newline,
|
adamc@311
|
924 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@307
|
925 newline,
|
adamc@307
|
926 case prepared of
|
adamc@307
|
927 NONE => box [string "char *dml = ",
|
adamc@307
|
928 p_exp env dml,
|
adamc@307
|
929 string ";",
|
adamc@307
|
930 newline]
|
adamc@307
|
931 | SOME _ =>
|
adamc@307
|
932 let
|
adamc@307
|
933 val ets = getPargs dml
|
adamc@307
|
934 in
|
adamc@307
|
935 box [p_list_sepi newline
|
adamc@307
|
936 (fn i => fn (e, t) =>
|
adamc@307
|
937 box [p_sql_type t,
|
adamc@307
|
938 space,
|
adamc@307
|
939 string "arg",
|
adamc@307
|
940 string (Int.toString (i + 1)),
|
adamc@307
|
941 space,
|
adamc@307
|
942 string "=",
|
adamc@307
|
943 space,
|
adamc@307
|
944 p_exp env e,
|
adamc@307
|
945 string ";"])
|
adamc@307
|
946 ets,
|
adamc@307
|
947 newline,
|
adamc@307
|
948 newline,
|
adamc@307
|
949
|
adamc@307
|
950 string "const char *paramValues[] = { ",
|
adamc@307
|
951 p_list_sepi (box [string ",", space])
|
adamc@307
|
952 (fn i => fn (_, t) => p_ensql t (box [string "arg",
|
adamc@307
|
953 string (Int.toString (i + 1))]))
|
adamc@307
|
954 ets,
|
adamc@307
|
955 string " };",
|
adamc@307
|
956 newline,
|
adamc@307
|
957 newline]
|
adamc@307
|
958 end,
|
adamc@307
|
959 newline,
|
adamc@307
|
960 newline,
|
adamc@307
|
961 string "PGresult *res = ",
|
adamc@307
|
962 case prepared of
|
adamc@307
|
963 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@311
|
964 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@307
|
965 string (Int.toString n),
|
adamc@307
|
966 string "\", ",
|
adamc@307
|
967 string (Int.toString (length (getPargs dml))),
|
adamc@307
|
968 string ", paramValues, NULL, NULL, 0);"],
|
adamc@307
|
969 newline,
|
adamc@307
|
970 newline,
|
adamc@307
|
971
|
adamc@311
|
972 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
|
adamc@307
|
973 newline,
|
adamc@307
|
974 newline,
|
adamc@307
|
975
|
adamc@307
|
976 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@307
|
977 newline,
|
adamc@307
|
978 box [string "PQclear(res);",
|
adamc@307
|
979 newline,
|
adamc@311
|
980 string "uw_error(ctx, FATAL, \"",
|
adamc@307
|
981 string (ErrorMsg.spanToString loc),
|
adamc@307
|
982 string ": DML failed:\\n%s\\n%s\", ",
|
adamc@307
|
983 case prepared of
|
adamc@307
|
984 NONE => string "dml"
|
adamc@307
|
985 | SOME _ => p_exp env dml,
|
adamc@307
|
986 string ", PQerrorMessage(conn));",
|
adamc@307
|
987 newline],
|
adamc@307
|
988 string "}",
|
adamc@307
|
989 newline,
|
adamc@307
|
990 newline,
|
adamc@307
|
991
|
adamc@307
|
992 string "PQclear(res);",
|
adamc@307
|
993 newline,
|
adamc@337
|
994 string "uw_end_region(ctx);",
|
adamc@337
|
995 newline,
|
adamc@311
|
996 string "uw_unit_v;",
|
adamc@307
|
997 newline,
|
adamc@337
|
998 string "}))"]
|
adamc@307
|
999
|
adamc@338
|
1000 | ENextval {seq, prepared} =>
|
adamc@338
|
1001 let
|
adamc@338
|
1002 val query = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
|
adamc@338
|
1003 val query = (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), query]), loc)
|
adamc@338
|
1004 in
|
adamc@338
|
1005 box [string "(uw_begin_region(ctx), ",
|
adamc@338
|
1006 string "({",
|
adamc@338
|
1007 newline,
|
adamc@338
|
1008 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@338
|
1009 newline,
|
adamc@338
|
1010 case prepared of
|
adamc@338
|
1011 NONE => box [string "char *query = ",
|
adamc@338
|
1012 p_exp env query,
|
adamc@338
|
1013 string ";",
|
adamc@338
|
1014 newline]
|
adamc@338
|
1015 | SOME _ =>
|
adamc@338
|
1016 box [],
|
adamc@338
|
1017 newline,
|
adamc@338
|
1018 string "PGresult *res = ",
|
adamc@338
|
1019 case prepared of
|
adamc@338
|
1020 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
|
adamc@338
|
1021 | SOME n => box [string "PQexecPrepared(conn, \"uw",
|
adamc@338
|
1022 string (Int.toString n),
|
adamc@338
|
1023 string "\", 0, NULL, NULL, NULL, 0);"],
|
adamc@338
|
1024 newline,
|
adamc@338
|
1025 string "uw_Basis_int n;",
|
adamc@338
|
1026 newline,
|
adamc@338
|
1027 newline,
|
adamc@338
|
1028
|
adamc@338
|
1029 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
|
adamc@338
|
1030 newline,
|
adamc@338
|
1031 newline,
|
adamc@338
|
1032
|
adamc@338
|
1033 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@338
|
1034 newline,
|
adamc@338
|
1035 box [string "PQclear(res);",
|
adamc@338
|
1036 newline,
|
adamc@338
|
1037 string "uw_error(ctx, FATAL, \"",
|
adamc@338
|
1038 string (ErrorMsg.spanToString loc),
|
adamc@338
|
1039 string ": Query failed:\\n%s\\n%s\", ",
|
adamc@338
|
1040 case prepared of
|
adamc@338
|
1041 NONE => string "query"
|
adamc@338
|
1042 | SOME _ => p_exp env query,
|
adamc@338
|
1043 string ", PQerrorMessage(conn));",
|
adamc@338
|
1044 newline],
|
adamc@338
|
1045 string "}",
|
adamc@338
|
1046 newline,
|
adamc@338
|
1047 newline,
|
adamc@338
|
1048
|
adamc@338
|
1049 string "uw_end_region(ctx);",
|
adamc@338
|
1050 newline,
|
adamc@338
|
1051 string "n = PQntuples(res);",
|
adamc@338
|
1052 newline,
|
adamc@338
|
1053 string "if (n != 1) {",
|
adamc@338
|
1054 newline,
|
adamc@338
|
1055 box [string "PQclear(res);",
|
adamc@338
|
1056 newline,
|
adamc@338
|
1057 string "uw_error(ctx, FATAL, \"",
|
adamc@338
|
1058 string (ErrorMsg.spanToString loc),
|
adamc@338
|
1059 string ": Wrong number of result rows:\\n%s\\n%s\", ",
|
adamc@338
|
1060 case prepared of
|
adamc@338
|
1061 NONE => string "query"
|
adamc@338
|
1062 | SOME _ => p_exp env query,
|
adamc@338
|
1063 string ", PQerrorMessage(conn));",
|
adamc@338
|
1064 newline],
|
adamc@338
|
1065 string "}",
|
adamc@338
|
1066 newline,
|
adamc@338
|
1067 newline,
|
adamc@338
|
1068
|
adamc@338
|
1069 string "n = ",
|
adamc@338
|
1070 p_unsql true env (TFfi ("Basis", "int"), loc)
|
adamc@338
|
1071 (string "PQgetvalue(res, 0, 0)"),
|
adamc@338
|
1072 string ";",
|
adamc@338
|
1073 newline,
|
adamc@338
|
1074 string "PQclear(res);",
|
adamc@338
|
1075 newline,
|
adamc@338
|
1076 string "n;",
|
adamc@338
|
1077 newline,
|
adamc@338
|
1078 string "}))"]
|
adamc@338
|
1079 end
|
adamc@338
|
1080
|
adamc@29
|
1081 and p_exp env = p_exp' false env
|
adamc@29
|
1082
|
adamc@129
|
1083 fun p_fun env (fx, n, args, ran, e) =
|
adamc@129
|
1084 let
|
adamc@129
|
1085 val nargs = length args
|
adamc@129
|
1086 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
|
adamc@129
|
1087 in
|
adamc@129
|
1088 box [string "static",
|
adamc@129
|
1089 space,
|
adamc@129
|
1090 p_typ env ran,
|
adamc@129
|
1091 space,
|
adamc@316
|
1092 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@129
|
1093 string "(",
|
adamc@129
|
1094 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@311
|
1095 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
|
adamc@129
|
1096 box [p_typ env dom,
|
adamc@129
|
1097 space,
|
adamc@129
|
1098 p_rel env' (nargs - i - 1)]) args),
|
adamc@129
|
1099 string ")",
|
adamc@129
|
1100 space,
|
adamc@129
|
1101 string "{",
|
adamc@129
|
1102 newline,
|
adamc@129
|
1103 box[string "return(",
|
adamc@129
|
1104 p_exp env' e,
|
adamc@129
|
1105 string ");"],
|
adamc@129
|
1106 newline,
|
adamc@129
|
1107 string "}"]
|
adamc@129
|
1108 end
|
adamc@129
|
1109
|
adamc@129
|
1110 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@29
|
1111 case d of
|
adamc@29
|
1112 DStruct (n, xts) =>
|
adamc@196
|
1113 let
|
adamc@196
|
1114 val env = E.declBinds env dAll
|
adamc@196
|
1115 in
|
adamc@196
|
1116 box [string "struct",
|
adamc@196
|
1117 space,
|
adamc@311
|
1118 string ("__uws_" ^ Int.toString n),
|
adamc@196
|
1119 space,
|
adamc@196
|
1120 string "{",
|
adamc@196
|
1121 newline,
|
adamc@196
|
1122 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
|
adamc@196
|
1123 space,
|
adamc@311
|
1124 string "__uwf_",
|
adamc@316
|
1125 p_ident x,
|
adamc@196
|
1126 string ";",
|
adamc@196
|
1127 newline]) xts,
|
adamc@196
|
1128 string "};"]
|
adamc@196
|
1129 end
|
adamc@188
|
1130 | DDatatype (Enum, x, n, xncs) =>
|
adamc@188
|
1131 box [string "enum",
|
adamc@188
|
1132 space,
|
adamc@316
|
1133 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@188
|
1134 space,
|
adamc@188
|
1135 string "{",
|
adamc@188
|
1136 space,
|
adamc@316
|
1137 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adamc@316
|
1138 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
|
adamc@188
|
1139 space,
|
adamc@188
|
1140 string "};"]
|
adamc@198
|
1141 | DDatatype (Option, _, _, _) => box []
|
adamc@188
|
1142 | DDatatype (Default, x, n, xncs) =>
|
adamc@165
|
1143 let
|
adamc@165
|
1144 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
|
adamc@165
|
1145 | (x, n, SOME t) => SOME (x, n, t)) xncs
|
adamc@165
|
1146 in
|
adamc@165
|
1147 box [string "enum",
|
adamc@165
|
1148 space,
|
adamc@316
|
1149 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1150 space,
|
adamc@165
|
1151 string "{",
|
adamc@165
|
1152 space,
|
adamc@316
|
1153 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
|
adamc@316
|
1154 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
|
adamc@165
|
1155 space,
|
adamc@165
|
1156 string "};",
|
adamc@165
|
1157 newline,
|
adamc@165
|
1158 newline,
|
adamc@165
|
1159 string "struct",
|
adamc@165
|
1160 space,
|
adamc@316
|
1161 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1162 space,
|
adamc@165
|
1163 string "{",
|
adamc@165
|
1164 newline,
|
adamc@165
|
1165 string "enum",
|
adamc@165
|
1166 space,
|
adamc@316
|
1167 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@165
|
1168 space,
|
adamc@165
|
1169 string "tag;",
|
adamc@165
|
1170 newline,
|
adamc@165
|
1171 box (case xncsArgs of
|
adamc@165
|
1172 [] => []
|
adamc@165
|
1173 | _ => [string "union",
|
adamc@165
|
1174 space,
|
adamc@165
|
1175 string "{",
|
adamc@165
|
1176 newline,
|
adamc@165
|
1177 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
|
adamc@165
|
1178 space,
|
adamc@316
|
1179 string ("uw_" ^ ident x),
|
adamc@165
|
1180 string ";"]) xncsArgs,
|
adamc@165
|
1181 newline,
|
adamc@165
|
1182 string "}",
|
adamc@165
|
1183 space,
|
adamc@165
|
1184 string "data;",
|
adamc@165
|
1185 newline]),
|
adamc@165
|
1186 string "};"]
|
adamc@188
|
1187 end
|
adamc@29
|
1188
|
adamc@196
|
1189 | DDatatypeForward _ => box []
|
adamc@196
|
1190
|
adamc@29
|
1191 | DVal (x, n, t, e) =>
|
adamc@29
|
1192 box [p_typ env t,
|
adamc@29
|
1193 space,
|
adamc@316
|
1194 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
|
adamc@29
|
1195 space,
|
adamc@29
|
1196 string "=",
|
adamc@29
|
1197 space,
|
adamc@29
|
1198 p_exp env e,
|
adamc@29
|
1199 string ";"]
|
adamc@129
|
1200 | DFun vi => p_fun env vi
|
adamc@129
|
1201 | DFunRec vis =>
|
adamc@29
|
1202 let
|
adamc@129
|
1203 val env = E.declBinds env dAll
|
adamc@29
|
1204 in
|
adamc@129
|
1205 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
|
adamc@129
|
1206 box [string "static",
|
adamc@129
|
1207 space,
|
adamc@129
|
1208 p_typ env ran,
|
adamc@129
|
1209 space,
|
adamc@316
|
1210 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
|
adamc@311
|
1211 string "(uw_context,",
|
adamc@129
|
1212 space,
|
adamc@129
|
1213 p_list_sep (box [string ",", space])
|
adamc@129
|
1214 (fn (_, dom) => p_typ env dom) args,
|
adamc@129
|
1215 string ");"]) vis,
|
adamc@29
|
1216 newline,
|
adamc@129
|
1217 p_list_sep newline (p_fun env) vis,
|
adamc@129
|
1218 newline]
|
adamc@29
|
1219 end
|
adamc@273
|
1220 | DTable (x, _) => box [string "/* SQL table ",
|
adamc@273
|
1221 string x,
|
adamc@273
|
1222 string " */",
|
adamc@273
|
1223 newline]
|
adamc@338
|
1224 | DSequence x => box [string "/* SQL sequence ",
|
adamc@338
|
1225 string x,
|
adamc@338
|
1226 string " */",
|
adamc@338
|
1227 newline]
|
adamc@311
|
1228 | DDatabase s => box [string "static void uw_db_validate(uw_context);",
|
adamc@272
|
1229 newline,
|
adamc@311
|
1230 string "static void uw_db_prepare(uw_context);",
|
adamc@282
|
1231 newline,
|
adamc@275
|
1232 newline,
|
adamc@311
|
1233 string "void uw_db_init(uw_context ctx) {",
|
adamc@273
|
1234 newline,
|
adamc@272
|
1235 string "PGconn *conn = PQconnectdb(\"",
|
adamc@272
|
1236 string (String.toString s),
|
adamc@272
|
1237 string "\");",
|
adamc@272
|
1238 newline,
|
adamc@311
|
1239 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
1240 string "\"libpq can't allocate a connection.\");",
|
adamc@272
|
1241 newline,
|
adamc@272
|
1242 string "if (PQstatus(conn) != CONNECTION_OK) {",
|
adamc@272
|
1243 newline,
|
adamc@272
|
1244 box [string "char msg[1024];",
|
adamc@272
|
1245 newline,
|
adamc@272
|
1246 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@272
|
1247 newline,
|
adamc@272
|
1248 string "msg[1023] = 0;",
|
adamc@272
|
1249 newline,
|
adamc@272
|
1250 string "PQfinish(conn);",
|
adamc@272
|
1251 newline,
|
adamc@311
|
1252 string "uw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
1253 string "\"Connection to Postgres server failed: %s\", msg);"],
|
adamc@272
|
1254 newline,
|
adamc@272
|
1255 string "}",
|
adamc@272
|
1256 newline,
|
adamc@311
|
1257 string "uw_set_db(ctx, conn);",
|
adamc@272
|
1258 newline,
|
adamc@311
|
1259 string "uw_db_validate(ctx);",
|
adamc@275
|
1260 newline,
|
adamc@311
|
1261 string "uw_db_prepare(ctx);",
|
adamc@282
|
1262 newline,
|
adamc@272
|
1263 string "}",
|
adamc@272
|
1264 newline,
|
adamc@272
|
1265 newline,
|
adamc@311
|
1266 string "void uw_db_close(uw_context ctx) {",
|
adamc@272
|
1267 newline,
|
adamc@311
|
1268 string "PQfinish(uw_get_db(ctx));",
|
adamc@272
|
1269 newline,
|
adamc@272
|
1270 string "}",
|
adamc@272
|
1271 newline]
|
adamc@29
|
1272
|
adamc@282
|
1273 | DPreparedStatements ss =>
|
adamc@311
|
1274 box [string "static void uw_db_prepare(uw_context ctx) {",
|
adamc@282
|
1275 newline,
|
adamc@311
|
1276 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@282
|
1277 newline,
|
adamc@282
|
1278 string "PGresult *res;",
|
adamc@282
|
1279 newline,
|
adamc@282
|
1280 newline,
|
adamc@282
|
1281
|
adamc@282
|
1282 p_list_sepi newline (fn i => fn (s, n) =>
|
adamc@311
|
1283 box [string "res = PQprepare(conn, \"uw",
|
adamc@282
|
1284 string (Int.toString i),
|
adamc@282
|
1285 string "\", \"",
|
adamc@282
|
1286 string (String.toString s),
|
adamc@282
|
1287 string "\", ",
|
adamc@282
|
1288 string (Int.toString n),
|
adamc@282
|
1289 string ", NULL);",
|
adamc@282
|
1290 newline,
|
adamc@282
|
1291 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@282
|
1292 newline,
|
adamc@282
|
1293 box [string "char msg[1024];",
|
adamc@282
|
1294 newline,
|
adamc@282
|
1295 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@282
|
1296 newline,
|
adamc@282
|
1297 string "msg[1023] = 0;",
|
adamc@282
|
1298 newline,
|
adamc@282
|
1299 string "PQclear(res);",
|
adamc@282
|
1300 newline,
|
adamc@282
|
1301 string "PQfinish(conn);",
|
adamc@282
|
1302 newline,
|
adamc@311
|
1303 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
|
adamc@282
|
1304 string (String.toString s),
|
adamc@282
|
1305 string "\\n%s\", msg);",
|
adamc@282
|
1306 newline],
|
adamc@282
|
1307 string "}",
|
adamc@282
|
1308 newline,
|
adamc@282
|
1309 string "PQclear(res);",
|
adamc@282
|
1310 newline])
|
adamc@282
|
1311 ss,
|
adamc@282
|
1312
|
adamc@282
|
1313 string "}"]
|
adamc@282
|
1314
|
adamc@144
|
1315 datatype 'a search =
|
adamc@144
|
1316 Found of 'a
|
adamc@144
|
1317 | NotFound
|
adamc@144
|
1318 | Error
|
adamc@120
|
1319
|
adamc@275
|
1320 fun p_sqltype' env (tAll as (t, loc)) =
|
adamc@275
|
1321 case t of
|
adamc@275
|
1322 TFfi ("Basis", "int") => "int8"
|
adamc@275
|
1323 | TFfi ("Basis", "float") => "float8"
|
adamc@275
|
1324 | TFfi ("Basis", "string") => "text"
|
adamc@275
|
1325 | TFfi ("Basis", "bool") => "bool"
|
adamc@275
|
1326 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
|
adamc@275
|
1327 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@275
|
1328 "ERROR")
|
adamc@275
|
1329
|
adamc@275
|
1330 fun p_sqltype env t = string (p_sqltype' env t)
|
adamc@101
|
1331
|
adamc@101
|
1332 fun p_file env (ds, ps) =
|
adamc@29
|
1333 let
|
adamc@101
|
1334 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
1335 (p_decl env d,
|
adamc@31
|
1336 E.declBinds env d))
|
adamc@101
|
1337 env ds
|
adamc@144
|
1338
|
adamc@144
|
1339 val fields = foldl (fn ((ek, _, _, ts), fields) =>
|
adamc@144
|
1340 case ek of
|
adamc@144
|
1341 Core.Link => fields
|
adamc@144
|
1342 | Core.Action =>
|
adamc@280
|
1343 case List.nth (ts, length ts - 2) of
|
adamc@144
|
1344 (TRecord i, _) =>
|
adamc@144
|
1345 let
|
adamc@144
|
1346 val xts = E.lookupStruct env i
|
adamc@144
|
1347 val xtsSet = SS.addList (SS.empty, map #1 xts)
|
adamc@144
|
1348 in
|
adamc@144
|
1349 foldl (fn ((x, _), fields) =>
|
adamc@144
|
1350 let
|
adamc@144
|
1351 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
|
adamc@144
|
1352 in
|
adamc@144
|
1353 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
|
adamc@144
|
1354 xtsSet'))
|
adamc@144
|
1355 end) fields xts
|
adamc@144
|
1356 end
|
adamc@144
|
1357 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
|
adamc@144
|
1358 SM.empty ps
|
adamc@144
|
1359
|
adamc@144
|
1360 val fnums = SM.foldli (fn (x, xs, fnums) =>
|
adamc@144
|
1361 let
|
adamc@144
|
1362 val unusable = SS.foldl (fn (x', unusable) =>
|
adamc@144
|
1363 case SM.find (fnums, x') of
|
adamc@144
|
1364 NONE => unusable
|
adamc@144
|
1365 | SOME n => IS.add (unusable, n))
|
adamc@144
|
1366 IS.empty xs
|
adamc@144
|
1367
|
adamc@144
|
1368 fun findAvailable n =
|
adamc@144
|
1369 if IS.member (unusable, n) then
|
adamc@144
|
1370 findAvailable (n + 1)
|
adamc@144
|
1371 else
|
adamc@144
|
1372 n
|
adamc@144
|
1373 in
|
adamc@144
|
1374 SM.insert (fnums, x, findAvailable 0)
|
adamc@144
|
1375 end)
|
adamc@144
|
1376 SM.empty fields
|
adamc@144
|
1377
|
adamc@144
|
1378 fun makeSwitch (fnums, i) =
|
adamc@144
|
1379 case SM.foldl (fn (n, NotFound) => Found n
|
adamc@144
|
1380 | (n, Error) => Error
|
adamc@144
|
1381 | (n, Found n') => if n = n' then
|
adamc@144
|
1382 Found n'
|
adamc@144
|
1383 else
|
adamc@144
|
1384 Error) NotFound fnums of
|
adamc@144
|
1385 NotFound => box [string "return",
|
adamc@144
|
1386 space,
|
adamc@144
|
1387 string "-1;"]
|
adamc@144
|
1388 | Found n => box [string "return",
|
adamc@144
|
1389 space,
|
adamc@144
|
1390 string (Int.toString n),
|
adamc@144
|
1391 string ";"]
|
adamc@144
|
1392 | Error =>
|
adamc@144
|
1393 let
|
adamc@144
|
1394 val cmap = SM.foldli (fn (x, n, cmap) =>
|
adamc@144
|
1395 let
|
adamc@144
|
1396 val ch = if i < size x then
|
adamc@144
|
1397 String.sub (x, i)
|
adamc@144
|
1398 else
|
adamc@144
|
1399 chr 0
|
adamc@144
|
1400
|
adamc@144
|
1401 val fnums = case CM.find (cmap, ch) of
|
adamc@144
|
1402 NONE => SM.empty
|
adamc@144
|
1403 | SOME fnums => fnums
|
adamc@144
|
1404 val fnums = SM.insert (fnums, x, n)
|
adamc@144
|
1405 in
|
adamc@144
|
1406 CM.insert (cmap, ch, fnums)
|
adamc@144
|
1407 end)
|
adamc@144
|
1408 CM.empty fnums
|
adamc@144
|
1409
|
adamc@144
|
1410 val cmap = CM.listItemsi cmap
|
adamc@144
|
1411 in
|
adamc@144
|
1412 case cmap of
|
adamc@144
|
1413 [(_, fnums)] =>
|
adamc@144
|
1414 box [string "if",
|
adamc@144
|
1415 space,
|
adamc@144
|
1416 string "(name[",
|
adamc@144
|
1417 string (Int.toString i),
|
adamc@144
|
1418 string "]",
|
adamc@144
|
1419 space,
|
adamc@144
|
1420 string "==",
|
adamc@144
|
1421 space,
|
adamc@144
|
1422 string "0)",
|
adamc@144
|
1423 space,
|
adamc@144
|
1424 string "return",
|
adamc@144
|
1425 space,
|
adamc@144
|
1426 string "-1;",
|
adamc@144
|
1427 newline,
|
adamc@144
|
1428 makeSwitch (fnums, i+1)]
|
adamc@144
|
1429 | _ =>
|
adamc@144
|
1430 box [string "switch",
|
adamc@144
|
1431 space,
|
adamc@144
|
1432 string "(name[",
|
adamc@144
|
1433 string (Int.toString i),
|
adamc@144
|
1434 string "])",
|
adamc@144
|
1435 space,
|
adamc@144
|
1436 string "{",
|
adamc@144
|
1437 newline,
|
adamc@144
|
1438 box (map (fn (ch, fnums) =>
|
adamc@144
|
1439 box [string "case",
|
adamc@144
|
1440 space,
|
adamc@144
|
1441 if ch = chr 0 then
|
adamc@144
|
1442 string "0:"
|
adamc@144
|
1443 else
|
adamc@144
|
1444 box [string "'",
|
adamc@144
|
1445 string (Char.toString ch),
|
adamc@144
|
1446 string "':"],
|
adamc@144
|
1447 newline,
|
adamc@144
|
1448 makeSwitch (fnums, i+1),
|
adamc@144
|
1449 newline]) cmap),
|
adamc@144
|
1450 string "default:",
|
adamc@144
|
1451 newline,
|
adamc@144
|
1452 string "return",
|
adamc@144
|
1453 space,
|
adamc@144
|
1454 string "-1;",
|
adamc@144
|
1455 newline,
|
adamc@144
|
1456 string "}"]
|
adamc@144
|
1457 end
|
adamc@144
|
1458
|
adamc@186
|
1459 fun capitalize s =
|
adamc@186
|
1460 if s = "" then
|
adamc@186
|
1461 ""
|
adamc@186
|
1462 else
|
adamc@186
|
1463 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@186
|
1464
|
adamc@144
|
1465 fun unurlify (t, loc) =
|
adamc@398
|
1466 let
|
adamc@398
|
1467 fun unurlify' rf t =
|
adamc@398
|
1468 case t of
|
adamc@398
|
1469 TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
|
adamc@144
|
1470
|
adamc@398
|
1471 | TRecord 0 => string "uw_unit_v"
|
adamc@398
|
1472 | TRecord i =>
|
adamc@398
|
1473 let
|
adamc@398
|
1474 val xts = E.lookupStruct env i
|
adamc@398
|
1475 in
|
adamc@398
|
1476 box [string "({",
|
adamc@166
|
1477 newline,
|
adamc@398
|
1478 box (map (fn (x, t) =>
|
adamc@398
|
1479 box [p_typ env t,
|
adamc@398
|
1480 space,
|
adamc@398
|
1481 string "uwr_",
|
adamc@398
|
1482 string x,
|
adamc@398
|
1483 space,
|
adamc@398
|
1484 string "=",
|
adamc@398
|
1485 space,
|
adamc@398
|
1486 unurlify' rf (#1 t),
|
adamc@398
|
1487 string ";",
|
adamc@398
|
1488 newline]) xts),
|
adamc@167
|
1489 string "struct",
|
adamc@167
|
1490 space,
|
adamc@398
|
1491 string "__uws_",
|
adamc@398
|
1492 string (Int.toString i),
|
adamc@166
|
1493 space,
|
adamc@398
|
1494 string "tmp",
|
adamc@166
|
1495 space,
|
adamc@166
|
1496 string "=",
|
adamc@166
|
1497 space,
|
adamc@398
|
1498 string "{",
|
adamc@398
|
1499 space,
|
adamc@398
|
1500 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
|
adamc@398
|
1501 string x]) xts,
|
adamc@398
|
1502 space,
|
adamc@398
|
1503 string "};",
|
adamc@166
|
1504 newline,
|
adamc@181
|
1505 string "tmp;",
|
adamc@166
|
1506 newline,
|
adamc@398
|
1507 string "})"]
|
adamc@398
|
1508 end
|
adamc@166
|
1509
|
adamc@398
|
1510 | TDatatype (Enum, i, _) =>
|
adamc@398
|
1511 let
|
adamc@398
|
1512 val (x, xncs) = E.lookupDatatype env i
|
adamc@144
|
1513
|
adamc@398
|
1514 fun doEm xncs =
|
adamc@398
|
1515 case xncs of
|
adamc@398
|
1516 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), (enum __uwe_"
|
adamc@398
|
1517 ^ x ^ "_" ^ Int.toString i ^ ")0)")
|
adamc@398
|
1518 | (x', n, to) :: rest =>
|
adamc@398
|
1519 box [string "((!strncmp(request, \"",
|
adamc@398
|
1520 string x',
|
adamc@398
|
1521 string "\", ",
|
adamc@398
|
1522 string (Int.toString (size x')),
|
adamc@398
|
1523 string ") && (request[",
|
adamc@398
|
1524 string (Int.toString (size x')),
|
adamc@398
|
1525 string "] == 0 || request[",
|
adamc@398
|
1526 string (Int.toString (size x')),
|
adamc@398
|
1527 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
|
adamc@398
|
1528 space,
|
adamc@398
|
1529 string ":",
|
adamc@398
|
1530 space,
|
adamc@398
|
1531 doEm rest,
|
adamc@398
|
1532 string ")"]
|
adamc@398
|
1533 in
|
adamc@398
|
1534 doEm xncs
|
adamc@398
|
1535 end
|
adamc@398
|
1536
|
adamc@398
|
1537 | TDatatype (Option, i, xncs) =>
|
adamc@398
|
1538 if IS.member (rf, i) then
|
adamc@398
|
1539 box [string "unurlify_",
|
adamc@398
|
1540 string (Int.toString i),
|
adamc@398
|
1541 string "()"]
|
adamc@398
|
1542 else
|
adamc@398
|
1543 let
|
adamc@398
|
1544 val (x, _) = E.lookupDatatype env i
|
adamc@398
|
1545
|
adamc@398
|
1546 val (no_arg, has_arg, t) =
|
adamc@398
|
1547 case !xncs of
|
adamc@398
|
1548 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@398
|
1549 (no_arg, has_arg, t)
|
adamc@398
|
1550 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@398
|
1551 (no_arg, has_arg, t)
|
adamc@398
|
1552 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
|
adamc@398
|
1553
|
adamc@398
|
1554 val rf = IS.add (rf, i)
|
adamc@398
|
1555 in
|
adamc@398
|
1556 box [string "({",
|
adamc@398
|
1557 space,
|
adamc@398
|
1558 p_typ env t,
|
adamc@398
|
1559 space,
|
adamc@398
|
1560 string "*unurlify_",
|
adamc@398
|
1561 string (Int.toString i),
|
adamc@398
|
1562 string "(void) {",
|
adamc@398
|
1563 newline,
|
adamc@398
|
1564 box [string "return (request[0] == '/' ? ++request : request,",
|
adamc@398
|
1565 newline,
|
adamc@398
|
1566 string "((!strncmp(request, \"",
|
adamc@398
|
1567 string no_arg,
|
adamc@398
|
1568 string "\", ",
|
adamc@398
|
1569 string (Int.toString (size no_arg)),
|
adamc@398
|
1570 string ") && (request[",
|
adamc@398
|
1571 string (Int.toString (size no_arg)),
|
adamc@398
|
1572 string "] == 0 || request[",
|
adamc@398
|
1573 string (Int.toString (size no_arg)),
|
adamc@398
|
1574 string "] == '/')) ? (request",
|
adamc@398
|
1575 space,
|
adamc@398
|
1576 string "+=",
|
adamc@398
|
1577 space,
|
adamc@398
|
1578 string (Int.toString (size no_arg)),
|
adamc@398
|
1579 string ", NULL) : ((!strncmp(request, \"",
|
adamc@398
|
1580 string has_arg,
|
adamc@398
|
1581 string "\", ",
|
adamc@398
|
1582 string (Int.toString (size has_arg)),
|
adamc@398
|
1583 string ") && (request[",
|
adamc@398
|
1584 string (Int.toString (size has_arg)),
|
adamc@398
|
1585 string "] == 0 || request[",
|
adamc@398
|
1586 string (Int.toString (size has_arg)),
|
adamc@398
|
1587 string "] == '/')) ? (request",
|
adamc@398
|
1588 space,
|
adamc@398
|
1589 string "+=",
|
adamc@398
|
1590 space,
|
adamc@398
|
1591 string (Int.toString (size has_arg)),
|
adamc@398
|
1592 string ", (request[0] == '/' ? ++request : NULL), ",
|
adamc@398
|
1593 newline,
|
adamc@398
|
1594
|
adamc@398
|
1595 case #1 t of
|
adamc@398
|
1596 TDatatype _ => unurlify' rf (#1 t)
|
adamc@398
|
1597 | TFfi ("Basis", "string") => unurlify' rf (#1 t)
|
adamc@398
|
1598 | _ => box [string "({",
|
adamc@398
|
1599 newline,
|
adamc@398
|
1600 p_typ env t,
|
adamc@398
|
1601 space,
|
adamc@398
|
1602 string "*tmp",
|
adamc@398
|
1603 space,
|
adamc@398
|
1604 string "=",
|
adamc@398
|
1605 space,
|
adamc@398
|
1606 string "uw_malloc(ctx, sizeof(",
|
adamc@398
|
1607 p_typ env t,
|
adamc@398
|
1608 string "));",
|
adamc@398
|
1609 newline,
|
adamc@398
|
1610 string "*tmp",
|
adamc@398
|
1611 space,
|
adamc@398
|
1612 string "=",
|
adamc@398
|
1613 space,
|
adamc@398
|
1614 unurlify' rf (#1 t),
|
adamc@398
|
1615 string ";",
|
adamc@398
|
1616 newline,
|
adamc@398
|
1617 string "tmp;",
|
adamc@398
|
1618 newline,
|
adamc@398
|
1619 string "})"],
|
adamc@398
|
1620 string ")",
|
adamc@398
|
1621 newline,
|
adamc@398
|
1622 string ":",
|
adamc@398
|
1623 space,
|
adamc@398
|
1624 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
|
adamc@398
|
1625 ^ "\"), NULL))));"),
|
adamc@398
|
1626 newline],
|
adamc@398
|
1627 string "}",
|
adamc@398
|
1628 newline,
|
adamc@398
|
1629 newline,
|
adamc@398
|
1630
|
adamc@398
|
1631 string "unurlify_",
|
adamc@398
|
1632 string (Int.toString i),
|
adamc@398
|
1633 string "();",
|
adamc@398
|
1634 newline,
|
adamc@398
|
1635 string "})"]
|
adamc@398
|
1636 end
|
adamc@398
|
1637
|
adamc@398
|
1638 | TDatatype (Default, i, _) =>
|
adamc@398
|
1639 let
|
adamc@398
|
1640 val (x, xncs) = E.lookupDatatype env i
|
adamc@398
|
1641
|
adamc@398
|
1642 fun doEm xncs =
|
adamc@398
|
1643 case xncs of
|
adamc@398
|
1644 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL)")
|
adamc@398
|
1645 | (x', n, to) :: rest =>
|
adamc@398
|
1646 box [string "((!strncmp(request, \"",
|
adamc@398
|
1647 string x',
|
adamc@398
|
1648 string "\", ",
|
adamc@398
|
1649 string (Int.toString (size x')),
|
adamc@398
|
1650 string ") && (request[",
|
adamc@398
|
1651 string (Int.toString (size x')),
|
adamc@398
|
1652 string "] == 0 || request[",
|
adamc@398
|
1653 string (Int.toString (size x')),
|
adamc@398
|
1654 string "] == '/')) ? ({",
|
adamc@398
|
1655 newline,
|
adamc@398
|
1656 string "struct",
|
adamc@398
|
1657 space,
|
adamc@398
|
1658 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
|
adamc@398
|
1659 space,
|
adamc@398
|
1660 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
|
adamc@398
|
1661 string x,
|
adamc@398
|
1662 string "_",
|
adamc@398
|
1663 string (Int.toString i),
|
adamc@398
|
1664 string "));",
|
adamc@398
|
1665 newline,
|
adamc@398
|
1666 string "tmp->tag",
|
adamc@398
|
1667 space,
|
adamc@398
|
1668 string "=",
|
adamc@398
|
1669 space,
|
adamc@398
|
1670 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
|
adamc@398
|
1671 string ";",
|
adamc@398
|
1672 newline,
|
adamc@398
|
1673 string "request",
|
adamc@398
|
1674 space,
|
adamc@398
|
1675 string "+=",
|
adamc@398
|
1676 space,
|
adamc@398
|
1677 string (Int.toString (size x')),
|
adamc@398
|
1678 string ";",
|
adamc@398
|
1679 newline,
|
adamc@398
|
1680 string "if (request[0] == '/') ++request;",
|
adamc@398
|
1681 newline,
|
adamc@398
|
1682 case to of
|
adamc@398
|
1683 NONE => box []
|
adamc@398
|
1684 | SOME (t, _) => box [string "tmp->data.uw_",
|
adamc@398
|
1685 p_ident x',
|
adamc@398
|
1686 space,
|
adamc@398
|
1687 string "=",
|
adamc@398
|
1688 space,
|
adamc@398
|
1689 unurlify' rf t,
|
adamc@398
|
1690 string ";",
|
adamc@398
|
1691 newline],
|
adamc@398
|
1692 string "tmp;",
|
adamc@398
|
1693 newline,
|
adamc@398
|
1694 string "})",
|
adamc@398
|
1695 space,
|
adamc@398
|
1696 string ":",
|
adamc@398
|
1697 space,
|
adamc@398
|
1698 doEm rest,
|
adamc@398
|
1699 string ")"]
|
adamc@398
|
1700 in
|
adamc@398
|
1701 doEm xncs
|
adamc@398
|
1702 end
|
adamc@398
|
1703
|
adamc@398
|
1704 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
|
adamc@398
|
1705 space)
|
adamc@398
|
1706 in
|
adamc@398
|
1707 unurlify' IS.empty t
|
adamc@398
|
1708 end
|
adamc@144
|
1709
|
adamc@144
|
1710 fun p_page (ek, s, n, ts) =
|
adamc@144
|
1711 let
|
adamc@144
|
1712 val (ts, defInputs, inputsVar) =
|
adamc@144
|
1713 case ek of
|
adamc@314
|
1714 Core.Link => (List.take (ts, length ts - 1), string "", string "")
|
adamc@144
|
1715 | Core.Action =>
|
adamc@280
|
1716 case List.nth (ts, length ts - 2) of
|
adamc@144
|
1717 (TRecord i, _) =>
|
adamc@144
|
1718 let
|
adamc@144
|
1719 val xts = E.lookupStruct env i
|
adamc@144
|
1720 in
|
adamc@280
|
1721 (List.take (ts, length ts - 2),
|
adamc@144
|
1722 box [box (map (fn (x, t) => box [p_typ env t,
|
adamc@144
|
1723 space,
|
adamc@311
|
1724 string "uw_input_",
|
adamc@316
|
1725 p_ident x,
|
adamc@144
|
1726 string ";",
|
adamc@144
|
1727 newline]) xts),
|
adamc@144
|
1728 newline,
|
adamc@144
|
1729 box (map (fn (x, t) =>
|
adamc@144
|
1730 let
|
adamc@144
|
1731 val n = case SM.find (fnums, x) of
|
adamc@144
|
1732 NONE => raise Fail "CjrPrint: Can't find in fnums"
|
adamc@144
|
1733 | SOME n => n
|
adamc@190
|
1734
|
adamc@190
|
1735 val f = case t of
|
adamc@190
|
1736 (TFfi ("Basis", "bool"), _) => "optional_"
|
adamc@190
|
1737 | _ => ""
|
adamc@144
|
1738 in
|
adamc@311
|
1739 box [string "request = uw_get_",
|
adamc@190
|
1740 string f,
|
adamc@190
|
1741 string "input(ctx, ",
|
adamc@144
|
1742 string (Int.toString n),
|
adamc@144
|
1743 string ");",
|
adamc@144
|
1744 newline,
|
adamc@144
|
1745 string "if (request == NULL) {",
|
adamc@144
|
1746 newline,
|
adamc@144
|
1747 box [string "printf(\"Missing input ",
|
adamc@144
|
1748 string x,
|
adamc@144
|
1749 string "\\n\");",
|
adamc@144
|
1750 newline,
|
adamc@144
|
1751 string "exit(1);"],
|
adamc@144
|
1752 newline,
|
adamc@144
|
1753 string "}",
|
adamc@144
|
1754 newline,
|
adamc@311
|
1755 string "uw_input_",
|
adamc@316
|
1756 p_ident x,
|
adamc@144
|
1757 space,
|
adamc@144
|
1758 string "=",
|
adamc@144
|
1759 space,
|
adamc@144
|
1760 unurlify t,
|
adamc@144
|
1761 string ";",
|
adamc@144
|
1762 newline]
|
adamc@144
|
1763 end) xts),
|
adamc@311
|
1764 string "struct __uws_",
|
adamc@144
|
1765 string (Int.toString i),
|
adamc@144
|
1766 space,
|
adamc@311
|
1767 string "uw_inputs",
|
adamc@144
|
1768 space,
|
adamc@144
|
1769 string "= {",
|
adamc@144
|
1770 newline,
|
adamc@311
|
1771 box (map (fn (x, _) => box [string "uw_input_",
|
adamc@316
|
1772 p_ident x,
|
adamc@144
|
1773 string ",",
|
adamc@144
|
1774 newline]) xts),
|
adamc@144
|
1775 string "};",
|
adamc@144
|
1776 newline],
|
adamc@144
|
1777 box [string ",",
|
adamc@144
|
1778 space,
|
adamc@311
|
1779 string "uw_inputs"])
|
adamc@144
|
1780 end
|
adamc@144
|
1781
|
adamc@144
|
1782 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
|
adamc@144
|
1783 in
|
adamc@144
|
1784 box [string "if (!strncmp(request, \"",
|
adamc@144
|
1785 string (String.toString s),
|
adamc@144
|
1786 string "\", ",
|
adamc@144
|
1787 string (Int.toString (size s)),
|
adamc@198
|
1788 string ") && (request[",
|
adamc@198
|
1789 string (Int.toString (size s)),
|
adamc@198
|
1790 string "] == 0 || request[",
|
adamc@198
|
1791 string (Int.toString (size s)),
|
adamc@198
|
1792 string "] == '/')) {",
|
adamc@144
|
1793 newline,
|
adamc@144
|
1794 string "request += ",
|
adamc@144
|
1795 string (Int.toString (size s)),
|
adamc@144
|
1796 string ";",
|
adamc@144
|
1797 newline,
|
adamc@144
|
1798 string "if (*request == '/') ++request;",
|
adamc@144
|
1799 newline,
|
adamc@144
|
1800 box [string "{",
|
adamc@144
|
1801 newline,
|
adamc@144
|
1802 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
|
adamc@144
|
1803 space,
|
adamc@144
|
1804 string "arg",
|
adamc@144
|
1805 string (Int.toString i),
|
adamc@144
|
1806 space,
|
adamc@144
|
1807 string "=",
|
adamc@144
|
1808 space,
|
adamc@144
|
1809 unurlify t,
|
adamc@144
|
1810 string ";",
|
adamc@144
|
1811 newline]) ts),
|
adamc@144
|
1812 defInputs,
|
adamc@144
|
1813 p_enamed env n,
|
adamc@144
|
1814 string "(",
|
adamc@144
|
1815 p_list_sep (box [string ",", space])
|
adamc@144
|
1816 (fn x => x)
|
adamc@272
|
1817 (string "ctx"
|
adamc@280
|
1818 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
|
adamc@144
|
1819 inputsVar,
|
adamc@311
|
1820 string ", uw_unit_v);",
|
adamc@144
|
1821 newline,
|
adamc@144
|
1822 string "return;",
|
adamc@144
|
1823 newline,
|
adamc@144
|
1824 string "}",
|
adamc@144
|
1825 newline,
|
adamc@144
|
1826 string "}"]
|
adamc@144
|
1827 ]
|
adamc@144
|
1828 end
|
adamc@144
|
1829
|
adamc@144
|
1830 val pds' = map p_page ps
|
adamc@275
|
1831
|
adamc@275
|
1832 val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts)
|
adamc@275
|
1833 | _ => NONE) ds
|
adamc@377
|
1834 val sequences = List.mapPartial (fn (DSequence s, _) => SOME s
|
adamc@377
|
1835 | _ => NONE) ds
|
adamc@275
|
1836
|
adamc@275
|
1837 val validate =
|
adamc@311
|
1838 box [string "static void uw_db_validate(uw_context ctx) {",
|
adamc@275
|
1839 newline,
|
adamc@311
|
1840 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@275
|
1841 newline,
|
adamc@275
|
1842 string "PGresult *res;",
|
adamc@275
|
1843 newline,
|
adamc@275
|
1844 newline,
|
adamc@275
|
1845 p_list_sep newline
|
adamc@275
|
1846 (fn (s, xts) =>
|
adamc@275
|
1847 let
|
adamc@377
|
1848 val sl = CharVector.map Char.toLower s
|
adamc@377
|
1849
|
adamc@275
|
1850 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
|
adamc@377
|
1851 ^ sl ^ "'"
|
adamc@275
|
1852
|
adamc@275
|
1853 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@377
|
1854 sl,
|
adamc@275
|
1855 "') AND (",
|
adamc@275
|
1856 String.concatWith " OR "
|
adamc@275
|
1857 (map (fn (x, t) =>
|
adamc@311
|
1858 String.concat ["(attname = 'uw_",
|
adamc@275
|
1859 CharVector.map
|
adamc@316
|
1860 Char.toLower (ident x),
|
adamc@275
|
1861 "' AND atttypid = (SELECT oid FROM pg_type",
|
adamc@275
|
1862 " WHERE typname = '",
|
adamc@275
|
1863 p_sqltype' env t,
|
adamc@275
|
1864 "'))"]) xts),
|
adamc@275
|
1865 ")"]
|
adamc@275
|
1866
|
adamc@275
|
1867 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@377
|
1868 sl,
|
adamc@325
|
1869 "') AND attname LIKE 'uw_%'"]
|
adamc@275
|
1870 in
|
adamc@275
|
1871 box [string "res = PQexec(conn, \"",
|
adamc@275
|
1872 string q,
|
adamc@275
|
1873 string "\");",
|
adamc@275
|
1874 newline,
|
adamc@275
|
1875 newline,
|
adamc@275
|
1876 string "if (res == NULL) {",
|
adamc@275
|
1877 newline,
|
adamc@275
|
1878 box [string "PQfinish(conn);",
|
adamc@275
|
1879 newline,
|
adamc@311
|
1880 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1881 newline],
|
adamc@275
|
1882 string "}",
|
adamc@275
|
1883 newline,
|
adamc@275
|
1884 newline,
|
adamc@275
|
1885 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1886 newline,
|
adamc@275
|
1887 box [string "char msg[1024];",
|
adamc@275
|
1888 newline,
|
adamc@275
|
1889 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1890 newline,
|
adamc@275
|
1891 string "msg[1023] = 0;",
|
adamc@275
|
1892 newline,
|
adamc@275
|
1893 string "PQclear(res);",
|
adamc@275
|
1894 newline,
|
adamc@275
|
1895 string "PQfinish(conn);",
|
adamc@275
|
1896 newline,
|
adamc@311
|
1897 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1898 string q,
|
adamc@275
|
1899 string "\\n%s\", msg);",
|
adamc@275
|
1900 newline],
|
adamc@275
|
1901 string "}",
|
adamc@275
|
1902 newline,
|
adamc@275
|
1903 newline,
|
adamc@275
|
1904 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@275
|
1905 newline,
|
adamc@275
|
1906 box [string "PQclear(res);",
|
adamc@275
|
1907 newline,
|
adamc@275
|
1908 string "PQfinish(conn);",
|
adamc@275
|
1909 newline,
|
adamc@311
|
1910 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1911 string s,
|
adamc@275
|
1912 string "' does not exist.\");",
|
adamc@275
|
1913 newline],
|
adamc@275
|
1914 string "}",
|
adamc@275
|
1915 newline,
|
adamc@275
|
1916 newline,
|
adamc@275
|
1917 string "PQclear(res);",
|
adamc@275
|
1918 newline,
|
adamc@275
|
1919
|
adamc@275
|
1920 string "res = PQexec(conn, \"",
|
adamc@275
|
1921 string q',
|
adamc@275
|
1922 string "\");",
|
adamc@275
|
1923 newline,
|
adamc@275
|
1924 newline,
|
adamc@275
|
1925 string "if (res == NULL) {",
|
adamc@275
|
1926 newline,
|
adamc@275
|
1927 box [string "PQfinish(conn);",
|
adamc@275
|
1928 newline,
|
adamc@311
|
1929 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1930 newline],
|
adamc@275
|
1931 string "}",
|
adamc@275
|
1932 newline,
|
adamc@275
|
1933 newline,
|
adamc@275
|
1934 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1935 newline,
|
adamc@275
|
1936 box [string "char msg[1024];",
|
adamc@275
|
1937 newline,
|
adamc@275
|
1938 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1939 newline,
|
adamc@275
|
1940 string "msg[1023] = 0;",
|
adamc@275
|
1941 newline,
|
adamc@275
|
1942 string "PQclear(res);",
|
adamc@275
|
1943 newline,
|
adamc@275
|
1944 string "PQfinish(conn);",
|
adamc@275
|
1945 newline,
|
adamc@311
|
1946 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1947 string q',
|
adamc@275
|
1948 string "\\n%s\", msg);",
|
adamc@275
|
1949 newline],
|
adamc@275
|
1950 string "}",
|
adamc@275
|
1951 newline,
|
adamc@275
|
1952 newline,
|
adamc@275
|
1953 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
1954 string (Int.toString (length xts)),
|
adamc@275
|
1955 string "\")) {",
|
adamc@275
|
1956 newline,
|
adamc@275
|
1957 box [string "PQclear(res);",
|
adamc@275
|
1958 newline,
|
adamc@275
|
1959 string "PQfinish(conn);",
|
adamc@275
|
1960 newline,
|
adamc@311
|
1961 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1962 string s,
|
adamc@275
|
1963 string "' has the wrong column types.\");",
|
adamc@275
|
1964 newline],
|
adamc@275
|
1965 string "}",
|
adamc@275
|
1966 newline,
|
adamc@275
|
1967 newline,
|
adamc@275
|
1968 string "PQclear(res);",
|
adamc@275
|
1969 newline,
|
adamc@275
|
1970 newline,
|
adamc@275
|
1971
|
adamc@275
|
1972 string "res = PQexec(conn, \"",
|
adamc@275
|
1973 string q'',
|
adamc@275
|
1974 string "\");",
|
adamc@275
|
1975 newline,
|
adamc@275
|
1976 newline,
|
adamc@275
|
1977 string "if (res == NULL) {",
|
adamc@275
|
1978 newline,
|
adamc@275
|
1979 box [string "PQfinish(conn);",
|
adamc@275
|
1980 newline,
|
adamc@311
|
1981 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1982 newline],
|
adamc@275
|
1983 string "}",
|
adamc@275
|
1984 newline,
|
adamc@275
|
1985 newline,
|
adamc@275
|
1986 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1987 newline,
|
adamc@275
|
1988 box [string "char msg[1024];",
|
adamc@275
|
1989 newline,
|
adamc@275
|
1990 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1991 newline,
|
adamc@275
|
1992 string "msg[1023] = 0;",
|
adamc@275
|
1993 newline,
|
adamc@275
|
1994 string "PQclear(res);",
|
adamc@275
|
1995 newline,
|
adamc@275
|
1996 string "PQfinish(conn);",
|
adamc@275
|
1997 newline,
|
adamc@311
|
1998 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1999 string q'',
|
adamc@275
|
2000 string "\\n%s\", msg);",
|
adamc@275
|
2001 newline],
|
adamc@275
|
2002 string "}",
|
adamc@275
|
2003 newline,
|
adamc@275
|
2004 newline,
|
adamc@275
|
2005 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
2006 string (Int.toString (length xts)),
|
adamc@275
|
2007 string "\")) {",
|
adamc@275
|
2008 newline,
|
adamc@275
|
2009 box [string "PQclear(res);",
|
adamc@275
|
2010 newline,
|
adamc@275
|
2011 string "PQfinish(conn);",
|
adamc@275
|
2012 newline,
|
adamc@311
|
2013 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
2014 string s,
|
adamc@275
|
2015 string "' has extra columns.\");",
|
adamc@275
|
2016 newline],
|
adamc@275
|
2017 string "}",
|
adamc@275
|
2018 newline,
|
adamc@275
|
2019 newline,
|
adamc@275
|
2020 string "PQclear(res);",
|
adamc@275
|
2021 newline]
|
adamc@275
|
2022 end) tables,
|
adamc@377
|
2023
|
adamc@377
|
2024 p_list_sep newline
|
adamc@377
|
2025 (fn s =>
|
adamc@377
|
2026 let
|
adamc@377
|
2027 val sl = CharVector.map Char.toLower s
|
adamc@377
|
2028
|
adamc@377
|
2029 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
|
adamc@377
|
2030 ^ sl ^ "' AND relkind = 'S'"
|
adamc@377
|
2031 in
|
adamc@377
|
2032 box [string "res = PQexec(conn, \"",
|
adamc@377
|
2033 string q,
|
adamc@377
|
2034 string "\");",
|
adamc@377
|
2035 newline,
|
adamc@377
|
2036 newline,
|
adamc@377
|
2037 string "if (res == NULL) {",
|
adamc@377
|
2038 newline,
|
adamc@377
|
2039 box [string "PQfinish(conn);",
|
adamc@377
|
2040 newline,
|
adamc@377
|
2041 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@377
|
2042 newline],
|
adamc@377
|
2043 string "}",
|
adamc@377
|
2044 newline,
|
adamc@377
|
2045 newline,
|
adamc@377
|
2046 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@377
|
2047 newline,
|
adamc@377
|
2048 box [string "char msg[1024];",
|
adamc@377
|
2049 newline,
|
adamc@377
|
2050 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@377
|
2051 newline,
|
adamc@377
|
2052 string "msg[1023] = 0;",
|
adamc@377
|
2053 newline,
|
adamc@377
|
2054 string "PQclear(res);",
|
adamc@377
|
2055 newline,
|
adamc@377
|
2056 string "PQfinish(conn);",
|
adamc@377
|
2057 newline,
|
adamc@377
|
2058 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@377
|
2059 string q,
|
adamc@377
|
2060 string "\\n%s\", msg);",
|
adamc@377
|
2061 newline],
|
adamc@377
|
2062 string "}",
|
adamc@377
|
2063 newline,
|
adamc@377
|
2064 newline,
|
adamc@377
|
2065 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@377
|
2066 newline,
|
adamc@377
|
2067 box [string "PQclear(res);",
|
adamc@377
|
2068 newline,
|
adamc@377
|
2069 string "PQfinish(conn);",
|
adamc@377
|
2070 newline,
|
adamc@377
|
2071 string "uw_error(ctx, FATAL, \"Sequence '",
|
adamc@377
|
2072 string s,
|
adamc@377
|
2073 string "' does not exist.\");",
|
adamc@377
|
2074 newline],
|
adamc@377
|
2075 string "}",
|
adamc@377
|
2076 newline,
|
adamc@377
|
2077 newline,
|
adamc@377
|
2078 string "PQclear(res);",
|
adamc@377
|
2079 newline]
|
adamc@377
|
2080 end) sequences,
|
adamc@377
|
2081
|
adamc@275
|
2082 string "}"]
|
adamc@29
|
2083 in
|
adamc@144
|
2084 box [string "#include <stdio.h>",
|
adamc@144
|
2085 newline,
|
adamc@144
|
2086 string "#include <stdlib.h>",
|
adamc@144
|
2087 newline,
|
adamc@272
|
2088 string "#include <string.h>",
|
adamc@272
|
2089 newline,
|
adamc@390
|
2090 string "#include <math.h>",
|
adamc@390
|
2091 newline,
|
adamc@272
|
2092 string "#include <postgresql/libpq-fe.h>",
|
adamc@272
|
2093 newline,
|
adamc@144
|
2094 newline,
|
adamc@378
|
2095 string "#include \"",
|
adamc@378
|
2096 string (OS.Path.joinDirFile {dir = Config.includ,
|
adamc@378
|
2097 file = "urweb.h"}),
|
adamc@378
|
2098 string "\"",
|
adamc@101
|
2099 newline,
|
adamc@101
|
2100 newline,
|
adamc@101
|
2101 p_list_sep newline (fn x => x) pds,
|
adamc@101
|
2102 newline,
|
adamc@311
|
2103 string "int uw_inputs_len = ",
|
adamc@144
|
2104 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
|
adamc@144
|
2105 string ";",
|
adamc@144
|
2106 newline,
|
adamc@144
|
2107 newline,
|
adamc@311
|
2108 string "int uw_input_num(char *name) {",
|
adamc@144
|
2109 newline,
|
adamc@144
|
2110 makeSwitch (fnums, 0),
|
adamc@144
|
2111 string "}",
|
adamc@144
|
2112 newline,
|
adamc@144
|
2113 newline,
|
adamc@311
|
2114 string "void uw_handle(uw_context ctx, char *request) {",
|
adamc@101
|
2115 newline,
|
adamc@101
|
2116 p_list_sep newline (fn x => x) pds',
|
adamc@101
|
2117 newline,
|
adamc@387
|
2118 string "uw_error(ctx, FATAL, \"Unknown page\");",
|
adamc@387
|
2119 newline,
|
adamc@101
|
2120 string "}",
|
adamc@275
|
2121 newline,
|
adamc@275
|
2122 newline,
|
adamc@275
|
2123 validate,
|
adamc@376
|
2124 newline,
|
adamc@376
|
2125 if List.exists (fn (DDatabase _, _) => true | _ => false) ds then
|
adamc@376
|
2126 box []
|
adamc@376
|
2127 else
|
adamc@376
|
2128 box [newline,
|
adamc@376
|
2129 string "void uw_db_init(uw_context ctx) { };",
|
adamc@376
|
2130 newline]]
|
adamc@29
|
2131 end
|
adamc@29
|
2132
|
adamc@274
|
2133 fun p_sql env (ds, _) =
|
adamc@274
|
2134 let
|
adamc@274
|
2135 val (pps, _) = ListUtil.foldlMap
|
adamc@274
|
2136 (fn (dAll as (d, _), env) =>
|
adamc@274
|
2137 let
|
adamc@274
|
2138 val pp = case d of
|
adamc@274
|
2139 DTable (s, xts) =>
|
adamc@274
|
2140 box [string "CREATE TABLE ",
|
adamc@274
|
2141 string s,
|
adamc@274
|
2142 string "(",
|
adamc@274
|
2143 p_list (fn (x, t) =>
|
adamc@311
|
2144 box [string "uw_",
|
adamc@275
|
2145 string (CharVector.map Char.toLower x),
|
adamc@274
|
2146 space,
|
adamc@274
|
2147 p_sqltype env t,
|
adamc@274
|
2148 space,
|
adamc@274
|
2149 string "NOT",
|
adamc@274
|
2150 space,
|
adamc@274
|
2151 string "NULL"]) xts,
|
adamc@274
|
2152 string ");",
|
adamc@274
|
2153 newline,
|
adamc@274
|
2154 newline]
|
adamc@338
|
2155 | DSequence s =>
|
adamc@338
|
2156 box [string "CREATE SEQUENCE ",
|
adamc@338
|
2157 string s,
|
adamc@338
|
2158 string ";",
|
adamc@338
|
2159 newline,
|
adamc@338
|
2160 newline]
|
adamc@274
|
2161 | _ => box []
|
adamc@274
|
2162 in
|
adamc@274
|
2163 (pp, E.declBinds env dAll)
|
adamc@274
|
2164 end)
|
adamc@274
|
2165 env ds
|
adamc@274
|
2166 in
|
adamc@274
|
2167 box pps
|
adamc@274
|
2168 end
|
adamc@274
|
2169
|
adamc@29
|
2170 end
|