annotate src/cjr_print.sml @ 438:1c27f03d9bd2

Reading timestamps from SQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 30 Oct 2008 14:57:15 -0400
parents 659c17441250
children 322c8620bbdf
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@316 60 val ident = String.translate (fn #"'" => "PRIME"
adamc@316 61 | ch => str ch)
adamc@316 62
adamc@316 63 val p_ident = string o ident
adamc@316 64
adamc@29 65 fun p_typ' par env (t, loc) =
adamc@29 66 case t of
adamc@269 67 TFun (t1, t2) => parenIf par (box [p_typ' true env t2,
adamc@109 68 space,
adamc@109 69 string "(*)",
adamc@109 70 space,
adamc@109 71 string "(",
adamc@109 72 p_typ env t1,
adamc@109 73 string ")"])
adamc@29 74 | TRecord i => box [string "struct",
adamc@29 75 space,
adamc@311 76 string "__uws_",
adamc@29 77 string (Int.toString i)]
adamc@188 78 | TDatatype (Enum, n, _) =>
adamc@188 79 (box [string "enum",
adamc@188 80 space,
adamc@311 81 string ("__uwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
adamc@311 82 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@198 83 | TDatatype (Option, n, xncs) =>
adamc@198 84 (case ListUtil.search #3 (!xncs) of
adamc@198 85 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
adamc@198 86 | SOME t =>
adamc@198 87 case #1 t of
adamc@198 88 TDatatype _ => p_typ' par env t
adamc@199 89 | TFfi ("Basis", "string") => p_typ' par env t
adamc@198 90 | _ => box [p_typ' par env t,
adamc@198 91 string "*"])
adamc@188 92 | TDatatype (Default, n, _) =>
adamc@165 93 (box [string "struct",
adamc@165 94 space,
adamc@311 95 string ("__uwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
adamc@311 96 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@316 97 | TFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@288 98 | TOption t =>
adamc@288 99 (case #1 t of
adamc@288 100 TDatatype _ => p_typ' par env t
adamc@288 101 | TFfi ("Basis", "string") => p_typ' par env t
adamc@288 102 | _ => box [p_typ' par env t,
adamc@288 103 string "*"])
adamc@29 104
adamc@29 105 and p_typ env = p_typ' false env
adamc@29 106
adamc@316 107 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@311 108 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 109
adamc@109 110 fun p_enamed env n =
adamc@316 111 string ("__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n)
adamc@311 112 handle CjrEnv.UnboundNamed _ => string ("__uwn_UNBOUND_" ^ Int.toString n)
adamc@109 113
adamc@182 114 fun p_con_named env n =
adamc@316 115 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
adamc@311 116 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
adamc@182 117
adamc@182 118 fun p_pat_preamble env (p, _) =
adamc@182 119 case p of
adamc@182 120 PWild => (box [],
adamc@182 121 env)
adamc@182 122 | PVar (x, t) => (box [p_typ env t,
adamc@182 123 space,
adamc@311 124 string "__uwr_",
adamc@316 125 p_ident x,
adamc@182 126 string "_",
adamc@182 127 string (Int.toString (E.countERels env)),
adamc@182 128 string ";",
adamc@182 129 newline],
adamc@196 130 E.pushERel env x t)
adamc@182 131 | PPrim _ => (box [], env)
adamc@188 132 | PCon (_, _, NONE) => (box [], env)
adamc@188 133 | PCon (_, _, SOME p) => p_pat_preamble env p
adamc@182 134 | PRecord xps =>
adamc@182 135 foldl (fn ((_, p, _), (pp, env)) =>
adamc@182 136 let
adamc@182 137 val (pp', env) = p_pat_preamble env p
adamc@182 138 in
adamc@182 139 (box [pp', pp], env)
adamc@182 140 end) (box [], env) xps
adamc@288 141 | PNone _ => (box [], env)
adamc@288 142 | PSome (_, p) => p_pat_preamble env p
adamc@182 143
adamc@182 144 fun p_patCon env pc =
adamc@182 145 case pc of
adamc@182 146 PConVar n => p_con_named env n
adamc@316 147 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
adamc@182 148
adamc@182 149 fun p_pat (env, exit, depth) (p, _) =
adamc@182 150 case p of
adamc@182 151 PWild =>
adamc@182 152 (box [], env)
adamc@182 153 | PVar (x, t) =>
adamc@311 154 (box [string "__uwr_",
adamc@316 155 p_ident x,
adamc@182 156 string "_",
adamc@182 157 string (Int.toString (E.countERels env)),
adamc@182 158 space,
adamc@182 159 string "=",
adamc@182 160 space,
adamc@182 161 string "disc",
adamc@182 162 string (Int.toString depth),
adamc@182 163 string ";"],
adamc@182 164 E.pushERel env x t)
adamc@182 165 | PPrim (Prim.Int n) =>
adamc@182 166 (box [string "if",
adamc@182 167 space,
adamc@182 168 string "(disc",
adamc@182 169 string (Int.toString depth),
adamc@182 170 space,
adamc@182 171 string "!=",
adamc@182 172 space,
adamc@276 173 Prim.p_t_GCC (Prim.Int n),
adamc@182 174 string ")",
adamc@182 175 space,
adamc@182 176 exit],
adamc@182 177 env)
adamc@182 178 | PPrim (Prim.String s) =>
adamc@182 179 (box [string "if",
adamc@182 180 space,
adamc@182 181 string "(strcmp(disc",
adamc@182 182 string (Int.toString depth),
adamc@182 183 string ",",
adamc@182 184 space,
adamc@276 185 Prim.p_t_GCC (Prim.String s),
adamc@182 186 string "))",
adamc@182 187 space,
adamc@182 188 exit],
adamc@182 189 env)
adamc@182 190 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
adamc@182 191
adamc@188 192 | PCon (dk, pc, po) =>
adamc@182 193 let
adamc@182 194 val (p, env) =
adamc@182 195 case po of
adamc@182 196 NONE => (box [], env)
adamc@182 197 | SOME p =>
adamc@182 198 let
adamc@182 199 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 200
adamc@182 201 val (x, to) = case pc of
adamc@182 202 PConVar n =>
adamc@182 203 let
adamc@182 204 val (x, to, _) = E.lookupConstructor env n
adamc@182 205 in
adamc@316 206 ("uw_" ^ ident x, to)
adamc@182 207 end
adamc@188 208 | PConFfi {mod = m, con, arg, ...} =>
adamc@316 209 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
adamc@182 210
adamc@182 211 val t = case to of
adamc@182 212 NONE => raise Fail "CjrPrint: Constructor mismatch"
adamc@182 213 | SOME t => t
adamc@182 214 in
adamc@182 215 (box [string "{",
adamc@182 216 newline,
adamc@182 217 p_typ env t,
adamc@182 218 space,
adamc@182 219 string "disc",
adamc@182 220 string (Int.toString (depth + 1)),
adamc@182 221 space,
adamc@182 222 string "=",
adamc@182 223 space,
adamc@198 224 case dk of
adamc@198 225 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
adamc@198 226 | Default => box [string "disc",
adamc@198 227 string (Int.toString depth),
adamc@198 228 string "->data.",
adamc@198 229 string x]
adamc@198 230 | Option =>
adamc@198 231 case #1 t of
adamc@198 232 TDatatype _ => box [string "disc",
adamc@198 233 string (Int.toString depth)]
adamc@199 234 | TFfi ("Basis", "string") => box [string "disc",
adamc@199 235 string (Int.toString depth)]
adamc@198 236 | _ => box [string "*disc",
adamc@198 237 string (Int.toString depth)],
adamc@182 238 string ";",
adamc@182 239 newline,
adamc@182 240 p,
adamc@182 241 newline,
adamc@182 242 string "}"],
adamc@182 243 env)
adamc@182 244 end
adamc@182 245 in
adamc@182 246 (box [string "if",
adamc@182 247 space,
adamc@182 248 string "(disc",
adamc@182 249 string (Int.toString depth),
adamc@198 250 case (dk, po) of
adamc@198 251 (Enum, _) => box [space,
adamc@198 252 string "!=",
adamc@198 253 space,
adamc@198 254 p_patCon env pc]
adamc@198 255 | (Default, _) => box [string "->tag",
adamc@198 256 space,
adamc@198 257 string "!=",
adamc@198 258 space,
adamc@198 259 p_patCon env pc]
adamc@198 260 | (Option, NONE) => box [space,
adamc@198 261 string "!=",
adamc@198 262 space,
adamc@198 263 string "NULL"]
adamc@198 264 | (Option, SOME _) => box [space,
adamc@198 265 string "==",
adamc@198 266 space,
adamc@198 267 string "NULL"],
adamc@182 268 string ")",
adamc@182 269 space,
adamc@182 270 exit,
adamc@182 271 newline,
adamc@182 272 p],
adamc@182 273 env)
adamc@182 274 end
adamc@182 275
adamc@182 276 | PRecord xps =>
adamc@182 277 let
adamc@182 278 val (xps, env) =
adamc@182 279 ListUtil.foldlMap (fn ((x, p, t), env) =>
adamc@182 280 let
adamc@182 281 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 282
adamc@182 283 val p = box [string "{",
adamc@182 284 newline,
adamc@182 285 p_typ env t,
adamc@182 286 space,
adamc@182 287 string "disc",
adamc@182 288 string (Int.toString (depth + 1)),
adamc@182 289 space,
adamc@182 290 string "=",
adamc@182 291 space,
adamc@182 292 string "disc",
adamc@182 293 string (Int.toString depth),
adamc@311 294 string ".__uwf_",
adamc@316 295 p_ident x,
adamc@182 296 string ";",
adamc@182 297 newline,
adamc@182 298 p,
adamc@182 299 newline,
adamc@182 300 string "}"]
adamc@182 301 in
adamc@182 302 (p, env)
adamc@182 303 end) env xps
adamc@182 304 in
adamc@182 305 (p_list_sep newline (fn x => x) xps,
adamc@182 306 env)
adamc@182 307 end
adamc@182 308
adamc@288 309 | PNone t =>
adamc@288 310 (box [string "if",
adamc@288 311 space,
adamc@288 312 string "(disc",
adamc@288 313 string (Int.toString depth),
adamc@288 314 space,
adamc@288 315 string "!=",
adamc@288 316 space,
adamc@288 317 string "NULL)",
adamc@288 318 space,
adamc@288 319 exit,
adamc@288 320 newline],
adamc@288 321 env)
adamc@288 322
adamc@288 323 | PSome (t, p) =>
adamc@288 324 let
adamc@288 325 val (p, env) =
adamc@288 326 let
adamc@288 327 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@288 328 in
adamc@288 329 (box [string "{",
adamc@288 330 newline,
adamc@288 331 p_typ env t,
adamc@288 332 space,
adamc@288 333 string "disc",
adamc@288 334 string (Int.toString (depth + 1)),
adamc@288 335 space,
adamc@288 336 string "=",
adamc@288 337 space,
adamc@288 338 case #1 t of
adamc@288 339 TDatatype _ => box [string "disc",
adamc@288 340 string (Int.toString depth)]
adamc@288 341 | TFfi ("Basis", "string") => box [string "disc",
adamc@288 342 string (Int.toString depth)]
adamc@288 343 | _ => box [string "*disc",
adamc@288 344 string (Int.toString depth)],
adamc@288 345 string ";",
adamc@288 346 newline,
adamc@288 347 p,
adamc@288 348 newline,
adamc@288 349 string "}"],
adamc@288 350 env)
adamc@288 351 end
adamc@288 352 in
adamc@288 353 (box [string "if",
adamc@288 354 space,
adamc@288 355 string "(disc",
adamc@288 356 string (Int.toString depth),
adamc@288 357 space,
adamc@288 358 string "==",
adamc@288 359 space,
adamc@288 360 string "NULL)",
adamc@288 361 space,
adamc@288 362 exit,
adamc@288 363 newline,
adamc@288 364 p],
adamc@288 365 env)
adamc@288 366 end
adamc@288 367
adamc@182 368 local
adamc@182 369 val count = ref 0
adamc@182 370 in
adamc@182 371 fun newGoto () =
adamc@182 372 let
adamc@182 373 val r = !count
adamc@182 374 in
adamc@182 375 count := r + 1;
adamc@182 376 string ("L" ^ Int.toString r)
adamc@182 377 end
adamc@182 378 end
adamc@182 379
adamc@185 380 fun patConInfo env pc =
adamc@185 381 case pc of
adamc@185 382 PConVar n =>
adamc@185 383 let
adamc@185 384 val (x, _, dn) = E.lookupConstructor env n
adamc@185 385 val (dx, _) = E.lookupDatatype env dn
adamc@185 386 in
adamc@316 387 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
adamc@316 388 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
adamc@316 389 "uw_" ^ ident x)
adamc@185 390 end
adamc@186 391 | PConFfi {mod = m, datatyp, con, ...} =>
adamc@316 392 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
adamc@316 393 "uw_" ^ ident m ^ "_" ^ ident con,
adamc@316 394 "uw_" ^ ident con)
adamc@185 395
adamc@324 396 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e =
adamc@278 397 case t of
adamc@311 398 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
adamc@311 399 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
adamc@324 400 | TFfi ("Basis", "string") =>
adamc@324 401 if wontLeakStrings then
adamc@324 402 e
adamc@324 403 else
adamc@324 404 box [string "uw_Basis_strdup(ctx, ", e, string ")"]
adamc@311 405 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
adamc@438 406 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
adamc@278 407 | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
adamc@278 408 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@278 409 string "ERROR")
adamc@278 410
adamc@282 411 datatype sql_type =
adamc@282 412 Int
adamc@282 413 | Float
adamc@282 414 | String
adamc@282 415 | Bool
adamc@282 416
adamc@282 417 fun p_sql_type t =
adamc@282 418 string (case t of
adamc@311 419 Int => "uw_Basis_int"
adamc@311 420 | Float => "uw_Basis_float"
adamc@311 421 | String => "uw_Basis_string"
adamc@311 422 | Bool => "uw_Basis_bool")
adamc@282 423
adamc@282 424 fun getPargs (e, _) =
adamc@282 425 case e of
adamc@282 426 EPrim (Prim.String _) => []
adamc@282 427 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
adamc@282 428
adamc@282 429 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
adamc@282 430 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
adamc@282 431 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
adamc@282 432 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
adamc@322 433 | ECase (e, _, _) => [(e, Bool)]
adamc@282 434
adamc@282 435 | _ => raise Fail "CjrPrint: getPargs"
adamc@282 436
adamc@282 437 fun p_ensql t e =
adamc@282 438 case t of
adamc@311 439 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
adamc@311 440 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
adamc@282 441 | String => e
adamc@295 442 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
adamc@282 443
adamc@282 444 fun p_ensql_len t e =
adamc@282 445 case t of
adamc@311 446 Int => string "sizeof(uw_Basis_int)"
adamc@311 447 | Float => string "sizeof(uw_Basis_float)"
adamc@282 448 | String => box [string "strlen(", e, string ")"]
adamc@311 449 | Bool => string "sizeof(uw_Basis_bool)"
adamc@282 450
adamc@324 451 fun notLeaky env allowHeapAllocated =
adamc@324 452 let
adamc@324 453 fun nl (t, _) =
adamc@324 454 case t of
adamc@324 455 TFun _ => false
adamc@324 456 | TRecord n =>
adamc@324 457 let
adamc@324 458 val xts = E.lookupStruct env n
adamc@324 459 in
adamc@324 460 List.all (fn (_, t) => nl t) xts
adamc@324 461 end
adamc@324 462 | TDatatype (dk, _, ref cons) =>
adamc@324 463 (allowHeapAllocated orelse dk = Enum)
adamc@324 464 andalso List.all (fn (_, _, to) => case to of
adamc@324 465 NONE => true
adamc@324 466 | SOME t => nl t) cons
adamc@324 467 | TFfi ("Basis", "string") => false
adamc@324 468 | TFfi _ => true
adamc@324 469 | TOption t => allowHeapAllocated andalso nl t
adamc@324 470 in
adamc@324 471 nl
adamc@324 472 end
adamc@324 473
adamc@182 474 fun p_exp' par env (e, loc) =
adamc@29 475 case e of
adamc@276 476 EPrim p => Prim.p_t_GCC p
adamc@29 477 | ERel n => p_rel env n
adamc@109 478 | ENamed n => p_enamed env n
adamc@188 479 | ECon (Enum, pc, _) => p_patCon env pc
adamc@198 480 | ECon (Option, pc, NONE) => string "NULL"
adamc@198 481 | ECon (Option, pc, SOME e) =>
adamc@198 482 let
adamc@198 483 val to = case pc of
adamc@198 484 PConVar n => #2 (E.lookupConstructor env n)
adamc@198 485 | PConFfi {arg, ...} => arg
adamc@198 486
adamc@198 487 val t = case to of
adamc@198 488 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
adamc@198 489 | SOME t => t
adamc@198 490 in
adamc@198 491 case #1 t of
adamc@198 492 TDatatype _ => p_exp' par env e
adamc@199 493 | TFfi ("Basis", "string") => p_exp' par env e
adamc@198 494 | _ => box [string "({",
adamc@198 495 newline,
adamc@198 496 p_typ env t,
adamc@198 497 space,
adamc@198 498 string "*tmp",
adamc@198 499 space,
adamc@198 500 string "=",
adamc@198 501 space,
adamc@311 502 string "uw_malloc(ctx, sizeof(",
adamc@198 503 p_typ env t,
adamc@198 504 string "));",
adamc@198 505 newline,
adamc@198 506 string "*tmp",
adamc@198 507 space,
adamc@198 508 string "=",
adamc@198 509 p_exp' par env e,
adamc@198 510 string ";",
adamc@198 511 newline,
adamc@198 512 string "tmp;",
adamc@198 513 newline,
adamc@198 514 string "})"]
adamc@198 515 end
adamc@188 516 | ECon (Default, pc, eo) =>
adamc@181 517 let
adamc@196 518 val (xd, xc, xn) = patConInfo env pc
adamc@181 519 in
adamc@182 520 box [string "({",
adamc@181 521 newline,
adamc@181 522 string "struct",
adamc@181 523 space,
adamc@185 524 string xd,
adamc@181 525 space,
adamc@181 526 string "*tmp",
adamc@181 527 space,
adamc@181 528 string "=",
adamc@181 529 space,
adamc@311 530 string "uw_malloc(ctx, sizeof(struct ",
adamc@185 531 string xd,
adamc@181 532 string "));",
adamc@181 533 newline,
adamc@181 534 string "tmp->tag",
adamc@181 535 space,
adamc@181 536 string "=",
adamc@181 537 space,
adamc@185 538 string xc,
adamc@181 539 string ";",
adamc@181 540 newline,
adamc@181 541 case eo of
adamc@181 542 NONE => box []
adamc@185 543 | SOME e => box [string "tmp->data.",
adamc@196 544 string xn,
adamc@181 545 space,
adamc@181 546 string "=",
adamc@181 547 space,
adamc@181 548 p_exp env e,
adamc@181 549 string ";",
adamc@181 550 newline],
adamc@181 551 string "tmp;",
adamc@181 552 newline,
adamc@181 553 string "})"]
adamc@181 554 end
adamc@297 555 | ENone _ => string "NULL"
adamc@290 556 | ESome (t, e) =>
adamc@290 557 (case #1 t of
adamc@290 558 TDatatype _ => p_exp' par env e
adamc@290 559 | TFfi ("Basis", "string") => p_exp' par env e
adamc@290 560 | _ => box [string "({",
adamc@290 561 newline,
adamc@290 562 p_typ env t,
adamc@290 563 space,
adamc@290 564 string "*tmp",
adamc@290 565 space,
adamc@290 566 string "=",
adamc@290 567 space,
adamc@311 568 string "uw_malloc(ctx, sizeof(",
adamc@290 569 p_typ env t,
adamc@290 570 string "));",
adamc@290 571 newline,
adamc@290 572 string "*tmp",
adamc@290 573 space,
adamc@290 574 string "=",
adamc@290 575 p_exp' par env e,
adamc@290 576 string ";",
adamc@290 577 newline,
adamc@290 578 string "tmp;",
adamc@290 579 newline,
adamc@290 580 string "})"])
adamc@109 581
adamc@316 582 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@283 583 | EError (e, t) =>
adamc@283 584 box [string "({",
adamc@283 585 newline,
adamc@283 586 p_typ env t,
adamc@283 587 space,
adamc@283 588 string "tmp;",
adamc@283 589 newline,
adamc@311 590 string "uw_error(ctx, FATAL, \"",
adamc@292 591 string (ErrorMsg.spanToString loc),
adamc@292 592 string ": %s\", ",
adamc@283 593 p_exp env e,
adamc@283 594 string ");",
adamc@283 595 newline,
adamc@283 596 string "tmp;",
adamc@283 597 newline,
adamc@283 598 string "})"]
adamc@311 599 | EFfiApp (m, x, es) => box [string "uw_",
adamc@316 600 p_ident m,
adamc@53 601 string "_",
adamc@316 602 p_ident x,
adamc@117 603 string "(ctx, ",
adamc@53 604 p_list (p_exp env) es,
adamc@53 605 string ")"]
adamc@316 606 | EApp (f, args) =>
adamc@316 607 parenIf par (box [p_exp' true env f,
adamc@316 608 string "(ctx,",
adamc@316 609 space,
adamc@316 610 p_list_sep (box [string ",", space]) (p_exp env) args,
adamc@316 611 string ")"])
adamc@29 612
adamc@387 613 | EUnop (s, e1) =>
adamc@387 614 parenIf par (box [string s,
adamc@387 615 space,
adamc@387 616 p_exp' true env e1])
adamc@387 617
adamc@387 618 | EBinop (s, e1, e2) =>
adamc@389 619 if Char.isAlpha (String.sub (s, size s - 1)) then
adamc@389 620 box [string s,
adamc@390 621 string "(",
adamc@389 622 p_exp env e1,
adamc@389 623 string ",",
adamc@389 624 space,
adamc@389 625 p_exp env e2,
adamc@389 626 string ")"]
adamc@389 627 else
adamc@389 628 parenIf par (box [p_exp' true env e1,
adamc@389 629 space,
adamc@389 630 string s,
adamc@389 631 space,
adamc@389 632 p_exp' true env e2])
adamc@387 633
adamc@29 634 | ERecord (i, xes) => box [string "({",
adamc@29 635 space,
adamc@29 636 string "struct",
adamc@29 637 space,
adamc@311 638 string ("__uws_" ^ Int.toString i),
adamc@29 639 space,
adamc@181 640 string "tmp",
adamc@29 641 space,
adamc@29 642 string "=",
adamc@29 643 space,
adamc@29 644 string "{",
adamc@29 645 p_list (fn (_, e) =>
adamc@29 646 p_exp env e) xes,
adamc@29 647 string "};",
adamc@29 648 space,
adamc@181 649 string "tmp;",
adamc@29 650 space,
adamc@29 651 string "})" ]
adamc@29 652 | EField (e, x) =>
adamc@29 653 box [p_exp' true env e,
adamc@311 654 string ".__uwf_",
adamc@316 655 p_ident x]
adamc@29 656
adamc@182 657 | ECase (e, pes, {disc, result}) =>
adamc@182 658 let
adamc@182 659 val final = newGoto ()
adamc@182 660
adamc@182 661 val body = foldl (fn ((p, e), body) =>
adamc@182 662 let
adamc@182 663 val exit = newGoto ()
adamc@182 664 val (pr, _) = p_pat_preamble env p
adamc@182 665 val (p, env) = p_pat (env,
adamc@182 666 box [string "goto",
adamc@182 667 space,
adamc@182 668 exit,
adamc@182 669 string ";"],
adamc@182 670 0) p
adamc@182 671 in
adamc@182 672 box [body,
adamc@182 673 box [string "{",
adamc@182 674 newline,
adamc@182 675 pr,
adamc@182 676 newline,
adamc@182 677 p,
adamc@182 678 newline,
adamc@182 679 string "result",
adamc@182 680 space,
adamc@182 681 string "=",
adamc@182 682 space,
adamc@182 683 p_exp env e,
adamc@182 684 string ";",
adamc@182 685 newline,
adamc@182 686 string "goto",
adamc@182 687 space,
adamc@182 688 final,
adamc@182 689 string ";",
adamc@182 690 newline,
adamc@182 691 string "}"],
adamc@182 692 newline,
adamc@182 693 exit,
adamc@182 694 string ":",
adamc@182 695 newline]
adamc@182 696 end) (box []) pes
adamc@182 697 in
adamc@182 698 box [string "({",
adamc@182 699 newline,
adamc@182 700 p_typ env disc,
adamc@182 701 space,
adamc@182 702 string "disc0",
adamc@182 703 space,
adamc@182 704 string "=",
adamc@182 705 space,
adamc@182 706 p_exp env e,
adamc@182 707 string ";",
adamc@182 708 newline,
adamc@182 709 p_typ env result,
adamc@182 710 space,
adamc@182 711 string "result;",
adamc@182 712 newline,
adamc@182 713 body,
adamc@311 714 string "uw_error(ctx, FATAL, \"",
adamc@182 715 string (ErrorMsg.spanToString loc),
adamc@182 716 string ": pattern match failure\");",
adamc@182 717 newline,
adamc@182 718 final,
adamc@182 719 string ":",
adamc@182 720 space,
adamc@182 721 string "result;",
adamc@182 722 newline,
adamc@182 723 string "})"]
adamc@182 724 end
adamc@181 725
adamc@311 726 | EWrite e => box [string "(uw_write(ctx, ",
adamc@102 727 p_exp env e,
adamc@311 728 string "), uw_unit_v)"]
adamc@102 729
adamc@106 730 | ESeq (e1, e2) => box [string "(",
adamc@106 731 p_exp env e1,
adamc@106 732 string ",",
adamc@106 733 space,
adamc@106 734 p_exp env e2,
adamc@106 735 string ")"]
adamc@269 736 | ELet (x, t, e1, e2) => box [string "({",
adamc@269 737 newline,
adamc@269 738 p_typ env t,
adamc@269 739 space,
adamc@311 740 string "__uwr_",
adamc@316 741 p_ident x,
adamc@272 742 string "_",
adamc@272 743 string (Int.toString (E.countERels env)),
adamc@269 744 space,
adamc@269 745 string "=",
adamc@269 746 space,
adamc@269 747 p_exp env e1,
adamc@269 748 string ";",
adamc@269 749 newline,
adamc@269 750 p_exp (E.pushERel env x t) e2,
adamc@269 751 string ";",
adamc@269 752 newline,
adamc@269 753 string "})"]
adamc@269 754
adamc@282 755 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
adamc@278 756 let
adamc@316 757 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
adamc@278 758 val tables = ListUtil.mapConcat (fn (x, xts) =>
adamc@316 759 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
adamc@278 760 tables
adamc@278 761
adamc@278 762 val outputs = exps @ tables
adamc@325 763 val outputs = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER) outputs
adamc@324 764
adamc@324 765 val wontLeakStrings = notLeaky env true state
adamc@324 766 val wontLeakAnything = notLeaky env false state
adamc@278 767 in
adamc@324 768 box [string "(uw_begin_region(ctx), ",
adamc@324 769 if wontLeakAnything then
adamc@331 770 string "uw_begin_region(ctx), "
adamc@324 771 else
adamc@324 772 box [],
adamc@324 773 string "({",
adamc@278 774 newline,
adamc@311 775 string "PGconn *conn = uw_get_db(ctx);",
adamc@278 776 newline,
adamc@282 777 case prepared of
adamc@282 778 NONE => box [string "char *query = ",
adamc@282 779 p_exp env query,
adamc@282 780 string ";",
adamc@282 781 newline]
adamc@282 782 | SOME _ =>
adamc@282 783 let
adamc@282 784 val ets = getPargs query
adamc@282 785 in
adamc@282 786 box [p_list_sepi newline
adamc@282 787 (fn i => fn (e, t) =>
adamc@282 788 box [p_sql_type t,
adamc@282 789 space,
adamc@282 790 string "arg",
adamc@282 791 string (Int.toString (i + 1)),
adamc@282 792 space,
adamc@282 793 string "=",
adamc@282 794 space,
adamc@282 795 p_exp env e,
adamc@282 796 string ";"])
adamc@282 797 ets,
adamc@282 798 newline,
adamc@282 799 newline,
adamc@282 800
adamc@282 801 string "const char *paramValues[] = { ",
adamc@282 802 p_list_sepi (box [string ",", space])
adamc@282 803 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@282 804 string (Int.toString (i + 1))]))
adamc@282 805 ets,
adamc@282 806 string " };",
adamc@282 807 newline,
adamc@282 808 newline]
adamc@282 809 end,
adamc@278 810 string "int n, i;",
adamc@278 811 newline,
adamc@278 812 p_typ env state,
adamc@278 813 space,
adamc@278 814 string "acc",
adamc@278 815 space,
adamc@278 816 string "=",
adamc@278 817 space,
adamc@278 818 p_exp env initial,
adamc@278 819 string ";",
adamc@278 820 newline,
adamc@282 821 string "PGresult *res = ",
adamc@282 822 case prepared of
adamc@295 823 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 824 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@282 825 string (Int.toString n),
adamc@282 826 string "\", ",
adamc@282 827 string (Int.toString (length (getPargs query))),
adamc@295 828 string ", paramValues, NULL, NULL, 0);"],
adamc@278 829 newline,
adamc@278 830 newline,
adamc@277 831
adamc@311 832 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@278 833 newline,
adamc@278 834 newline,
adamc@277 835
adamc@278 836 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@278 837 newline,
adamc@278 838 box [string "PQclear(res);",
adamc@278 839 newline,
adamc@311 840 string "uw_error(ctx, FATAL, \"",
adamc@278 841 string (ErrorMsg.spanToString loc),
adamc@282 842 string ": Query failed:\\n%s\\n%s\", ",
adamc@282 843 case prepared of
adamc@282 844 NONE => string "query"
adamc@282 845 | SOME _ => p_exp env query,
adamc@282 846 string ", PQerrorMessage(conn));",
adamc@278 847 newline],
adamc@278 848 string "}",
adamc@278 849 newline,
adamc@278 850 newline,
adamc@277 851
adamc@323 852 string "uw_end_region(ctx);",
adamc@323 853 newline,
adamc@425 854 string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",
adamc@425 855 newline,
adamc@278 856 string "n = PQntuples(res);",
adamc@278 857 newline,
adamc@278 858 string "for (i = 0; i < n; ++i) {",
adamc@278 859 newline,
adamc@278 860 box [string "struct",
adamc@278 861 space,
adamc@311 862 string "__uws_",
adamc@278 863 string (Int.toString rnum),
adamc@278 864 space,
adamc@311 865 string "__uwr_r_",
adamc@278 866 string (Int.toString (E.countERels env)),
adamc@278 867 string ";",
adamc@278 868 newline,
adamc@278 869 p_typ env state,
adamc@278 870 space,
adamc@311 871 string "__uwr_acc_",
adamc@278 872 string (Int.toString (E.countERels env + 1)),
adamc@278 873 space,
adamc@278 874 string "=",
adamc@278 875 space,
adamc@278 876 string "acc;",
adamc@278 877 newline,
adamc@278 878 newline,
adamc@278 879
adamc@278 880 p_list_sepi (box []) (fn i =>
adamc@278 881 fn (proj, t) =>
adamc@311 882 box [string "__uwr_r_",
adamc@278 883 string (Int.toString (E.countERels env)),
adamc@278 884 string ".",
adamc@278 885 string proj,
adamc@278 886 space,
adamc@278 887 string "=",
adamc@278 888 space,
adamc@324 889 p_unsql wontLeakStrings env t
adamc@278 890 (box [string "PQgetvalue(res, i, ",
adamc@278 891 string (Int.toString i),
adamc@278 892 string ")"]),
adamc@278 893 string ";",
adamc@278 894 newline]) outputs,
adamc@278 895
adamc@278 896 newline,
adamc@278 897 newline,
adamc@278 898
adamc@278 899 string "acc",
adamc@278 900 space,
adamc@278 901 string "=",
adamc@278 902 space,
adamc@278 903 p_exp (E.pushERel
adamc@278 904 (E.pushERel env "r" (TRecord rnum, loc))
adamc@278 905 "acc" state)
adamc@278 906 body,
adamc@278 907 string ";",
adamc@278 908 newline],
adamc@278 909 string "}",
adamc@278 910 newline,
adamc@278 911 newline,
adamc@425 912 string "uw_pop_cleanup(ctx);",
adamc@278 913 newline,
adamc@324 914 if wontLeakAnything then
adamc@324 915 box [string "uw_end_region(ctx);",
adamc@324 916 newline]
adamc@324 917 else
adamc@324 918 box [],
adamc@278 919 string "acc;",
adamc@278 920 newline,
adamc@323 921 string "}))"]
adamc@278 922 end
adamc@106 923
adamc@307 924 | EDml {dml, prepared} =>
adamc@337 925 box [string "(uw_begin_region(ctx), ({",
adamc@307 926 newline,
adamc@311 927 string "PGconn *conn = uw_get_db(ctx);",
adamc@307 928 newline,
adamc@307 929 case prepared of
adamc@307 930 NONE => box [string "char *dml = ",
adamc@307 931 p_exp env dml,
adamc@307 932 string ";",
adamc@307 933 newline]
adamc@307 934 | SOME _ =>
adamc@307 935 let
adamc@307 936 val ets = getPargs dml
adamc@307 937 in
adamc@307 938 box [p_list_sepi newline
adamc@307 939 (fn i => fn (e, t) =>
adamc@307 940 box [p_sql_type t,
adamc@307 941 space,
adamc@307 942 string "arg",
adamc@307 943 string (Int.toString (i + 1)),
adamc@307 944 space,
adamc@307 945 string "=",
adamc@307 946 space,
adamc@307 947 p_exp env e,
adamc@307 948 string ";"])
adamc@307 949 ets,
adamc@307 950 newline,
adamc@307 951 newline,
adamc@307 952
adamc@307 953 string "const char *paramValues[] = { ",
adamc@307 954 p_list_sepi (box [string ",", space])
adamc@307 955 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@307 956 string (Int.toString (i + 1))]))
adamc@307 957 ets,
adamc@307 958 string " };",
adamc@307 959 newline,
adamc@307 960 newline]
adamc@307 961 end,
adamc@307 962 newline,
adamc@307 963 newline,
adamc@307 964 string "PGresult *res = ",
adamc@307 965 case prepared of
adamc@307 966 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 967 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@307 968 string (Int.toString n),
adamc@307 969 string "\", ",
adamc@307 970 string (Int.toString (length (getPargs dml))),
adamc@307 971 string ", paramValues, NULL, NULL, 0);"],
adamc@307 972 newline,
adamc@307 973 newline,
adamc@307 974
adamc@311 975 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
adamc@307 976 newline,
adamc@307 977 newline,
adamc@307 978
adamc@307 979 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@307 980 newline,
adamc@307 981 box [string "PQclear(res);",
adamc@307 982 newline,
adamc@311 983 string "uw_error(ctx, FATAL, \"",
adamc@307 984 string (ErrorMsg.spanToString loc),
adamc@307 985 string ": DML failed:\\n%s\\n%s\", ",
adamc@307 986 case prepared of
adamc@307 987 NONE => string "dml"
adamc@307 988 | SOME _ => p_exp env dml,
adamc@307 989 string ", PQerrorMessage(conn));",
adamc@307 990 newline],
adamc@307 991 string "}",
adamc@307 992 newline,
adamc@307 993 newline,
adamc@307 994
adamc@307 995 string "PQclear(res);",
adamc@307 996 newline,
adamc@337 997 string "uw_end_region(ctx);",
adamc@337 998 newline,
adamc@311 999 string "uw_unit_v;",
adamc@307 1000 newline,
adamc@337 1001 string "}))"]
adamc@307 1002
adamc@338 1003 | ENextval {seq, prepared} =>
adamc@338 1004 let
adamc@338 1005 val query = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@338 1006 val query = (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), query]), loc)
adamc@338 1007 in
adamc@338 1008 box [string "(uw_begin_region(ctx), ",
adamc@338 1009 string "({",
adamc@338 1010 newline,
adamc@338 1011 string "PGconn *conn = uw_get_db(ctx);",
adamc@338 1012 newline,
adamc@338 1013 case prepared of
adamc@338 1014 NONE => box [string "char *query = ",
adamc@338 1015 p_exp env query,
adamc@338 1016 string ";",
adamc@338 1017 newline]
adamc@338 1018 | SOME _ =>
adamc@338 1019 box [],
adamc@338 1020 newline,
adamc@338 1021 string "PGresult *res = ",
adamc@338 1022 case prepared of
adamc@338 1023 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@338 1024 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@338 1025 string (Int.toString n),
adamc@338 1026 string "\", 0, NULL, NULL, NULL, 0);"],
adamc@338 1027 newline,
adamc@338 1028 string "uw_Basis_int n;",
adamc@338 1029 newline,
adamc@338 1030 newline,
adamc@338 1031
adamc@338 1032 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
adamc@338 1033 newline,
adamc@338 1034 newline,
adamc@338 1035
adamc@338 1036 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@338 1037 newline,
adamc@338 1038 box [string "PQclear(res);",
adamc@338 1039 newline,
adamc@338 1040 string "uw_error(ctx, FATAL, \"",
adamc@338 1041 string (ErrorMsg.spanToString loc),
adamc@338 1042 string ": Query failed:\\n%s\\n%s\", ",
adamc@338 1043 case prepared of
adamc@338 1044 NONE => string "query"
adamc@338 1045 | SOME _ => p_exp env query,
adamc@338 1046 string ", PQerrorMessage(conn));",
adamc@338 1047 newline],
adamc@338 1048 string "}",
adamc@338 1049 newline,
adamc@338 1050 newline,
adamc@338 1051
adamc@338 1052 string "uw_end_region(ctx);",
adamc@338 1053 newline,
adamc@338 1054 string "n = PQntuples(res);",
adamc@338 1055 newline,
adamc@338 1056 string "if (n != 1) {",
adamc@338 1057 newline,
adamc@338 1058 box [string "PQclear(res);",
adamc@338 1059 newline,
adamc@338 1060 string "uw_error(ctx, FATAL, \"",
adamc@338 1061 string (ErrorMsg.spanToString loc),
adamc@338 1062 string ": Wrong number of result rows:\\n%s\\n%s\", ",
adamc@338 1063 case prepared of
adamc@338 1064 NONE => string "query"
adamc@338 1065 | SOME _ => p_exp env query,
adamc@338 1066 string ", PQerrorMessage(conn));",
adamc@338 1067 newline],
adamc@338 1068 string "}",
adamc@338 1069 newline,
adamc@338 1070 newline,
adamc@338 1071
adamc@338 1072 string "n = ",
adamc@338 1073 p_unsql true env (TFfi ("Basis", "int"), loc)
adamc@338 1074 (string "PQgetvalue(res, 0, 0)"),
adamc@338 1075 string ";",
adamc@338 1076 newline,
adamc@338 1077 string "PQclear(res);",
adamc@338 1078 newline,
adamc@338 1079 string "n;",
adamc@338 1080 newline,
adamc@338 1081 string "}))"]
adamc@338 1082 end
adamc@338 1083
adamc@29 1084 and p_exp env = p_exp' false env
adamc@29 1085
adamc@129 1086 fun p_fun env (fx, n, args, ran, e) =
adamc@129 1087 let
adamc@129 1088 val nargs = length args
adamc@129 1089 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
adamc@129 1090 in
adamc@129 1091 box [string "static",
adamc@129 1092 space,
adamc@129 1093 p_typ env ran,
adamc@129 1094 space,
adamc@316 1095 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@129 1096 string "(",
adamc@129 1097 p_list_sep (box [string ",", space]) (fn x => x)
adamc@311 1098 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
adamc@129 1099 box [p_typ env dom,
adamc@129 1100 space,
adamc@129 1101 p_rel env' (nargs - i - 1)]) args),
adamc@129 1102 string ")",
adamc@129 1103 space,
adamc@129 1104 string "{",
adamc@129 1105 newline,
adamc@129 1106 box[string "return(",
adamc@129 1107 p_exp env' e,
adamc@129 1108 string ");"],
adamc@129 1109 newline,
adamc@129 1110 string "}"]
adamc@129 1111 end
adamc@129 1112
adamc@129 1113 fun p_decl env (dAll as (d, _) : decl) =
adamc@29 1114 case d of
adamc@29 1115 DStruct (n, xts) =>
adamc@196 1116 let
adamc@196 1117 val env = E.declBinds env dAll
adamc@196 1118 in
adamc@196 1119 box [string "struct",
adamc@196 1120 space,
adamc@311 1121 string ("__uws_" ^ Int.toString n),
adamc@196 1122 space,
adamc@196 1123 string "{",
adamc@196 1124 newline,
adamc@196 1125 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@196 1126 space,
adamc@311 1127 string "__uwf_",
adamc@316 1128 p_ident x,
adamc@196 1129 string ";",
adamc@196 1130 newline]) xts,
adamc@196 1131 string "};"]
adamc@196 1132 end
adamc@188 1133 | DDatatype (Enum, x, n, xncs) =>
adamc@188 1134 box [string "enum",
adamc@188 1135 space,
adamc@316 1136 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@188 1137 space,
adamc@188 1138 string "{",
adamc@188 1139 space,
adamc@316 1140 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1141 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@188 1142 space,
adamc@188 1143 string "};"]
adamc@198 1144 | DDatatype (Option, _, _, _) => box []
adamc@188 1145 | DDatatype (Default, x, n, xncs) =>
adamc@165 1146 let
adamc@165 1147 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
adamc@165 1148 | (x, n, SOME t) => SOME (x, n, t)) xncs
adamc@165 1149 in
adamc@165 1150 box [string "enum",
adamc@165 1151 space,
adamc@316 1152 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1153 space,
adamc@165 1154 string "{",
adamc@165 1155 space,
adamc@316 1156 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1157 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@165 1158 space,
adamc@165 1159 string "};",
adamc@165 1160 newline,
adamc@165 1161 newline,
adamc@165 1162 string "struct",
adamc@165 1163 space,
adamc@316 1164 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1165 space,
adamc@165 1166 string "{",
adamc@165 1167 newline,
adamc@165 1168 string "enum",
adamc@165 1169 space,
adamc@316 1170 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1171 space,
adamc@165 1172 string "tag;",
adamc@165 1173 newline,
adamc@165 1174 box (case xncsArgs of
adamc@165 1175 [] => []
adamc@165 1176 | _ => [string "union",
adamc@165 1177 space,
adamc@165 1178 string "{",
adamc@165 1179 newline,
adamc@165 1180 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
adamc@165 1181 space,
adamc@316 1182 string ("uw_" ^ ident x),
adamc@165 1183 string ";"]) xncsArgs,
adamc@165 1184 newline,
adamc@165 1185 string "}",
adamc@165 1186 space,
adamc@165 1187 string "data;",
adamc@165 1188 newline]),
adamc@165 1189 string "};"]
adamc@188 1190 end
adamc@29 1191
adamc@196 1192 | DDatatypeForward _ => box []
adamc@196 1193
adamc@29 1194 | DVal (x, n, t, e) =>
adamc@29 1195 box [p_typ env t,
adamc@29 1196 space,
adamc@316 1197 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
adamc@29 1198 space,
adamc@29 1199 string "=",
adamc@29 1200 space,
adamc@29 1201 p_exp env e,
adamc@29 1202 string ";"]
adamc@129 1203 | DFun vi => p_fun env vi
adamc@129 1204 | DFunRec vis =>
adamc@29 1205 let
adamc@129 1206 val env = E.declBinds env dAll
adamc@29 1207 in
adamc@129 1208 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
adamc@129 1209 box [string "static",
adamc@129 1210 space,
adamc@129 1211 p_typ env ran,
adamc@129 1212 space,
adamc@316 1213 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@311 1214 string "(uw_context,",
adamc@129 1215 space,
adamc@129 1216 p_list_sep (box [string ",", space])
adamc@129 1217 (fn (_, dom) => p_typ env dom) args,
adamc@129 1218 string ");"]) vis,
adamc@29 1219 newline,
adamc@129 1220 p_list_sep newline (p_fun env) vis,
adamc@129 1221 newline]
adamc@29 1222 end
adamc@273 1223 | DTable (x, _) => box [string "/* SQL table ",
adamc@273 1224 string x,
adamc@273 1225 string " */",
adamc@273 1226 newline]
adamc@338 1227 | DSequence x => box [string "/* SQL sequence ",
adamc@338 1228 string x,
adamc@338 1229 string " */",
adamc@338 1230 newline]
adamc@311 1231 | DDatabase s => box [string "static void uw_db_validate(uw_context);",
adamc@272 1232 newline,
adamc@311 1233 string "static void uw_db_prepare(uw_context);",
adamc@282 1234 newline,
adamc@275 1235 newline,
adamc@311 1236 string "void uw_db_init(uw_context ctx) {",
adamc@273 1237 newline,
adamc@272 1238 string "PGconn *conn = PQconnectdb(\"",
adamc@272 1239 string (String.toString s),
adamc@272 1240 string "\");",
adamc@272 1241 newline,
adamc@311 1242 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
adamc@272 1243 string "\"libpq can't allocate a connection.\");",
adamc@272 1244 newline,
adamc@272 1245 string "if (PQstatus(conn) != CONNECTION_OK) {",
adamc@272 1246 newline,
adamc@272 1247 box [string "char msg[1024];",
adamc@272 1248 newline,
adamc@272 1249 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@272 1250 newline,
adamc@272 1251 string "msg[1023] = 0;",
adamc@272 1252 newline,
adamc@272 1253 string "PQfinish(conn);",
adamc@272 1254 newline,
adamc@311 1255 string "uw_error(ctx, BOUNDED_RETRY, ",
adamc@272 1256 string "\"Connection to Postgres server failed: %s\", msg);"],
adamc@272 1257 newline,
adamc@272 1258 string "}",
adamc@272 1259 newline,
adamc@311 1260 string "uw_set_db(ctx, conn);",
adamc@272 1261 newline,
adamc@311 1262 string "uw_db_validate(ctx);",
adamc@275 1263 newline,
adamc@311 1264 string "uw_db_prepare(ctx);",
adamc@282 1265 newline,
adamc@272 1266 string "}",
adamc@272 1267 newline,
adamc@272 1268 newline,
adamc@311 1269 string "void uw_db_close(uw_context ctx) {",
adamc@272 1270 newline,
adamc@311 1271 string "PQfinish(uw_get_db(ctx));",
adamc@272 1272 newline,
adamc@272 1273 string "}",
adamc@424 1274 newline,
adamc@424 1275 newline,
adamc@424 1276
adamc@424 1277 string "int uw_db_begin(uw_context ctx) {",
adamc@424 1278 newline,
adamc@424 1279 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 1280 newline,
adamc@424 1281 string "PGresult *res = PQexec(conn, \"BEGIN\");",
adamc@424 1282 newline,
adamc@424 1283 newline,
adamc@424 1284 string "if (res == NULL) return 1;",
adamc@424 1285 newline,
adamc@424 1286 newline,
adamc@424 1287 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 1288 box [string "PQclear(res);",
adamc@424 1289 newline,
adamc@424 1290 string "return 1;",
adamc@424 1291 newline],
adamc@424 1292 string "}",
adamc@424 1293 newline,
adamc@424 1294 string "return 0;",
adamc@424 1295 newline,
adamc@424 1296 string "}",
adamc@424 1297 newline,
adamc@424 1298 newline,
adamc@424 1299
adamc@424 1300 string "int uw_db_commit(uw_context ctx) {",
adamc@424 1301 newline,
adamc@424 1302 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 1303 newline,
adamc@424 1304 string "PGresult *res = PQexec(conn, \"COMMIT\");",
adamc@424 1305 newline,
adamc@424 1306 newline,
adamc@424 1307 string "if (res == NULL) return 1;",
adamc@424 1308 newline,
adamc@424 1309 newline,
adamc@424 1310 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 1311 box [string "PQclear(res);",
adamc@424 1312 newline,
adamc@424 1313 string "return 1;",
adamc@424 1314 newline],
adamc@424 1315 string "}",
adamc@424 1316 newline,
adamc@424 1317 string "return 0;",
adamc@424 1318 newline,
adamc@424 1319 string "}",
adamc@424 1320 newline,
adamc@424 1321 newline,
adamc@424 1322
adamc@424 1323 string "int uw_db_rollback(uw_context ctx) {",
adamc@424 1324 newline,
adamc@424 1325 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 1326 newline,
adamc@424 1327 string "PGresult *res = PQexec(conn, \"ROLLBACK\");",
adamc@424 1328 newline,
adamc@424 1329 newline,
adamc@424 1330 string "if (res == NULL) return 1;",
adamc@424 1331 newline,
adamc@424 1332 newline,
adamc@424 1333 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 1334 box [string "PQclear(res);",
adamc@424 1335 newline,
adamc@424 1336 string "return 1;",
adamc@424 1337 newline],
adamc@424 1338 string "}",
adamc@424 1339 newline,
adamc@424 1340 string "return 0;",
adamc@424 1341 newline,
adamc@424 1342 string "}",
adamc@272 1343 newline]
adamc@29 1344
adamc@432 1345 | DPreparedStatements [] => box []
adamc@282 1346 | DPreparedStatements ss =>
adamc@311 1347 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@282 1348 newline,
adamc@311 1349 string "PGconn *conn = uw_get_db(ctx);",
adamc@282 1350 newline,
adamc@282 1351 string "PGresult *res;",
adamc@282 1352 newline,
adamc@282 1353 newline,
adamc@282 1354
adamc@282 1355 p_list_sepi newline (fn i => fn (s, n) =>
adamc@311 1356 box [string "res = PQprepare(conn, \"uw",
adamc@282 1357 string (Int.toString i),
adamc@282 1358 string "\", \"",
adamc@282 1359 string (String.toString s),
adamc@282 1360 string "\", ",
adamc@282 1361 string (Int.toString n),
adamc@282 1362 string ", NULL);",
adamc@282 1363 newline,
adamc@282 1364 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@282 1365 newline,
adamc@282 1366 box [string "char msg[1024];",
adamc@282 1367 newline,
adamc@282 1368 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@282 1369 newline,
adamc@282 1370 string "msg[1023] = 0;",
adamc@282 1371 newline,
adamc@282 1372 string "PQclear(res);",
adamc@282 1373 newline,
adamc@282 1374 string "PQfinish(conn);",
adamc@282 1375 newline,
adamc@311 1376 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
adamc@282 1377 string (String.toString s),
adamc@282 1378 string "\\n%s\", msg);",
adamc@282 1379 newline],
adamc@282 1380 string "}",
adamc@282 1381 newline,
adamc@282 1382 string "PQclear(res);",
adamc@282 1383 newline])
adamc@282 1384 ss,
adamc@282 1385
adamc@282 1386 string "}"]
adamc@282 1387
adamc@144 1388 datatype 'a search =
adamc@144 1389 Found of 'a
adamc@144 1390 | NotFound
adamc@144 1391 | Error
adamc@120 1392
adamc@275 1393 fun p_sqltype' env (tAll as (t, loc)) =
adamc@275 1394 case t of
adamc@275 1395 TFfi ("Basis", "int") => "int8"
adamc@275 1396 | TFfi ("Basis", "float") => "float8"
adamc@275 1397 | TFfi ("Basis", "string") => "text"
adamc@275 1398 | TFfi ("Basis", "bool") => "bool"
adamc@438 1399 | TFfi ("Basis", "time") => "timestamp"
adamc@275 1400 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
adamc@275 1401 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@275 1402 "ERROR")
adamc@275 1403
adamc@275 1404 fun p_sqltype env t = string (p_sqltype' env t)
adamc@101 1405
adamc@101 1406 fun p_file env (ds, ps) =
adamc@29 1407 let
adamc@101 1408 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 1409 (p_decl env d,
adamc@31 1410 E.declBinds env d))
adamc@101 1411 env ds
adamc@144 1412
adamc@144 1413 val fields = foldl (fn ((ek, _, _, ts), fields) =>
adamc@144 1414 case ek of
adamc@144 1415 Core.Link => fields
adamc@144 1416 | Core.Action =>
adamc@280 1417 case List.nth (ts, length ts - 2) of
adamc@144 1418 (TRecord i, _) =>
adamc@144 1419 let
adamc@144 1420 val xts = E.lookupStruct env i
adamc@144 1421 val xtsSet = SS.addList (SS.empty, map #1 xts)
adamc@144 1422 in
adamc@144 1423 foldl (fn ((x, _), fields) =>
adamc@144 1424 let
adamc@144 1425 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
adamc@144 1426 in
adamc@144 1427 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
adamc@144 1428 xtsSet'))
adamc@144 1429 end) fields xts
adamc@144 1430 end
adamc@144 1431 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
adamc@144 1432 SM.empty ps
adamc@144 1433
adamc@144 1434 val fnums = SM.foldli (fn (x, xs, fnums) =>
adamc@144 1435 let
adamc@144 1436 val unusable = SS.foldl (fn (x', unusable) =>
adamc@144 1437 case SM.find (fnums, x') of
adamc@144 1438 NONE => unusable
adamc@144 1439 | SOME n => IS.add (unusable, n))
adamc@144 1440 IS.empty xs
adamc@144 1441
adamc@144 1442 fun findAvailable n =
adamc@144 1443 if IS.member (unusable, n) then
adamc@144 1444 findAvailable (n + 1)
adamc@144 1445 else
adamc@144 1446 n
adamc@144 1447 in
adamc@144 1448 SM.insert (fnums, x, findAvailable 0)
adamc@144 1449 end)
adamc@144 1450 SM.empty fields
adamc@144 1451
adamc@144 1452 fun makeSwitch (fnums, i) =
adamc@144 1453 case SM.foldl (fn (n, NotFound) => Found n
adamc@144 1454 | (n, Error) => Error
adamc@144 1455 | (n, Found n') => if n = n' then
adamc@144 1456 Found n'
adamc@144 1457 else
adamc@144 1458 Error) NotFound fnums of
adamc@144 1459 NotFound => box [string "return",
adamc@144 1460 space,
adamc@144 1461 string "-1;"]
adamc@144 1462 | Found n => box [string "return",
adamc@144 1463 space,
adamc@144 1464 string (Int.toString n),
adamc@144 1465 string ";"]
adamc@144 1466 | Error =>
adamc@144 1467 let
adamc@144 1468 val cmap = SM.foldli (fn (x, n, cmap) =>
adamc@144 1469 let
adamc@144 1470 val ch = if i < size x then
adamc@144 1471 String.sub (x, i)
adamc@144 1472 else
adamc@144 1473 chr 0
adamc@144 1474
adamc@144 1475 val fnums = case CM.find (cmap, ch) of
adamc@144 1476 NONE => SM.empty
adamc@144 1477 | SOME fnums => fnums
adamc@144 1478 val fnums = SM.insert (fnums, x, n)
adamc@144 1479 in
adamc@144 1480 CM.insert (cmap, ch, fnums)
adamc@144 1481 end)
adamc@144 1482 CM.empty fnums
adamc@144 1483
adamc@144 1484 val cmap = CM.listItemsi cmap
adamc@144 1485 in
adamc@144 1486 case cmap of
adamc@144 1487 [(_, fnums)] =>
adamc@144 1488 box [string "if",
adamc@144 1489 space,
adamc@144 1490 string "(name[",
adamc@144 1491 string (Int.toString i),
adamc@144 1492 string "]",
adamc@144 1493 space,
adamc@144 1494 string "==",
adamc@144 1495 space,
adamc@144 1496 string "0)",
adamc@144 1497 space,
adamc@144 1498 string "return",
adamc@144 1499 space,
adamc@144 1500 string "-1;",
adamc@144 1501 newline,
adamc@144 1502 makeSwitch (fnums, i+1)]
adamc@144 1503 | _ =>
adamc@144 1504 box [string "switch",
adamc@144 1505 space,
adamc@144 1506 string "(name[",
adamc@144 1507 string (Int.toString i),
adamc@144 1508 string "])",
adamc@144 1509 space,
adamc@144 1510 string "{",
adamc@144 1511 newline,
adamc@144 1512 box (map (fn (ch, fnums) =>
adamc@144 1513 box [string "case",
adamc@144 1514 space,
adamc@144 1515 if ch = chr 0 then
adamc@144 1516 string "0:"
adamc@144 1517 else
adamc@144 1518 box [string "'",
adamc@144 1519 string (Char.toString ch),
adamc@144 1520 string "':"],
adamc@144 1521 newline,
adamc@144 1522 makeSwitch (fnums, i+1),
adamc@144 1523 newline]) cmap),
adamc@144 1524 string "default:",
adamc@144 1525 newline,
adamc@144 1526 string "return",
adamc@144 1527 space,
adamc@144 1528 string "-1;",
adamc@144 1529 newline,
adamc@144 1530 string "}"]
adamc@144 1531 end
adamc@144 1532
adamc@186 1533 fun capitalize s =
adamc@186 1534 if s = "" then
adamc@186 1535 ""
adamc@186 1536 else
adamc@186 1537 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@186 1538
adamc@144 1539 fun unurlify (t, loc) =
adamc@398 1540 let
adamc@398 1541 fun unurlify' rf t =
adamc@398 1542 case t of
adamc@418 1543 TFfi ("Basis", "unit") => string ("uw_unit_v")
adamc@418 1544 | TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
adamc@144 1545
adamc@398 1546 | TRecord 0 => string "uw_unit_v"
adamc@398 1547 | TRecord i =>
adamc@398 1548 let
adamc@398 1549 val xts = E.lookupStruct env i
adamc@398 1550 in
adamc@398 1551 box [string "({",
adamc@166 1552 newline,
adamc@398 1553 box (map (fn (x, t) =>
adamc@398 1554 box [p_typ env t,
adamc@398 1555 space,
adamc@398 1556 string "uwr_",
adamc@398 1557 string x,
adamc@398 1558 space,
adamc@398 1559 string "=",
adamc@398 1560 space,
adamc@398 1561 unurlify' rf (#1 t),
adamc@398 1562 string ";",
adamc@398 1563 newline]) xts),
adamc@167 1564 string "struct",
adamc@167 1565 space,
adamc@398 1566 string "__uws_",
adamc@398 1567 string (Int.toString i),
adamc@166 1568 space,
adamc@398 1569 string "tmp",
adamc@166 1570 space,
adamc@166 1571 string "=",
adamc@166 1572 space,
adamc@398 1573 string "{",
adamc@398 1574 space,
adamc@398 1575 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
adamc@398 1576 string x]) xts,
adamc@398 1577 space,
adamc@398 1578 string "};",
adamc@166 1579 newline,
adamc@181 1580 string "tmp;",
adamc@166 1581 newline,
adamc@398 1582 string "})"]
adamc@398 1583 end
adamc@166 1584
adamc@398 1585 | TDatatype (Enum, i, _) =>
adamc@398 1586 let
adamc@398 1587 val (x, xncs) = E.lookupDatatype env i
adamc@144 1588
adamc@398 1589 fun doEm xncs =
adamc@398 1590 case xncs of
adamc@402 1591 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@402 1592 ^ x ^ "\"), (enum __uwe_"
adamc@398 1593 ^ x ^ "_" ^ Int.toString i ^ ")0)")
adamc@398 1594 | (x', n, to) :: rest =>
adamc@398 1595 box [string "((!strncmp(request, \"",
adamc@398 1596 string x',
adamc@398 1597 string "\", ",
adamc@398 1598 string (Int.toString (size x')),
adamc@398 1599 string ") && (request[",
adamc@398 1600 string (Int.toString (size x')),
adamc@398 1601 string "] == 0 || request[",
adamc@398 1602 string (Int.toString (size x')),
adamc@398 1603 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@398 1604 space,
adamc@398 1605 string ":",
adamc@398 1606 space,
adamc@398 1607 doEm rest,
adamc@398 1608 string ")"]
adamc@398 1609 in
adamc@398 1610 doEm xncs
adamc@398 1611 end
adamc@398 1612
adamc@398 1613 | TDatatype (Option, i, xncs) =>
adamc@398 1614 if IS.member (rf, i) then
adamc@398 1615 box [string "unurlify_",
adamc@398 1616 string (Int.toString i),
adamc@398 1617 string "()"]
adamc@398 1618 else
adamc@398 1619 let
adamc@398 1620 val (x, _) = E.lookupDatatype env i
adamc@398 1621
adamc@398 1622 val (no_arg, has_arg, t) =
adamc@398 1623 case !xncs of
adamc@398 1624 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@398 1625 (no_arg, has_arg, t)
adamc@398 1626 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@398 1627 (no_arg, has_arg, t)
adamc@398 1628 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
adamc@398 1629
adamc@398 1630 val rf = IS.add (rf, i)
adamc@398 1631 in
adamc@398 1632 box [string "({",
adamc@398 1633 space,
adamc@398 1634 p_typ env t,
adamc@398 1635 space,
adamc@398 1636 string "*unurlify_",
adamc@398 1637 string (Int.toString i),
adamc@398 1638 string "(void) {",
adamc@398 1639 newline,
adamc@398 1640 box [string "return (request[0] == '/' ? ++request : request,",
adamc@398 1641 newline,
adamc@398 1642 string "((!strncmp(request, \"",
adamc@398 1643 string no_arg,
adamc@398 1644 string "\", ",
adamc@398 1645 string (Int.toString (size no_arg)),
adamc@398 1646 string ") && (request[",
adamc@398 1647 string (Int.toString (size no_arg)),
adamc@398 1648 string "] == 0 || request[",
adamc@398 1649 string (Int.toString (size no_arg)),
adamc@398 1650 string "] == '/')) ? (request",
adamc@398 1651 space,
adamc@398 1652 string "+=",
adamc@398 1653 space,
adamc@398 1654 string (Int.toString (size no_arg)),
adamc@398 1655 string ", NULL) : ((!strncmp(request, \"",
adamc@398 1656 string has_arg,
adamc@398 1657 string "\", ",
adamc@398 1658 string (Int.toString (size has_arg)),
adamc@398 1659 string ") && (request[",
adamc@398 1660 string (Int.toString (size has_arg)),
adamc@398 1661 string "] == 0 || request[",
adamc@398 1662 string (Int.toString (size has_arg)),
adamc@398 1663 string "] == '/')) ? (request",
adamc@398 1664 space,
adamc@398 1665 string "+=",
adamc@398 1666 space,
adamc@398 1667 string (Int.toString (size has_arg)),
adamc@398 1668 string ", (request[0] == '/' ? ++request : NULL), ",
adamc@398 1669 newline,
adamc@398 1670
adamc@398 1671 case #1 t of
adamc@398 1672 TDatatype _ => unurlify' rf (#1 t)
adamc@398 1673 | TFfi ("Basis", "string") => unurlify' rf (#1 t)
adamc@398 1674 | _ => box [string "({",
adamc@398 1675 newline,
adamc@398 1676 p_typ env t,
adamc@398 1677 space,
adamc@398 1678 string "*tmp",
adamc@398 1679 space,
adamc@398 1680 string "=",
adamc@398 1681 space,
adamc@398 1682 string "uw_malloc(ctx, sizeof(",
adamc@398 1683 p_typ env t,
adamc@398 1684 string "));",
adamc@398 1685 newline,
adamc@398 1686 string "*tmp",
adamc@398 1687 space,
adamc@398 1688 string "=",
adamc@398 1689 space,
adamc@398 1690 unurlify' rf (#1 t),
adamc@398 1691 string ";",
adamc@398 1692 newline,
adamc@398 1693 string "tmp;",
adamc@398 1694 newline,
adamc@398 1695 string "})"],
adamc@398 1696 string ")",
adamc@398 1697 newline,
adamc@398 1698 string ":",
adamc@398 1699 space,
adamc@398 1700 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
adamc@398 1701 ^ "\"), NULL))));"),
adamc@398 1702 newline],
adamc@398 1703 string "}",
adamc@398 1704 newline,
adamc@398 1705 newline,
adamc@398 1706
adamc@398 1707 string "unurlify_",
adamc@398 1708 string (Int.toString i),
adamc@398 1709 string "();",
adamc@398 1710 newline,
adamc@398 1711 string "})"]
adamc@398 1712 end
adamc@398 1713
adamc@398 1714 | TDatatype (Default, i, _) =>
adamc@402 1715 if IS.member (rf, i) then
adamc@402 1716 box [string "unurlify_",
adamc@402 1717 string (Int.toString i),
adamc@402 1718 string "()"]
adamc@402 1719 else
adamc@402 1720 let
adamc@402 1721 val (x, xncs) = E.lookupDatatype env i
adamc@398 1722
adamc@402 1723 val rf = IS.add (rf, i)
adamc@402 1724
adamc@402 1725 fun doEm xncs =
adamc@402 1726 case xncs of
adamc@402 1727 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@402 1728 ^ x ^ "\"), NULL)")
adamc@402 1729 | (x', n, to) :: rest =>
adamc@402 1730 box [string "((!strncmp(request, \"",
adamc@402 1731 string x',
adamc@402 1732 string "\", ",
adamc@402 1733 string (Int.toString (size x')),
adamc@402 1734 string ") && (request[",
adamc@402 1735 string (Int.toString (size x')),
adamc@402 1736 string "] == 0 || request[",
adamc@402 1737 string (Int.toString (size x')),
adamc@402 1738 string "] == '/')) ? ({",
adamc@402 1739 newline,
adamc@402 1740 string "struct",
adamc@402 1741 space,
adamc@402 1742 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
adamc@402 1743 space,
adamc@402 1744 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
adamc@402 1745 string x,
adamc@402 1746 string "_",
adamc@402 1747 string (Int.toString i),
adamc@402 1748 string "));",
adamc@402 1749 newline,
adamc@402 1750 string "tmp->tag",
adamc@402 1751 space,
adamc@402 1752 string "=",
adamc@402 1753 space,
adamc@402 1754 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@402 1755 string ";",
adamc@402 1756 newline,
adamc@402 1757 string "request",
adamc@402 1758 space,
adamc@402 1759 string "+=",
adamc@402 1760 space,
adamc@402 1761 string (Int.toString (size x')),
adamc@402 1762 string ";",
adamc@402 1763 newline,
adamc@402 1764 string "if (request[0] == '/') ++request;",
adamc@402 1765 newline,
adamc@402 1766 case to of
adamc@402 1767 NONE => box []
adamc@402 1768 | SOME (t, _) => box [string "tmp->data.uw_",
adamc@402 1769 p_ident x',
adamc@402 1770 space,
adamc@402 1771 string "=",
adamc@402 1772 space,
adamc@402 1773 unurlify' rf t,
adamc@402 1774 string ";",
adamc@402 1775 newline],
adamc@402 1776 string "tmp;",
adamc@402 1777 newline,
adamc@402 1778 string "})",
adamc@402 1779 space,
adamc@402 1780 string ":",
adamc@402 1781 space,
adamc@402 1782 doEm rest,
adamc@402 1783 string ")"]
adamc@402 1784 in
adamc@402 1785 box [string "({",
adamc@402 1786 space,
adamc@402 1787 p_typ env (t, ErrorMsg.dummySpan),
adamc@402 1788 space,
adamc@402 1789 string "unurlify_",
adamc@402 1790 string (Int.toString i),
adamc@402 1791 string "(void) {",
adamc@402 1792 newline,
adamc@402 1793 box [string "return",
adamc@402 1794 space,
adamc@402 1795 doEm xncs,
adamc@402 1796 string ";",
adamc@402 1797 newline],
adamc@402 1798 string "}",
adamc@402 1799 newline,
adamc@402 1800 newline,
adamc@402 1801
adamc@402 1802 string "unurlify_",
adamc@402 1803 string (Int.toString i),
adamc@402 1804 string "();",
adamc@402 1805 newline,
adamc@402 1806 string "})"]
adamc@402 1807 end
adamc@398 1808
adamc@398 1809 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
adamc@398 1810 space)
adamc@398 1811 in
adamc@398 1812 unurlify' IS.empty t
adamc@398 1813 end
adamc@144 1814
adamc@144 1815 fun p_page (ek, s, n, ts) =
adamc@144 1816 let
adamc@144 1817 val (ts, defInputs, inputsVar) =
adamc@144 1818 case ek of
adamc@314 1819 Core.Link => (List.take (ts, length ts - 1), string "", string "")
adamc@144 1820 | Core.Action =>
adamc@280 1821 case List.nth (ts, length ts - 2) of
adamc@144 1822 (TRecord i, _) =>
adamc@144 1823 let
adamc@144 1824 val xts = E.lookupStruct env i
adamc@144 1825 in
adamc@280 1826 (List.take (ts, length ts - 2),
adamc@144 1827 box [box (map (fn (x, t) => box [p_typ env t,
adamc@144 1828 space,
adamc@311 1829 string "uw_input_",
adamc@316 1830 p_ident x,
adamc@144 1831 string ";",
adamc@144 1832 newline]) xts),
adamc@144 1833 newline,
adamc@144 1834 box (map (fn (x, t) =>
adamc@144 1835 let
adamc@144 1836 val n = case SM.find (fnums, x) of
adamc@144 1837 NONE => raise Fail "CjrPrint: Can't find in fnums"
adamc@144 1838 | SOME n => n
adamc@190 1839
adamc@190 1840 val f = case t of
adamc@190 1841 (TFfi ("Basis", "bool"), _) => "optional_"
adamc@190 1842 | _ => ""
adamc@144 1843 in
adamc@311 1844 box [string "request = uw_get_",
adamc@190 1845 string f,
adamc@190 1846 string "input(ctx, ",
adamc@144 1847 string (Int.toString n),
adamc@144 1848 string ");",
adamc@144 1849 newline,
adamc@433 1850 string "if (request == NULL)",
adamc@144 1851 newline,
adamc@433 1852 box [string "uw_error(ctx, FATAL, \"Missing input ",
adamc@144 1853 string x,
adamc@433 1854 string "\");"],
adamc@144 1855 newline,
adamc@311 1856 string "uw_input_",
adamc@316 1857 p_ident x,
adamc@144 1858 space,
adamc@144 1859 string "=",
adamc@144 1860 space,
adamc@144 1861 unurlify t,
adamc@144 1862 string ";",
adamc@144 1863 newline]
adamc@144 1864 end) xts),
adamc@311 1865 string "struct __uws_",
adamc@144 1866 string (Int.toString i),
adamc@144 1867 space,
adamc@311 1868 string "uw_inputs",
adamc@144 1869 space,
adamc@144 1870 string "= {",
adamc@144 1871 newline,
adamc@311 1872 box (map (fn (x, _) => box [string "uw_input_",
adamc@316 1873 p_ident x,
adamc@144 1874 string ",",
adamc@144 1875 newline]) xts),
adamc@144 1876 string "};",
adamc@144 1877 newline],
adamc@144 1878 box [string ",",
adamc@144 1879 space,
adamc@311 1880 string "uw_inputs"])
adamc@144 1881 end
adamc@144 1882
adamc@144 1883 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
adamc@144 1884 in
adamc@144 1885 box [string "if (!strncmp(request, \"",
adamc@144 1886 string (String.toString s),
adamc@144 1887 string "\", ",
adamc@144 1888 string (Int.toString (size s)),
adamc@198 1889 string ") && (request[",
adamc@198 1890 string (Int.toString (size s)),
adamc@198 1891 string "] == 0 || request[",
adamc@198 1892 string (Int.toString (size s)),
adamc@198 1893 string "] == '/')) {",
adamc@144 1894 newline,
adamc@144 1895 string "request += ",
adamc@144 1896 string (Int.toString (size s)),
adamc@144 1897 string ";",
adamc@144 1898 newline,
adamc@144 1899 string "if (*request == '/') ++request;",
adamc@144 1900 newline,
adamc@144 1901 box [string "{",
adamc@144 1902 newline,
adamc@144 1903 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
adamc@144 1904 space,
adamc@144 1905 string "arg",
adamc@144 1906 string (Int.toString i),
adamc@144 1907 space,
adamc@144 1908 string "=",
adamc@144 1909 space,
adamc@144 1910 unurlify t,
adamc@144 1911 string ";",
adamc@144 1912 newline]) ts),
adamc@144 1913 defInputs,
adamc@144 1914 p_enamed env n,
adamc@144 1915 string "(",
adamc@144 1916 p_list_sep (box [string ",", space])
adamc@144 1917 (fn x => x)
adamc@272 1918 (string "ctx"
adamc@280 1919 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
adamc@144 1920 inputsVar,
adamc@311 1921 string ", uw_unit_v);",
adamc@144 1922 newline,
adamc@144 1923 string "return;",
adamc@144 1924 newline,
adamc@144 1925 string "}",
adamc@144 1926 newline,
adamc@144 1927 string "}"]
adamc@144 1928 ]
adamc@144 1929 end
adamc@144 1930
adamc@144 1931 val pds' = map p_page ps
adamc@275 1932
adamc@275 1933 val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts)
adamc@275 1934 | _ => NONE) ds
adamc@377 1935 val sequences = List.mapPartial (fn (DSequence s, _) => SOME s
adamc@377 1936 | _ => NONE) ds
adamc@275 1937
adamc@275 1938 val validate =
adamc@311 1939 box [string "static void uw_db_validate(uw_context ctx) {",
adamc@275 1940 newline,
adamc@311 1941 string "PGconn *conn = uw_get_db(ctx);",
adamc@275 1942 newline,
adamc@275 1943 string "PGresult *res;",
adamc@275 1944 newline,
adamc@275 1945 newline,
adamc@275 1946 p_list_sep newline
adamc@275 1947 (fn (s, xts) =>
adamc@275 1948 let
adamc@377 1949 val sl = CharVector.map Char.toLower s
adamc@377 1950
adamc@275 1951 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 1952 ^ sl ^ "'"
adamc@275 1953
adamc@275 1954 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 1955 sl,
adamc@275 1956 "') AND (",
adamc@275 1957 String.concatWith " OR "
adamc@275 1958 (map (fn (x, t) =>
adamc@311 1959 String.concat ["(attname = 'uw_",
adamc@275 1960 CharVector.map
adamc@316 1961 Char.toLower (ident x),
adamc@275 1962 "' AND atttypid = (SELECT oid FROM pg_type",
adamc@275 1963 " WHERE typname = '",
adamc@275 1964 p_sqltype' env t,
adamc@275 1965 "'))"]) xts),
adamc@275 1966 ")"]
adamc@275 1967
adamc@275 1968 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 1969 sl,
adamc@325 1970 "') AND attname LIKE 'uw_%'"]
adamc@275 1971 in
adamc@275 1972 box [string "res = PQexec(conn, \"",
adamc@275 1973 string q,
adamc@275 1974 string "\");",
adamc@275 1975 newline,
adamc@275 1976 newline,
adamc@275 1977 string "if (res == NULL) {",
adamc@275 1978 newline,
adamc@275 1979 box [string "PQfinish(conn);",
adamc@275 1980 newline,
adamc@311 1981 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 1982 newline],
adamc@275 1983 string "}",
adamc@275 1984 newline,
adamc@275 1985 newline,
adamc@275 1986 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 1987 newline,
adamc@275 1988 box [string "char msg[1024];",
adamc@275 1989 newline,
adamc@275 1990 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 1991 newline,
adamc@275 1992 string "msg[1023] = 0;",
adamc@275 1993 newline,
adamc@275 1994 string "PQclear(res);",
adamc@275 1995 newline,
adamc@275 1996 string "PQfinish(conn);",
adamc@275 1997 newline,
adamc@311 1998 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 1999 string q,
adamc@275 2000 string "\\n%s\", msg);",
adamc@275 2001 newline],
adamc@275 2002 string "}",
adamc@275 2003 newline,
adamc@275 2004 newline,
adamc@275 2005 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@275 2006 newline,
adamc@275 2007 box [string "PQclear(res);",
adamc@275 2008 newline,
adamc@275 2009 string "PQfinish(conn);",
adamc@275 2010 newline,
adamc@311 2011 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2012 string s,
adamc@275 2013 string "' does not exist.\");",
adamc@275 2014 newline],
adamc@275 2015 string "}",
adamc@275 2016 newline,
adamc@275 2017 newline,
adamc@275 2018 string "PQclear(res);",
adamc@275 2019 newline,
adamc@275 2020
adamc@275 2021 string "res = PQexec(conn, \"",
adamc@275 2022 string q',
adamc@275 2023 string "\");",
adamc@275 2024 newline,
adamc@275 2025 newline,
adamc@275 2026 string "if (res == NULL) {",
adamc@275 2027 newline,
adamc@275 2028 box [string "PQfinish(conn);",
adamc@275 2029 newline,
adamc@311 2030 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2031 newline],
adamc@275 2032 string "}",
adamc@275 2033 newline,
adamc@275 2034 newline,
adamc@275 2035 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2036 newline,
adamc@275 2037 box [string "char msg[1024];",
adamc@275 2038 newline,
adamc@275 2039 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2040 newline,
adamc@275 2041 string "msg[1023] = 0;",
adamc@275 2042 newline,
adamc@275 2043 string "PQclear(res);",
adamc@275 2044 newline,
adamc@275 2045 string "PQfinish(conn);",
adamc@275 2046 newline,
adamc@311 2047 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2048 string q',
adamc@275 2049 string "\\n%s\", msg);",
adamc@275 2050 newline],
adamc@275 2051 string "}",
adamc@275 2052 newline,
adamc@275 2053 newline,
adamc@275 2054 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2055 string (Int.toString (length xts)),
adamc@275 2056 string "\")) {",
adamc@275 2057 newline,
adamc@275 2058 box [string "PQclear(res);",
adamc@275 2059 newline,
adamc@275 2060 string "PQfinish(conn);",
adamc@275 2061 newline,
adamc@311 2062 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2063 string s,
adamc@275 2064 string "' has the wrong column types.\");",
adamc@275 2065 newline],
adamc@275 2066 string "}",
adamc@275 2067 newline,
adamc@275 2068 newline,
adamc@275 2069 string "PQclear(res);",
adamc@275 2070 newline,
adamc@275 2071 newline,
adamc@275 2072
adamc@275 2073 string "res = PQexec(conn, \"",
adamc@275 2074 string q'',
adamc@275 2075 string "\");",
adamc@275 2076 newline,
adamc@275 2077 newline,
adamc@275 2078 string "if (res == NULL) {",
adamc@275 2079 newline,
adamc@275 2080 box [string "PQfinish(conn);",
adamc@275 2081 newline,
adamc@311 2082 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2083 newline],
adamc@275 2084 string "}",
adamc@275 2085 newline,
adamc@275 2086 newline,
adamc@275 2087 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2088 newline,
adamc@275 2089 box [string "char msg[1024];",
adamc@275 2090 newline,
adamc@275 2091 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2092 newline,
adamc@275 2093 string "msg[1023] = 0;",
adamc@275 2094 newline,
adamc@275 2095 string "PQclear(res);",
adamc@275 2096 newline,
adamc@275 2097 string "PQfinish(conn);",
adamc@275 2098 newline,
adamc@311 2099 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2100 string q'',
adamc@275 2101 string "\\n%s\", msg);",
adamc@275 2102 newline],
adamc@275 2103 string "}",
adamc@275 2104 newline,
adamc@275 2105 newline,
adamc@275 2106 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2107 string (Int.toString (length xts)),
adamc@275 2108 string "\")) {",
adamc@275 2109 newline,
adamc@275 2110 box [string "PQclear(res);",
adamc@275 2111 newline,
adamc@275 2112 string "PQfinish(conn);",
adamc@275 2113 newline,
adamc@311 2114 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2115 string s,
adamc@275 2116 string "' has extra columns.\");",
adamc@275 2117 newline],
adamc@275 2118 string "}",
adamc@275 2119 newline,
adamc@275 2120 newline,
adamc@275 2121 string "PQclear(res);",
adamc@275 2122 newline]
adamc@275 2123 end) tables,
adamc@377 2124
adamc@377 2125 p_list_sep newline
adamc@377 2126 (fn s =>
adamc@377 2127 let
adamc@377 2128 val sl = CharVector.map Char.toLower s
adamc@377 2129
adamc@377 2130 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2131 ^ sl ^ "' AND relkind = 'S'"
adamc@377 2132 in
adamc@377 2133 box [string "res = PQexec(conn, \"",
adamc@377 2134 string q,
adamc@377 2135 string "\");",
adamc@377 2136 newline,
adamc@377 2137 newline,
adamc@377 2138 string "if (res == NULL) {",
adamc@377 2139 newline,
adamc@377 2140 box [string "PQfinish(conn);",
adamc@377 2141 newline,
adamc@377 2142 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@377 2143 newline],
adamc@377 2144 string "}",
adamc@377 2145 newline,
adamc@377 2146 newline,
adamc@377 2147 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@377 2148 newline,
adamc@377 2149 box [string "char msg[1024];",
adamc@377 2150 newline,
adamc@377 2151 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@377 2152 newline,
adamc@377 2153 string "msg[1023] = 0;",
adamc@377 2154 newline,
adamc@377 2155 string "PQclear(res);",
adamc@377 2156 newline,
adamc@377 2157 string "PQfinish(conn);",
adamc@377 2158 newline,
adamc@377 2159 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@377 2160 string q,
adamc@377 2161 string "\\n%s\", msg);",
adamc@377 2162 newline],
adamc@377 2163 string "}",
adamc@377 2164 newline,
adamc@377 2165 newline,
adamc@377 2166 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@377 2167 newline,
adamc@377 2168 box [string "PQclear(res);",
adamc@377 2169 newline,
adamc@377 2170 string "PQfinish(conn);",
adamc@377 2171 newline,
adamc@377 2172 string "uw_error(ctx, FATAL, \"Sequence '",
adamc@377 2173 string s,
adamc@377 2174 string "' does not exist.\");",
adamc@377 2175 newline],
adamc@377 2176 string "}",
adamc@377 2177 newline,
adamc@377 2178 newline,
adamc@377 2179 string "PQclear(res);",
adamc@377 2180 newline]
adamc@377 2181 end) sequences,
adamc@377 2182
adamc@275 2183 string "}"]
adamc@432 2184
adamc@432 2185 val hasDb = List.exists (fn (DDatabase _, _) => true | _ => false) ds
adamc@29 2186 in
adamc@144 2187 box [string "#include <stdio.h>",
adamc@144 2188 newline,
adamc@144 2189 string "#include <stdlib.h>",
adamc@144 2190 newline,
adamc@272 2191 string "#include <string.h>",
adamc@272 2192 newline,
adamc@390 2193 string "#include <math.h>",
adamc@390 2194 newline,
adamc@432 2195 if hasDb then
adamc@432 2196 box [string "#include <postgresql/libpq-fe.h>",
adamc@432 2197 newline]
adamc@432 2198 else
adamc@432 2199 box [],
adamc@144 2200 newline,
adamc@378 2201 string "#include \"",
adamc@378 2202 string (OS.Path.joinDirFile {dir = Config.includ,
adamc@378 2203 file = "urweb.h"}),
adamc@378 2204 string "\"",
adamc@101 2205 newline,
adamc@101 2206 newline,
adamc@101 2207 p_list_sep newline (fn x => x) pds,
adamc@101 2208 newline,
adamc@311 2209 string "int uw_inputs_len = ",
adamc@144 2210 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
adamc@144 2211 string ";",
adamc@144 2212 newline,
adamc@144 2213 newline,
adamc@311 2214 string "int uw_input_num(char *name) {",
adamc@144 2215 newline,
adamc@144 2216 makeSwitch (fnums, 0),
adamc@144 2217 string "}",
adamc@144 2218 newline,
adamc@144 2219 newline,
adamc@311 2220 string "void uw_handle(uw_context ctx, char *request) {",
adamc@101 2221 newline,
adamc@101 2222 p_list_sep newline (fn x => x) pds',
adamc@101 2223 newline,
adamc@387 2224 string "uw_error(ctx, FATAL, \"Unknown page\");",
adamc@387 2225 newline,
adamc@101 2226 string "}",
adamc@275 2227 newline,
adamc@275 2228 newline,
adamc@432 2229 if hasDb then
adamc@432 2230 validate
adamc@432 2231 else
adamc@432 2232 box [],
adamc@376 2233 newline,
adamc@376 2234 if List.exists (fn (DDatabase _, _) => true | _ => false) ds then
adamc@376 2235 box []
adamc@376 2236 else
adamc@376 2237 box [newline,
adamc@376 2238 string "void uw_db_init(uw_context ctx) { };",
adamc@424 2239 newline,
adamc@424 2240 string "int uw_db_begin(uw_context ctx) { return 0; };",
adamc@424 2241 newline,
adamc@424 2242 string "int uw_db_commit(uw_context ctx) { return 0; };",
adamc@424 2243 newline,
adamc@424 2244 string "int uw_db_rollback(uw_context ctx) { return 0; };",
adamc@376 2245 newline]]
adamc@29 2246 end
adamc@29 2247
adamc@274 2248 fun p_sql env (ds, _) =
adamc@274 2249 let
adamc@274 2250 val (pps, _) = ListUtil.foldlMap
adamc@274 2251 (fn (dAll as (d, _), env) =>
adamc@274 2252 let
adamc@274 2253 val pp = case d of
adamc@274 2254 DTable (s, xts) =>
adamc@274 2255 box [string "CREATE TABLE ",
adamc@274 2256 string s,
adamc@274 2257 string "(",
adamc@274 2258 p_list (fn (x, t) =>
adamc@311 2259 box [string "uw_",
adamc@275 2260 string (CharVector.map Char.toLower x),
adamc@274 2261 space,
adamc@274 2262 p_sqltype env t,
adamc@274 2263 space,
adamc@274 2264 string "NOT",
adamc@274 2265 space,
adamc@274 2266 string "NULL"]) xts,
adamc@274 2267 string ");",
adamc@274 2268 newline,
adamc@274 2269 newline]
adamc@338 2270 | DSequence s =>
adamc@338 2271 box [string "CREATE SEQUENCE ",
adamc@338 2272 string s,
adamc@338 2273 string ";",
adamc@338 2274 newline,
adamc@338 2275 newline]
adamc@274 2276 | _ => box []
adamc@274 2277 in
adamc@274 2278 (pp, E.declBinds env dAll)
adamc@274 2279 end)
adamc@274 2280 env ds
adamc@274 2281 in
adamc@274 2282 box pps
adamc@274 2283 end
adamc@274 2284
adamc@29 2285 end