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@29
|
60 fun p_typ' par env (t, loc) =
|
adamc@29
|
61 case t of
|
adamc@269
|
62 TFun (t1, t2) => parenIf par (box [p_typ' true env t2,
|
adamc@109
|
63 space,
|
adamc@109
|
64 string "(*)",
|
adamc@109
|
65 space,
|
adamc@109
|
66 string "(",
|
adamc@109
|
67 p_typ env t1,
|
adamc@109
|
68 string ")"])
|
adamc@29
|
69 | TRecord i => box [string "struct",
|
adamc@29
|
70 space,
|
adamc@29
|
71 string "__lws_",
|
adamc@29
|
72 string (Int.toString i)]
|
adamc@188
|
73 | TDatatype (Enum, n, _) =>
|
adamc@188
|
74 (box [string "enum",
|
adamc@188
|
75 space,
|
adamc@188
|
76 string ("__lwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
|
adamc@188
|
77 handle CjrEnv.UnboundNamed _ => string ("__lwd_UNBOUND__" ^ Int.toString n))
|
adamc@198
|
78 | TDatatype (Option, n, xncs) =>
|
adamc@198
|
79 (case ListUtil.search #3 (!xncs) of
|
adamc@198
|
80 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
|
adamc@198
|
81 | SOME t =>
|
adamc@198
|
82 case #1 t of
|
adamc@198
|
83 TDatatype _ => p_typ' par env t
|
adamc@199
|
84 | TFfi ("Basis", "string") => p_typ' par env t
|
adamc@198
|
85 | _ => box [p_typ' par env t,
|
adamc@198
|
86 string "*"])
|
adamc@188
|
87 | TDatatype (Default, n, _) =>
|
adamc@165
|
88 (box [string "struct",
|
adamc@165
|
89 space,
|
adamc@166
|
90 string ("__lwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
|
adamc@166
|
91 handle CjrEnv.UnboundNamed _ => string ("__lwd_UNBOUND__" ^ Int.toString n))
|
adamc@53
|
92 | TFfi (m, x) => box [string "lw_", string m, string "_", string x]
|
adamc@29
|
93
|
adamc@29
|
94 and p_typ env = p_typ' false env
|
adamc@29
|
95
|
adamc@29
|
96 fun p_rel env n = string ("__lwr_" ^ #1 (E.lookupERel env n) ^ "_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@29
|
97 handle CjrEnv.UnboundRel _ => string ("__lwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
|
adamc@29
|
98
|
adamc@109
|
99 fun p_enamed env n =
|
adamc@109
|
100 string ("__lwn_" ^ #1 (E.lookupENamed env n) ^ "_" ^ Int.toString n)
|
adamc@109
|
101 handle CjrEnv.UnboundNamed _ => string ("__lwn_UNBOUND_" ^ Int.toString n)
|
adamc@109
|
102
|
adamc@182
|
103 fun p_con_named env n =
|
adamc@182
|
104 string ("__lwc_" ^ #1 (E.lookupConstructor env n) ^ "_" ^ Int.toString n)
|
adamc@182
|
105 handle CjrEnv.UnboundNamed _ => string ("__lwc_UNBOUND_" ^ Int.toString n)
|
adamc@182
|
106
|
adamc@182
|
107 fun p_pat_preamble env (p, _) =
|
adamc@182
|
108 case p of
|
adamc@182
|
109 PWild => (box [],
|
adamc@182
|
110 env)
|
adamc@182
|
111 | PVar (x, t) => (box [p_typ env t,
|
adamc@182
|
112 space,
|
adamc@182
|
113 string "__lwr_",
|
adamc@182
|
114 string x,
|
adamc@182
|
115 string "_",
|
adamc@182
|
116 string (Int.toString (E.countERels env)),
|
adamc@182
|
117 string ";",
|
adamc@182
|
118 newline],
|
adamc@196
|
119 E.pushERel env x t)
|
adamc@182
|
120 | PPrim _ => (box [], env)
|
adamc@188
|
121 | PCon (_, _, NONE) => (box [], env)
|
adamc@188
|
122 | PCon (_, _, SOME p) => p_pat_preamble env p
|
adamc@182
|
123 | PRecord xps =>
|
adamc@182
|
124 foldl (fn ((_, p, _), (pp, env)) =>
|
adamc@182
|
125 let
|
adamc@182
|
126 val (pp', env) = p_pat_preamble env p
|
adamc@182
|
127 in
|
adamc@182
|
128 (box [pp', pp], env)
|
adamc@182
|
129 end) (box [], env) xps
|
adamc@182
|
130
|
adamc@182
|
131 fun p_patCon env pc =
|
adamc@182
|
132 case pc of
|
adamc@182
|
133 PConVar n => p_con_named env n
|
adamc@186
|
134 | PConFfi {mod = m, con, ...} => string ("lw_" ^ m ^ "_" ^ con)
|
adamc@182
|
135
|
adamc@182
|
136 fun p_pat (env, exit, depth) (p, _) =
|
adamc@182
|
137 case p of
|
adamc@182
|
138 PWild =>
|
adamc@182
|
139 (box [], env)
|
adamc@182
|
140 | PVar (x, t) =>
|
adamc@182
|
141 (box [string "__lwr_",
|
adamc@182
|
142 string x,
|
adamc@182
|
143 string "_",
|
adamc@182
|
144 string (Int.toString (E.countERels env)),
|
adamc@182
|
145 space,
|
adamc@182
|
146 string "=",
|
adamc@182
|
147 space,
|
adamc@182
|
148 string "disc",
|
adamc@182
|
149 string (Int.toString depth),
|
adamc@182
|
150 string ";"],
|
adamc@182
|
151 E.pushERel env x t)
|
adamc@182
|
152 | PPrim (Prim.Int n) =>
|
adamc@182
|
153 (box [string "if",
|
adamc@182
|
154 space,
|
adamc@182
|
155 string "(disc",
|
adamc@182
|
156 string (Int.toString depth),
|
adamc@182
|
157 space,
|
adamc@182
|
158 string "!=",
|
adamc@182
|
159 space,
|
adamc@276
|
160 Prim.p_t_GCC (Prim.Int n),
|
adamc@182
|
161 string ")",
|
adamc@182
|
162 space,
|
adamc@182
|
163 exit],
|
adamc@182
|
164 env)
|
adamc@182
|
165 | PPrim (Prim.String s) =>
|
adamc@182
|
166 (box [string "if",
|
adamc@182
|
167 space,
|
adamc@182
|
168 string "(strcmp(disc",
|
adamc@182
|
169 string (Int.toString depth),
|
adamc@182
|
170 string ",",
|
adamc@182
|
171 space,
|
adamc@276
|
172 Prim.p_t_GCC (Prim.String s),
|
adamc@182
|
173 string "))",
|
adamc@182
|
174 space,
|
adamc@182
|
175 exit],
|
adamc@182
|
176 env)
|
adamc@182
|
177 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
|
adamc@182
|
178
|
adamc@188
|
179 | PCon (dk, pc, po) =>
|
adamc@182
|
180 let
|
adamc@182
|
181 val (p, env) =
|
adamc@182
|
182 case po of
|
adamc@182
|
183 NONE => (box [], env)
|
adamc@182
|
184 | SOME p =>
|
adamc@182
|
185 let
|
adamc@182
|
186 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
187
|
adamc@182
|
188 val (x, to) = case pc of
|
adamc@182
|
189 PConVar n =>
|
adamc@182
|
190 let
|
adamc@182
|
191 val (x, to, _) = E.lookupConstructor env n
|
adamc@182
|
192 in
|
adamc@196
|
193 ("lw_" ^ x, to)
|
adamc@182
|
194 end
|
adamc@188
|
195 | PConFfi {mod = m, con, arg, ...} =>
|
adamc@188
|
196 ("lw_" ^ m ^ "_" ^ con, arg)
|
adamc@182
|
197
|
adamc@182
|
198 val t = case to of
|
adamc@182
|
199 NONE => raise Fail "CjrPrint: Constructor mismatch"
|
adamc@182
|
200 | SOME t => t
|
adamc@182
|
201 in
|
adamc@182
|
202 (box [string "{",
|
adamc@182
|
203 newline,
|
adamc@182
|
204 p_typ env t,
|
adamc@182
|
205 space,
|
adamc@182
|
206 string "disc",
|
adamc@182
|
207 string (Int.toString (depth + 1)),
|
adamc@182
|
208 space,
|
adamc@182
|
209 string "=",
|
adamc@182
|
210 space,
|
adamc@198
|
211 case dk of
|
adamc@198
|
212 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
|
adamc@198
|
213 | Default => box [string "disc",
|
adamc@198
|
214 string (Int.toString depth),
|
adamc@198
|
215 string "->data.",
|
adamc@198
|
216 string x]
|
adamc@198
|
217 | Option =>
|
adamc@198
|
218 case #1 t of
|
adamc@198
|
219 TDatatype _ => box [string "disc",
|
adamc@198
|
220 string (Int.toString depth)]
|
adamc@199
|
221 | TFfi ("Basis", "string") => box [string "disc",
|
adamc@199
|
222 string (Int.toString depth)]
|
adamc@198
|
223 | _ => box [string "*disc",
|
adamc@198
|
224 string (Int.toString depth)],
|
adamc@182
|
225 string ";",
|
adamc@182
|
226 newline,
|
adamc@182
|
227 p,
|
adamc@182
|
228 newline,
|
adamc@182
|
229 string "}"],
|
adamc@182
|
230 env)
|
adamc@182
|
231 end
|
adamc@182
|
232 in
|
adamc@182
|
233 (box [string "if",
|
adamc@182
|
234 space,
|
adamc@182
|
235 string "(disc",
|
adamc@182
|
236 string (Int.toString depth),
|
adamc@198
|
237 case (dk, po) of
|
adamc@198
|
238 (Enum, _) => box [space,
|
adamc@198
|
239 string "!=",
|
adamc@198
|
240 space,
|
adamc@198
|
241 p_patCon env pc]
|
adamc@198
|
242 | (Default, _) => box [string "->tag",
|
adamc@198
|
243 space,
|
adamc@198
|
244 string "!=",
|
adamc@198
|
245 space,
|
adamc@198
|
246 p_patCon env pc]
|
adamc@198
|
247 | (Option, NONE) => box [space,
|
adamc@198
|
248 string "!=",
|
adamc@198
|
249 space,
|
adamc@198
|
250 string "NULL"]
|
adamc@198
|
251 | (Option, SOME _) => box [space,
|
adamc@198
|
252 string "==",
|
adamc@198
|
253 space,
|
adamc@198
|
254 string "NULL"],
|
adamc@182
|
255 string ")",
|
adamc@182
|
256 space,
|
adamc@182
|
257 exit,
|
adamc@182
|
258 newline,
|
adamc@182
|
259 p],
|
adamc@182
|
260 env)
|
adamc@182
|
261 end
|
adamc@182
|
262
|
adamc@182
|
263 | PRecord xps =>
|
adamc@182
|
264 let
|
adamc@182
|
265 val (xps, env) =
|
adamc@182
|
266 ListUtil.foldlMap (fn ((x, p, t), env) =>
|
adamc@182
|
267 let
|
adamc@182
|
268 val (p, env) = p_pat (env, exit, depth + 1) p
|
adamc@182
|
269
|
adamc@182
|
270 val p = box [string "{",
|
adamc@182
|
271 newline,
|
adamc@182
|
272 p_typ env t,
|
adamc@182
|
273 space,
|
adamc@182
|
274 string "disc",
|
adamc@182
|
275 string (Int.toString (depth + 1)),
|
adamc@182
|
276 space,
|
adamc@182
|
277 string "=",
|
adamc@182
|
278 space,
|
adamc@182
|
279 string "disc",
|
adamc@182
|
280 string (Int.toString depth),
|
adamc@196
|
281 string ".__lwf_",
|
adamc@182
|
282 string x,
|
adamc@182
|
283 string ";",
|
adamc@182
|
284 newline,
|
adamc@182
|
285 p,
|
adamc@182
|
286 newline,
|
adamc@182
|
287 string "}"]
|
adamc@182
|
288 in
|
adamc@182
|
289 (p, env)
|
adamc@182
|
290 end) env xps
|
adamc@182
|
291 in
|
adamc@182
|
292 (p_list_sep newline (fn x => x) xps,
|
adamc@182
|
293 env)
|
adamc@182
|
294 end
|
adamc@182
|
295
|
adamc@182
|
296 local
|
adamc@182
|
297 val count = ref 0
|
adamc@182
|
298 in
|
adamc@182
|
299 fun newGoto () =
|
adamc@182
|
300 let
|
adamc@182
|
301 val r = !count
|
adamc@182
|
302 in
|
adamc@182
|
303 count := r + 1;
|
adamc@182
|
304 string ("L" ^ Int.toString r)
|
adamc@182
|
305 end
|
adamc@182
|
306 end
|
adamc@182
|
307
|
adamc@185
|
308 fun patConInfo env pc =
|
adamc@185
|
309 case pc of
|
adamc@185
|
310 PConVar n =>
|
adamc@185
|
311 let
|
adamc@185
|
312 val (x, _, dn) = E.lookupConstructor env n
|
adamc@185
|
313 val (dx, _) = E.lookupDatatype env dn
|
adamc@185
|
314 in
|
adamc@185
|
315 ("__lwd_" ^ dx ^ "_" ^ Int.toString dn,
|
adamc@196
|
316 "__lwc_" ^ x ^ "_" ^ Int.toString n,
|
adamc@196
|
317 "lw_" ^ x)
|
adamc@185
|
318 end
|
adamc@186
|
319 | PConFfi {mod = m, datatyp, con, ...} =>
|
adamc@185
|
320 ("lw_" ^ m ^ "_" ^ datatyp,
|
adamc@196
|
321 "lw_" ^ m ^ "_" ^ con,
|
adamc@196
|
322 "lw_" ^ con)
|
adamc@185
|
323
|
adamc@182
|
324 fun p_exp' par env (e, loc) =
|
adamc@29
|
325 case e of
|
adamc@276
|
326 EPrim p => Prim.p_t_GCC p
|
adamc@29
|
327 | ERel n => p_rel env n
|
adamc@109
|
328 | ENamed n => p_enamed env n
|
adamc@188
|
329 | ECon (Enum, pc, _) => p_patCon env pc
|
adamc@198
|
330 | ECon (Option, pc, NONE) => string "NULL"
|
adamc@198
|
331 | ECon (Option, pc, SOME e) =>
|
adamc@198
|
332 let
|
adamc@198
|
333 val to = case pc of
|
adamc@198
|
334 PConVar n => #2 (E.lookupConstructor env n)
|
adamc@198
|
335 | PConFfi {arg, ...} => arg
|
adamc@198
|
336
|
adamc@198
|
337 val t = case to of
|
adamc@198
|
338 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
|
adamc@198
|
339 | SOME t => t
|
adamc@198
|
340 in
|
adamc@198
|
341 case #1 t of
|
adamc@198
|
342 TDatatype _ => p_exp' par env e
|
adamc@199
|
343 | TFfi ("Basis", "string") => p_exp' par env e
|
adamc@198
|
344 | _ => box [string "({",
|
adamc@198
|
345 newline,
|
adamc@198
|
346 p_typ env t,
|
adamc@198
|
347 space,
|
adamc@198
|
348 string "*tmp",
|
adamc@198
|
349 space,
|
adamc@198
|
350 string "=",
|
adamc@198
|
351 space,
|
adamc@198
|
352 string "lw_malloc(ctx, sizeof(",
|
adamc@198
|
353 p_typ env t,
|
adamc@198
|
354 string "));",
|
adamc@198
|
355 newline,
|
adamc@198
|
356 string "*tmp",
|
adamc@198
|
357 space,
|
adamc@198
|
358 string "=",
|
adamc@198
|
359 p_exp' par env e,
|
adamc@198
|
360 string ";",
|
adamc@198
|
361 newline,
|
adamc@198
|
362 string "tmp;",
|
adamc@198
|
363 newline,
|
adamc@198
|
364 string "})"]
|
adamc@198
|
365 end
|
adamc@188
|
366 | ECon (Default, pc, eo) =>
|
adamc@181
|
367 let
|
adamc@196
|
368 val (xd, xc, xn) = patConInfo env pc
|
adamc@181
|
369 in
|
adamc@182
|
370 box [string "({",
|
adamc@181
|
371 newline,
|
adamc@181
|
372 string "struct",
|
adamc@181
|
373 space,
|
adamc@185
|
374 string xd,
|
adamc@181
|
375 space,
|
adamc@181
|
376 string "*tmp",
|
adamc@181
|
377 space,
|
adamc@181
|
378 string "=",
|
adamc@181
|
379 space,
|
adamc@185
|
380 string "lw_malloc(ctx, sizeof(struct ",
|
adamc@185
|
381 string xd,
|
adamc@181
|
382 string "));",
|
adamc@181
|
383 newline,
|
adamc@181
|
384 string "tmp->tag",
|
adamc@181
|
385 space,
|
adamc@181
|
386 string "=",
|
adamc@181
|
387 space,
|
adamc@185
|
388 string xc,
|
adamc@181
|
389 string ";",
|
adamc@181
|
390 newline,
|
adamc@181
|
391 case eo of
|
adamc@181
|
392 NONE => box []
|
adamc@185
|
393 | SOME e => box [string "tmp->data.",
|
adamc@196
|
394 string xn,
|
adamc@181
|
395 space,
|
adamc@181
|
396 string "=",
|
adamc@181
|
397 space,
|
adamc@181
|
398 p_exp env e,
|
adamc@181
|
399 string ";",
|
adamc@181
|
400 newline],
|
adamc@181
|
401 string "tmp;",
|
adamc@181
|
402 newline,
|
adamc@181
|
403 string "})"]
|
adamc@181
|
404 end
|
adamc@109
|
405
|
adamc@53
|
406 | EFfi (m, x) => box [string "lw_", string m, string "_", string x]
|
adamc@53
|
407 | EFfiApp (m, x, es) => box [string "lw_",
|
adamc@53
|
408 string m,
|
adamc@53
|
409 string "_",
|
adamc@53
|
410 string x,
|
adamc@117
|
411 string "(ctx, ",
|
adamc@53
|
412 p_list (p_exp env) es,
|
adamc@53
|
413 string ")"]
|
adamc@129
|
414 | EApp (e1, e2) =>
|
adamc@129
|
415 let
|
adamc@129
|
416 fun unravel (f, acc) =
|
adamc@129
|
417 case #1 f of
|
adamc@129
|
418 EApp (f', arg) => unravel (f', arg :: acc)
|
adamc@129
|
419 | _ => (f, acc)
|
adamc@129
|
420
|
adamc@129
|
421 val (f, args) = unravel (e1, [e2])
|
adamc@129
|
422 in
|
adamc@129
|
423 parenIf par (box [p_exp' true env e1,
|
adamc@129
|
424 string "(ctx,",
|
adamc@129
|
425 space,
|
adamc@129
|
426 p_list_sep (box [string ",", space]) (p_exp env) args,
|
adamc@129
|
427 string ")"])
|
adamc@129
|
428 end
|
adamc@29
|
429
|
adamc@29
|
430 | ERecord (i, xes) => box [string "({",
|
adamc@29
|
431 space,
|
adamc@29
|
432 string "struct",
|
adamc@29
|
433 space,
|
adamc@29
|
434 string ("__lws_" ^ Int.toString i),
|
adamc@29
|
435 space,
|
adamc@181
|
436 string "tmp",
|
adamc@29
|
437 space,
|
adamc@29
|
438 string "=",
|
adamc@29
|
439 space,
|
adamc@29
|
440 string "{",
|
adamc@29
|
441 p_list (fn (_, e) =>
|
adamc@29
|
442 p_exp env e) xes,
|
adamc@29
|
443 string "};",
|
adamc@29
|
444 space,
|
adamc@181
|
445 string "tmp;",
|
adamc@29
|
446 space,
|
adamc@29
|
447 string "})" ]
|
adamc@29
|
448 | EField (e, x) =>
|
adamc@29
|
449 box [p_exp' true env e,
|
adamc@182
|
450 string ".__lwf_",
|
adamc@29
|
451 string x]
|
adamc@29
|
452
|
adamc@182
|
453 | ECase (e, pes, {disc, result}) =>
|
adamc@182
|
454 let
|
adamc@182
|
455 val final = newGoto ()
|
adamc@182
|
456
|
adamc@182
|
457 val body = foldl (fn ((p, e), body) =>
|
adamc@182
|
458 let
|
adamc@182
|
459 val exit = newGoto ()
|
adamc@182
|
460 val (pr, _) = p_pat_preamble env p
|
adamc@182
|
461 val (p, env) = p_pat (env,
|
adamc@182
|
462 box [string "goto",
|
adamc@182
|
463 space,
|
adamc@182
|
464 exit,
|
adamc@182
|
465 string ";"],
|
adamc@182
|
466 0) p
|
adamc@182
|
467 in
|
adamc@182
|
468 box [body,
|
adamc@182
|
469 box [string "{",
|
adamc@182
|
470 newline,
|
adamc@182
|
471 pr,
|
adamc@182
|
472 newline,
|
adamc@182
|
473 p,
|
adamc@182
|
474 newline,
|
adamc@182
|
475 string "result",
|
adamc@182
|
476 space,
|
adamc@182
|
477 string "=",
|
adamc@182
|
478 space,
|
adamc@182
|
479 p_exp env e,
|
adamc@182
|
480 string ";",
|
adamc@182
|
481 newline,
|
adamc@182
|
482 string "goto",
|
adamc@182
|
483 space,
|
adamc@182
|
484 final,
|
adamc@182
|
485 string ";",
|
adamc@182
|
486 newline,
|
adamc@182
|
487 string "}"],
|
adamc@182
|
488 newline,
|
adamc@182
|
489 exit,
|
adamc@182
|
490 string ":",
|
adamc@182
|
491 newline]
|
adamc@182
|
492 end) (box []) pes
|
adamc@182
|
493 in
|
adamc@182
|
494 box [string "({",
|
adamc@182
|
495 newline,
|
adamc@182
|
496 p_typ env disc,
|
adamc@182
|
497 space,
|
adamc@182
|
498 string "disc0",
|
adamc@182
|
499 space,
|
adamc@182
|
500 string "=",
|
adamc@182
|
501 space,
|
adamc@182
|
502 p_exp env e,
|
adamc@182
|
503 string ";",
|
adamc@182
|
504 newline,
|
adamc@182
|
505 p_typ env result,
|
adamc@182
|
506 space,
|
adamc@182
|
507 string "result;",
|
adamc@182
|
508 newline,
|
adamc@182
|
509 body,
|
adamc@182
|
510 string "lw_error(ctx, FATAL, \"",
|
adamc@182
|
511 string (ErrorMsg.spanToString loc),
|
adamc@182
|
512 string ": pattern match failure\");",
|
adamc@182
|
513 newline,
|
adamc@182
|
514 final,
|
adamc@182
|
515 string ":",
|
adamc@182
|
516 space,
|
adamc@182
|
517 string "result;",
|
adamc@182
|
518 newline,
|
adamc@182
|
519 string "})"]
|
adamc@182
|
520 end
|
adamc@181
|
521
|
adamc@117
|
522 | EWrite e => box [string "(lw_write(ctx, ",
|
adamc@102
|
523 p_exp env e,
|
adamc@102
|
524 string "), lw_unit_v)"]
|
adamc@102
|
525
|
adamc@106
|
526 | ESeq (e1, e2) => box [string "(",
|
adamc@106
|
527 p_exp env e1,
|
adamc@106
|
528 string ",",
|
adamc@106
|
529 space,
|
adamc@106
|
530 p_exp env e2,
|
adamc@106
|
531 string ")"]
|
adamc@269
|
532 | ELet (x, t, e1, e2) => box [string "({",
|
adamc@269
|
533 newline,
|
adamc@269
|
534 p_typ env t,
|
adamc@269
|
535 space,
|
adamc@272
|
536 string "__lwr_",
|
adamc@272
|
537 string x,
|
adamc@272
|
538 string "_",
|
adamc@272
|
539 string (Int.toString (E.countERels env)),
|
adamc@269
|
540 space,
|
adamc@269
|
541 string "=",
|
adamc@269
|
542 space,
|
adamc@269
|
543 p_exp env e1,
|
adamc@269
|
544 string ";",
|
adamc@269
|
545 newline,
|
adamc@269
|
546 p_exp (E.pushERel env x t) e2,
|
adamc@269
|
547 string ";",
|
adamc@269
|
548 newline,
|
adamc@269
|
549 string "})"]
|
adamc@269
|
550
|
adamc@269
|
551 | EQuery {exps, tables, rnum, state, query, body, initial} =>
|
adamc@272
|
552 string "(lw_error(ctx, FATAL, \"I would have run a query.\"), NULL)"
|
adamc@106
|
553
|
adamc@29
|
554 and p_exp env = p_exp' false env
|
adamc@29
|
555
|
adamc@129
|
556 fun p_fun env (fx, n, args, ran, e) =
|
adamc@129
|
557 let
|
adamc@129
|
558 val nargs = length args
|
adamc@129
|
559 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
|
adamc@129
|
560 in
|
adamc@129
|
561 box [string "static",
|
adamc@129
|
562 space,
|
adamc@129
|
563 p_typ env ran,
|
adamc@129
|
564 space,
|
adamc@129
|
565 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
|
adamc@129
|
566 string "(",
|
adamc@129
|
567 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@129
|
568 (string "lw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
|
adamc@129
|
569 box [p_typ env dom,
|
adamc@129
|
570 space,
|
adamc@129
|
571 p_rel env' (nargs - i - 1)]) args),
|
adamc@129
|
572 string ")",
|
adamc@129
|
573 space,
|
adamc@129
|
574 string "{",
|
adamc@129
|
575 newline,
|
adamc@129
|
576 box[string "return(",
|
adamc@129
|
577 p_exp env' e,
|
adamc@129
|
578 string ");"],
|
adamc@129
|
579 newline,
|
adamc@129
|
580 string "}"]
|
adamc@129
|
581 end
|
adamc@129
|
582
|
adamc@129
|
583 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@29
|
584 case d of
|
adamc@29
|
585 DStruct (n, xts) =>
|
adamc@196
|
586 let
|
adamc@196
|
587 val env = E.declBinds env dAll
|
adamc@196
|
588 in
|
adamc@196
|
589 box [string "struct",
|
adamc@196
|
590 space,
|
adamc@196
|
591 string ("__lws_" ^ Int.toString n),
|
adamc@196
|
592 space,
|
adamc@196
|
593 string "{",
|
adamc@196
|
594 newline,
|
adamc@196
|
595 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
|
adamc@196
|
596 space,
|
adamc@196
|
597 string "__lwf_",
|
adamc@196
|
598 string x,
|
adamc@196
|
599 string ";",
|
adamc@196
|
600 newline]) xts,
|
adamc@196
|
601 string "};"]
|
adamc@196
|
602 end
|
adamc@188
|
603 | DDatatype (Enum, x, n, xncs) =>
|
adamc@188
|
604 box [string "enum",
|
adamc@188
|
605 space,
|
adamc@188
|
606 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
|
adamc@188
|
607 space,
|
adamc@188
|
608 string "{",
|
adamc@188
|
609 space,
|
adamc@188
|
610 p_list_sep (box [string ",", space]) (fn (x, n, _) => string ("__lwc_" ^ x ^ "_" ^ Int.toString n)) xncs,
|
adamc@188
|
611 space,
|
adamc@188
|
612 string "};"]
|
adamc@198
|
613 | DDatatype (Option, _, _, _) => box []
|
adamc@188
|
614 | DDatatype (Default, x, n, xncs) =>
|
adamc@165
|
615 let
|
adamc@165
|
616 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
|
adamc@165
|
617 | (x, n, SOME t) => SOME (x, n, t)) xncs
|
adamc@165
|
618 in
|
adamc@165
|
619 box [string "enum",
|
adamc@165
|
620 space,
|
adamc@165
|
621 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
|
adamc@165
|
622 space,
|
adamc@165
|
623 string "{",
|
adamc@165
|
624 space,
|
adamc@165
|
625 p_list_sep (box [string ",", space]) (fn (x, n, _) => string ("__lwc_" ^ x ^ "_" ^ Int.toString n)) xncs,
|
adamc@165
|
626 space,
|
adamc@165
|
627 string "};",
|
adamc@165
|
628 newline,
|
adamc@165
|
629 newline,
|
adamc@165
|
630 string "struct",
|
adamc@165
|
631 space,
|
adamc@167
|
632 string ("__lwd_" ^ x ^ "_" ^ Int.toString n),
|
adamc@165
|
633 space,
|
adamc@165
|
634 string "{",
|
adamc@165
|
635 newline,
|
adamc@165
|
636 string "enum",
|
adamc@165
|
637 space,
|
adamc@165
|
638 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
|
adamc@165
|
639 space,
|
adamc@165
|
640 string "tag;",
|
adamc@165
|
641 newline,
|
adamc@165
|
642 box (case xncsArgs of
|
adamc@165
|
643 [] => []
|
adamc@165
|
644 | _ => [string "union",
|
adamc@165
|
645 space,
|
adamc@165
|
646 string "{",
|
adamc@165
|
647 newline,
|
adamc@165
|
648 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
|
adamc@165
|
649 space,
|
adamc@196
|
650 string ("lw_" ^ x),
|
adamc@165
|
651 string ";"]) xncsArgs,
|
adamc@165
|
652 newline,
|
adamc@165
|
653 string "}",
|
adamc@165
|
654 space,
|
adamc@165
|
655 string "data;",
|
adamc@165
|
656 newline]),
|
adamc@165
|
657 string "};"]
|
adamc@188
|
658 end
|
adamc@29
|
659
|
adamc@196
|
660 | DDatatypeForward _ => box []
|
adamc@196
|
661
|
adamc@29
|
662 | DVal (x, n, t, e) =>
|
adamc@29
|
663 box [p_typ env t,
|
adamc@29
|
664 space,
|
adamc@29
|
665 string ("__lwn_" ^ x ^ "_" ^ Int.toString n),
|
adamc@29
|
666 space,
|
adamc@29
|
667 string "=",
|
adamc@29
|
668 space,
|
adamc@29
|
669 p_exp env e,
|
adamc@29
|
670 string ";"]
|
adamc@129
|
671 | DFun vi => p_fun env vi
|
adamc@129
|
672 | DFunRec vis =>
|
adamc@29
|
673 let
|
adamc@129
|
674 val env = E.declBinds env dAll
|
adamc@29
|
675 in
|
adamc@129
|
676 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
|
adamc@129
|
677 box [string "static",
|
adamc@129
|
678 space,
|
adamc@129
|
679 p_typ env ran,
|
adamc@129
|
680 space,
|
adamc@129
|
681 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
|
adamc@129
|
682 string "(lw_context,",
|
adamc@129
|
683 space,
|
adamc@129
|
684 p_list_sep (box [string ",", space])
|
adamc@129
|
685 (fn (_, dom) => p_typ env dom) args,
|
adamc@129
|
686 string ");"]) vis,
|
adamc@29
|
687 newline,
|
adamc@129
|
688 p_list_sep newline (p_fun env) vis,
|
adamc@129
|
689 newline]
|
adamc@29
|
690 end
|
adamc@273
|
691 | DTable (x, _) => box [string "/* SQL table ",
|
adamc@273
|
692 string x,
|
adamc@273
|
693 string " */",
|
adamc@273
|
694 newline]
|
adamc@275
|
695 | DDatabase s => box [string "static void lw_db_validate(lw_context);",
|
adamc@272
|
696 newline,
|
adamc@275
|
697 newline,
|
adamc@275
|
698 string "void lw_db_init(lw_context ctx) {",
|
adamc@273
|
699 newline,
|
adamc@272
|
700 string "PGconn *conn = PQconnectdb(\"",
|
adamc@272
|
701 string (String.toString s),
|
adamc@272
|
702 string "\");",
|
adamc@272
|
703 newline,
|
adamc@272
|
704 string "if (conn == NULL) lw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
705 string "\"libpq can't allocate a connection.\");",
|
adamc@272
|
706 newline,
|
adamc@272
|
707 string "if (PQstatus(conn) != CONNECTION_OK) {",
|
adamc@272
|
708 newline,
|
adamc@272
|
709 box [string "char msg[1024];",
|
adamc@272
|
710 newline,
|
adamc@272
|
711 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@272
|
712 newline,
|
adamc@272
|
713 string "msg[1023] = 0;",
|
adamc@272
|
714 newline,
|
adamc@272
|
715 string "PQfinish(conn);",
|
adamc@272
|
716 newline,
|
adamc@272
|
717 string "lw_error(ctx, BOUNDED_RETRY, ",
|
adamc@272
|
718 string "\"Connection to Postgres server failed: %s\", msg);"],
|
adamc@272
|
719 newline,
|
adamc@272
|
720 string "}",
|
adamc@272
|
721 newline,
|
adamc@272
|
722 string "lw_set_db(ctx, conn);",
|
adamc@272
|
723 newline,
|
adamc@275
|
724 string "lw_db_validate(ctx);",
|
adamc@275
|
725 newline,
|
adamc@272
|
726 string "}",
|
adamc@272
|
727 newline,
|
adamc@272
|
728 newline,
|
adamc@272
|
729 string "void lw_db_close(lw_context ctx) {",
|
adamc@272
|
730 newline,
|
adamc@272
|
731 string "PQfinish(lw_get_db(ctx));",
|
adamc@272
|
732 newline,
|
adamc@272
|
733 string "}",
|
adamc@272
|
734 newline]
|
adamc@29
|
735
|
adamc@144
|
736 datatype 'a search =
|
adamc@144
|
737 Found of 'a
|
adamc@144
|
738 | NotFound
|
adamc@144
|
739 | Error
|
adamc@120
|
740
|
adamc@275
|
741 fun p_sqltype' env (tAll as (t, loc)) =
|
adamc@275
|
742 case t of
|
adamc@275
|
743 TFfi ("Basis", "int") => "int8"
|
adamc@275
|
744 | TFfi ("Basis", "float") => "float8"
|
adamc@275
|
745 | TFfi ("Basis", "string") => "text"
|
adamc@275
|
746 | TFfi ("Basis", "bool") => "bool"
|
adamc@275
|
747 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
|
adamc@275
|
748 Print.eprefaces' [("Type", p_typ env tAll)];
|
adamc@275
|
749 "ERROR")
|
adamc@275
|
750
|
adamc@275
|
751 fun p_sqltype env t = string (p_sqltype' env t)
|
adamc@101
|
752
|
adamc@101
|
753 fun p_file env (ds, ps) =
|
adamc@29
|
754 let
|
adamc@101
|
755 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
756 (p_decl env d,
|
adamc@31
|
757 E.declBinds env d))
|
adamc@101
|
758 env ds
|
adamc@144
|
759
|
adamc@144
|
760 val fields = foldl (fn ((ek, _, _, ts), fields) =>
|
adamc@144
|
761 case ek of
|
adamc@144
|
762 Core.Link => fields
|
adamc@144
|
763 | Core.Action =>
|
adamc@144
|
764 case List.last ts of
|
adamc@144
|
765 (TRecord i, _) =>
|
adamc@144
|
766 let
|
adamc@144
|
767 val xts = E.lookupStruct env i
|
adamc@144
|
768 val xtsSet = SS.addList (SS.empty, map #1 xts)
|
adamc@144
|
769 in
|
adamc@144
|
770 foldl (fn ((x, _), fields) =>
|
adamc@144
|
771 let
|
adamc@144
|
772 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
|
adamc@144
|
773 in
|
adamc@144
|
774 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
|
adamc@144
|
775 xtsSet'))
|
adamc@144
|
776 end) fields xts
|
adamc@144
|
777 end
|
adamc@144
|
778 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
|
adamc@144
|
779 SM.empty ps
|
adamc@144
|
780
|
adamc@144
|
781 val fnums = SM.foldli (fn (x, xs, fnums) =>
|
adamc@144
|
782 let
|
adamc@144
|
783 val unusable = SS.foldl (fn (x', unusable) =>
|
adamc@144
|
784 case SM.find (fnums, x') of
|
adamc@144
|
785 NONE => unusable
|
adamc@144
|
786 | SOME n => IS.add (unusable, n))
|
adamc@144
|
787 IS.empty xs
|
adamc@144
|
788
|
adamc@144
|
789 fun findAvailable n =
|
adamc@144
|
790 if IS.member (unusable, n) then
|
adamc@144
|
791 findAvailable (n + 1)
|
adamc@144
|
792 else
|
adamc@144
|
793 n
|
adamc@144
|
794 in
|
adamc@144
|
795 SM.insert (fnums, x, findAvailable 0)
|
adamc@144
|
796 end)
|
adamc@144
|
797 SM.empty fields
|
adamc@144
|
798
|
adamc@144
|
799 fun makeSwitch (fnums, i) =
|
adamc@144
|
800 case SM.foldl (fn (n, NotFound) => Found n
|
adamc@144
|
801 | (n, Error) => Error
|
adamc@144
|
802 | (n, Found n') => if n = n' then
|
adamc@144
|
803 Found n'
|
adamc@144
|
804 else
|
adamc@144
|
805 Error) NotFound fnums of
|
adamc@144
|
806 NotFound => box [string "return",
|
adamc@144
|
807 space,
|
adamc@144
|
808 string "-1;"]
|
adamc@144
|
809 | Found n => box [string "return",
|
adamc@144
|
810 space,
|
adamc@144
|
811 string (Int.toString n),
|
adamc@144
|
812 string ";"]
|
adamc@144
|
813 | Error =>
|
adamc@144
|
814 let
|
adamc@144
|
815 val cmap = SM.foldli (fn (x, n, cmap) =>
|
adamc@144
|
816 let
|
adamc@144
|
817 val ch = if i < size x then
|
adamc@144
|
818 String.sub (x, i)
|
adamc@144
|
819 else
|
adamc@144
|
820 chr 0
|
adamc@144
|
821
|
adamc@144
|
822 val fnums = case CM.find (cmap, ch) of
|
adamc@144
|
823 NONE => SM.empty
|
adamc@144
|
824 | SOME fnums => fnums
|
adamc@144
|
825 val fnums = SM.insert (fnums, x, n)
|
adamc@144
|
826 in
|
adamc@144
|
827 CM.insert (cmap, ch, fnums)
|
adamc@144
|
828 end)
|
adamc@144
|
829 CM.empty fnums
|
adamc@144
|
830
|
adamc@144
|
831 val cmap = CM.listItemsi cmap
|
adamc@144
|
832 in
|
adamc@144
|
833 case cmap of
|
adamc@144
|
834 [(_, fnums)] =>
|
adamc@144
|
835 box [string "if",
|
adamc@144
|
836 space,
|
adamc@144
|
837 string "(name[",
|
adamc@144
|
838 string (Int.toString i),
|
adamc@144
|
839 string "]",
|
adamc@144
|
840 space,
|
adamc@144
|
841 string "==",
|
adamc@144
|
842 space,
|
adamc@144
|
843 string "0)",
|
adamc@144
|
844 space,
|
adamc@144
|
845 string "return",
|
adamc@144
|
846 space,
|
adamc@144
|
847 string "-1;",
|
adamc@144
|
848 newline,
|
adamc@144
|
849 makeSwitch (fnums, i+1)]
|
adamc@144
|
850 | _ =>
|
adamc@144
|
851 box [string "switch",
|
adamc@144
|
852 space,
|
adamc@144
|
853 string "(name[",
|
adamc@144
|
854 string (Int.toString i),
|
adamc@144
|
855 string "])",
|
adamc@144
|
856 space,
|
adamc@144
|
857 string "{",
|
adamc@144
|
858 newline,
|
adamc@144
|
859 box (map (fn (ch, fnums) =>
|
adamc@144
|
860 box [string "case",
|
adamc@144
|
861 space,
|
adamc@144
|
862 if ch = chr 0 then
|
adamc@144
|
863 string "0:"
|
adamc@144
|
864 else
|
adamc@144
|
865 box [string "'",
|
adamc@144
|
866 string (Char.toString ch),
|
adamc@144
|
867 string "':"],
|
adamc@144
|
868 newline,
|
adamc@144
|
869 makeSwitch (fnums, i+1),
|
adamc@144
|
870 newline]) cmap),
|
adamc@144
|
871 string "default:",
|
adamc@144
|
872 newline,
|
adamc@144
|
873 string "return",
|
adamc@144
|
874 space,
|
adamc@144
|
875 string "-1;",
|
adamc@144
|
876 newline,
|
adamc@144
|
877 string "}"]
|
adamc@144
|
878 end
|
adamc@144
|
879
|
adamc@186
|
880 fun capitalize s =
|
adamc@186
|
881 if s = "" then
|
adamc@186
|
882 ""
|
adamc@186
|
883 else
|
adamc@186
|
884 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@186
|
885
|
adamc@144
|
886 fun unurlify (t, loc) =
|
adamc@144
|
887 case t of
|
adamc@186
|
888 TFfi (m, t) => string ("lw_" ^ m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
|
adamc@144
|
889
|
adamc@144
|
890 | TRecord 0 => string "lw_unit_v"
|
adamc@144
|
891 | TRecord i =>
|
adamc@144
|
892 let
|
adamc@144
|
893 val xts = E.lookupStruct env i
|
adamc@144
|
894 in
|
adamc@144
|
895 box [string "({",
|
adamc@144
|
896 newline,
|
adamc@144
|
897 box (map (fn (x, t) =>
|
adamc@144
|
898 box [p_typ env t,
|
adamc@144
|
899 space,
|
adamc@144
|
900 string x,
|
adamc@144
|
901 space,
|
adamc@144
|
902 string "=",
|
adamc@144
|
903 space,
|
adamc@144
|
904 unurlify t,
|
adamc@144
|
905 string ";",
|
adamc@144
|
906 newline]) xts),
|
adamc@144
|
907 string "struct",
|
adamc@144
|
908 space,
|
adamc@144
|
909 string "__lws_",
|
adamc@144
|
910 string (Int.toString i),
|
adamc@144
|
911 space,
|
adamc@181
|
912 string "tmp",
|
adamc@144
|
913 space,
|
adamc@144
|
914 string "=",
|
adamc@144
|
915 space,
|
adamc@144
|
916 string "{",
|
adamc@144
|
917 space,
|
adamc@144
|
918 p_list_sep (box [string ",", space]) (fn (x, _) => string x) xts,
|
adamc@144
|
919 space,
|
adamc@144
|
920 string "};",
|
adamc@144
|
921 newline,
|
adamc@181
|
922 string "tmp;",
|
adamc@144
|
923 newline,
|
adamc@144
|
924 string "})"]
|
adamc@144
|
925 end
|
adamc@144
|
926
|
adamc@188
|
927 | TDatatype (Enum, i, _) =>
|
adamc@188
|
928 let
|
adamc@188
|
929 val (x, xncs) = E.lookupDatatype env i
|
adamc@188
|
930
|
adamc@188
|
931 fun doEm xncs =
|
adamc@188
|
932 case xncs of
|
adamc@188
|
933 [] => string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), (enum __lwe_"
|
adamc@188
|
934 ^ x ^ "_" ^ Int.toString i ^ ")0)")
|
adamc@188
|
935 | (x', n, to) :: rest =>
|
adamc@188
|
936 box [string "((!strncmp(request, \"",
|
adamc@188
|
937 string x',
|
adamc@188
|
938 string "\", ",
|
adamc@188
|
939 string (Int.toString (size x')),
|
adamc@188
|
940 string ") && (request[",
|
adamc@188
|
941 string (Int.toString (size x')),
|
adamc@188
|
942 string "] == 0 || request[",
|
adamc@188
|
943 string (Int.toString (size x')),
|
adamc@188
|
944 string ("] == '/')) ? __lwc_" ^ x' ^ "_" ^ Int.toString n),
|
adamc@188
|
945 space,
|
adamc@188
|
946 string ":",
|
adamc@188
|
947 space,
|
adamc@188
|
948 doEm rest,
|
adamc@188
|
949 string ")"]
|
adamc@188
|
950 in
|
adamc@188
|
951 doEm xncs
|
adamc@188
|
952 end
|
adamc@188
|
953
|
adamc@198
|
954 | TDatatype (Option, i, xncs) =>
|
adamc@198
|
955 let
|
adamc@198
|
956 val (x, _) = E.lookupDatatype env i
|
adamc@198
|
957
|
adamc@198
|
958 val (no_arg, has_arg, t) =
|
adamc@198
|
959 case !xncs of
|
adamc@198
|
960 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
|
adamc@198
|
961 (no_arg, has_arg, t)
|
adamc@198
|
962 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
|
adamc@198
|
963 (no_arg, has_arg, t)
|
adamc@198
|
964 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
|
adamc@198
|
965 in
|
adamc@198
|
966 box [string "(request[0] == '/' ? ++request : request,",
|
adamc@198
|
967 newline,
|
adamc@198
|
968 string "((!strncmp(request, \"",
|
adamc@198
|
969 string no_arg,
|
adamc@198
|
970 string "\", ",
|
adamc@198
|
971 string (Int.toString (size no_arg)),
|
adamc@198
|
972 string ") && (request[",
|
adamc@198
|
973 string (Int.toString (size no_arg)),
|
adamc@198
|
974 string "] == 0 || request[",
|
adamc@198
|
975 string (Int.toString (size no_arg)),
|
adamc@198
|
976 string "] == '/')) ? (request",
|
adamc@198
|
977 space,
|
adamc@198
|
978 string "+=",
|
adamc@198
|
979 space,
|
adamc@198
|
980 string (Int.toString (size no_arg)),
|
adamc@198
|
981 string ", NULL) : ((!strncmp(request, \"",
|
adamc@198
|
982 string has_arg,
|
adamc@198
|
983 string "\", ",
|
adamc@198
|
984 string (Int.toString (size has_arg)),
|
adamc@198
|
985 string ") && (request[",
|
adamc@198
|
986 string (Int.toString (size has_arg)),
|
adamc@198
|
987 string "] == 0 || request[",
|
adamc@198
|
988 string (Int.toString (size has_arg)),
|
adamc@198
|
989 string "] == '/')) ? (request",
|
adamc@198
|
990 space,
|
adamc@198
|
991 string "+=",
|
adamc@198
|
992 space,
|
adamc@198
|
993 string (Int.toString (size has_arg)),
|
adamc@200
|
994 string ", (request[0] == '/' ? ++request : NULL), ",
|
adamc@200
|
995 newline,
|
adamc@198
|
996
|
adamc@198
|
997 case #1 t of
|
adamc@198
|
998 TDatatype _ => unurlify t
|
adamc@199
|
999 | TFfi ("Basis", "string") => unurlify t
|
adamc@198
|
1000 | _ => box [string "({",
|
adamc@198
|
1001 newline,
|
adamc@198
|
1002 p_typ env t,
|
adamc@198
|
1003 space,
|
adamc@198
|
1004 string "*tmp",
|
adamc@198
|
1005 space,
|
adamc@198
|
1006 string "=",
|
adamc@198
|
1007 space,
|
adamc@198
|
1008 string "lw_malloc(ctx, sizeof(",
|
adamc@198
|
1009 p_typ env t,
|
adamc@198
|
1010 string "));",
|
adamc@198
|
1011 newline,
|
adamc@198
|
1012 string "*tmp",
|
adamc@198
|
1013 space,
|
adamc@198
|
1014 string "=",
|
adamc@198
|
1015 space,
|
adamc@198
|
1016 unurlify t,
|
adamc@198
|
1017 string ";",
|
adamc@198
|
1018 newline,
|
adamc@198
|
1019 string "tmp;",
|
adamc@198
|
1020 newline,
|
adamc@198
|
1021 string "})"],
|
adamc@198
|
1022 string ")",
|
adamc@198
|
1023 newline,
|
adamc@198
|
1024 string ":",
|
adamc@198
|
1025 space,
|
adamc@198
|
1026 string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL))))")]
|
adamc@198
|
1027 end
|
adamc@198
|
1028
|
adamc@188
|
1029 | TDatatype (Default, i, _) =>
|
adamc@166
|
1030 let
|
adamc@166
|
1031 val (x, xncs) = E.lookupDatatype env i
|
adamc@166
|
1032
|
adamc@166
|
1033 fun doEm xncs =
|
adamc@166
|
1034 case xncs of
|
adamc@167
|
1035 [] => string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL)")
|
adamc@167
|
1036 | (x', n, to) :: rest =>
|
adamc@167
|
1037 box [string "((!strncmp(request, \"",
|
adamc@167
|
1038 string x',
|
adamc@167
|
1039 string "\", ",
|
adamc@167
|
1040 string (Int.toString (size x')),
|
adamc@167
|
1041 string ") && (request[",
|
adamc@167
|
1042 string (Int.toString (size x')),
|
adamc@167
|
1043 string "] == 0 || request[",
|
adamc@167
|
1044 string (Int.toString (size x')),
|
adamc@167
|
1045 string "] == '/')) ? ({",
|
adamc@166
|
1046 newline,
|
adamc@167
|
1047 string "struct",
|
adamc@167
|
1048 space,
|
adamc@166
|
1049 string ("__lwd_" ^ x ^ "_" ^ Int.toString i),
|
adamc@166
|
1050 space,
|
adamc@181
|
1051 string "*tmp = lw_malloc(ctx, sizeof(struct __lwd_",
|
adamc@167
|
1052 string x,
|
adamc@167
|
1053 string "_",
|
adamc@167
|
1054 string (Int.toString i),
|
adamc@167
|
1055 string "));",
|
adamc@166
|
1056 newline,
|
adamc@181
|
1057 string "tmp->tag",
|
adamc@166
|
1058 space,
|
adamc@166
|
1059 string "=",
|
adamc@166
|
1060 space,
|
adamc@167
|
1061 string ("__lwc_" ^ x' ^ "_" ^ Int.toString n),
|
adamc@166
|
1062 string ";",
|
adamc@166
|
1063 newline,
|
adamc@166
|
1064 string "request",
|
adamc@166
|
1065 space,
|
adamc@166
|
1066 string "+=",
|
adamc@166
|
1067 space,
|
adamc@167
|
1068 string (Int.toString (size x')),
|
adamc@166
|
1069 string ";",
|
adamc@166
|
1070 newline,
|
adamc@200
|
1071 string "if (request[0] == '/') ++request;",
|
adamc@200
|
1072 newline,
|
adamc@166
|
1073 case to of
|
adamc@166
|
1074 NONE => box []
|
adamc@197
|
1075 | SOME t => box [string "tmp->data.lw_",
|
adamc@167
|
1076 string x',
|
adamc@166
|
1077 space,
|
adamc@166
|
1078 string "=",
|
adamc@166
|
1079 space,
|
adamc@166
|
1080 unurlify t,
|
adamc@166
|
1081 string ";",
|
adamc@166
|
1082 newline],
|
adamc@181
|
1083 string "tmp;",
|
adamc@166
|
1084 newline,
|
adamc@166
|
1085 string "})",
|
adamc@166
|
1086 space,
|
adamc@166
|
1087 string ":",
|
adamc@166
|
1088 space,
|
adamc@166
|
1089 doEm rest,
|
adamc@166
|
1090 string ")"]
|
adamc@166
|
1091 in
|
adamc@166
|
1092 doEm xncs
|
adamc@166
|
1093 end
|
adamc@166
|
1094
|
adamc@144
|
1095 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
|
adamc@144
|
1096 space)
|
adamc@144
|
1097
|
adamc@144
|
1098
|
adamc@144
|
1099 fun p_page (ek, s, n, ts) =
|
adamc@144
|
1100 let
|
adamc@144
|
1101 val (ts, defInputs, inputsVar) =
|
adamc@144
|
1102 case ek of
|
adamc@144
|
1103 Core.Link => (ts, string "", string "")
|
adamc@144
|
1104 | Core.Action =>
|
adamc@144
|
1105 case List.last ts of
|
adamc@144
|
1106 (TRecord i, _) =>
|
adamc@144
|
1107 let
|
adamc@144
|
1108 val xts = E.lookupStruct env i
|
adamc@144
|
1109 in
|
adamc@144
|
1110 (List.drop (ts, 1),
|
adamc@144
|
1111 box [box (map (fn (x, t) => box [p_typ env t,
|
adamc@144
|
1112 space,
|
adamc@144
|
1113 string "lw_input_",
|
adamc@144
|
1114 string x,
|
adamc@144
|
1115 string ";",
|
adamc@144
|
1116 newline]) xts),
|
adamc@144
|
1117 newline,
|
adamc@144
|
1118 box (map (fn (x, t) =>
|
adamc@144
|
1119 let
|
adamc@144
|
1120 val n = case SM.find (fnums, x) of
|
adamc@144
|
1121 NONE => raise Fail "CjrPrint: Can't find in fnums"
|
adamc@144
|
1122 | SOME n => n
|
adamc@190
|
1123
|
adamc@190
|
1124 val f = case t of
|
adamc@190
|
1125 (TFfi ("Basis", "bool"), _) => "optional_"
|
adamc@190
|
1126 | _ => ""
|
adamc@144
|
1127 in
|
adamc@190
|
1128 box [string "request = lw_get_",
|
adamc@190
|
1129 string f,
|
adamc@190
|
1130 string "input(ctx, ",
|
adamc@144
|
1131 string (Int.toString n),
|
adamc@144
|
1132 string ");",
|
adamc@144
|
1133 newline,
|
adamc@144
|
1134 string "if (request == NULL) {",
|
adamc@144
|
1135 newline,
|
adamc@144
|
1136 box [string "printf(\"Missing input ",
|
adamc@144
|
1137 string x,
|
adamc@144
|
1138 string "\\n\");",
|
adamc@144
|
1139 newline,
|
adamc@144
|
1140 string "exit(1);"],
|
adamc@144
|
1141 newline,
|
adamc@144
|
1142 string "}",
|
adamc@144
|
1143 newline,
|
adamc@144
|
1144 string "lw_input_",
|
adamc@144
|
1145 string x,
|
adamc@144
|
1146 space,
|
adamc@144
|
1147 string "=",
|
adamc@144
|
1148 space,
|
adamc@144
|
1149 unurlify t,
|
adamc@144
|
1150 string ";",
|
adamc@144
|
1151 newline]
|
adamc@144
|
1152 end) xts),
|
adamc@144
|
1153 string "struct __lws_",
|
adamc@144
|
1154 string (Int.toString i),
|
adamc@144
|
1155 space,
|
adamc@144
|
1156 string "lw_inputs",
|
adamc@144
|
1157 space,
|
adamc@144
|
1158 string "= {",
|
adamc@144
|
1159 newline,
|
adamc@144
|
1160 box (map (fn (x, _) => box [string "lw_input_",
|
adamc@144
|
1161 string x,
|
adamc@144
|
1162 string ",",
|
adamc@144
|
1163 newline]) xts),
|
adamc@144
|
1164 string "};",
|
adamc@144
|
1165 newline],
|
adamc@144
|
1166 box [string ",",
|
adamc@144
|
1167 space,
|
adamc@144
|
1168 string "lw_inputs"])
|
adamc@144
|
1169 end
|
adamc@144
|
1170
|
adamc@144
|
1171 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
|
adamc@144
|
1172 in
|
adamc@144
|
1173 box [string "if (!strncmp(request, \"",
|
adamc@144
|
1174 string (String.toString s),
|
adamc@144
|
1175 string "\", ",
|
adamc@144
|
1176 string (Int.toString (size s)),
|
adamc@198
|
1177 string ") && (request[",
|
adamc@198
|
1178 string (Int.toString (size s)),
|
adamc@198
|
1179 string "] == 0 || request[",
|
adamc@198
|
1180 string (Int.toString (size s)),
|
adamc@198
|
1181 string "] == '/')) {",
|
adamc@144
|
1182 newline,
|
adamc@144
|
1183 string "request += ",
|
adamc@144
|
1184 string (Int.toString (size s)),
|
adamc@144
|
1185 string ";",
|
adamc@144
|
1186 newline,
|
adamc@144
|
1187 string "if (*request == '/') ++request;",
|
adamc@144
|
1188 newline,
|
adamc@144
|
1189 box [string "{",
|
adamc@144
|
1190 newline,
|
adamc@144
|
1191 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
|
adamc@144
|
1192 space,
|
adamc@144
|
1193 string "arg",
|
adamc@144
|
1194 string (Int.toString i),
|
adamc@144
|
1195 space,
|
adamc@144
|
1196 string "=",
|
adamc@144
|
1197 space,
|
adamc@144
|
1198 unurlify t,
|
adamc@144
|
1199 string ";",
|
adamc@144
|
1200 newline]) ts),
|
adamc@144
|
1201 defInputs,
|
adamc@144
|
1202 p_enamed env n,
|
adamc@144
|
1203 string "(",
|
adamc@144
|
1204 p_list_sep (box [string ",", space])
|
adamc@144
|
1205 (fn x => x)
|
adamc@272
|
1206 (string "ctx"
|
adamc@272
|
1207 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts
|
adamc@272
|
1208 @ [string "lw_unit_v"]),
|
adamc@144
|
1209 inputsVar,
|
adamc@144
|
1210 string ");",
|
adamc@144
|
1211 newline,
|
adamc@144
|
1212 string "return;",
|
adamc@144
|
1213 newline,
|
adamc@144
|
1214 string "}",
|
adamc@144
|
1215 newline,
|
adamc@144
|
1216 string "}"]
|
adamc@144
|
1217 ]
|
adamc@144
|
1218 end
|
adamc@144
|
1219
|
adamc@144
|
1220 val pds' = map p_page ps
|
adamc@275
|
1221
|
adamc@275
|
1222 val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts)
|
adamc@275
|
1223 | _ => NONE) ds
|
adamc@275
|
1224
|
adamc@275
|
1225 val validate =
|
adamc@275
|
1226 box [string "static void lw_db_validate(lw_context ctx) {",
|
adamc@275
|
1227 newline,
|
adamc@275
|
1228 string "PGconn *conn = lw_get_db(ctx);",
|
adamc@275
|
1229 newline,
|
adamc@275
|
1230 string "PGresult *res;",
|
adamc@275
|
1231 newline,
|
adamc@275
|
1232 newline,
|
adamc@275
|
1233 p_list_sep newline
|
adamc@275
|
1234 (fn (s, xts) =>
|
adamc@275
|
1235 let
|
adamc@275
|
1236 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
|
adamc@275
|
1237 ^ s ^ "'"
|
adamc@275
|
1238
|
adamc@275
|
1239 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@275
|
1240 s,
|
adamc@275
|
1241 "') AND (",
|
adamc@275
|
1242 String.concatWith " OR "
|
adamc@275
|
1243 (map (fn (x, t) =>
|
adamc@275
|
1244 String.concat ["(attname = 'lw_",
|
adamc@275
|
1245 CharVector.map
|
adamc@275
|
1246 Char.toLower x,
|
adamc@275
|
1247 "' AND atttypid = (SELECT oid FROM pg_type",
|
adamc@275
|
1248 " WHERE typname = '",
|
adamc@275
|
1249 p_sqltype' env t,
|
adamc@275
|
1250 "'))"]) xts),
|
adamc@275
|
1251 ")"]
|
adamc@275
|
1252
|
adamc@275
|
1253 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
|
adamc@275
|
1254 s,
|
adamc@275
|
1255 "') AND attnum >= 0"]
|
adamc@275
|
1256 in
|
adamc@275
|
1257 box [string "res = PQexec(conn, \"",
|
adamc@275
|
1258 string q,
|
adamc@275
|
1259 string "\");",
|
adamc@275
|
1260 newline,
|
adamc@275
|
1261 newline,
|
adamc@275
|
1262 string "if (res == NULL) {",
|
adamc@275
|
1263 newline,
|
adamc@275
|
1264 box [string "PQfinish(conn);",
|
adamc@275
|
1265 newline,
|
adamc@275
|
1266 string "lw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1267 newline],
|
adamc@275
|
1268 string "}",
|
adamc@275
|
1269 newline,
|
adamc@275
|
1270 newline,
|
adamc@275
|
1271 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1272 newline,
|
adamc@275
|
1273 box [string "char msg[1024];",
|
adamc@275
|
1274 newline,
|
adamc@275
|
1275 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1276 newline,
|
adamc@275
|
1277 string "msg[1023] = 0;",
|
adamc@275
|
1278 newline,
|
adamc@275
|
1279 string "PQclear(res);",
|
adamc@275
|
1280 newline,
|
adamc@275
|
1281 string "PQfinish(conn);",
|
adamc@275
|
1282 newline,
|
adamc@275
|
1283 string "lw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1284 string q,
|
adamc@275
|
1285 string "\\n%s\", msg);",
|
adamc@275
|
1286 newline],
|
adamc@275
|
1287 string "}",
|
adamc@275
|
1288 newline,
|
adamc@275
|
1289 newline,
|
adamc@275
|
1290 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@275
|
1291 newline,
|
adamc@275
|
1292 box [string "PQclear(res);",
|
adamc@275
|
1293 newline,
|
adamc@275
|
1294 string "PQfinish(conn);",
|
adamc@275
|
1295 newline,
|
adamc@275
|
1296 string "lw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1297 string s,
|
adamc@275
|
1298 string "' does not exist.\");",
|
adamc@275
|
1299 newline],
|
adamc@275
|
1300 string "}",
|
adamc@275
|
1301 newline,
|
adamc@275
|
1302 newline,
|
adamc@275
|
1303 string "PQclear(res);",
|
adamc@275
|
1304 newline,
|
adamc@275
|
1305
|
adamc@275
|
1306 string "res = PQexec(conn, \"",
|
adamc@275
|
1307 string q',
|
adamc@275
|
1308 string "\");",
|
adamc@275
|
1309 newline,
|
adamc@275
|
1310 newline,
|
adamc@275
|
1311 string "if (res == NULL) {",
|
adamc@275
|
1312 newline,
|
adamc@275
|
1313 box [string "PQfinish(conn);",
|
adamc@275
|
1314 newline,
|
adamc@275
|
1315 string "lw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1316 newline],
|
adamc@275
|
1317 string "}",
|
adamc@275
|
1318 newline,
|
adamc@275
|
1319 newline,
|
adamc@275
|
1320 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1321 newline,
|
adamc@275
|
1322 box [string "char msg[1024];",
|
adamc@275
|
1323 newline,
|
adamc@275
|
1324 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1325 newline,
|
adamc@275
|
1326 string "msg[1023] = 0;",
|
adamc@275
|
1327 newline,
|
adamc@275
|
1328 string "PQclear(res);",
|
adamc@275
|
1329 newline,
|
adamc@275
|
1330 string "PQfinish(conn);",
|
adamc@275
|
1331 newline,
|
adamc@275
|
1332 string "lw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1333 string q',
|
adamc@275
|
1334 string "\\n%s\", msg);",
|
adamc@275
|
1335 newline],
|
adamc@275
|
1336 string "}",
|
adamc@275
|
1337 newline,
|
adamc@275
|
1338 newline,
|
adamc@275
|
1339 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
1340 string (Int.toString (length xts)),
|
adamc@275
|
1341 string "\")) {",
|
adamc@275
|
1342 newline,
|
adamc@275
|
1343 box [string "PQclear(res);",
|
adamc@275
|
1344 newline,
|
adamc@275
|
1345 string "PQfinish(conn);",
|
adamc@275
|
1346 newline,
|
adamc@275
|
1347 string "lw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1348 string s,
|
adamc@275
|
1349 string "' has the wrong column types.\");",
|
adamc@275
|
1350 newline],
|
adamc@275
|
1351 string "}",
|
adamc@275
|
1352 newline,
|
adamc@275
|
1353 newline,
|
adamc@275
|
1354 string "PQclear(res);",
|
adamc@275
|
1355 newline,
|
adamc@275
|
1356 newline,
|
adamc@275
|
1357
|
adamc@275
|
1358 string "res = PQexec(conn, \"",
|
adamc@275
|
1359 string q'',
|
adamc@275
|
1360 string "\");",
|
adamc@275
|
1361 newline,
|
adamc@275
|
1362 newline,
|
adamc@275
|
1363 string "if (res == NULL) {",
|
adamc@275
|
1364 newline,
|
adamc@275
|
1365 box [string "PQfinish(conn);",
|
adamc@275
|
1366 newline,
|
adamc@275
|
1367 string "lw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@275
|
1368 newline],
|
adamc@275
|
1369 string "}",
|
adamc@275
|
1370 newline,
|
adamc@275
|
1371 newline,
|
adamc@275
|
1372 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@275
|
1373 newline,
|
adamc@275
|
1374 box [string "char msg[1024];",
|
adamc@275
|
1375 newline,
|
adamc@275
|
1376 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@275
|
1377 newline,
|
adamc@275
|
1378 string "msg[1023] = 0;",
|
adamc@275
|
1379 newline,
|
adamc@275
|
1380 string "PQclear(res);",
|
adamc@275
|
1381 newline,
|
adamc@275
|
1382 string "PQfinish(conn);",
|
adamc@275
|
1383 newline,
|
adamc@275
|
1384 string "lw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@275
|
1385 string q'',
|
adamc@275
|
1386 string "\\n%s\", msg);",
|
adamc@275
|
1387 newline],
|
adamc@275
|
1388 string "}",
|
adamc@275
|
1389 newline,
|
adamc@275
|
1390 newline,
|
adamc@275
|
1391 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@275
|
1392 string (Int.toString (length xts)),
|
adamc@275
|
1393 string "\")) {",
|
adamc@275
|
1394 newline,
|
adamc@275
|
1395 box [string "PQclear(res);",
|
adamc@275
|
1396 newline,
|
adamc@275
|
1397 string "PQfinish(conn);",
|
adamc@275
|
1398 newline,
|
adamc@275
|
1399 string "lw_error(ctx, FATAL, \"Table '",
|
adamc@275
|
1400 string s,
|
adamc@275
|
1401 string "' has extra columns.\");",
|
adamc@275
|
1402 newline],
|
adamc@275
|
1403 string "}",
|
adamc@275
|
1404 newline,
|
adamc@275
|
1405 newline,
|
adamc@275
|
1406 string "PQclear(res);",
|
adamc@275
|
1407 newline]
|
adamc@275
|
1408 end) tables,
|
adamc@275
|
1409 string "}"]
|
adamc@29
|
1410 in
|
adamc@144
|
1411 box [string "#include <stdio.h>",
|
adamc@144
|
1412 newline,
|
adamc@144
|
1413 string "#include <stdlib.h>",
|
adamc@144
|
1414 newline,
|
adamc@272
|
1415 string "#include <string.h>",
|
adamc@272
|
1416 newline,
|
adamc@272
|
1417 string "#include <postgresql/libpq-fe.h>",
|
adamc@272
|
1418 newline,
|
adamc@144
|
1419 newline,
|
adamc@244
|
1420 string "#include \"urweb.h\"",
|
adamc@101
|
1421 newline,
|
adamc@101
|
1422 newline,
|
adamc@101
|
1423 p_list_sep newline (fn x => x) pds,
|
adamc@101
|
1424 newline,
|
adamc@144
|
1425 string "int lw_inputs_len = ",
|
adamc@144
|
1426 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
|
adamc@144
|
1427 string ";",
|
adamc@144
|
1428 newline,
|
adamc@144
|
1429 newline,
|
adamc@144
|
1430 string "int lw_input_num(char *name) {",
|
adamc@144
|
1431 newline,
|
adamc@144
|
1432 makeSwitch (fnums, 0),
|
adamc@144
|
1433 string "}",
|
adamc@144
|
1434 newline,
|
adamc@144
|
1435 newline,
|
adamc@117
|
1436 string "void lw_handle(lw_context ctx, char *request) {",
|
adamc@101
|
1437 newline,
|
adamc@101
|
1438 p_list_sep newline (fn x => x) pds',
|
adamc@101
|
1439 newline,
|
adamc@101
|
1440 string "}",
|
adamc@275
|
1441 newline,
|
adamc@275
|
1442 newline,
|
adamc@275
|
1443 validate,
|
adamc@101
|
1444 newline]
|
adamc@29
|
1445 end
|
adamc@29
|
1446
|
adamc@274
|
1447 fun p_sql env (ds, _) =
|
adamc@274
|
1448 let
|
adamc@274
|
1449 val (pps, _) = ListUtil.foldlMap
|
adamc@274
|
1450 (fn (dAll as (d, _), env) =>
|
adamc@274
|
1451 let
|
adamc@274
|
1452 val pp = case d of
|
adamc@274
|
1453 DTable (s, xts) =>
|
adamc@274
|
1454 box [string "CREATE TABLE ",
|
adamc@274
|
1455 string s,
|
adamc@274
|
1456 string "(",
|
adamc@274
|
1457 p_list (fn (x, t) =>
|
adamc@274
|
1458 box [string "lw_",
|
adamc@275
|
1459 string (CharVector.map Char.toLower x),
|
adamc@274
|
1460 space,
|
adamc@274
|
1461 p_sqltype env t,
|
adamc@274
|
1462 space,
|
adamc@274
|
1463 string "NOT",
|
adamc@274
|
1464 space,
|
adamc@274
|
1465 string "NULL"]) xts,
|
adamc@274
|
1466 string ");",
|
adamc@274
|
1467 newline,
|
adamc@274
|
1468 newline]
|
adamc@274
|
1469 | _ => box []
|
adamc@274
|
1470 in
|
adamc@274
|
1471 (pp, E.declBinds env dAll)
|
adamc@274
|
1472 end)
|
adamc@274
|
1473 env ds
|
adamc@274
|
1474 in
|
adamc@274
|
1475 box pps
|
adamc@274
|
1476 end
|
adamc@274
|
1477
|
adamc@29
|
1478 end
|