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