annotate src/cjr_print.sml @ 947:e2305dcc3965

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