annotate src/cjr_print.sml @ 282:0236d9412ad2

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