annotate src/cjr_print.sml @ 613:c5991cdb0c4b

Initial parsing of RPC results
author Adam Chlipala <adamc@hcoop.net>
date Sun, 15 Feb 2009 12:33:41 -0500
parents a8704dfc58cf
children 3ee6bb48f6e8
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@463 65 fun isUnboxable (t : typ) =
adamc@463 66 case #1 t of
adamc@463 67 TDatatype (Default, _, _) => true
adamc@463 68 | TFfi ("Basis", "string") => true
adamc@463 69 | _ => false
adamc@463 70
adamc@29 71 fun p_typ' par env (t, loc) =
adamc@29 72 case t of
adamc@476 73 TFun (t1, t2) => (EM.errorAt loc "Function type remains";
adamc@476 74 string "<FUNCTION>")
adamc@29 75 | TRecord i => box [string "struct",
adamc@29 76 space,
adamc@311 77 string "__uws_",
adamc@29 78 string (Int.toString i)]
adamc@188 79 | TDatatype (Enum, n, _) =>
adamc@188 80 (box [string "enum",
adamc@188 81 space,
adamc@311 82 string ("__uwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
adamc@311 83 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@198 84 | TDatatype (Option, n, xncs) =>
adamc@198 85 (case ListUtil.search #3 (!xncs) of
adamc@198 86 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
adamc@198 87 | SOME t =>
adamc@198 88 case #1 t of
adamc@198 89 TDatatype _ => p_typ' par env t
adamc@199 90 | TFfi ("Basis", "string") => p_typ' par env t
adamc@198 91 | _ => box [p_typ' par env t,
adamc@198 92 string "*"])
adamc@188 93 | TDatatype (Default, n, _) =>
adamc@165 94 (box [string "struct",
adamc@165 95 space,
adamc@311 96 string ("__uwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
adamc@311 97 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@316 98 | TFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@288 99 | TOption t =>
adamc@463 100 if isUnboxable t then
adamc@463 101 p_typ' par env t
adamc@463 102 else
adamc@463 103 box [p_typ' par env t,
adamc@463 104 string "*"]
adamc@29 105
adamc@29 106 and p_typ env = p_typ' false env
adamc@29 107
adamc@316 108 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@311 109 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 110
adamc@109 111 fun p_enamed env n =
adamc@316 112 string ("__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n)
adamc@311 113 handle CjrEnv.UnboundNamed _ => string ("__uwn_UNBOUND_" ^ Int.toString n)
adamc@109 114
adamc@182 115 fun p_con_named env n =
adamc@316 116 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
adamc@311 117 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
adamc@182 118
adamc@182 119 fun p_pat_preamble env (p, _) =
adamc@182 120 case p of
adamc@182 121 PWild => (box [],
adamc@182 122 env)
adamc@182 123 | PVar (x, t) => (box [p_typ env t,
adamc@182 124 space,
adamc@311 125 string "__uwr_",
adamc@316 126 p_ident x,
adamc@182 127 string "_",
adamc@182 128 string (Int.toString (E.countERels env)),
adamc@182 129 string ";",
adamc@182 130 newline],
adamc@196 131 E.pushERel env x t)
adamc@182 132 | PPrim _ => (box [], env)
adamc@188 133 | PCon (_, _, NONE) => (box [], env)
adamc@188 134 | PCon (_, _, SOME p) => p_pat_preamble env p
adamc@182 135 | PRecord xps =>
adamc@182 136 foldl (fn ((_, p, _), (pp, env)) =>
adamc@182 137 let
adamc@182 138 val (pp', env) = p_pat_preamble env p
adamc@182 139 in
adamc@182 140 (box [pp', pp], env)
adamc@182 141 end) (box [], env) xps
adamc@288 142 | PNone _ => (box [], env)
adamc@288 143 | PSome (_, p) => p_pat_preamble env p
adamc@182 144
adamc@182 145 fun p_patCon env pc =
adamc@182 146 case pc of
adamc@182 147 PConVar n => p_con_named env n
adamc@316 148 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
adamc@182 149
adamc@182 150 fun p_pat (env, exit, depth) (p, _) =
adamc@182 151 case p of
adamc@182 152 PWild =>
adamc@182 153 (box [], env)
adamc@182 154 | PVar (x, t) =>
adamc@311 155 (box [string "__uwr_",
adamc@316 156 p_ident x,
adamc@182 157 string "_",
adamc@182 158 string (Int.toString (E.countERels env)),
adamc@182 159 space,
adamc@182 160 string "=",
adamc@182 161 space,
adamc@182 162 string "disc",
adamc@182 163 string (Int.toString depth),
adamc@182 164 string ";"],
adamc@182 165 E.pushERel env x t)
adamc@182 166 | PPrim (Prim.Int n) =>
adamc@182 167 (box [string "if",
adamc@182 168 space,
adamc@182 169 string "(disc",
adamc@182 170 string (Int.toString depth),
adamc@182 171 space,
adamc@182 172 string "!=",
adamc@182 173 space,
adamc@276 174 Prim.p_t_GCC (Prim.Int n),
adamc@182 175 string ")",
adamc@182 176 space,
adamc@182 177 exit],
adamc@182 178 env)
adamc@182 179 | PPrim (Prim.String s) =>
adamc@182 180 (box [string "if",
adamc@182 181 space,
adamc@182 182 string "(strcmp(disc",
adamc@182 183 string (Int.toString depth),
adamc@182 184 string ",",
adamc@182 185 space,
adamc@276 186 Prim.p_t_GCC (Prim.String s),
adamc@182 187 string "))",
adamc@182 188 space,
adamc@182 189 exit],
adamc@182 190 env)
adamc@182 191 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
adamc@182 192
adamc@188 193 | PCon (dk, pc, po) =>
adamc@182 194 let
adamc@182 195 val (p, env) =
adamc@182 196 case po of
adamc@182 197 NONE => (box [], env)
adamc@182 198 | SOME p =>
adamc@182 199 let
adamc@182 200 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 201
adamc@182 202 val (x, to) = case pc of
adamc@182 203 PConVar n =>
adamc@182 204 let
adamc@182 205 val (x, to, _) = E.lookupConstructor env n
adamc@182 206 in
adamc@316 207 ("uw_" ^ ident x, to)
adamc@182 208 end
adamc@188 209 | PConFfi {mod = m, con, arg, ...} =>
adamc@316 210 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
adamc@182 211
adamc@182 212 val t = case to of
adamc@182 213 NONE => raise Fail "CjrPrint: Constructor mismatch"
adamc@182 214 | SOME t => t
adamc@182 215 in
adamc@182 216 (box [string "{",
adamc@182 217 newline,
adamc@182 218 p_typ env t,
adamc@182 219 space,
adamc@182 220 string "disc",
adamc@182 221 string (Int.toString (depth + 1)),
adamc@182 222 space,
adamc@182 223 string "=",
adamc@182 224 space,
adamc@198 225 case dk of
adamc@198 226 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
adamc@198 227 | Default => box [string "disc",
adamc@198 228 string (Int.toString depth),
adamc@198 229 string "->data.",
adamc@198 230 string x]
adamc@198 231 | Option =>
adamc@463 232 if isUnboxable t then
adamc@463 233 box [string "disc",
adamc@463 234 string (Int.toString depth)]
adamc@463 235 else
adamc@463 236 box [string "*disc",
adamc@463 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@463 338 if isUnboxable t then
adamc@463 339 box [string "disc",
adamc@463 340 string (Int.toString depth)]
adamc@463 341 else
adamc@463 342 box [string "*disc",
adamc@463 343 string (Int.toString depth)],
adamc@288 344 string ";",
adamc@288 345 newline,
adamc@288 346 p,
adamc@288 347 newline,
adamc@288 348 string "}"],
adamc@288 349 env)
adamc@288 350 end
adamc@288 351 in
adamc@288 352 (box [string "if",
adamc@288 353 space,
adamc@288 354 string "(disc",
adamc@288 355 string (Int.toString depth),
adamc@288 356 space,
adamc@288 357 string "==",
adamc@288 358 space,
adamc@288 359 string "NULL)",
adamc@288 360 space,
adamc@288 361 exit,
adamc@288 362 newline,
adamc@288 363 p],
adamc@288 364 env)
adamc@288 365 end
adamc@288 366
adamc@182 367 local
adamc@182 368 val count = ref 0
adamc@182 369 in
adamc@182 370 fun newGoto () =
adamc@182 371 let
adamc@182 372 val r = !count
adamc@182 373 in
adamc@182 374 count := r + 1;
adamc@182 375 string ("L" ^ Int.toString r)
adamc@182 376 end
adamc@182 377 end
adamc@182 378
adamc@185 379 fun patConInfo env pc =
adamc@185 380 case pc of
adamc@185 381 PConVar n =>
adamc@185 382 let
adamc@185 383 val (x, _, dn) = E.lookupConstructor env n
adamc@185 384 val (dx, _) = E.lookupDatatype env dn
adamc@185 385 in
adamc@316 386 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
adamc@316 387 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
adamc@316 388 "uw_" ^ ident x)
adamc@185 389 end
adamc@186 390 | PConFfi {mod = m, datatyp, con, ...} =>
adamc@316 391 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
adamc@316 392 "uw_" ^ ident m ^ "_" ^ ident con,
adamc@316 393 "uw_" ^ ident con)
adamc@185 394
adamc@324 395 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e =
adamc@278 396 case t of
adamc@311 397 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
adamc@311 398 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
adamc@324 399 | TFfi ("Basis", "string") =>
adamc@324 400 if wontLeakStrings then
adamc@324 401 e
adamc@324 402 else
adamc@324 403 box [string "uw_Basis_strdup(ctx, ", e, string ")"]
adamc@311 404 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
adamc@438 405 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
adamc@467 406
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@467 411 fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
adamc@467 412 case t of
adamc@467 413 TOption t =>
adamc@467 414 box [string "(PQgetisnull (res, i, ",
adamc@467 415 string (Int.toString i),
adamc@467 416 string ") ? NULL : ",
adamc@467 417 case t of
adamc@467 418 (TFfi ("Basis", "string"), _) => p_getcol wontLeakStrings env t i
adamc@467 419 | _ => box [string "({",
adamc@467 420 newline,
adamc@467 421 p_typ env t,
adamc@467 422 space,
adamc@467 423 string "*tmp = uw_malloc(ctx, sizeof(",
adamc@467 424 p_typ env t,
adamc@467 425 string "));",
adamc@467 426 newline,
adamc@467 427 string "*tmp = ",
adamc@467 428 p_getcol wontLeakStrings env t i,
adamc@467 429 string ";",
adamc@467 430 newline,
adamc@467 431 string "tmp;",
adamc@467 432 newline,
adamc@467 433 string "})"],
adamc@467 434 string ")"]
adamc@467 435
adamc@467 436 | _ =>
adamc@467 437 p_unsql wontLeakStrings env tAll
adamc@467 438 (box [string "PQgetvalue(res, i, ",
adamc@467 439 string (Int.toString i),
adamc@467 440 string ")"])
adamc@467 441
adamc@282 442 datatype sql_type =
adamc@282 443 Int
adamc@282 444 | Float
adamc@282 445 | String
adamc@282 446 | Bool
adamc@439 447 | Time
adamc@467 448 | Nullable of sql_type
adamc@282 449
adamc@467 450 fun p_sql_type' t =
adamc@467 451 case t of
adamc@467 452 Int => "uw_Basis_int"
adamc@467 453 | Float => "uw_Basis_float"
adamc@467 454 | String => "uw_Basis_string"
adamc@467 455 | Bool => "uw_Basis_bool"
adamc@467 456 | Time => "uw_Basis_time"
adamc@467 457 | Nullable String => "uw_Basis_string"
adamc@467 458 | Nullable t => p_sql_type' t ^ "*"
adamc@467 459
adamc@467 460 fun p_sql_type t = string (p_sql_type' t)
adamc@282 461
adamc@282 462 fun getPargs (e, _) =
adamc@282 463 case e of
adamc@282 464 EPrim (Prim.String _) => []
adamc@282 465 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
adamc@282 466
adamc@282 467 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
adamc@282 468 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
adamc@282 469 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
adamc@282 470 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
adamc@439 471 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)]
adamc@468 472
adamc@468 473 | EFfiApp ("Basis", "sqlifyIntN", [e]) => [(e, Nullable Int)]
adamc@468 474 | EFfiApp ("Basis", "sqlifyFloatN", [e]) => [(e, Nullable Float)]
adamc@468 475 | EFfiApp ("Basis", "sqlifyStringN", [e]) => [(e, Nullable String)]
adamc@468 476 | EFfiApp ("Basis", "sqlifyBoolN", [e]) => [(e, Nullable Bool)]
adamc@468 477 | EFfiApp ("Basis", "sqlifyTimeN", [e]) => [(e, Nullable Time)]
adamc@468 478
adamc@491 479 | ECase (e,
adamc@491 480 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
adamc@491 481 (EPrim (Prim.String "TRUE"), _)),
adamc@491 482 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
adamc@491 483 (EPrim (Prim.String "FALSE"), _))],
adamc@491 484 _) => [(e, Bool)]
adamc@282 485
adamc@282 486 | _ => raise Fail "CjrPrint: getPargs"
adamc@282 487
adamc@282 488 fun p_ensql t e =
adamc@282 489 case t of
adamc@311 490 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
adamc@311 491 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
adamc@282 492 | String => e
adamc@295 493 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
adamc@439 494 | Time => box [string "uw_Basis_sqlifyTime(ctx, ", e, string ")"]
adamc@467 495 | Nullable String => e
adamc@467 496 | Nullable t => box [string "(",
adamc@467 497 e,
adamc@467 498 string " == NULL ? NULL : ",
adamc@467 499 p_ensql t (box [string "*", e]),
adamc@467 500 string ")"]
adamc@282 501
adamc@324 502 fun notLeaky env allowHeapAllocated =
adamc@324 503 let
adamc@324 504 fun nl (t, _) =
adamc@324 505 case t of
adamc@324 506 TFun _ => false
adamc@324 507 | TRecord n =>
adamc@324 508 let
adamc@324 509 val xts = E.lookupStruct env n
adamc@324 510 in
adamc@324 511 List.all (fn (_, t) => nl t) xts
adamc@324 512 end
adamc@324 513 | TDatatype (dk, _, ref cons) =>
adamc@324 514 (allowHeapAllocated orelse dk = Enum)
adamc@324 515 andalso List.all (fn (_, _, to) => case to of
adamc@324 516 NONE => true
adamc@324 517 | SOME t => nl t) cons
adamc@324 518 | TFfi ("Basis", "string") => false
adamc@324 519 | TFfi _ => true
adamc@324 520 | TOption t => allowHeapAllocated andalso nl t
adamc@324 521 in
adamc@324 522 nl
adamc@324 523 end
adamc@324 524
adamc@463 525 fun capitalize s =
adamc@463 526 if s = "" then
adamc@463 527 ""
adamc@463 528 else
adamc@463 529 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@463 530
adamc@463 531 fun unurlify env (t, loc) =
adamc@463 532 let
adamc@463 533 fun unurlify' rf t =
adamc@463 534 case t of
adamc@463 535 TFfi ("Basis", "unit") => string ("uw_unit_v")
adamc@463 536 | TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
adamc@463 537
adamc@463 538 | TRecord 0 => string "uw_unit_v"
adamc@463 539 | TRecord i =>
adamc@463 540 let
adamc@463 541 val xts = E.lookupStruct env i
adamc@463 542 in
adamc@463 543 box [string "({",
adamc@463 544 newline,
adamc@463 545 box (map (fn (x, t) =>
adamc@463 546 box [p_typ env t,
adamc@463 547 space,
adamc@463 548 string "uwr_",
adamc@463 549 string x,
adamc@463 550 space,
adamc@463 551 string "=",
adamc@463 552 space,
adamc@463 553 unurlify' rf (#1 t),
adamc@463 554 string ";",
adamc@463 555 newline]) xts),
adamc@463 556 string "struct",
adamc@463 557 space,
adamc@463 558 string "__uws_",
adamc@463 559 string (Int.toString i),
adamc@463 560 space,
adamc@463 561 string "tmp",
adamc@463 562 space,
adamc@463 563 string "=",
adamc@463 564 space,
adamc@463 565 string "{",
adamc@463 566 space,
adamc@463 567 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
adamc@463 568 string x]) xts,
adamc@463 569 space,
adamc@463 570 string "};",
adamc@463 571 newline,
adamc@463 572 string "tmp;",
adamc@463 573 newline,
adamc@463 574 string "})"]
adamc@463 575 end
adamc@463 576
adamc@463 577 | TDatatype (Enum, i, _) =>
adamc@463 578 let
adamc@463 579 val (x, xncs) = E.lookupDatatype env i
adamc@463 580
adamc@463 581 fun doEm xncs =
adamc@463 582 case xncs of
adamc@463 583 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 584 ^ x ^ "\"), (enum __uwe_"
adamc@463 585 ^ x ^ "_" ^ Int.toString i ^ ")0)")
adamc@463 586 | (x', n, to) :: rest =>
adamc@463 587 box [string "((!strncmp(request, \"",
adamc@463 588 string x',
adamc@463 589 string "\", ",
adamc@463 590 string (Int.toString (size x')),
adamc@463 591 string ") && (request[",
adamc@463 592 string (Int.toString (size x')),
adamc@463 593 string "] == 0 || request[",
adamc@463 594 string (Int.toString (size x')),
adamc@463 595 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 596 space,
adamc@463 597 string ":",
adamc@463 598 space,
adamc@463 599 doEm rest,
adamc@463 600 string ")"]
adamc@463 601 in
adamc@463 602 doEm xncs
adamc@463 603 end
adamc@463 604
adamc@463 605 | TDatatype (Option, i, xncs) =>
adamc@463 606 if IS.member (rf, i) then
adamc@463 607 box [string "unurlify_",
adamc@463 608 string (Int.toString i),
adamc@463 609 string "()"]
adamc@463 610 else
adamc@463 611 let
adamc@463 612 val (x, _) = E.lookupDatatype env i
adamc@463 613
adamc@463 614 val (no_arg, has_arg, t) =
adamc@463 615 case !xncs of
adamc@463 616 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@463 617 (no_arg, has_arg, t)
adamc@463 618 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@463 619 (no_arg, has_arg, t)
adamc@463 620 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
adamc@463 621
adamc@463 622 val rf = IS.add (rf, i)
adamc@463 623 in
adamc@463 624 box [string "({",
adamc@463 625 space,
adamc@463 626 p_typ env t,
adamc@463 627 space,
adamc@463 628 string "*unurlify_",
adamc@463 629 string (Int.toString i),
adamc@463 630 string "(void) {",
adamc@463 631 newline,
adamc@463 632 box [string "return (request[0] == '/' ? ++request : request,",
adamc@463 633 newline,
adamc@463 634 string "((!strncmp(request, \"",
adamc@463 635 string no_arg,
adamc@463 636 string "\", ",
adamc@463 637 string (Int.toString (size no_arg)),
adamc@463 638 string ") && (request[",
adamc@463 639 string (Int.toString (size no_arg)),
adamc@463 640 string "] == 0 || request[",
adamc@463 641 string (Int.toString (size no_arg)),
adamc@463 642 string "] == '/')) ? (request",
adamc@463 643 space,
adamc@463 644 string "+=",
adamc@463 645 space,
adamc@463 646 string (Int.toString (size no_arg)),
adamc@463 647 string ", NULL) : ((!strncmp(request, \"",
adamc@463 648 string has_arg,
adamc@463 649 string "\", ",
adamc@463 650 string (Int.toString (size has_arg)),
adamc@463 651 string ") && (request[",
adamc@463 652 string (Int.toString (size has_arg)),
adamc@463 653 string "] == 0 || request[",
adamc@463 654 string (Int.toString (size has_arg)),
adamc@463 655 string "] == '/')) ? (request",
adamc@463 656 space,
adamc@463 657 string "+=",
adamc@463 658 space,
adamc@463 659 string (Int.toString (size has_arg)),
adamc@463 660 string ", (request[0] == '/' ? ++request : NULL), ",
adamc@463 661 newline,
adamc@463 662
adamc@463 663 if isUnboxable t then
adamc@463 664 unurlify' rf (#1 t)
adamc@463 665 else
adamc@463 666 box [string "({",
adamc@463 667 newline,
adamc@463 668 p_typ env t,
adamc@463 669 space,
adamc@463 670 string "*tmp",
adamc@463 671 space,
adamc@463 672 string "=",
adamc@463 673 space,
adamc@463 674 string "uw_malloc(ctx, sizeof(",
adamc@463 675 p_typ env t,
adamc@463 676 string "));",
adamc@463 677 newline,
adamc@463 678 string "*tmp",
adamc@463 679 space,
adamc@463 680 string "=",
adamc@463 681 space,
adamc@463 682 unurlify' rf (#1 t),
adamc@463 683 string ";",
adamc@463 684 newline,
adamc@463 685 string "tmp;",
adamc@463 686 newline,
adamc@463 687 string "})"],
adamc@463 688 string ")",
adamc@463 689 newline,
adamc@463 690 string ":",
adamc@463 691 space,
adamc@463 692 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
adamc@463 693 ^ "\"), NULL))));"),
adamc@463 694 newline],
adamc@463 695 string "}",
adamc@463 696 newline,
adamc@463 697 newline,
adamc@463 698
adamc@463 699 string "unurlify_",
adamc@463 700 string (Int.toString i),
adamc@463 701 string "();",
adamc@463 702 newline,
adamc@463 703 string "})"]
adamc@463 704 end
adamc@463 705
adamc@463 706 | TDatatype (Default, i, _) =>
adamc@463 707 if IS.member (rf, i) then
adamc@463 708 box [string "unurlify_",
adamc@463 709 string (Int.toString i),
adamc@463 710 string "()"]
adamc@463 711 else
adamc@463 712 let
adamc@463 713 val (x, xncs) = E.lookupDatatype env i
adamc@463 714
adamc@463 715 val rf = IS.add (rf, i)
adamc@463 716
adamc@463 717 fun doEm xncs =
adamc@463 718 case xncs of
adamc@463 719 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 720 ^ x ^ "\"), NULL)")
adamc@463 721 | (x', n, to) :: rest =>
adamc@463 722 box [string "((!strncmp(request, \"",
adamc@463 723 string x',
adamc@463 724 string "\", ",
adamc@463 725 string (Int.toString (size x')),
adamc@463 726 string ") && (request[",
adamc@463 727 string (Int.toString (size x')),
adamc@463 728 string "] == 0 || request[",
adamc@463 729 string (Int.toString (size x')),
adamc@463 730 string "] == '/')) ? ({",
adamc@463 731 newline,
adamc@463 732 string "struct",
adamc@463 733 space,
adamc@463 734 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
adamc@463 735 space,
adamc@463 736 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
adamc@463 737 string x,
adamc@463 738 string "_",
adamc@463 739 string (Int.toString i),
adamc@463 740 string "));",
adamc@463 741 newline,
adamc@463 742 string "tmp->tag",
adamc@463 743 space,
adamc@463 744 string "=",
adamc@463 745 space,
adamc@463 746 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 747 string ";",
adamc@463 748 newline,
adamc@463 749 string "request",
adamc@463 750 space,
adamc@463 751 string "+=",
adamc@463 752 space,
adamc@463 753 string (Int.toString (size x')),
adamc@463 754 string ";",
adamc@463 755 newline,
adamc@463 756 string "if (request[0] == '/') ++request;",
adamc@463 757 newline,
adamc@463 758 case to of
adamc@463 759 NONE => box []
adamc@463 760 | SOME (t, _) => box [string "tmp->data.uw_",
adamc@463 761 p_ident x',
adamc@463 762 space,
adamc@463 763 string "=",
adamc@463 764 space,
adamc@463 765 unurlify' rf t,
adamc@463 766 string ";",
adamc@463 767 newline],
adamc@463 768 string "tmp;",
adamc@463 769 newline,
adamc@463 770 string "})",
adamc@463 771 space,
adamc@463 772 string ":",
adamc@463 773 space,
adamc@463 774 doEm rest,
adamc@463 775 string ")"]
adamc@463 776 in
adamc@463 777 box [string "({",
adamc@463 778 space,
adamc@463 779 p_typ env (t, ErrorMsg.dummySpan),
adamc@463 780 space,
adamc@463 781 string "unurlify_",
adamc@463 782 string (Int.toString i),
adamc@463 783 string "(void) {",
adamc@463 784 newline,
adamc@463 785 box [string "return",
adamc@463 786 space,
adamc@463 787 doEm xncs,
adamc@463 788 string ";",
adamc@463 789 newline],
adamc@463 790 string "}",
adamc@463 791 newline,
adamc@463 792 newline,
adamc@463 793
adamc@463 794 string "unurlify_",
adamc@463 795 string (Int.toString i),
adamc@463 796 string "();",
adamc@463 797 newline,
adamc@463 798 string "})"]
adamc@463 799 end
adamc@463 800
adamc@471 801 | TOption t =>
adamc@471 802 box [string "(request[0] == '/' ? ++request : request, ",
adamc@471 803 string "((!strncmp(request, \"None\", 4) ",
adamc@471 804 string "&& (request[4] == 0 || request[4] == '/')) ",
adamc@471 805 string "? (request += 4, NULL) ",
adamc@471 806 string ": ((!strncmp(request, \"Some\", 4) ",
adamc@471 807 string "&& request[4] == '/') ",
adamc@471 808 string "? (request += 5, ",
adamc@471 809 if isUnboxable t then
adamc@471 810 unurlify' rf (#1 t)
adamc@471 811 else
adamc@471 812 box [string "({",
adamc@471 813 newline,
adamc@471 814 p_typ env t,
adamc@471 815 space,
adamc@471 816 string "*tmp",
adamc@471 817 space,
adamc@471 818 string "=",
adamc@471 819 space,
adamc@471 820 string "uw_malloc(ctx, sizeof(",
adamc@471 821 p_typ env t,
adamc@471 822 string "));",
adamc@471 823 newline,
adamc@471 824 string "*tmp",
adamc@471 825 space,
adamc@471 826 string "=",
adamc@471 827 space,
adamc@471 828 unurlify' rf (#1 t),
adamc@471 829 string ";",
adamc@471 830 newline,
adamc@471 831 string "tmp;",
adamc@471 832 newline,
adamc@471 833 string "})"],
adamc@471 834 string ") :",
adamc@471 835 space,
adamc@471 836 string "(uw_error(ctx, FATAL, \"Error unurlifying option type\"), NULL))))"]
adamc@471 837
adamc@463 838 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
adamc@463 839 space)
adamc@463 840 in
adamc@463 841 unurlify' IS.empty t
adamc@463 842 end
adamc@463 843
adamc@610 844 fun urlify env t =
adamc@610 845 let
adamc@610 846 fun urlify' rf level (t as (_, loc)) =
adamc@610 847 case #1 t of
adamc@610 848 TFfi ("Basis", "unit") => box []
adamc@610 849 | TFfi (m, t) => box [string ("uw_" ^ ident m ^ "_urlify" ^ capitalize t
adamc@610 850 ^ "_w(ctx, it" ^ Int.toString level ^ ");"),
adamc@610 851 newline]
adamc@610 852
adamc@610 853 | TRecord 0 => box []
adamc@610 854 | TRecord i =>
adamc@610 855 let
adamc@611 856 fun empty (t, _) =
adamc@611 857 case t of
adamc@611 858 TFfi ("Basis", "unit") => true
adamc@611 859 | TRecord 0 => true
adamc@611 860 | TRecord j =>
adamc@611 861 List.all (fn (_, t) => empty t) (E.lookupStruct env j)
adamc@611 862 | _ => false
adamc@611 863
adamc@610 864 val xts = E.lookupStruct env i
adamc@611 865
adamc@613 866 val (blocks, _) = foldl
adamc@613 867 (fn ((x, t), (blocks, printingSinceLastSlash)) =>
adamc@613 868 let
adamc@613 869 val thisEmpty = empty t
adamc@613 870 in
adamc@613 871 if thisEmpty then
adamc@613 872 (blocks, printingSinceLastSlash)
adamc@613 873 else
adamc@613 874 (box [string "{",
adamc@613 875 newline,
adamc@613 876 p_typ env t,
adamc@613 877 space,
adamc@613 878 string ("it" ^ Int.toString (level + 1)),
adamc@613 879 space,
adamc@613 880 string "=",
adamc@613 881 space,
adamc@613 882 string ("it" ^ Int.toString level ^ ".__uwf_" ^ x ^ ";"),
adamc@613 883 newline,
adamc@613 884 box (if printingSinceLastSlash then
adamc@613 885 [string "uw_write(ctx, \"/\");",
adamc@613 886 newline]
adamc@613 887 else
adamc@613 888 []),
adamc@613 889 urlify' rf (level + 1) t,
adamc@613 890 string "}",
adamc@613 891 newline] :: blocks,
adamc@613 892 true)
adamc@613 893 end)
adamc@613 894 ([], false) xts
adamc@610 895 in
adamc@613 896 box (rev blocks)
adamc@610 897 end
adamc@610 898
adamc@610 899 | TDatatype (Enum, i, _) => box []
adamc@610 900 (*let
adamc@610 901 val (x, xncs) = E.lookupDatatype env i
adamc@610 902
adamc@610 903 fun doEm xncs =
adamc@610 904 case xncs of
adamc@610 905 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@610 906 ^ x ^ "\"), (enum __uwe_"
adamc@610 907 ^ x ^ "_" ^ Int.toString i ^ ")0)")
adamc@610 908 | (x', n, to) :: rest =>
adamc@610 909 box [string "((!strncmp(request, \"",
adamc@610 910 string x',
adamc@610 911 string "\", ",
adamc@610 912 string (Int.toString (size x')),
adamc@610 913 string ") && (request[",
adamc@610 914 string (Int.toString (size x')),
adamc@610 915 string "] == 0 || request[",
adamc@610 916 string (Int.toString (size x')),
adamc@610 917 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@610 918 space,
adamc@610 919 string ":",
adamc@610 920 space,
adamc@610 921 doEm rest,
adamc@610 922 string ")"]
adamc@610 923 in
adamc@610 924 doEm xncs
adamc@610 925 end*)
adamc@610 926
adamc@610 927 | TDatatype (Option, i, xncs) => box []
adamc@610 928 (*if IS.member (rf, i) then
adamc@610 929 box [string "unurlify_",
adamc@610 930 string (Int.toString i),
adamc@610 931 string "()"]
adamc@610 932 else
adamc@610 933 let
adamc@610 934 val (x, _) = E.lookupDatatype env i
adamc@610 935
adamc@610 936 val (no_arg, has_arg, t) =
adamc@610 937 case !xncs of
adamc@610 938 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@610 939 (no_arg, has_arg, t)
adamc@610 940 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@610 941 (no_arg, has_arg, t)
adamc@610 942 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
adamc@610 943
adamc@610 944 val rf = IS.add (rf, i)
adamc@610 945 in
adamc@610 946 box [string "({",
adamc@610 947 space,
adamc@610 948 p_typ env t,
adamc@610 949 space,
adamc@610 950 string "*unurlify_",
adamc@610 951 string (Int.toString i),
adamc@610 952 string "(void) {",
adamc@610 953 newline,
adamc@610 954 box [string "return (request[0] == '/' ? ++request : request,",
adamc@610 955 newline,
adamc@610 956 string "((!strncmp(request, \"",
adamc@610 957 string no_arg,
adamc@610 958 string "\", ",
adamc@610 959 string (Int.toString (size no_arg)),
adamc@610 960 string ") && (request[",
adamc@610 961 string (Int.toString (size no_arg)),
adamc@610 962 string "] == 0 || request[",
adamc@610 963 string (Int.toString (size no_arg)),
adamc@610 964 string "] == '/')) ? (request",
adamc@610 965 space,
adamc@610 966 string "+=",
adamc@610 967 space,
adamc@610 968 string (Int.toString (size no_arg)),
adamc@610 969 string ", NULL) : ((!strncmp(request, \"",
adamc@610 970 string has_arg,
adamc@610 971 string "\", ",
adamc@610 972 string (Int.toString (size has_arg)),
adamc@610 973 string ") && (request[",
adamc@610 974 string (Int.toString (size has_arg)),
adamc@610 975 string "] == 0 || request[",
adamc@610 976 string (Int.toString (size has_arg)),
adamc@610 977 string "] == '/')) ? (request",
adamc@610 978 space,
adamc@610 979 string "+=",
adamc@610 980 space,
adamc@610 981 string (Int.toString (size has_arg)),
adamc@610 982 string ", (request[0] == '/' ? ++request : NULL), ",
adamc@610 983 newline,
adamc@610 984
adamc@610 985 if isUnboxable t then
adamc@610 986 unurlify' rf (#1 t)
adamc@610 987 else
adamc@610 988 box [string "({",
adamc@610 989 newline,
adamc@610 990 p_typ env t,
adamc@610 991 space,
adamc@610 992 string "*tmp",
adamc@610 993 space,
adamc@610 994 string "=",
adamc@610 995 space,
adamc@610 996 string "uw_malloc(ctx, sizeof(",
adamc@610 997 p_typ env t,
adamc@610 998 string "));",
adamc@610 999 newline,
adamc@610 1000 string "*tmp",
adamc@610 1001 space,
adamc@610 1002 string "=",
adamc@610 1003 space,
adamc@610 1004 unurlify' rf (#1 t),
adamc@610 1005 string ";",
adamc@610 1006 newline,
adamc@610 1007 string "tmp;",
adamc@610 1008 newline,
adamc@610 1009 string "})"],
adamc@610 1010 string ")",
adamc@610 1011 newline,
adamc@610 1012 string ":",
adamc@610 1013 space,
adamc@610 1014 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
adamc@610 1015 ^ "\"), NULL))));"),
adamc@610 1016 newline],
adamc@610 1017 string "}",
adamc@610 1018 newline,
adamc@610 1019 newline,
adamc@610 1020
adamc@610 1021 string "unurlify_",
adamc@610 1022 string (Int.toString i),
adamc@610 1023 string "();",
adamc@610 1024 newline,
adamc@610 1025 string "})"]
adamc@610 1026 end*)
adamc@610 1027
adamc@610 1028 | TDatatype (Default, i, _) => box []
adamc@610 1029 (*if IS.member (rf, i) then
adamc@610 1030 box [string "unurlify_",
adamc@610 1031 string (Int.toString i),
adamc@610 1032 string "()"]
adamc@610 1033 else
adamc@610 1034 let
adamc@610 1035 val (x, xncs) = E.lookupDatatype env i
adamc@610 1036
adamc@610 1037 val rf = IS.add (rf, i)
adamc@610 1038
adamc@610 1039 fun doEm xncs =
adamc@610 1040 case xncs of
adamc@610 1041 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@610 1042 ^ x ^ "\"), NULL)")
adamc@610 1043 | (x', n, to) :: rest =>
adamc@610 1044 box [string "((!strncmp(request, \"",
adamc@610 1045 string x',
adamc@610 1046 string "\", ",
adamc@610 1047 string (Int.toString (size x')),
adamc@610 1048 string ") && (request[",
adamc@610 1049 string (Int.toString (size x')),
adamc@610 1050 string "] == 0 || request[",
adamc@610 1051 string (Int.toString (size x')),
adamc@610 1052 string "] == '/')) ? ({",
adamc@610 1053 newline,
adamc@610 1054 string "struct",
adamc@610 1055 space,
adamc@610 1056 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
adamc@610 1057 space,
adamc@610 1058 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
adamc@610 1059 string x,
adamc@610 1060 string "_",
adamc@610 1061 string (Int.toString i),
adamc@610 1062 string "));",
adamc@610 1063 newline,
adamc@610 1064 string "tmp->tag",
adamc@610 1065 space,
adamc@610 1066 string "=",
adamc@610 1067 space,
adamc@610 1068 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@610 1069 string ";",
adamc@610 1070 newline,
adamc@610 1071 string "request",
adamc@610 1072 space,
adamc@610 1073 string "+=",
adamc@610 1074 space,
adamc@610 1075 string (Int.toString (size x')),
adamc@610 1076 string ";",
adamc@610 1077 newline,
adamc@610 1078 string "if (request[0] == '/') ++request;",
adamc@610 1079 newline,
adamc@610 1080 case to of
adamc@610 1081 NONE => box []
adamc@610 1082 | SOME (t, _) => box [string "tmp->data.uw_",
adamc@610 1083 p_ident x',
adamc@610 1084 space,
adamc@610 1085 string "=",
adamc@610 1086 space,
adamc@610 1087 unurlify' rf t,
adamc@610 1088 string ";",
adamc@610 1089 newline],
adamc@610 1090 string "tmp;",
adamc@610 1091 newline,
adamc@610 1092 string "})",
adamc@610 1093 space,
adamc@610 1094 string ":",
adamc@610 1095 space,
adamc@610 1096 doEm rest,
adamc@610 1097 string ")"]
adamc@610 1098 in
adamc@610 1099 box [string "({",
adamc@610 1100 space,
adamc@610 1101 p_typ env (t, ErrorMsg.dummySpan),
adamc@610 1102 space,
adamc@610 1103 string "unurlify_",
adamc@610 1104 string (Int.toString i),
adamc@610 1105 string "(void) {",
adamc@610 1106 newline,
adamc@610 1107 box [string "return",
adamc@610 1108 space,
adamc@610 1109 doEm xncs,
adamc@610 1110 string ";",
adamc@610 1111 newline],
adamc@610 1112 string "}",
adamc@610 1113 newline,
adamc@610 1114 newline,
adamc@610 1115
adamc@610 1116 string "unurlify_",
adamc@610 1117 string (Int.toString i),
adamc@610 1118 string "();",
adamc@610 1119 newline,
adamc@610 1120 string "})"]
adamc@610 1121 end*)
adamc@610 1122
adamc@610 1123 | TOption t => box []
adamc@610 1124 (*box [string "(request[0] == '/' ? ++request : request, ",
adamc@610 1125 string "((!strncmp(request, \"None\", 4) ",
adamc@610 1126 string "&& (request[4] == 0 || request[4] == '/')) ",
adamc@610 1127 string "? (request += 4, NULL) ",
adamc@610 1128 string ": ((!strncmp(request, \"Some\", 4) ",
adamc@610 1129 string "&& request[4] == '/') ",
adamc@610 1130 string "? (request += 5, ",
adamc@610 1131 if isUnboxable t then
adamc@610 1132 unurlify' rf (#1 t)
adamc@610 1133 else
adamc@610 1134 box [string "({",
adamc@610 1135 newline,
adamc@610 1136 p_typ env t,
adamc@610 1137 space,
adamc@610 1138 string "*tmp",
adamc@610 1139 space,
adamc@610 1140 string "=",
adamc@610 1141 space,
adamc@610 1142 string "uw_malloc(ctx, sizeof(",
adamc@610 1143 p_typ env t,
adamc@610 1144 string "));",
adamc@610 1145 newline,
adamc@610 1146 string "*tmp",
adamc@610 1147 space,
adamc@610 1148 string "=",
adamc@610 1149 space,
adamc@610 1150 unurlify' rf (#1 t),
adamc@610 1151 string ";",
adamc@610 1152 newline,
adamc@610 1153 string "tmp;",
adamc@610 1154 newline,
adamc@610 1155 string "})"],
adamc@610 1156 string ") :",
adamc@610 1157 space,
adamc@610 1158 string "(uw_error(ctx, FATAL, \"Error unurlifying option type\"), NULL))))"]*)
adamc@610 1159
adamc@610 1160 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL encoding function";
adamc@610 1161 space)
adamc@610 1162 in
adamc@610 1163 urlify' IS.empty 0 t
adamc@610 1164 end
adamc@610 1165
adamc@182 1166 fun p_exp' par env (e, loc) =
adamc@29 1167 case e of
adamc@276 1168 EPrim p => Prim.p_t_GCC p
adamc@29 1169 | ERel n => p_rel env n
adamc@109 1170 | ENamed n => p_enamed env n
adamc@188 1171 | ECon (Enum, pc, _) => p_patCon env pc
adamc@198 1172 | ECon (Option, pc, NONE) => string "NULL"
adamc@198 1173 | ECon (Option, pc, SOME e) =>
adamc@198 1174 let
adamc@198 1175 val to = case pc of
adamc@198 1176 PConVar n => #2 (E.lookupConstructor env n)
adamc@198 1177 | PConFfi {arg, ...} => arg
adamc@198 1178
adamc@198 1179 val t = case to of
adamc@198 1180 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
adamc@198 1181 | SOME t => t
adamc@198 1182 in
adamc@463 1183 if isUnboxable t then
adamc@463 1184 p_exp' par env e
adamc@463 1185 else
adamc@463 1186 box [string "({",
adamc@463 1187 newline,
adamc@463 1188 p_typ env t,
adamc@463 1189 space,
adamc@463 1190 string "*tmp",
adamc@463 1191 space,
adamc@463 1192 string "=",
adamc@463 1193 space,
adamc@463 1194 string "uw_malloc(ctx, sizeof(",
adamc@463 1195 p_typ env t,
adamc@463 1196 string "));",
adamc@463 1197 newline,
adamc@463 1198 string "*tmp",
adamc@463 1199 space,
adamc@463 1200 string "=",
adamc@463 1201 p_exp' par env e,
adamc@463 1202 string ";",
adamc@463 1203 newline,
adamc@463 1204 string "tmp;",
adamc@463 1205 newline,
adamc@463 1206 string "})"]
adamc@198 1207 end
adamc@188 1208 | ECon (Default, pc, eo) =>
adamc@181 1209 let
adamc@196 1210 val (xd, xc, xn) = patConInfo env pc
adamc@181 1211 in
adamc@182 1212 box [string "({",
adamc@181 1213 newline,
adamc@181 1214 string "struct",
adamc@181 1215 space,
adamc@185 1216 string xd,
adamc@181 1217 space,
adamc@181 1218 string "*tmp",
adamc@181 1219 space,
adamc@181 1220 string "=",
adamc@181 1221 space,
adamc@311 1222 string "uw_malloc(ctx, sizeof(struct ",
adamc@185 1223 string xd,
adamc@181 1224 string "));",
adamc@181 1225 newline,
adamc@181 1226 string "tmp->tag",
adamc@181 1227 space,
adamc@181 1228 string "=",
adamc@181 1229 space,
adamc@185 1230 string xc,
adamc@181 1231 string ";",
adamc@181 1232 newline,
adamc@181 1233 case eo of
adamc@181 1234 NONE => box []
adamc@185 1235 | SOME e => box [string "tmp->data.",
adamc@196 1236 string xn,
adamc@181 1237 space,
adamc@181 1238 string "=",
adamc@181 1239 space,
adamc@181 1240 p_exp env e,
adamc@181 1241 string ";",
adamc@181 1242 newline],
adamc@181 1243 string "tmp;",
adamc@181 1244 newline,
adamc@181 1245 string "})"]
adamc@181 1246 end
adamc@297 1247 | ENone _ => string "NULL"
adamc@290 1248 | ESome (t, e) =>
adamc@463 1249 if isUnboxable t then
adamc@463 1250 p_exp' par env e
adamc@463 1251 else
adamc@463 1252 box [string "({",
adamc@463 1253 newline,
adamc@463 1254 p_typ env t,
adamc@463 1255 space,
adamc@463 1256 string "*tmp",
adamc@463 1257 space,
adamc@463 1258 string "=",
adamc@463 1259 space,
adamc@463 1260 string "uw_malloc(ctx, sizeof(",
adamc@463 1261 p_typ env t,
adamc@463 1262 string "));",
adamc@463 1263 newline,
adamc@463 1264 string "*tmp",
adamc@463 1265 space,
adamc@463 1266 string "=",
adamc@463 1267 p_exp' par env e,
adamc@463 1268 string ";",
adamc@463 1269 newline,
adamc@463 1270 string "tmp;",
adamc@463 1271 newline,
adamc@463 1272 string "})"]
adamc@109 1273
adamc@316 1274 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@283 1275 | EError (e, t) =>
adamc@283 1276 box [string "({",
adamc@283 1277 newline,
adamc@283 1278 p_typ env t,
adamc@283 1279 space,
adamc@283 1280 string "tmp;",
adamc@283 1281 newline,
adamc@311 1282 string "uw_error(ctx, FATAL, \"",
adamc@292 1283 string (ErrorMsg.spanToString loc),
adamc@292 1284 string ": %s\", ",
adamc@283 1285 p_exp env e,
adamc@283 1286 string ");",
adamc@283 1287 newline,
adamc@283 1288 string "tmp;",
adamc@283 1289 newline,
adamc@283 1290 string "})"]
adamc@476 1291 | EApp ((EError (e, (TFun (_, ran), _)), loc), _) =>
adamc@476 1292 p_exp env (EError (e, ran), loc)
adamc@476 1293
adamc@311 1294 | EFfiApp (m, x, es) => box [string "uw_",
adamc@316 1295 p_ident m,
adamc@53 1296 string "_",
adamc@316 1297 p_ident x,
adamc@117 1298 string "(ctx, ",
adamc@53 1299 p_list (p_exp env) es,
adamc@53 1300 string ")"]
adamc@316 1301 | EApp (f, args) =>
adamc@316 1302 parenIf par (box [p_exp' true env f,
adamc@316 1303 string "(ctx,",
adamc@316 1304 space,
adamc@316 1305 p_list_sep (box [string ",", space]) (p_exp env) args,
adamc@316 1306 string ")"])
adamc@29 1307
adamc@387 1308 | EUnop (s, e1) =>
adamc@387 1309 parenIf par (box [string s,
adamc@387 1310 space,
adamc@387 1311 p_exp' true env e1])
adamc@387 1312
adamc@387 1313 | EBinop (s, e1, e2) =>
adamc@389 1314 if Char.isAlpha (String.sub (s, size s - 1)) then
adamc@389 1315 box [string s,
adamc@390 1316 string "(",
adamc@389 1317 p_exp env e1,
adamc@389 1318 string ",",
adamc@389 1319 space,
adamc@389 1320 p_exp env e2,
adamc@389 1321 string ")"]
adamc@389 1322 else
adamc@389 1323 parenIf par (box [p_exp' true env e1,
adamc@389 1324 space,
adamc@389 1325 string s,
adamc@389 1326 space,
adamc@389 1327 p_exp' true env e2])
adamc@387 1328
adamc@29 1329 | ERecord (i, xes) => box [string "({",
adamc@29 1330 space,
adamc@29 1331 string "struct",
adamc@29 1332 space,
adamc@311 1333 string ("__uws_" ^ Int.toString i),
adamc@29 1334 space,
adamc@181 1335 string "tmp",
adamc@29 1336 space,
adamc@29 1337 string "=",
adamc@29 1338 space,
adamc@29 1339 string "{",
adamc@29 1340 p_list (fn (_, e) =>
adamc@29 1341 p_exp env e) xes,
adamc@29 1342 string "};",
adamc@29 1343 space,
adamc@181 1344 string "tmp;",
adamc@29 1345 space,
adamc@29 1346 string "})" ]
adamc@29 1347 | EField (e, x) =>
adamc@29 1348 box [p_exp' true env e,
adamc@311 1349 string ".__uwf_",
adamc@316 1350 p_ident x]
adamc@29 1351
adamc@182 1352 | ECase (e, pes, {disc, result}) =>
adamc@182 1353 let
adamc@182 1354 val final = newGoto ()
adamc@182 1355
adamc@182 1356 val body = foldl (fn ((p, e), body) =>
adamc@182 1357 let
adamc@182 1358 val exit = newGoto ()
adamc@182 1359 val (pr, _) = p_pat_preamble env p
adamc@182 1360 val (p, env) = p_pat (env,
adamc@182 1361 box [string "goto",
adamc@182 1362 space,
adamc@182 1363 exit,
adamc@182 1364 string ";"],
adamc@182 1365 0) p
adamc@182 1366 in
adamc@182 1367 box [body,
adamc@182 1368 box [string "{",
adamc@182 1369 newline,
adamc@182 1370 pr,
adamc@182 1371 newline,
adamc@182 1372 p,
adamc@182 1373 newline,
adamc@182 1374 string "result",
adamc@182 1375 space,
adamc@182 1376 string "=",
adamc@182 1377 space,
adamc@182 1378 p_exp env e,
adamc@182 1379 string ";",
adamc@182 1380 newline,
adamc@182 1381 string "goto",
adamc@182 1382 space,
adamc@182 1383 final,
adamc@182 1384 string ";",
adamc@182 1385 newline,
adamc@182 1386 string "}"],
adamc@182 1387 newline,
adamc@182 1388 exit,
adamc@182 1389 string ":",
adamc@182 1390 newline]
adamc@182 1391 end) (box []) pes
adamc@182 1392 in
adamc@182 1393 box [string "({",
adamc@182 1394 newline,
adamc@182 1395 p_typ env disc,
adamc@182 1396 space,
adamc@182 1397 string "disc0",
adamc@182 1398 space,
adamc@182 1399 string "=",
adamc@182 1400 space,
adamc@182 1401 p_exp env e,
adamc@182 1402 string ";",
adamc@182 1403 newline,
adamc@182 1404 p_typ env result,
adamc@182 1405 space,
adamc@182 1406 string "result;",
adamc@182 1407 newline,
adamc@182 1408 body,
adamc@311 1409 string "uw_error(ctx, FATAL, \"",
adamc@182 1410 string (ErrorMsg.spanToString loc),
adamc@182 1411 string ": pattern match failure\");",
adamc@182 1412 newline,
adamc@182 1413 final,
adamc@182 1414 string ":",
adamc@182 1415 space,
adamc@182 1416 string "result;",
adamc@182 1417 newline,
adamc@182 1418 string "})"]
adamc@182 1419 end
adamc@181 1420
adamc@311 1421 | EWrite e => box [string "(uw_write(ctx, ",
adamc@102 1422 p_exp env e,
adamc@311 1423 string "), uw_unit_v)"]
adamc@102 1424
adamc@106 1425 | ESeq (e1, e2) => box [string "(",
adamc@106 1426 p_exp env e1,
adamc@106 1427 string ",",
adamc@106 1428 space,
adamc@106 1429 p_exp env e2,
adamc@106 1430 string ")"]
adamc@269 1431 | ELet (x, t, e1, e2) => box [string "({",
adamc@269 1432 newline,
adamc@269 1433 p_typ env t,
adamc@269 1434 space,
adamc@311 1435 string "__uwr_",
adamc@316 1436 p_ident x,
adamc@272 1437 string "_",
adamc@272 1438 string (Int.toString (E.countERels env)),
adamc@269 1439 space,
adamc@269 1440 string "=",
adamc@269 1441 space,
adamc@269 1442 p_exp env e1,
adamc@269 1443 string ";",
adamc@269 1444 newline,
adamc@269 1445 p_exp (E.pushERel env x t) e2,
adamc@269 1446 string ";",
adamc@269 1447 newline,
adamc@269 1448 string "})"]
adamc@269 1449
adamc@282 1450 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
adamc@278 1451 let
adamc@316 1452 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
adamc@278 1453 val tables = ListUtil.mapConcat (fn (x, xts) =>
adamc@316 1454 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
adamc@278 1455 tables
adamc@278 1456
adamc@278 1457 val outputs = exps @ tables
adamc@325 1458 val outputs = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER) outputs
adamc@324 1459
adamc@324 1460 val wontLeakStrings = notLeaky env true state
adamc@324 1461 val wontLeakAnything = notLeaky env false state
adamc@278 1462 in
adamc@324 1463 box [string "(uw_begin_region(ctx), ",
adamc@324 1464 if wontLeakAnything then
adamc@331 1465 string "uw_begin_region(ctx), "
adamc@324 1466 else
adamc@324 1467 box [],
adamc@324 1468 string "({",
adamc@278 1469 newline,
adamc@311 1470 string "PGconn *conn = uw_get_db(ctx);",
adamc@278 1471 newline,
adamc@282 1472 case prepared of
adamc@282 1473 NONE => box [string "char *query = ",
adamc@282 1474 p_exp env query,
adamc@282 1475 string ";",
adamc@282 1476 newline]
adamc@282 1477 | SOME _ =>
adamc@282 1478 let
adamc@282 1479 val ets = getPargs query
adamc@282 1480 in
adamc@282 1481 box [p_list_sepi newline
adamc@282 1482 (fn i => fn (e, t) =>
adamc@282 1483 box [p_sql_type t,
adamc@282 1484 space,
adamc@282 1485 string "arg",
adamc@282 1486 string (Int.toString (i + 1)),
adamc@282 1487 space,
adamc@282 1488 string "=",
adamc@282 1489 space,
adamc@282 1490 p_exp env e,
adamc@282 1491 string ";"])
adamc@282 1492 ets,
adamc@282 1493 newline,
adamc@282 1494 newline,
adamc@282 1495
adamc@282 1496 string "const char *paramValues[] = { ",
adamc@282 1497 p_list_sepi (box [string ",", space])
adamc@282 1498 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@282 1499 string (Int.toString (i + 1))]))
adamc@282 1500 ets,
adamc@282 1501 string " };",
adamc@282 1502 newline,
adamc@282 1503 newline]
adamc@282 1504 end,
adamc@278 1505 string "int n, i;",
adamc@278 1506 newline,
adamc@278 1507 p_typ env state,
adamc@278 1508 space,
adamc@278 1509 string "acc",
adamc@278 1510 space,
adamc@278 1511 string "=",
adamc@278 1512 space,
adamc@278 1513 p_exp env initial,
adamc@278 1514 string ";",
adamc@278 1515 newline,
adamc@282 1516 string "PGresult *res = ",
adamc@282 1517 case prepared of
adamc@295 1518 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1519 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@282 1520 string (Int.toString n),
adamc@282 1521 string "\", ",
adamc@282 1522 string (Int.toString (length (getPargs query))),
adamc@295 1523 string ", paramValues, NULL, NULL, 0);"],
adamc@278 1524 newline,
adamc@278 1525 newline,
adamc@277 1526
adamc@311 1527 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@278 1528 newline,
adamc@278 1529 newline,
adamc@277 1530
adamc@278 1531 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@278 1532 newline,
adamc@278 1533 box [string "PQclear(res);",
adamc@278 1534 newline,
adamc@311 1535 string "uw_error(ctx, FATAL, \"",
adamc@278 1536 string (ErrorMsg.spanToString loc),
adamc@282 1537 string ": Query failed:\\n%s\\n%s\", ",
adamc@282 1538 case prepared of
adamc@282 1539 NONE => string "query"
adamc@282 1540 | SOME _ => p_exp env query,
adamc@282 1541 string ", PQerrorMessage(conn));",
adamc@278 1542 newline],
adamc@278 1543 string "}",
adamc@278 1544 newline,
adamc@278 1545 newline,
adamc@277 1546
adamc@323 1547 string "uw_end_region(ctx);",
adamc@323 1548 newline,
adamc@425 1549 string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",
adamc@425 1550 newline,
adamc@278 1551 string "n = PQntuples(res);",
adamc@278 1552 newline,
adamc@278 1553 string "for (i = 0; i < n; ++i) {",
adamc@278 1554 newline,
adamc@278 1555 box [string "struct",
adamc@278 1556 space,
adamc@311 1557 string "__uws_",
adamc@278 1558 string (Int.toString rnum),
adamc@278 1559 space,
adamc@311 1560 string "__uwr_r_",
adamc@278 1561 string (Int.toString (E.countERels env)),
adamc@278 1562 string ";",
adamc@278 1563 newline,
adamc@278 1564 p_typ env state,
adamc@278 1565 space,
adamc@311 1566 string "__uwr_acc_",
adamc@278 1567 string (Int.toString (E.countERels env + 1)),
adamc@278 1568 space,
adamc@278 1569 string "=",
adamc@278 1570 space,
adamc@278 1571 string "acc;",
adamc@278 1572 newline,
adamc@278 1573 newline,
adamc@278 1574
adamc@278 1575 p_list_sepi (box []) (fn i =>
adamc@278 1576 fn (proj, t) =>
adamc@311 1577 box [string "__uwr_r_",
adamc@278 1578 string (Int.toString (E.countERels env)),
adamc@278 1579 string ".",
adamc@278 1580 string proj,
adamc@278 1581 space,
adamc@278 1582 string "=",
adamc@278 1583 space,
adamc@467 1584 p_getcol wontLeakStrings env t i,
adamc@278 1585 string ";",
adamc@278 1586 newline]) outputs,
adamc@278 1587
adamc@278 1588 newline,
adamc@278 1589 newline,
adamc@278 1590
adamc@278 1591 string "acc",
adamc@278 1592 space,
adamc@278 1593 string "=",
adamc@278 1594 space,
adamc@278 1595 p_exp (E.pushERel
adamc@278 1596 (E.pushERel env "r" (TRecord rnum, loc))
adamc@278 1597 "acc" state)
adamc@278 1598 body,
adamc@278 1599 string ";",
adamc@278 1600 newline],
adamc@278 1601 string "}",
adamc@278 1602 newline,
adamc@278 1603 newline,
adamc@425 1604 string "uw_pop_cleanup(ctx);",
adamc@278 1605 newline,
adamc@324 1606 if wontLeakAnything then
adamc@324 1607 box [string "uw_end_region(ctx);",
adamc@324 1608 newline]
adamc@324 1609 else
adamc@324 1610 box [],
adamc@278 1611 string "acc;",
adamc@278 1612 newline,
adamc@323 1613 string "}))"]
adamc@278 1614 end
adamc@106 1615
adamc@307 1616 | EDml {dml, prepared} =>
adamc@337 1617 box [string "(uw_begin_region(ctx), ({",
adamc@307 1618 newline,
adamc@311 1619 string "PGconn *conn = uw_get_db(ctx);",
adamc@307 1620 newline,
adamc@307 1621 case prepared of
adamc@307 1622 NONE => box [string "char *dml = ",
adamc@307 1623 p_exp env dml,
adamc@307 1624 string ";",
adamc@307 1625 newline]
adamc@307 1626 | SOME _ =>
adamc@307 1627 let
adamc@307 1628 val ets = getPargs dml
adamc@307 1629 in
adamc@307 1630 box [p_list_sepi newline
adamc@307 1631 (fn i => fn (e, t) =>
adamc@307 1632 box [p_sql_type t,
adamc@307 1633 space,
adamc@307 1634 string "arg",
adamc@307 1635 string (Int.toString (i + 1)),
adamc@307 1636 space,
adamc@307 1637 string "=",
adamc@307 1638 space,
adamc@307 1639 p_exp env e,
adamc@307 1640 string ";"])
adamc@307 1641 ets,
adamc@307 1642 newline,
adamc@307 1643 newline,
adamc@307 1644
adamc@307 1645 string "const char *paramValues[] = { ",
adamc@307 1646 p_list_sepi (box [string ",", space])
adamc@307 1647 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@307 1648 string (Int.toString (i + 1))]))
adamc@307 1649 ets,
adamc@307 1650 string " };",
adamc@307 1651 newline,
adamc@307 1652 newline]
adamc@307 1653 end,
adamc@307 1654 newline,
adamc@307 1655 newline,
adamc@307 1656 string "PGresult *res = ",
adamc@307 1657 case prepared of
adamc@307 1658 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1659 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@307 1660 string (Int.toString n),
adamc@307 1661 string "\", ",
adamc@307 1662 string (Int.toString (length (getPargs dml))),
adamc@307 1663 string ", paramValues, NULL, NULL, 0);"],
adamc@307 1664 newline,
adamc@307 1665 newline,
adamc@307 1666
adamc@311 1667 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
adamc@307 1668 newline,
adamc@307 1669 newline,
adamc@307 1670
adamc@307 1671 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@307 1672 newline,
adamc@307 1673 box [string "PQclear(res);",
adamc@307 1674 newline,
adamc@311 1675 string "uw_error(ctx, FATAL, \"",
adamc@307 1676 string (ErrorMsg.spanToString loc),
adamc@307 1677 string ": DML failed:\\n%s\\n%s\", ",
adamc@307 1678 case prepared of
adamc@307 1679 NONE => string "dml"
adamc@307 1680 | SOME _ => p_exp env dml,
adamc@307 1681 string ", PQerrorMessage(conn));",
adamc@307 1682 newline],
adamc@307 1683 string "}",
adamc@307 1684 newline,
adamc@307 1685 newline,
adamc@307 1686
adamc@307 1687 string "PQclear(res);",
adamc@307 1688 newline,
adamc@337 1689 string "uw_end_region(ctx);",
adamc@337 1690 newline,
adamc@311 1691 string "uw_unit_v;",
adamc@307 1692 newline,
adamc@337 1693 string "}))"]
adamc@307 1694
adamc@338 1695 | ENextval {seq, prepared} =>
adamc@338 1696 let
adamc@486 1697 val query = case seq of
adamc@486 1698 (EPrim (Prim.String s), loc) =>
adamc@486 1699 (EPrim (Prim.String ("SELECT NEXTVAL('" ^ s ^ "')")), loc)
adamc@486 1700 | _ =>
adamc@486 1701 let
adamc@486 1702 val query = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@486 1703 in
adamc@486 1704 (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), query]), loc)
adamc@486 1705 end
adamc@338 1706 in
adamc@338 1707 box [string "(uw_begin_region(ctx), ",
adamc@338 1708 string "({",
adamc@338 1709 newline,
adamc@338 1710 string "PGconn *conn = uw_get_db(ctx);",
adamc@338 1711 newline,
adamc@338 1712 case prepared of
adamc@338 1713 NONE => box [string "char *query = ",
adamc@338 1714 p_exp env query,
adamc@338 1715 string ";",
adamc@338 1716 newline]
adamc@338 1717 | SOME _ =>
adamc@338 1718 box [],
adamc@338 1719 newline,
adamc@338 1720 string "PGresult *res = ",
adamc@338 1721 case prepared of
adamc@338 1722 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@338 1723 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@338 1724 string (Int.toString n),
adamc@338 1725 string "\", 0, NULL, NULL, NULL, 0);"],
adamc@338 1726 newline,
adamc@338 1727 string "uw_Basis_int n;",
adamc@338 1728 newline,
adamc@338 1729 newline,
adamc@338 1730
adamc@338 1731 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
adamc@338 1732 newline,
adamc@338 1733 newline,
adamc@338 1734
adamc@338 1735 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@338 1736 newline,
adamc@338 1737 box [string "PQclear(res);",
adamc@338 1738 newline,
adamc@338 1739 string "uw_error(ctx, FATAL, \"",
adamc@338 1740 string (ErrorMsg.spanToString loc),
adamc@338 1741 string ": Query failed:\\n%s\\n%s\", ",
adamc@338 1742 case prepared of
adamc@338 1743 NONE => string "query"
adamc@338 1744 | SOME _ => p_exp env query,
adamc@338 1745 string ", PQerrorMessage(conn));",
adamc@338 1746 newline],
adamc@338 1747 string "}",
adamc@338 1748 newline,
adamc@338 1749 newline,
adamc@338 1750
adamc@338 1751 string "uw_end_region(ctx);",
adamc@338 1752 newline,
adamc@338 1753 string "n = PQntuples(res);",
adamc@338 1754 newline,
adamc@338 1755 string "if (n != 1) {",
adamc@338 1756 newline,
adamc@338 1757 box [string "PQclear(res);",
adamc@338 1758 newline,
adamc@338 1759 string "uw_error(ctx, FATAL, \"",
adamc@338 1760 string (ErrorMsg.spanToString loc),
adamc@338 1761 string ": Wrong number of result rows:\\n%s\\n%s\", ",
adamc@338 1762 case prepared of
adamc@338 1763 NONE => string "query"
adamc@338 1764 | SOME _ => p_exp env query,
adamc@338 1765 string ", PQerrorMessage(conn));",
adamc@338 1766 newline],
adamc@338 1767 string "}",
adamc@338 1768 newline,
adamc@338 1769 newline,
adamc@338 1770
adamc@338 1771 string "n = ",
adamc@338 1772 p_unsql true env (TFfi ("Basis", "int"), loc)
adamc@338 1773 (string "PQgetvalue(res, 0, 0)"),
adamc@338 1774 string ";",
adamc@338 1775 newline,
adamc@338 1776 string "PQclear(res);",
adamc@338 1777 newline,
adamc@338 1778 string "n;",
adamc@338 1779 newline,
adamc@338 1780 string "}))"]
adamc@338 1781 end
adamc@338 1782
adamc@463 1783 | EUnurlify (e, t) =>
adamc@463 1784 let
adamc@463 1785 fun getIt () =
adamc@463 1786 if isUnboxable t then
adamc@463 1787 unurlify env t
adamc@463 1788 else
adamc@463 1789 box [string "({",
adamc@463 1790 newline,
adamc@463 1791 p_typ env t,
adamc@463 1792 string " *tmp = uw_malloc(ctx, sizeof(",
adamc@463 1793 p_typ env t,
adamc@463 1794 string "));",
adamc@463 1795 newline,
adamc@463 1796 string "*tmp = ",
adamc@463 1797 unurlify env t,
adamc@463 1798 string ";",
adamc@463 1799 newline,
adamc@463 1800 string "tmp;",
adamc@463 1801 newline,
adamc@463 1802 string "})"]
adamc@463 1803 in
adamc@463 1804 box [string "({",
adamc@463 1805 newline,
adamc@493 1806 string "uw_Basis_string request = uw_Basis_maybe_strdup(ctx, ",
adamc@463 1807 p_exp env e,
adamc@492 1808 string ");",
adamc@463 1809 newline,
adamc@463 1810 newline,
adamc@463 1811 string "(request ? ",
adamc@463 1812 getIt (),
adamc@463 1813 string " : NULL);",
adamc@463 1814 newline,
adamc@463 1815 string "})"]
adamc@463 1816 end
adamc@463 1817
adamc@29 1818 and p_exp env = p_exp' false env
adamc@29 1819
adamc@129 1820 fun p_fun env (fx, n, args, ran, e) =
adamc@129 1821 let
adamc@129 1822 val nargs = length args
adamc@129 1823 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
adamc@129 1824 in
adamc@129 1825 box [string "static",
adamc@129 1826 space,
adamc@129 1827 p_typ env ran,
adamc@129 1828 space,
adamc@316 1829 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@129 1830 string "(",
adamc@129 1831 p_list_sep (box [string ",", space]) (fn x => x)
adamc@311 1832 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
adamc@129 1833 box [p_typ env dom,
adamc@129 1834 space,
adamc@129 1835 p_rel env' (nargs - i - 1)]) args),
adamc@129 1836 string ")",
adamc@129 1837 space,
adamc@129 1838 string "{",
adamc@129 1839 newline,
adamc@129 1840 box[string "return(",
adamc@129 1841 p_exp env' e,
adamc@129 1842 string ");"],
adamc@129 1843 newline,
adamc@129 1844 string "}"]
adamc@129 1845 end
adamc@129 1846
adamc@129 1847 fun p_decl env (dAll as (d, _) : decl) =
adamc@29 1848 case d of
adamc@29 1849 DStruct (n, xts) =>
adamc@196 1850 let
adamc@196 1851 val env = E.declBinds env dAll
adamc@196 1852 in
adamc@196 1853 box [string "struct",
adamc@196 1854 space,
adamc@311 1855 string ("__uws_" ^ Int.toString n),
adamc@196 1856 space,
adamc@196 1857 string "{",
adamc@196 1858 newline,
adamc@196 1859 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@196 1860 space,
adamc@311 1861 string "__uwf_",
adamc@316 1862 p_ident x,
adamc@196 1863 string ";",
adamc@196 1864 newline]) xts,
adamc@196 1865 string "};"]
adamc@196 1866 end
adamc@188 1867 | DDatatype (Enum, x, n, xncs) =>
adamc@188 1868 box [string "enum",
adamc@188 1869 space,
adamc@316 1870 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@188 1871 space,
adamc@188 1872 string "{",
adamc@188 1873 space,
adamc@316 1874 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1875 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@188 1876 space,
adamc@188 1877 string "};"]
adamc@198 1878 | DDatatype (Option, _, _, _) => box []
adamc@188 1879 | DDatatype (Default, x, n, xncs) =>
adamc@165 1880 let
adamc@165 1881 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
adamc@165 1882 | (x, n, SOME t) => SOME (x, n, t)) xncs
adamc@165 1883 in
adamc@165 1884 box [string "enum",
adamc@165 1885 space,
adamc@316 1886 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1887 space,
adamc@165 1888 string "{",
adamc@165 1889 space,
adamc@316 1890 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1891 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@165 1892 space,
adamc@165 1893 string "};",
adamc@165 1894 newline,
adamc@165 1895 newline,
adamc@165 1896 string "struct",
adamc@165 1897 space,
adamc@316 1898 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1899 space,
adamc@165 1900 string "{",
adamc@165 1901 newline,
adamc@165 1902 string "enum",
adamc@165 1903 space,
adamc@316 1904 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1905 space,
adamc@165 1906 string "tag;",
adamc@165 1907 newline,
adamc@165 1908 box (case xncsArgs of
adamc@165 1909 [] => []
adamc@165 1910 | _ => [string "union",
adamc@165 1911 space,
adamc@165 1912 string "{",
adamc@165 1913 newline,
adamc@165 1914 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
adamc@165 1915 space,
adamc@316 1916 string ("uw_" ^ ident x),
adamc@165 1917 string ";"]) xncsArgs,
adamc@165 1918 newline,
adamc@165 1919 string "}",
adamc@165 1920 space,
adamc@165 1921 string "data;",
adamc@165 1922 newline]),
adamc@165 1923 string "};"]
adamc@188 1924 end
adamc@29 1925
adamc@196 1926 | DDatatypeForward _ => box []
adamc@196 1927
adamc@29 1928 | DVal (x, n, t, e) =>
adamc@29 1929 box [p_typ env t,
adamc@29 1930 space,
adamc@316 1931 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
adamc@29 1932 space,
adamc@29 1933 string "=",
adamc@29 1934 space,
adamc@29 1935 p_exp env e,
adamc@29 1936 string ";"]
adamc@129 1937 | DFun vi => p_fun env vi
adamc@129 1938 | DFunRec vis =>
adamc@29 1939 let
adamc@129 1940 val env = E.declBinds env dAll
adamc@29 1941 in
adamc@129 1942 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
adamc@129 1943 box [string "static",
adamc@129 1944 space,
adamc@129 1945 p_typ env ran,
adamc@129 1946 space,
adamc@316 1947 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@311 1948 string "(uw_context,",
adamc@129 1949 space,
adamc@129 1950 p_list_sep (box [string ",", space])
adamc@129 1951 (fn (_, dom) => p_typ env dom) args,
adamc@129 1952 string ");"]) vis,
adamc@29 1953 newline,
adamc@129 1954 p_list_sep newline (p_fun env) vis,
adamc@129 1955 newline]
adamc@29 1956 end
adamc@273 1957 | DTable (x, _) => box [string "/* SQL table ",
adamc@273 1958 string x,
adamc@273 1959 string " */",
adamc@273 1960 newline]
adamc@338 1961 | DSequence x => box [string "/* SQL sequence ",
adamc@338 1962 string x,
adamc@338 1963 string " */",
adamc@338 1964 newline]
adamc@311 1965 | DDatabase s => box [string "static void uw_db_validate(uw_context);",
adamc@272 1966 newline,
adamc@311 1967 string "static void uw_db_prepare(uw_context);",
adamc@282 1968 newline,
adamc@275 1969 newline,
adamc@311 1970 string "void uw_db_init(uw_context ctx) {",
adamc@273 1971 newline,
adamc@272 1972 string "PGconn *conn = PQconnectdb(\"",
adamc@272 1973 string (String.toString s),
adamc@272 1974 string "\");",
adamc@272 1975 newline,
adamc@311 1976 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
adamc@272 1977 string "\"libpq can't allocate a connection.\");",
adamc@272 1978 newline,
adamc@272 1979 string "if (PQstatus(conn) != CONNECTION_OK) {",
adamc@272 1980 newline,
adamc@272 1981 box [string "char msg[1024];",
adamc@272 1982 newline,
adamc@272 1983 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@272 1984 newline,
adamc@272 1985 string "msg[1023] = 0;",
adamc@272 1986 newline,
adamc@272 1987 string "PQfinish(conn);",
adamc@272 1988 newline,
adamc@311 1989 string "uw_error(ctx, BOUNDED_RETRY, ",
adamc@272 1990 string "\"Connection to Postgres server failed: %s\", msg);"],
adamc@272 1991 newline,
adamc@272 1992 string "}",
adamc@272 1993 newline,
adamc@311 1994 string "uw_set_db(ctx, conn);",
adamc@272 1995 newline,
adamc@311 1996 string "uw_db_validate(ctx);",
adamc@275 1997 newline,
adamc@311 1998 string "uw_db_prepare(ctx);",
adamc@282 1999 newline,
adamc@272 2000 string "}",
adamc@272 2001 newline,
adamc@272 2002 newline,
adamc@311 2003 string "void uw_db_close(uw_context ctx) {",
adamc@272 2004 newline,
adamc@311 2005 string "PQfinish(uw_get_db(ctx));",
adamc@272 2006 newline,
adamc@272 2007 string "}",
adamc@424 2008 newline,
adamc@424 2009 newline,
adamc@424 2010
adamc@424 2011 string "int uw_db_begin(uw_context ctx) {",
adamc@424 2012 newline,
adamc@424 2013 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 2014 newline,
adamc@424 2015 string "PGresult *res = PQexec(conn, \"BEGIN\");",
adamc@424 2016 newline,
adamc@424 2017 newline,
adamc@424 2018 string "if (res == NULL) return 1;",
adamc@424 2019 newline,
adamc@424 2020 newline,
adamc@424 2021 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 2022 box [string "PQclear(res);",
adamc@424 2023 newline,
adamc@424 2024 string "return 1;",
adamc@424 2025 newline],
adamc@424 2026 string "}",
adamc@424 2027 newline,
adamc@424 2028 string "return 0;",
adamc@424 2029 newline,
adamc@424 2030 string "}",
adamc@424 2031 newline,
adamc@424 2032 newline,
adamc@424 2033
adamc@424 2034 string "int uw_db_commit(uw_context ctx) {",
adamc@424 2035 newline,
adamc@424 2036 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 2037 newline,
adamc@424 2038 string "PGresult *res = PQexec(conn, \"COMMIT\");",
adamc@424 2039 newline,
adamc@424 2040 newline,
adamc@424 2041 string "if (res == NULL) return 1;",
adamc@424 2042 newline,
adamc@424 2043 newline,
adamc@424 2044 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 2045 box [string "PQclear(res);",
adamc@424 2046 newline,
adamc@424 2047 string "return 1;",
adamc@424 2048 newline],
adamc@424 2049 string "}",
adamc@424 2050 newline,
adamc@424 2051 string "return 0;",
adamc@424 2052 newline,
adamc@424 2053 string "}",
adamc@424 2054 newline,
adamc@424 2055 newline,
adamc@424 2056
adamc@424 2057 string "int uw_db_rollback(uw_context ctx) {",
adamc@424 2058 newline,
adamc@424 2059 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 2060 newline,
adamc@424 2061 string "PGresult *res = PQexec(conn, \"ROLLBACK\");",
adamc@424 2062 newline,
adamc@424 2063 newline,
adamc@424 2064 string "if (res == NULL) return 1;",
adamc@424 2065 newline,
adamc@424 2066 newline,
adamc@424 2067 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 2068 box [string "PQclear(res);",
adamc@424 2069 newline,
adamc@424 2070 string "return 1;",
adamc@424 2071 newline],
adamc@424 2072 string "}",
adamc@424 2073 newline,
adamc@424 2074 string "return 0;",
adamc@424 2075 newline,
adamc@424 2076 string "}",
adamc@272 2077 newline]
adamc@29 2078
adamc@467 2079 | DPreparedStatements [] =>
adamc@467 2080 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@467 2081 newline,
adamc@467 2082 string "}"]
adamc@282 2083 | DPreparedStatements ss =>
adamc@311 2084 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@282 2085 newline,
adamc@311 2086 string "PGconn *conn = uw_get_db(ctx);",
adamc@282 2087 newline,
adamc@282 2088 string "PGresult *res;",
adamc@282 2089 newline,
adamc@282 2090 newline,
adamc@282 2091
adamc@282 2092 p_list_sepi newline (fn i => fn (s, n) =>
adamc@311 2093 box [string "res = PQprepare(conn, \"uw",
adamc@282 2094 string (Int.toString i),
adamc@282 2095 string "\", \"",
adamc@282 2096 string (String.toString s),
adamc@282 2097 string "\", ",
adamc@282 2098 string (Int.toString n),
adamc@282 2099 string ", NULL);",
adamc@282 2100 newline,
adamc@282 2101 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@282 2102 newline,
adamc@282 2103 box [string "char msg[1024];",
adamc@282 2104 newline,
adamc@282 2105 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@282 2106 newline,
adamc@282 2107 string "msg[1023] = 0;",
adamc@282 2108 newline,
adamc@282 2109 string "PQclear(res);",
adamc@282 2110 newline,
adamc@282 2111 string "PQfinish(conn);",
adamc@282 2112 newline,
adamc@311 2113 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
adamc@282 2114 string (String.toString s),
adamc@282 2115 string "\\n%s\", msg);",
adamc@282 2116 newline],
adamc@282 2117 string "}",
adamc@282 2118 newline,
adamc@282 2119 string "PQclear(res);",
adamc@282 2120 newline])
adamc@282 2121 ss,
adamc@282 2122
adamc@282 2123 string "}"]
adamc@282 2124
adamc@569 2125 | DJavaScript s => box [string "static char jslib[] = \"",
adamc@569 2126 string (String.toString s),
adamc@569 2127 string "\";"]
adamc@569 2128
adamc@144 2129 datatype 'a search =
adamc@144 2130 Found of 'a
adamc@144 2131 | NotFound
adamc@144 2132 | Error
adamc@120 2133
adamc@467 2134 fun p_sqltype'' env (tAll as (t, loc)) =
adamc@275 2135 case t of
adamc@275 2136 TFfi ("Basis", "int") => "int8"
adamc@275 2137 | TFfi ("Basis", "float") => "float8"
adamc@275 2138 | TFfi ("Basis", "string") => "text"
adamc@275 2139 | TFfi ("Basis", "bool") => "bool"
adamc@438 2140 | TFfi ("Basis", "time") => "timestamp"
adamc@275 2141 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
adamc@275 2142 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@275 2143 "ERROR")
adamc@275 2144
adamc@467 2145 fun p_sqltype' env (tAll as (t, loc)) =
adamc@467 2146 case t of
adamc@467 2147 (TOption t, _) => p_sqltype'' env t
adamc@467 2148 | _ => p_sqltype'' env t ^ " NOT NULL"
adamc@467 2149
adamc@275 2150 fun p_sqltype env t = string (p_sqltype' env t)
adamc@101 2151
adamc@467 2152 fun p_sqltype_base' env t =
adamc@467 2153 case t of
adamc@467 2154 (TOption t, _) => p_sqltype'' env t
adamc@467 2155 | _ => p_sqltype'' env t
adamc@467 2156
adamc@467 2157 fun p_sqltype_base env t = string (p_sqltype_base' env t)
adamc@467 2158
adamc@467 2159 fun is_not_null t =
adamc@467 2160 case t of
adamc@467 2161 (TOption _, _) => false
adamc@467 2162 | _ => true
adamc@467 2163
adamc@101 2164 fun p_file env (ds, ps) =
adamc@29 2165 let
adamc@101 2166 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 2167 (p_decl env d,
adamc@31 2168 E.declBinds env d))
adamc@101 2169 env ds
adamc@144 2170
adamc@609 2171 val fields = foldl (fn ((ek, _, _, ts, _), fields) =>
adamc@144 2172 case ek of
adamc@144 2173 Core.Link => fields
adamc@608 2174 | Core.Rpc => fields
adamc@144 2175 | Core.Action =>
adamc@280 2176 case List.nth (ts, length ts - 2) of
adamc@144 2177 (TRecord i, _) =>
adamc@144 2178 let
adamc@144 2179 val xts = E.lookupStruct env i
adamc@144 2180 val xtsSet = SS.addList (SS.empty, map #1 xts)
adamc@144 2181 in
adamc@144 2182 foldl (fn ((x, _), fields) =>
adamc@144 2183 let
adamc@144 2184 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
adamc@144 2185 in
adamc@144 2186 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
adamc@144 2187 xtsSet'))
adamc@144 2188 end) fields xts
adamc@144 2189 end
adamc@144 2190 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
adamc@144 2191 SM.empty ps
adamc@144 2192
adamc@144 2193 val fnums = SM.foldli (fn (x, xs, fnums) =>
adamc@144 2194 let
adamc@144 2195 val unusable = SS.foldl (fn (x', unusable) =>
adamc@144 2196 case SM.find (fnums, x') of
adamc@144 2197 NONE => unusable
adamc@144 2198 | SOME n => IS.add (unusable, n))
adamc@144 2199 IS.empty xs
adamc@144 2200
adamc@144 2201 fun findAvailable n =
adamc@144 2202 if IS.member (unusable, n) then
adamc@144 2203 findAvailable (n + 1)
adamc@144 2204 else
adamc@144 2205 n
adamc@144 2206 in
adamc@144 2207 SM.insert (fnums, x, findAvailable 0)
adamc@144 2208 end)
adamc@144 2209 SM.empty fields
adamc@144 2210
adamc@144 2211 fun makeSwitch (fnums, i) =
adamc@144 2212 case SM.foldl (fn (n, NotFound) => Found n
adamc@144 2213 | (n, Error) => Error
adamc@144 2214 | (n, Found n') => if n = n' then
adamc@144 2215 Found n'
adamc@144 2216 else
adamc@144 2217 Error) NotFound fnums of
adamc@144 2218 NotFound => box [string "return",
adamc@144 2219 space,
adamc@144 2220 string "-1;"]
adamc@144 2221 | Found n => box [string "return",
adamc@144 2222 space,
adamc@144 2223 string (Int.toString n),
adamc@144 2224 string ";"]
adamc@144 2225 | Error =>
adamc@144 2226 let
adamc@144 2227 val cmap = SM.foldli (fn (x, n, cmap) =>
adamc@144 2228 let
adamc@144 2229 val ch = if i < size x then
adamc@144 2230 String.sub (x, i)
adamc@144 2231 else
adamc@144 2232 chr 0
adamc@144 2233
adamc@144 2234 val fnums = case CM.find (cmap, ch) of
adamc@144 2235 NONE => SM.empty
adamc@144 2236 | SOME fnums => fnums
adamc@144 2237 val fnums = SM.insert (fnums, x, n)
adamc@144 2238 in
adamc@144 2239 CM.insert (cmap, ch, fnums)
adamc@144 2240 end)
adamc@144 2241 CM.empty fnums
adamc@144 2242
adamc@144 2243 val cmap = CM.listItemsi cmap
adamc@144 2244 in
adamc@144 2245 case cmap of
adamc@144 2246 [(_, fnums)] =>
adamc@144 2247 box [string "if",
adamc@144 2248 space,
adamc@144 2249 string "(name[",
adamc@144 2250 string (Int.toString i),
adamc@144 2251 string "]",
adamc@144 2252 space,
adamc@144 2253 string "==",
adamc@144 2254 space,
adamc@144 2255 string "0)",
adamc@144 2256 space,
adamc@144 2257 string "return",
adamc@144 2258 space,
adamc@144 2259 string "-1;",
adamc@144 2260 newline,
adamc@144 2261 makeSwitch (fnums, i+1)]
adamc@144 2262 | _ =>
adamc@144 2263 box [string "switch",
adamc@144 2264 space,
adamc@144 2265 string "(name[",
adamc@144 2266 string (Int.toString i),
adamc@144 2267 string "])",
adamc@144 2268 space,
adamc@144 2269 string "{",
adamc@144 2270 newline,
adamc@144 2271 box (map (fn (ch, fnums) =>
adamc@144 2272 box [string "case",
adamc@144 2273 space,
adamc@144 2274 if ch = chr 0 then
adamc@144 2275 string "0:"
adamc@144 2276 else
adamc@144 2277 box [string "'",
adamc@144 2278 string (Char.toString ch),
adamc@144 2279 string "':"],
adamc@144 2280 newline,
adamc@144 2281 makeSwitch (fnums, i+1),
adamc@144 2282 newline]) cmap),
adamc@144 2283 string "default:",
adamc@144 2284 newline,
adamc@144 2285 string "return",
adamc@144 2286 space,
adamc@144 2287 string "-1;",
adamc@144 2288 newline,
adamc@144 2289 string "}"]
adamc@144 2290 end
adamc@144 2291
adamc@609 2292 fun p_page (ek, s, n, ts, ran) =
adamc@144 2293 let
adamc@144 2294 val (ts, defInputs, inputsVar) =
adamc@144 2295 case ek of
adamc@314 2296 Core.Link => (List.take (ts, length ts - 1), string "", string "")
adamc@608 2297 | Core.Rpc => (List.take (ts, length ts - 1), string "", string "")
adamc@144 2298 | Core.Action =>
adamc@280 2299 case List.nth (ts, length ts - 2) of
adamc@144 2300 (TRecord i, _) =>
adamc@144 2301 let
adamc@144 2302 val xts = E.lookupStruct env i
adamc@144 2303 in
adamc@280 2304 (List.take (ts, length ts - 2),
adamc@144 2305 box [box (map (fn (x, t) => box [p_typ env t,
adamc@144 2306 space,
adamc@311 2307 string "uw_input_",
adamc@316 2308 p_ident x,
adamc@144 2309 string ";",
adamc@144 2310 newline]) xts),
adamc@144 2311 newline,
adamc@144 2312 box (map (fn (x, t) =>
adamc@144 2313 let
adamc@144 2314 val n = case SM.find (fnums, x) of
adamc@144 2315 NONE => raise Fail "CjrPrint: Can't find in fnums"
adamc@144 2316 | SOME n => n
adamc@190 2317
adamc@190 2318 val f = case t of
adamc@190 2319 (TFfi ("Basis", "bool"), _) => "optional_"
adamc@190 2320 | _ => ""
adamc@144 2321 in
adamc@311 2322 box [string "request = uw_get_",
adamc@190 2323 string f,
adamc@190 2324 string "input(ctx, ",
adamc@144 2325 string (Int.toString n),
adamc@144 2326 string ");",
adamc@144 2327 newline,
adamc@433 2328 string "if (request == NULL)",
adamc@144 2329 newline,
adamc@433 2330 box [string "uw_error(ctx, FATAL, \"Missing input ",
adamc@144 2331 string x,
adamc@433 2332 string "\");"],
adamc@144 2333 newline,
adamc@311 2334 string "uw_input_",
adamc@316 2335 p_ident x,
adamc@144 2336 space,
adamc@144 2337 string "=",
adamc@144 2338 space,
adamc@463 2339 unurlify env t,
adamc@144 2340 string ";",
adamc@144 2341 newline]
adamc@144 2342 end) xts),
adamc@311 2343 string "struct __uws_",
adamc@144 2344 string (Int.toString i),
adamc@144 2345 space,
adamc@311 2346 string "uw_inputs",
adamc@144 2347 space,
adamc@144 2348 string "= {",
adamc@144 2349 newline,
adamc@311 2350 box (map (fn (x, _) => box [string "uw_input_",
adamc@316 2351 p_ident x,
adamc@144 2352 string ",",
adamc@144 2353 newline]) xts),
adamc@144 2354 string "};",
adamc@144 2355 newline],
adamc@144 2356 box [string ",",
adamc@144 2357 space,
adamc@311 2358 string "uw_inputs"])
adamc@144 2359 end
adamc@144 2360
adamc@144 2361 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
adamc@144 2362 in
adamc@144 2363 box [string "if (!strncmp(request, \"",
adamc@144 2364 string (String.toString s),
adamc@144 2365 string "\", ",
adamc@144 2366 string (Int.toString (size s)),
adamc@198 2367 string ") && (request[",
adamc@198 2368 string (Int.toString (size s)),
adamc@198 2369 string "] == 0 || request[",
adamc@198 2370 string (Int.toString (size s)),
adamc@198 2371 string "] == '/')) {",
adamc@144 2372 newline,
adamc@144 2373 string "request += ",
adamc@144 2374 string (Int.toString (size s)),
adamc@144 2375 string ";",
adamc@144 2376 newline,
adamc@144 2377 string "if (*request == '/') ++request;",
adamc@144 2378 newline,
adamc@609 2379 box (case ek of
adamc@610 2380 Core.Rpc => [string "uw_write_header(ctx, \"Content-type: text/plain\\r\\n\");",
adamc@610 2381 newline]
adamc@609 2382 | _ => [string "uw_write_header(ctx, \"Content-type: text/html\\r\\n\");",
adamc@609 2383 newline,
adamc@609 2384 string "uw_write_header(ctx, \"Content-script-type: text/javascript\\r\\n\");",
adamc@609 2385 newline,
adamc@609 2386 string "uw_write(ctx, \"<html>\");",
adamc@609 2387 newline]),
adamc@144 2388 box [string "{",
adamc@144 2389 newline,
adamc@144 2390 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
adamc@144 2391 space,
adamc@144 2392 string "arg",
adamc@144 2393 string (Int.toString i),
adamc@144 2394 space,
adamc@144 2395 string "=",
adamc@144 2396 space,
adamc@463 2397 unurlify env t,
adamc@144 2398 string ";",
adamc@144 2399 newline]) ts),
adamc@144 2400 defInputs,
adamc@609 2401 box (case ek of
adamc@609 2402 Core.Rpc => [p_typ env ran,
adamc@609 2403 space,
adamc@610 2404 string "it0",
adamc@609 2405 space,
adamc@609 2406 string "=",
adamc@609 2407 space]
adamc@609 2408 | _ => []),
adamc@144 2409 p_enamed env n,
adamc@144 2410 string "(",
adamc@144 2411 p_list_sep (box [string ",", space])
adamc@144 2412 (fn x => x)
adamc@272 2413 (string "ctx"
adamc@280 2414 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
adamc@144 2415 inputsVar,
adamc@311 2416 string ", uw_unit_v);",
adamc@144 2417 newline,
adamc@609 2418 box (case ek of
adamc@610 2419 Core.Rpc => [urlify env ran]
adamc@609 2420 | _ => [string "uw_write(ctx, \"</html>\");",
adamc@609 2421 newline]),
adamc@144 2422 string "return;",
adamc@144 2423 newline,
adamc@144 2424 string "}",
adamc@144 2425 newline,
adamc@144 2426 string "}"]
adamc@144 2427 ]
adamc@144 2428 end
adamc@144 2429
adamc@144 2430 val pds' = map p_page ps
adamc@275 2431
adamc@275 2432 val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts)
adamc@275 2433 | _ => NONE) ds
adamc@377 2434 val sequences = List.mapPartial (fn (DSequence s, _) => SOME s
adamc@377 2435 | _ => NONE) ds
adamc@275 2436
adamc@275 2437 val validate =
adamc@311 2438 box [string "static void uw_db_validate(uw_context ctx) {",
adamc@275 2439 newline,
adamc@311 2440 string "PGconn *conn = uw_get_db(ctx);",
adamc@275 2441 newline,
adamc@275 2442 string "PGresult *res;",
adamc@275 2443 newline,
adamc@275 2444 newline,
adamc@275 2445 p_list_sep newline
adamc@275 2446 (fn (s, xts) =>
adamc@275 2447 let
adamc@377 2448 val sl = CharVector.map Char.toLower s
adamc@377 2449
adamc@275 2450 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2451 ^ sl ^ "'"
adamc@275 2452
adamc@275 2453 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2454 sl,
adamc@275 2455 "') AND (",
adamc@275 2456 String.concatWith " OR "
adamc@275 2457 (map (fn (x, t) =>
adamc@311 2458 String.concat ["(attname = 'uw_",
adamc@275 2459 CharVector.map
adamc@316 2460 Char.toLower (ident x),
adamc@275 2461 "' AND atttypid = (SELECT oid FROM pg_type",
adamc@275 2462 " WHERE typname = '",
adamc@467 2463 p_sqltype_base' env t,
adamc@467 2464 "') AND attnotnull = ",
adamc@467 2465 if is_not_null t then
adamc@467 2466 "TRUE"
adamc@467 2467 else
adamc@467 2468 "FALSE",
adamc@467 2469 ")"]) xts),
adamc@275 2470 ")"]
adamc@275 2471
adamc@275 2472 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2473 sl,
adamc@325 2474 "') AND attname LIKE 'uw_%'"]
adamc@275 2475 in
adamc@275 2476 box [string "res = PQexec(conn, \"",
adamc@275 2477 string q,
adamc@275 2478 string "\");",
adamc@275 2479 newline,
adamc@275 2480 newline,
adamc@275 2481 string "if (res == NULL) {",
adamc@275 2482 newline,
adamc@275 2483 box [string "PQfinish(conn);",
adamc@275 2484 newline,
adamc@311 2485 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2486 newline],
adamc@275 2487 string "}",
adamc@275 2488 newline,
adamc@275 2489 newline,
adamc@275 2490 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2491 newline,
adamc@275 2492 box [string "char msg[1024];",
adamc@275 2493 newline,
adamc@275 2494 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2495 newline,
adamc@275 2496 string "msg[1023] = 0;",
adamc@275 2497 newline,
adamc@275 2498 string "PQclear(res);",
adamc@275 2499 newline,
adamc@275 2500 string "PQfinish(conn);",
adamc@275 2501 newline,
adamc@311 2502 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2503 string q,
adamc@275 2504 string "\\n%s\", msg);",
adamc@275 2505 newline],
adamc@275 2506 string "}",
adamc@275 2507 newline,
adamc@275 2508 newline,
adamc@275 2509 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@275 2510 newline,
adamc@275 2511 box [string "PQclear(res);",
adamc@275 2512 newline,
adamc@275 2513 string "PQfinish(conn);",
adamc@275 2514 newline,
adamc@311 2515 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2516 string s,
adamc@275 2517 string "' does not exist.\");",
adamc@275 2518 newline],
adamc@275 2519 string "}",
adamc@275 2520 newline,
adamc@275 2521 newline,
adamc@275 2522 string "PQclear(res);",
adamc@275 2523 newline,
adamc@275 2524
adamc@275 2525 string "res = PQexec(conn, \"",
adamc@275 2526 string q',
adamc@275 2527 string "\");",
adamc@275 2528 newline,
adamc@275 2529 newline,
adamc@275 2530 string "if (res == NULL) {",
adamc@275 2531 newline,
adamc@275 2532 box [string "PQfinish(conn);",
adamc@275 2533 newline,
adamc@311 2534 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2535 newline],
adamc@275 2536 string "}",
adamc@275 2537 newline,
adamc@275 2538 newline,
adamc@275 2539 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2540 newline,
adamc@275 2541 box [string "char msg[1024];",
adamc@275 2542 newline,
adamc@275 2543 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2544 newline,
adamc@275 2545 string "msg[1023] = 0;",
adamc@275 2546 newline,
adamc@275 2547 string "PQclear(res);",
adamc@275 2548 newline,
adamc@275 2549 string "PQfinish(conn);",
adamc@275 2550 newline,
adamc@311 2551 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2552 string q',
adamc@275 2553 string "\\n%s\", msg);",
adamc@275 2554 newline],
adamc@275 2555 string "}",
adamc@275 2556 newline,
adamc@275 2557 newline,
adamc@275 2558 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2559 string (Int.toString (length xts)),
adamc@275 2560 string "\")) {",
adamc@275 2561 newline,
adamc@275 2562 box [string "PQclear(res);",
adamc@275 2563 newline,
adamc@275 2564 string "PQfinish(conn);",
adamc@275 2565 newline,
adamc@311 2566 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2567 string s,
adamc@275 2568 string "' has the wrong column types.\");",
adamc@275 2569 newline],
adamc@275 2570 string "}",
adamc@275 2571 newline,
adamc@275 2572 newline,
adamc@275 2573 string "PQclear(res);",
adamc@275 2574 newline,
adamc@275 2575 newline,
adamc@275 2576
adamc@275 2577 string "res = PQexec(conn, \"",
adamc@275 2578 string q'',
adamc@275 2579 string "\");",
adamc@275 2580 newline,
adamc@275 2581 newline,
adamc@275 2582 string "if (res == NULL) {",
adamc@275 2583 newline,
adamc@275 2584 box [string "PQfinish(conn);",
adamc@275 2585 newline,
adamc@311 2586 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2587 newline],
adamc@275 2588 string "}",
adamc@275 2589 newline,
adamc@275 2590 newline,
adamc@275 2591 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2592 newline,
adamc@275 2593 box [string "char msg[1024];",
adamc@275 2594 newline,
adamc@275 2595 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2596 newline,
adamc@275 2597 string "msg[1023] = 0;",
adamc@275 2598 newline,
adamc@275 2599 string "PQclear(res);",
adamc@275 2600 newline,
adamc@275 2601 string "PQfinish(conn);",
adamc@275 2602 newline,
adamc@311 2603 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2604 string q'',
adamc@275 2605 string "\\n%s\", msg);",
adamc@275 2606 newline],
adamc@275 2607 string "}",
adamc@275 2608 newline,
adamc@275 2609 newline,
adamc@275 2610 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2611 string (Int.toString (length xts)),
adamc@275 2612 string "\")) {",
adamc@275 2613 newline,
adamc@275 2614 box [string "PQclear(res);",
adamc@275 2615 newline,
adamc@275 2616 string "PQfinish(conn);",
adamc@275 2617 newline,
adamc@311 2618 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2619 string s,
adamc@275 2620 string "' has extra columns.\");",
adamc@275 2621 newline],
adamc@275 2622 string "}",
adamc@275 2623 newline,
adamc@275 2624 newline,
adamc@275 2625 string "PQclear(res);",
adamc@275 2626 newline]
adamc@275 2627 end) tables,
adamc@377 2628
adamc@377 2629 p_list_sep newline
adamc@377 2630 (fn s =>
adamc@377 2631 let
adamc@377 2632 val sl = CharVector.map Char.toLower s
adamc@377 2633
adamc@377 2634 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2635 ^ sl ^ "' AND relkind = 'S'"
adamc@377 2636 in
adamc@377 2637 box [string "res = PQexec(conn, \"",
adamc@377 2638 string q,
adamc@377 2639 string "\");",
adamc@377 2640 newline,
adamc@377 2641 newline,
adamc@377 2642 string "if (res == NULL) {",
adamc@377 2643 newline,
adamc@377 2644 box [string "PQfinish(conn);",
adamc@377 2645 newline,
adamc@377 2646 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@377 2647 newline],
adamc@377 2648 string "}",
adamc@377 2649 newline,
adamc@377 2650 newline,
adamc@377 2651 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@377 2652 newline,
adamc@377 2653 box [string "char msg[1024];",
adamc@377 2654 newline,
adamc@377 2655 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@377 2656 newline,
adamc@377 2657 string "msg[1023] = 0;",
adamc@377 2658 newline,
adamc@377 2659 string "PQclear(res);",
adamc@377 2660 newline,
adamc@377 2661 string "PQfinish(conn);",
adamc@377 2662 newline,
adamc@377 2663 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@377 2664 string q,
adamc@377 2665 string "\\n%s\", msg);",
adamc@377 2666 newline],
adamc@377 2667 string "}",
adamc@377 2668 newline,
adamc@377 2669 newline,
adamc@377 2670 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@377 2671 newline,
adamc@377 2672 box [string "PQclear(res);",
adamc@377 2673 newline,
adamc@377 2674 string "PQfinish(conn);",
adamc@377 2675 newline,
adamc@377 2676 string "uw_error(ctx, FATAL, \"Sequence '",
adamc@377 2677 string s,
adamc@377 2678 string "' does not exist.\");",
adamc@377 2679 newline],
adamc@377 2680 string "}",
adamc@377 2681 newline,
adamc@377 2682 newline,
adamc@377 2683 string "PQclear(res);",
adamc@377 2684 newline]
adamc@377 2685 end) sequences,
adamc@377 2686
adamc@275 2687 string "}"]
adamc@432 2688
adamc@432 2689 val hasDb = List.exists (fn (DDatabase _, _) => true | _ => false) ds
adamc@29 2690 in
adamc@144 2691 box [string "#include <stdio.h>",
adamc@144 2692 newline,
adamc@144 2693 string "#include <stdlib.h>",
adamc@144 2694 newline,
adamc@272 2695 string "#include <string.h>",
adamc@272 2696 newline,
adamc@390 2697 string "#include <math.h>",
adamc@390 2698 newline,
adamc@432 2699 if hasDb then
adamc@432 2700 box [string "#include <postgresql/libpq-fe.h>",
adamc@432 2701 newline]
adamc@432 2702 else
adamc@432 2703 box [],
adamc@144 2704 newline,
adamc@378 2705 string "#include \"",
adamc@378 2706 string (OS.Path.joinDirFile {dir = Config.includ,
adamc@378 2707 file = "urweb.h"}),
adamc@378 2708 string "\"",
adamc@101 2709 newline,
adamc@101 2710 newline,
adamc@101 2711 p_list_sep newline (fn x => x) pds,
adamc@101 2712 newline,
adamc@311 2713 string "int uw_inputs_len = ",
adamc@144 2714 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
adamc@144 2715 string ";",
adamc@144 2716 newline,
adamc@144 2717 newline,
adamc@311 2718 string "int uw_input_num(char *name) {",
adamc@144 2719 newline,
adamc@144 2720 makeSwitch (fnums, 0),
adamc@144 2721 string "}",
adamc@144 2722 newline,
adamc@144 2723 newline,
adamc@311 2724 string "void uw_handle(uw_context ctx, char *request) {",
adamc@101 2725 newline,
adamc@569 2726 string "if (!strcmp(request, \"/app.js\")) {",
adamc@569 2727 newline,
adamc@569 2728 box [string "uw_write_header(ctx, \"Content-type: text/javascript\\r\\n\");",
adamc@569 2729 newline,
adamc@569 2730 string "uw_write(ctx, jslib);",
adamc@569 2731 newline,
adamc@569 2732 string "return;",
adamc@569 2733 newline],
adamc@569 2734 string "}",
adamc@569 2735 newline,
adamc@101 2736 p_list_sep newline (fn x => x) pds',
adamc@101 2737 newline,
adamc@387 2738 string "uw_error(ctx, FATAL, \"Unknown page\");",
adamc@387 2739 newline,
adamc@101 2740 string "}",
adamc@275 2741 newline,
adamc@275 2742 newline,
adamc@432 2743 if hasDb then
adamc@432 2744 validate
adamc@432 2745 else
adamc@432 2746 box [],
adamc@376 2747 newline,
adamc@376 2748 if List.exists (fn (DDatabase _, _) => true | _ => false) ds then
adamc@376 2749 box []
adamc@376 2750 else
adamc@376 2751 box [newline,
adamc@376 2752 string "void uw_db_init(uw_context ctx) { };",
adamc@424 2753 newline,
adamc@424 2754 string "int uw_db_begin(uw_context ctx) { return 0; };",
adamc@424 2755 newline,
adamc@424 2756 string "int uw_db_commit(uw_context ctx) { return 0; };",
adamc@424 2757 newline,
adamc@424 2758 string "int uw_db_rollback(uw_context ctx) { return 0; };",
adamc@376 2759 newline]]
adamc@29 2760 end
adamc@29 2761
adamc@274 2762 fun p_sql env (ds, _) =
adamc@274 2763 let
adamc@274 2764 val (pps, _) = ListUtil.foldlMap
adamc@274 2765 (fn (dAll as (d, _), env) =>
adamc@274 2766 let
adamc@274 2767 val pp = case d of
adamc@274 2768 DTable (s, xts) =>
adamc@274 2769 box [string "CREATE TABLE ",
adamc@274 2770 string s,
adamc@274 2771 string "(",
adamc@274 2772 p_list (fn (x, t) =>
adamc@311 2773 box [string "uw_",
adamc@275 2774 string (CharVector.map Char.toLower x),
adamc@274 2775 space,
adamc@467 2776 p_sqltype env (t, ErrorMsg.dummySpan)]) xts,
adamc@274 2777 string ");",
adamc@274 2778 newline,
adamc@274 2779 newline]
adamc@338 2780 | DSequence s =>
adamc@338 2781 box [string "CREATE SEQUENCE ",
adamc@338 2782 string s,
adamc@338 2783 string ";",
adamc@338 2784 newline,
adamc@338 2785 newline]
adamc@274 2786 | _ => box []
adamc@274 2787 in
adamc@274 2788 (pp, E.declBinds env dAll)
adamc@274 2789 end)
adamc@274 2790 env ds
adamc@274 2791 in
adamc@274 2792 box pps
adamc@274 2793 end
adamc@274 2794
adamc@29 2795 end