annotate src/cjr_print.sml @ 652:9db6880184d0

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