annotate src/cjr_print.sml @ 197:b1b9bcfd8c42

Re-consistent-ify naming of union members
author Adam Chlipala <adamc@hcoop.net>
date Sat, 09 Aug 2008 16:54:04 -0400
parents 890a61991263
children ab86aa858e6c
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 (* Pretty-printing C jr. *)
adamc@29 29
adamc@29 30 structure CjrPrint :> CJR_PRINT = struct
adamc@29 31
adamc@29 32 open Print.PD
adamc@29 33 open Print
adamc@29 34
adamc@29 35 open Cjr
adamc@29 36
adamc@29 37 structure E = CjrEnv
adamc@29 38 structure EM = ErrorMsg
adamc@29 39
adamc@144 40 structure SK = struct
adamc@144 41 type ord_key = string
adamc@144 42 val compare = String.compare
adamc@144 43 end
adamc@144 44
adamc@144 45 structure SS = BinarySetFn(SK)
adamc@144 46 structure SM = BinaryMapFn(SK)
adamc@144 47 structure IS = IntBinarySet
adamc@144 48
adamc@144 49 structure CM = BinaryMapFn(struct
adamc@144 50 type ord_key = char
adamc@144 51 val compare = Char.compare
adamc@144 52 end)
adamc@144 53
adamc@29 54 val debug = ref false
adamc@29 55
adamc@196 56 val dummyTyp = (TDatatype (Enum, 0, ref []), ErrorMsg.dummySpan)
adamc@29 57
adamc@29 58 fun p_typ' par env (t, loc) =
adamc@29 59 case t of
adamc@101 60 TTop => string "void*"
adamc@109 61 | TFun (t1, t2) => parenIf par (box [p_typ' true env t2,
adamc@109 62 space,
adamc@109 63 string "(*)",
adamc@109 64 space,
adamc@109 65 string "(",
adamc@109 66 p_typ env t1,
adamc@109 67 string ")"])
adamc@29 68 | TRecord i => box [string "struct",
adamc@29 69 space,
adamc@29 70 string "__lws_",
adamc@29 71 string (Int.toString i)]
adamc@188 72 | TDatatype (Enum, n, _) =>
adamc@188 73 (box [string "enum",
adamc@188 74 space,
adamc@188 75 string ("__lwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
adamc@188 76 handle CjrEnv.UnboundNamed _ => string ("__lwd_UNBOUND__" ^ Int.toString n))
adamc@188 77 | TDatatype (Default, n, _) =>
adamc@165 78 (box [string "struct",
adamc@165 79 space,
adamc@166 80 string ("__lwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
adamc@166 81 handle CjrEnv.UnboundNamed _ => string ("__lwd_UNBOUND__" ^ Int.toString n))
adamc@53 82 | TFfi (m, x) => box [string "lw_", string m, string "_", string x]
adamc@29 83
adamc@29 84 and p_typ env = p_typ' false env
adamc@29 85
adamc@29 86 fun p_rel env n = string ("__lwr_" ^ #1 (E.lookupERel env n) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 87 handle CjrEnv.UnboundRel _ => string ("__lwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 88
adamc@109 89 fun p_enamed env n =
adamc@109 90 string ("__lwn_" ^ #1 (E.lookupENamed env n) ^ "_" ^ Int.toString n)
adamc@109 91 handle CjrEnv.UnboundNamed _ => string ("__lwn_UNBOUND_" ^ Int.toString n)
adamc@109 92
adamc@182 93 fun p_con_named env n =
adamc@182 94 string ("__lwc_" ^ #1 (E.lookupConstructor env n) ^ "_" ^ Int.toString n)
adamc@182 95 handle CjrEnv.UnboundNamed _ => string ("__lwc_UNBOUND_" ^ Int.toString n)
adamc@182 96
adamc@182 97 fun p_pat_preamble env (p, _) =
adamc@182 98 case p of
adamc@182 99 PWild => (box [],
adamc@182 100 env)
adamc@182 101 | PVar (x, t) => (box [p_typ env t,
adamc@182 102 space,
adamc@182 103 string "__lwr_",
adamc@182 104 string x,
adamc@182 105 string "_",
adamc@182 106 string (Int.toString (E.countERels env)),
adamc@182 107 string ";",
adamc@182 108 newline],
adamc@196 109 E.pushERel env x t)
adamc@182 110 | PPrim _ => (box [], env)
adamc@188 111 | PCon (_, _, NONE) => (box [], env)
adamc@188 112 | PCon (_, _, SOME p) => p_pat_preamble env p
adamc@182 113 | PRecord xps =>
adamc@182 114 foldl (fn ((_, p, _), (pp, env)) =>
adamc@182 115 let
adamc@182 116 val (pp', env) = p_pat_preamble env p
adamc@182 117 in
adamc@182 118 (box [pp', pp], env)
adamc@182 119 end) (box [], env) xps
adamc@182 120
adamc@182 121 fun p_patCon env pc =
adamc@182 122 case pc of
adamc@182 123 PConVar n => p_con_named env n
adamc@186 124 | PConFfi {mod = m, con, ...} => string ("lw_" ^ m ^ "_" ^ con)
adamc@182 125
adamc@182 126 fun p_pat (env, exit, depth) (p, _) =
adamc@182 127 case p of
adamc@182 128 PWild =>
adamc@182 129 (box [], env)
adamc@182 130 | PVar (x, t) =>
adamc@182 131 (box [string "__lwr_",
adamc@182 132 string x,
adamc@182 133 string "_",
adamc@182 134 string (Int.toString (E.countERels env)),
adamc@182 135 space,
adamc@182 136 string "=",
adamc@182 137 space,
adamc@182 138 string "disc",
adamc@182 139 string (Int.toString depth),
adamc@182 140 string ";"],
adamc@182 141 E.pushERel env x t)
adamc@182 142 | PPrim (Prim.Int n) =>
adamc@182 143 (box [string "if",
adamc@182 144 space,
adamc@182 145 string "(disc",
adamc@182 146 string (Int.toString depth),
adamc@182 147 space,
adamc@182 148 string "!=",
adamc@182 149 space,
adamc@182 150 Prim.p_t (Prim.Int n),
adamc@182 151 string ")",
adamc@182 152 space,
adamc@182 153 exit],
adamc@182 154 env)
adamc@182 155 | PPrim (Prim.String s) =>
adamc@182 156 (box [string "if",
adamc@182 157 space,
adamc@182 158 string "(strcmp(disc",
adamc@182 159 string (Int.toString depth),
adamc@182 160 string ",",
adamc@182 161 space,
adamc@182 162 Prim.p_t (Prim.String s),
adamc@182 163 string "))",
adamc@182 164 space,
adamc@182 165 exit],
adamc@182 166 env)
adamc@182 167 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
adamc@182 168
adamc@188 169 | PCon (dk, pc, po) =>
adamc@182 170 let
adamc@182 171 val (p, env) =
adamc@182 172 case po of
adamc@182 173 NONE => (box [], env)
adamc@182 174 | SOME p =>
adamc@182 175 let
adamc@182 176 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 177
adamc@182 178 val (x, to) = case pc of
adamc@182 179 PConVar n =>
adamc@182 180 let
adamc@182 181 val (x, to, _) = E.lookupConstructor env n
adamc@182 182 in
adamc@196 183 ("lw_" ^ x, to)
adamc@182 184 end
adamc@188 185 | PConFfi {mod = m, con, arg, ...} =>
adamc@188 186 ("lw_" ^ m ^ "_" ^ con, arg)
adamc@182 187
adamc@182 188 val t = case to of
adamc@182 189 NONE => raise Fail "CjrPrint: Constructor mismatch"
adamc@182 190 | SOME t => t
adamc@182 191 in
adamc@182 192 (box [string "{",
adamc@182 193 newline,
adamc@182 194 p_typ env t,
adamc@182 195 space,
adamc@182 196 string "disc",
adamc@182 197 string (Int.toString (depth + 1)),
adamc@182 198 space,
adamc@182 199 string "=",
adamc@182 200 space,
adamc@182 201 string "disc",
adamc@182 202 string (Int.toString depth),
adamc@188 203 string "->data.",
adamc@182 204 string x,
adamc@182 205 string ";",
adamc@182 206 newline,
adamc@182 207 p,
adamc@182 208 newline,
adamc@182 209 string "}"],
adamc@182 210 env)
adamc@182 211 end
adamc@182 212 in
adamc@182 213 (box [string "if",
adamc@182 214 space,
adamc@182 215 string "(disc",
adamc@182 216 string (Int.toString depth),
adamc@188 217 case dk of
adamc@188 218 Enum => box []
adamc@188 219 | Default => string "->tag",
adamc@182 220 space,
adamc@182 221 string "!=",
adamc@182 222 space,
adamc@182 223 p_patCon env pc,
adamc@182 224 string ")",
adamc@182 225 space,
adamc@182 226 exit,
adamc@182 227 newline,
adamc@182 228 p],
adamc@182 229 env)
adamc@182 230 end
adamc@182 231
adamc@182 232 | PRecord xps =>
adamc@182 233 let
adamc@182 234 val (xps, env) =
adamc@182 235 ListUtil.foldlMap (fn ((x, p, t), env) =>
adamc@182 236 let
adamc@182 237 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 238
adamc@182 239 val p = box [string "{",
adamc@182 240 newline,
adamc@182 241 p_typ env t,
adamc@182 242 space,
adamc@182 243 string "disc",
adamc@182 244 string (Int.toString (depth + 1)),
adamc@182 245 space,
adamc@182 246 string "=",
adamc@182 247 space,
adamc@182 248 string "disc",
adamc@182 249 string (Int.toString depth),
adamc@196 250 string ".__lwf_",
adamc@182 251 string x,
adamc@182 252 string ";",
adamc@182 253 newline,
adamc@182 254 p,
adamc@182 255 newline,
adamc@182 256 string "}"]
adamc@182 257 in
adamc@182 258 (p, env)
adamc@182 259 end) env xps
adamc@182 260 in
adamc@182 261 (p_list_sep newline (fn x => x) xps,
adamc@182 262 env)
adamc@182 263 end
adamc@182 264
adamc@182 265 local
adamc@182 266 val count = ref 0
adamc@182 267 in
adamc@182 268 fun newGoto () =
adamc@182 269 let
adamc@182 270 val r = !count
adamc@182 271 in
adamc@182 272 count := r + 1;
adamc@182 273 string ("L" ^ Int.toString r)
adamc@182 274 end
adamc@182 275 end
adamc@182 276
adamc@185 277 fun patConInfo env pc =
adamc@185 278 case pc of
adamc@185 279 PConVar n =>
adamc@185 280 let
adamc@185 281 val (x, _, dn) = E.lookupConstructor env n
adamc@185 282 val (dx, _) = E.lookupDatatype env dn
adamc@185 283 in
adamc@185 284 ("__lwd_" ^ dx ^ "_" ^ Int.toString dn,
adamc@196 285 "__lwc_" ^ x ^ "_" ^ Int.toString n,
adamc@196 286 "lw_" ^ x)
adamc@185 287 end
adamc@186 288 | PConFfi {mod = m, datatyp, con, ...} =>
adamc@185 289 ("lw_" ^ m ^ "_" ^ datatyp,
adamc@196 290 "lw_" ^ m ^ "_" ^ con,
adamc@196 291 "lw_" ^ con)
adamc@185 292
adamc@182 293 fun p_exp' par env (e, loc) =
adamc@29 294 case e of
adamc@29 295 EPrim p => Prim.p_t p
adamc@29 296 | ERel n => p_rel env n
adamc@109 297 | ENamed n => p_enamed env n
adamc@188 298 | ECon (Enum, pc, _) => p_patCon env pc
adamc@188 299 | ECon (Default, pc, eo) =>
adamc@181 300 let
adamc@196 301 val (xd, xc, xn) = patConInfo env pc
adamc@181 302 in
adamc@182 303 box [string "({",
adamc@181 304 newline,
adamc@181 305 string "struct",
adamc@181 306 space,
adamc@185 307 string xd,
adamc@181 308 space,
adamc@181 309 string "*tmp",
adamc@181 310 space,
adamc@181 311 string "=",
adamc@181 312 space,
adamc@185 313 string "lw_malloc(ctx, sizeof(struct ",
adamc@185 314 string xd,
adamc@181 315 string "));",
adamc@181 316 newline,
adamc@181 317 string "tmp->tag",
adamc@181 318 space,
adamc@181 319 string "=",
adamc@181 320 space,
adamc@185 321 string xc,
adamc@181 322 string ";",
adamc@181 323 newline,
adamc@181 324 case eo of
adamc@181 325 NONE => box []
adamc@185 326 | SOME e => box [string "tmp->data.",
adamc@196 327 string xn,
adamc@181 328 space,
adamc@181 329 string "=",
adamc@181 330 space,
adamc@181 331 p_exp env e,
adamc@181 332 string ";",
adamc@181 333 newline],
adamc@181 334 string "tmp;",
adamc@181 335 newline,
adamc@181 336 string "})"]
adamc@181 337 end
adamc@109 338
adamc@53 339 | EFfi (m, x) => box [string "lw_", string m, string "_", string x]
adamc@53 340 | EFfiApp (m, x, es) => box [string "lw_",
adamc@53 341 string m,
adamc@53 342 string "_",
adamc@53 343 string x,
adamc@117 344 string "(ctx, ",
adamc@53 345 p_list (p_exp env) es,
adamc@53 346 string ")"]
adamc@129 347 | EApp (e1, e2) =>
adamc@129 348 let
adamc@129 349 fun unravel (f, acc) =
adamc@129 350 case #1 f of
adamc@129 351 EApp (f', arg) => unravel (f', arg :: acc)
adamc@129 352 | _ => (f, acc)
adamc@129 353
adamc@129 354 val (f, args) = unravel (e1, [e2])
adamc@129 355 in
adamc@129 356 parenIf par (box [p_exp' true env e1,
adamc@129 357 string "(ctx,",
adamc@129 358 space,
adamc@129 359 p_list_sep (box [string ",", space]) (p_exp env) args,
adamc@129 360 string ")"])
adamc@129 361 end
adamc@29 362
adamc@29 363 | ERecord (i, xes) => box [string "({",
adamc@29 364 space,
adamc@29 365 string "struct",
adamc@29 366 space,
adamc@29 367 string ("__lws_" ^ Int.toString i),
adamc@29 368 space,
adamc@181 369 string "tmp",
adamc@29 370 space,
adamc@29 371 string "=",
adamc@29 372 space,
adamc@29 373 string "{",
adamc@29 374 p_list (fn (_, e) =>
adamc@29 375 p_exp env e) xes,
adamc@29 376 string "};",
adamc@29 377 space,
adamc@181 378 string "tmp;",
adamc@29 379 space,
adamc@29 380 string "})" ]
adamc@29 381 | EField (e, x) =>
adamc@29 382 box [p_exp' true env e,
adamc@182 383 string ".__lwf_",
adamc@29 384 string x]
adamc@29 385
adamc@182 386 | ECase (e, pes, {disc, result}) =>
adamc@182 387 let
adamc@182 388 val final = newGoto ()
adamc@182 389
adamc@182 390 val body = foldl (fn ((p, e), body) =>
adamc@182 391 let
adamc@182 392 val exit = newGoto ()
adamc@182 393 val (pr, _) = p_pat_preamble env p
adamc@182 394 val (p, env) = p_pat (env,
adamc@182 395 box [string "goto",
adamc@182 396 space,
adamc@182 397 exit,
adamc@182 398 string ";"],
adamc@182 399 0) p
adamc@182 400 in
adamc@182 401 box [body,
adamc@182 402 box [string "{",
adamc@182 403 newline,
adamc@182 404 pr,
adamc@182 405 newline,
adamc@182 406 p,
adamc@182 407 newline,
adamc@182 408 string "result",
adamc@182 409 space,
adamc@182 410 string "=",
adamc@182 411 space,
adamc@182 412 p_exp env e,
adamc@182 413 string ";",
adamc@182 414 newline,
adamc@182 415 string "goto",
adamc@182 416 space,
adamc@182 417 final,
adamc@182 418 string ";",
adamc@182 419 newline,
adamc@182 420 string "}"],
adamc@182 421 newline,
adamc@182 422 exit,
adamc@182 423 string ":",
adamc@182 424 newline]
adamc@182 425 end) (box []) pes
adamc@182 426 in
adamc@182 427 box [string "({",
adamc@182 428 newline,
adamc@182 429 p_typ env disc,
adamc@182 430 space,
adamc@182 431 string "disc0",
adamc@182 432 space,
adamc@182 433 string "=",
adamc@182 434 space,
adamc@182 435 p_exp env e,
adamc@182 436 string ";",
adamc@182 437 newline,
adamc@182 438 p_typ env result,
adamc@182 439 space,
adamc@182 440 string "result;",
adamc@182 441 newline,
adamc@182 442 body,
adamc@182 443 string "lw_error(ctx, FATAL, \"",
adamc@182 444 string (ErrorMsg.spanToString loc),
adamc@182 445 string ": pattern match failure\");",
adamc@182 446 newline,
adamc@182 447 final,
adamc@182 448 string ":",
adamc@182 449 space,
adamc@182 450 string "result;",
adamc@182 451 newline,
adamc@182 452 string "})"]
adamc@182 453 end
adamc@181 454
adamc@117 455 | EWrite e => box [string "(lw_write(ctx, ",
adamc@102 456 p_exp env e,
adamc@102 457 string "), lw_unit_v)"]
adamc@102 458
adamc@106 459 | ESeq (e1, e2) => box [string "(",
adamc@106 460 p_exp env e1,
adamc@106 461 string ",",
adamc@106 462 space,
adamc@106 463 p_exp env e2,
adamc@106 464 string ")"]
adamc@106 465
adamc@29 466 and p_exp env = p_exp' false env
adamc@29 467
adamc@129 468 fun p_fun env (fx, n, args, ran, e) =
adamc@129 469 let
adamc@129 470 val nargs = length args
adamc@129 471 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
adamc@129 472 in
adamc@129 473 box [string "static",
adamc@129 474 space,
adamc@129 475 p_typ env ran,
adamc@129 476 space,
adamc@129 477 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
adamc@129 478 string "(",
adamc@129 479 p_list_sep (box [string ",", space]) (fn x => x)
adamc@129 480 (string "lw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
adamc@129 481 box [p_typ env dom,
adamc@129 482 space,
adamc@129 483 p_rel env' (nargs - i - 1)]) args),
adamc@129 484 string ")",
adamc@129 485 space,
adamc@129 486 string "{",
adamc@129 487 newline,
adamc@129 488 box[string "return(",
adamc@129 489 p_exp env' e,
adamc@129 490 string ");"],
adamc@129 491 newline,
adamc@129 492 string "}"]
adamc@129 493 end
adamc@129 494
adamc@129 495 fun p_decl env (dAll as (d, _) : decl) =
adamc@29 496 case d of
adamc@29 497 DStruct (n, xts) =>
adamc@196 498 let
adamc@196 499 val env = E.declBinds env dAll
adamc@196 500 in
adamc@196 501 box [string "struct",
adamc@196 502 space,
adamc@196 503 string ("__lws_" ^ Int.toString n),
adamc@196 504 space,
adamc@196 505 string "{",
adamc@196 506 newline,
adamc@196 507 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@196 508 space,
adamc@196 509 string "__lwf_",
adamc@196 510 string x,
adamc@196 511 string ";",
adamc@196 512 newline]) xts,
adamc@196 513 string "};"]
adamc@196 514 end
adamc@188 515 | DDatatype (Enum, x, n, xncs) =>
adamc@188 516 box [string "enum",
adamc@188 517 space,
adamc@188 518 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
adamc@188 519 space,
adamc@188 520 string "{",
adamc@188 521 space,
adamc@188 522 p_list_sep (box [string ",", space]) (fn (x, n, _) => string ("__lwc_" ^ x ^ "_" ^ Int.toString n)) xncs,
adamc@188 523 space,
adamc@188 524 string "};"]
adamc@188 525 | DDatatype (Default, x, n, xncs) =>
adamc@165 526 let
adamc@165 527 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
adamc@165 528 | (x, n, SOME t) => SOME (x, n, t)) xncs
adamc@165 529 in
adamc@165 530 box [string "enum",
adamc@165 531 space,
adamc@165 532 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
adamc@165 533 space,
adamc@165 534 string "{",
adamc@165 535 space,
adamc@165 536 p_list_sep (box [string ",", space]) (fn (x, n, _) => string ("__lwc_" ^ x ^ "_" ^ Int.toString n)) xncs,
adamc@165 537 space,
adamc@165 538 string "};",
adamc@165 539 newline,
adamc@165 540 newline,
adamc@165 541 string "struct",
adamc@165 542 space,
adamc@167 543 string ("__lwd_" ^ x ^ "_" ^ Int.toString n),
adamc@165 544 space,
adamc@165 545 string "{",
adamc@165 546 newline,
adamc@165 547 string "enum",
adamc@165 548 space,
adamc@165 549 string ("__lwe_" ^ x ^ "_" ^ Int.toString n),
adamc@165 550 space,
adamc@165 551 string "tag;",
adamc@165 552 newline,
adamc@165 553 box (case xncsArgs of
adamc@165 554 [] => []
adamc@165 555 | _ => [string "union",
adamc@165 556 space,
adamc@165 557 string "{",
adamc@165 558 newline,
adamc@165 559 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
adamc@165 560 space,
adamc@196 561 string ("lw_" ^ x),
adamc@165 562 string ";"]) xncsArgs,
adamc@165 563 newline,
adamc@165 564 string "}",
adamc@165 565 space,
adamc@165 566 string "data;",
adamc@165 567 newline]),
adamc@165 568 string "};"]
adamc@188 569 end
adamc@29 570
adamc@196 571 | DDatatypeForward _ => box []
adamc@196 572
adamc@29 573 | DVal (x, n, t, e) =>
adamc@29 574 box [p_typ env t,
adamc@29 575 space,
adamc@29 576 string ("__lwn_" ^ x ^ "_" ^ Int.toString n),
adamc@29 577 space,
adamc@29 578 string "=",
adamc@29 579 space,
adamc@29 580 p_exp env e,
adamc@29 581 string ";"]
adamc@129 582 | DFun vi => p_fun env vi
adamc@129 583 | DFunRec vis =>
adamc@29 584 let
adamc@129 585 val env = E.declBinds env dAll
adamc@29 586 in
adamc@129 587 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
adamc@129 588 box [string "static",
adamc@129 589 space,
adamc@129 590 p_typ env ran,
adamc@129 591 space,
adamc@129 592 string ("__lwn_" ^ fx ^ "_" ^ Int.toString n),
adamc@129 593 string "(lw_context,",
adamc@129 594 space,
adamc@129 595 p_list_sep (box [string ",", space])
adamc@129 596 (fn (_, dom) => p_typ env dom) args,
adamc@129 597 string ");"]) vis,
adamc@29 598 newline,
adamc@129 599 p_list_sep newline (p_fun env) vis,
adamc@129 600 newline]
adamc@29 601 end
adamc@29 602
adamc@144 603 datatype 'a search =
adamc@144 604 Found of 'a
adamc@144 605 | NotFound
adamc@144 606 | Error
adamc@120 607
adamc@101 608
adamc@101 609 fun p_file env (ds, ps) =
adamc@29 610 let
adamc@101 611 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 612 (p_decl env d,
adamc@31 613 E.declBinds env d))
adamc@101 614 env ds
adamc@144 615
adamc@144 616 val fields = foldl (fn ((ek, _, _, ts), fields) =>
adamc@144 617 case ek of
adamc@144 618 Core.Link => fields
adamc@144 619 | Core.Action =>
adamc@144 620 case List.last ts of
adamc@144 621 (TRecord i, _) =>
adamc@144 622 let
adamc@144 623 val xts = E.lookupStruct env i
adamc@144 624 val xtsSet = SS.addList (SS.empty, map #1 xts)
adamc@144 625 in
adamc@144 626 foldl (fn ((x, _), fields) =>
adamc@144 627 let
adamc@144 628 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
adamc@144 629 in
adamc@144 630 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
adamc@144 631 xtsSet'))
adamc@144 632 end) fields xts
adamc@144 633 end
adamc@144 634 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
adamc@144 635 SM.empty ps
adamc@144 636
adamc@144 637 val fnums = SM.foldli (fn (x, xs, fnums) =>
adamc@144 638 let
adamc@144 639 val unusable = SS.foldl (fn (x', unusable) =>
adamc@144 640 case SM.find (fnums, x') of
adamc@144 641 NONE => unusable
adamc@144 642 | SOME n => IS.add (unusable, n))
adamc@144 643 IS.empty xs
adamc@144 644
adamc@144 645 fun findAvailable n =
adamc@144 646 if IS.member (unusable, n) then
adamc@144 647 findAvailable (n + 1)
adamc@144 648 else
adamc@144 649 n
adamc@144 650 in
adamc@144 651 SM.insert (fnums, x, findAvailable 0)
adamc@144 652 end)
adamc@144 653 SM.empty fields
adamc@144 654
adamc@144 655 fun makeSwitch (fnums, i) =
adamc@144 656 case SM.foldl (fn (n, NotFound) => Found n
adamc@144 657 | (n, Error) => Error
adamc@144 658 | (n, Found n') => if n = n' then
adamc@144 659 Found n'
adamc@144 660 else
adamc@144 661 Error) NotFound fnums of
adamc@144 662 NotFound => box [string "return",
adamc@144 663 space,
adamc@144 664 string "-1;"]
adamc@144 665 | Found n => box [string "return",
adamc@144 666 space,
adamc@144 667 string (Int.toString n),
adamc@144 668 string ";"]
adamc@144 669 | Error =>
adamc@144 670 let
adamc@144 671 val cmap = SM.foldli (fn (x, n, cmap) =>
adamc@144 672 let
adamc@144 673 val ch = if i < size x then
adamc@144 674 String.sub (x, i)
adamc@144 675 else
adamc@144 676 chr 0
adamc@144 677
adamc@144 678 val fnums = case CM.find (cmap, ch) of
adamc@144 679 NONE => SM.empty
adamc@144 680 | SOME fnums => fnums
adamc@144 681 val fnums = SM.insert (fnums, x, n)
adamc@144 682 in
adamc@144 683 CM.insert (cmap, ch, fnums)
adamc@144 684 end)
adamc@144 685 CM.empty fnums
adamc@144 686
adamc@144 687 val cmap = CM.listItemsi cmap
adamc@144 688 in
adamc@144 689 case cmap of
adamc@144 690 [(_, fnums)] =>
adamc@144 691 box [string "if",
adamc@144 692 space,
adamc@144 693 string "(name[",
adamc@144 694 string (Int.toString i),
adamc@144 695 string "]",
adamc@144 696 space,
adamc@144 697 string "==",
adamc@144 698 space,
adamc@144 699 string "0)",
adamc@144 700 space,
adamc@144 701 string "return",
adamc@144 702 space,
adamc@144 703 string "-1;",
adamc@144 704 newline,
adamc@144 705 makeSwitch (fnums, i+1)]
adamc@144 706 | _ =>
adamc@144 707 box [string "switch",
adamc@144 708 space,
adamc@144 709 string "(name[",
adamc@144 710 string (Int.toString i),
adamc@144 711 string "])",
adamc@144 712 space,
adamc@144 713 string "{",
adamc@144 714 newline,
adamc@144 715 box (map (fn (ch, fnums) =>
adamc@144 716 box [string "case",
adamc@144 717 space,
adamc@144 718 if ch = chr 0 then
adamc@144 719 string "0:"
adamc@144 720 else
adamc@144 721 box [string "'",
adamc@144 722 string (Char.toString ch),
adamc@144 723 string "':"],
adamc@144 724 newline,
adamc@144 725 makeSwitch (fnums, i+1),
adamc@144 726 newline]) cmap),
adamc@144 727 string "default:",
adamc@144 728 newline,
adamc@144 729 string "return",
adamc@144 730 space,
adamc@144 731 string "-1;",
adamc@144 732 newline,
adamc@144 733 string "}"]
adamc@144 734 end
adamc@144 735
adamc@186 736 fun capitalize s =
adamc@186 737 if s = "" then
adamc@186 738 ""
adamc@186 739 else
adamc@186 740 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@186 741
adamc@144 742 fun unurlify (t, loc) =
adamc@144 743 case t of
adamc@186 744 TFfi (m, t) => string ("lw_" ^ m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
adamc@144 745
adamc@144 746 | TRecord 0 => string "lw_unit_v"
adamc@144 747 | TRecord i =>
adamc@144 748 let
adamc@144 749 val xts = E.lookupStruct env i
adamc@144 750 in
adamc@144 751 box [string "({",
adamc@144 752 newline,
adamc@144 753 box (map (fn (x, t) =>
adamc@144 754 box [p_typ env t,
adamc@144 755 space,
adamc@144 756 string x,
adamc@144 757 space,
adamc@144 758 string "=",
adamc@144 759 space,
adamc@144 760 unurlify t,
adamc@144 761 string ";",
adamc@144 762 newline]) xts),
adamc@144 763 string "struct",
adamc@144 764 space,
adamc@144 765 string "__lws_",
adamc@144 766 string (Int.toString i),
adamc@144 767 space,
adamc@181 768 string "tmp",
adamc@144 769 space,
adamc@144 770 string "=",
adamc@144 771 space,
adamc@144 772 string "{",
adamc@144 773 space,
adamc@144 774 p_list_sep (box [string ",", space]) (fn (x, _) => string x) xts,
adamc@144 775 space,
adamc@144 776 string "};",
adamc@144 777 newline,
adamc@181 778 string "tmp;",
adamc@144 779 newline,
adamc@144 780 string "})"]
adamc@144 781 end
adamc@144 782
adamc@188 783 | TDatatype (Enum, i, _) =>
adamc@188 784 let
adamc@188 785 val (x, xncs) = E.lookupDatatype env i
adamc@188 786
adamc@188 787 fun doEm xncs =
adamc@188 788 case xncs of
adamc@188 789 [] => string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), (enum __lwe_"
adamc@188 790 ^ x ^ "_" ^ Int.toString i ^ ")0)")
adamc@188 791 | (x', n, to) :: rest =>
adamc@188 792 box [string "((!strncmp(request, \"",
adamc@188 793 string x',
adamc@188 794 string "\", ",
adamc@188 795 string (Int.toString (size x')),
adamc@188 796 string ") && (request[",
adamc@188 797 string (Int.toString (size x')),
adamc@188 798 string "] == 0 || request[",
adamc@188 799 string (Int.toString (size x')),
adamc@188 800 string ("] == '/')) ? __lwc_" ^ x' ^ "_" ^ Int.toString n),
adamc@188 801 space,
adamc@188 802 string ":",
adamc@188 803 space,
adamc@188 804 doEm rest,
adamc@188 805 string ")"]
adamc@188 806 in
adamc@188 807 doEm xncs
adamc@188 808 end
adamc@188 809
adamc@188 810 | TDatatype (Default, i, _) =>
adamc@166 811 let
adamc@166 812 val (x, xncs) = E.lookupDatatype env i
adamc@166 813
adamc@166 814 fun doEm xncs =
adamc@166 815 case xncs of
adamc@167 816 [] => string ("(lw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x ^ "\"), NULL)")
adamc@167 817 | (x', n, to) :: rest =>
adamc@167 818 box [string "((!strncmp(request, \"",
adamc@167 819 string x',
adamc@167 820 string "\", ",
adamc@167 821 string (Int.toString (size x')),
adamc@167 822 string ") && (request[",
adamc@167 823 string (Int.toString (size x')),
adamc@167 824 string "] == 0 || request[",
adamc@167 825 string (Int.toString (size x')),
adamc@167 826 string "] == '/')) ? ({",
adamc@166 827 newline,
adamc@167 828 string "struct",
adamc@167 829 space,
adamc@166 830 string ("__lwd_" ^ x ^ "_" ^ Int.toString i),
adamc@166 831 space,
adamc@181 832 string "*tmp = lw_malloc(ctx, sizeof(struct __lwd_",
adamc@167 833 string x,
adamc@167 834 string "_",
adamc@167 835 string (Int.toString i),
adamc@167 836 string "));",
adamc@166 837 newline,
adamc@181 838 string "tmp->tag",
adamc@166 839 space,
adamc@166 840 string "=",
adamc@166 841 space,
adamc@167 842 string ("__lwc_" ^ x' ^ "_" ^ Int.toString n),
adamc@166 843 string ";",
adamc@166 844 newline,
adamc@166 845 string "request",
adamc@166 846 space,
adamc@166 847 string "+=",
adamc@166 848 space,
adamc@167 849 string (Int.toString (size x')),
adamc@166 850 string ";",
adamc@166 851 newline,
adamc@167 852 string "if (request[0] == '/') ++request;",
adamc@167 853 newline,
adamc@166 854 case to of
adamc@166 855 NONE => box []
adamc@197 856 | SOME t => box [string "tmp->data.lw_",
adamc@167 857 string x',
adamc@166 858 space,
adamc@166 859 string "=",
adamc@166 860 space,
adamc@166 861 unurlify t,
adamc@166 862 string ";",
adamc@166 863 newline],
adamc@181 864 string "tmp;",
adamc@166 865 newline,
adamc@166 866 string "})",
adamc@166 867 space,
adamc@166 868 string ":",
adamc@166 869 space,
adamc@166 870 doEm rest,
adamc@166 871 string ")"]
adamc@166 872 in
adamc@166 873 doEm xncs
adamc@166 874 end
adamc@166 875
adamc@144 876 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
adamc@144 877 space)
adamc@144 878
adamc@144 879
adamc@144 880 fun p_page (ek, s, n, ts) =
adamc@144 881 let
adamc@144 882 val (ts, defInputs, inputsVar) =
adamc@144 883 case ek of
adamc@144 884 Core.Link => (ts, string "", string "")
adamc@144 885 | Core.Action =>
adamc@144 886 case List.last ts of
adamc@144 887 (TRecord i, _) =>
adamc@144 888 let
adamc@144 889 val xts = E.lookupStruct env i
adamc@144 890 in
adamc@144 891 (List.drop (ts, 1),
adamc@144 892 box [box (map (fn (x, t) => box [p_typ env t,
adamc@144 893 space,
adamc@144 894 string "lw_input_",
adamc@144 895 string x,
adamc@144 896 string ";",
adamc@144 897 newline]) xts),
adamc@144 898 newline,
adamc@144 899 box (map (fn (x, t) =>
adamc@144 900 let
adamc@144 901 val n = case SM.find (fnums, x) of
adamc@144 902 NONE => raise Fail "CjrPrint: Can't find in fnums"
adamc@144 903 | SOME n => n
adamc@190 904
adamc@190 905 val f = case t of
adamc@190 906 (TFfi ("Basis", "bool"), _) => "optional_"
adamc@190 907 | _ => ""
adamc@144 908 in
adamc@190 909 box [string "request = lw_get_",
adamc@190 910 string f,
adamc@190 911 string "input(ctx, ",
adamc@144 912 string (Int.toString n),
adamc@144 913 string ");",
adamc@144 914 newline,
adamc@144 915 string "if (request == NULL) {",
adamc@144 916 newline,
adamc@144 917 box [string "printf(\"Missing input ",
adamc@144 918 string x,
adamc@144 919 string "\\n\");",
adamc@144 920 newline,
adamc@144 921 string "exit(1);"],
adamc@144 922 newline,
adamc@144 923 string "}",
adamc@144 924 newline,
adamc@144 925 string "lw_input_",
adamc@144 926 string x,
adamc@144 927 space,
adamc@144 928 string "=",
adamc@144 929 space,
adamc@144 930 unurlify t,
adamc@144 931 string ";",
adamc@144 932 newline]
adamc@144 933 end) xts),
adamc@144 934 string "struct __lws_",
adamc@144 935 string (Int.toString i),
adamc@144 936 space,
adamc@144 937 string "lw_inputs",
adamc@144 938 space,
adamc@144 939 string "= {",
adamc@144 940 newline,
adamc@144 941 box (map (fn (x, _) => box [string "lw_input_",
adamc@144 942 string x,
adamc@144 943 string ",",
adamc@144 944 newline]) xts),
adamc@144 945 string "};",
adamc@144 946 newline],
adamc@144 947 box [string ",",
adamc@144 948 space,
adamc@144 949 string "lw_inputs"])
adamc@144 950 end
adamc@144 951
adamc@144 952 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
adamc@144 953 in
adamc@144 954 box [string "if (!strncmp(request, \"",
adamc@144 955 string (String.toString s),
adamc@144 956 string "\", ",
adamc@144 957 string (Int.toString (size s)),
adamc@144 958 string ")) {",
adamc@144 959 newline,
adamc@144 960 string "request += ",
adamc@144 961 string (Int.toString (size s)),
adamc@144 962 string ";",
adamc@144 963 newline,
adamc@144 964 string "if (*request == '/') ++request;",
adamc@144 965 newline,
adamc@144 966 box [string "{",
adamc@144 967 newline,
adamc@144 968 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
adamc@144 969 space,
adamc@144 970 string "arg",
adamc@144 971 string (Int.toString i),
adamc@144 972 space,
adamc@144 973 string "=",
adamc@144 974 space,
adamc@144 975 unurlify t,
adamc@144 976 string ";",
adamc@144 977 newline]) ts),
adamc@144 978 defInputs,
adamc@144 979 p_enamed env n,
adamc@144 980 string "(",
adamc@144 981 p_list_sep (box [string ",", space])
adamc@144 982 (fn x => x)
adamc@144 983 (string "ctx" :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
adamc@144 984 inputsVar,
adamc@144 985 string ");",
adamc@144 986 newline,
adamc@144 987 string "return;",
adamc@144 988 newline,
adamc@144 989 string "}",
adamc@144 990 newline,
adamc@144 991 string "}"]
adamc@144 992 ]
adamc@144 993 end
adamc@144 994
adamc@144 995 val pds' = map p_page ps
adamc@29 996 in
adamc@144 997 box [string "#include <stdio.h>",
adamc@144 998 newline,
adamc@144 999 string "#include <stdlib.h>",
adamc@144 1000 newline,
adamc@144 1001 newline,
adamc@144 1002 string "#include \"lacweb.h\"",
adamc@101 1003 newline,
adamc@101 1004 newline,
adamc@101 1005 p_list_sep newline (fn x => x) pds,
adamc@101 1006 newline,
adamc@144 1007 string "int lw_inputs_len = ",
adamc@144 1008 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
adamc@144 1009 string ";",
adamc@144 1010 newline,
adamc@144 1011 newline,
adamc@144 1012 string "int lw_input_num(char *name) {",
adamc@144 1013 newline,
adamc@144 1014 makeSwitch (fnums, 0),
adamc@144 1015 string "}",
adamc@144 1016 newline,
adamc@144 1017 newline,
adamc@117 1018 string "void lw_handle(lw_context ctx, char *request) {",
adamc@101 1019 newline,
adamc@101 1020 p_list_sep newline (fn x => x) pds',
adamc@101 1021 newline,
adamc@101 1022 string "}",
adamc@101 1023 newline]
adamc@29 1024 end
adamc@29 1025
adamc@29 1026 end