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